file-worker.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #pragma once
  2. #include "record-controller.hpp"
  3. #include <sd-card-api.h>
  4. #include <filesystem-api.h>
  5. #include <m-string.h>
  6. /**
  7. * @brief File operations helper class.
  8. * Automatically opens API records, shows error message on error.
  9. */
  10. class FileWorker {
  11. public:
  12. FileWorker(bool silent = false);
  13. ~FileWorker();
  14. RecordController<FS_Api> fs_api;
  15. RecordController<SdCard_Api> sd_ex_api;
  16. /**
  17. * @brief Open file
  18. *
  19. * @param filename
  20. * @param access_mode
  21. * @param open_mode
  22. * @return true on success
  23. */
  24. bool open(const char* filename, FS_AccessMode access_mode, FS_OpenMode open_mode);
  25. /**
  26. * @brief Close file
  27. *
  28. * @return true on success
  29. */
  30. bool close();
  31. /**
  32. * @brief Creates a directory. Doesn't show error if directory exist.
  33. *
  34. * @param dirname
  35. * @return true on success
  36. */
  37. bool mkdir(const char* dirname);
  38. /**
  39. * @brief Removes the file. Doesn't show error if file doesn't exist.
  40. *
  41. * @param filename
  42. * @return true on success
  43. */
  44. bool remove(const char* filename);
  45. /**
  46. * @brief Reads data from a file.
  47. *
  48. * @param buffer
  49. * @param bytes_to_read
  50. * @return true on success
  51. */
  52. bool read(void* buffer, uint16_t bytes_to_read = 1);
  53. /**
  54. * @brief Reads data from a file until separator or EOF is found.
  55. * Moves seek pointer to the next symbol after the separator. The separator is not included in the result.
  56. *
  57. * @param result
  58. * @param separator
  59. * @return true on success
  60. */
  61. bool read_until(string_t result, char separator = '\n');
  62. /**
  63. * @brief Reads data in hexadecimal space-delimited format. For example "AF FF" in a file - [175, 255] in a read buffer.
  64. *
  65. * @param buffer
  66. * @param bytes_to_read
  67. * @return true on success
  68. */
  69. bool read_hex(uint8_t* buffer, uint16_t bytes_to_read = 1);
  70. /**
  71. * @brief Read seek pointer value
  72. *
  73. * @param position
  74. * @return true on success
  75. */
  76. bool tell(uint64_t* position);
  77. /**
  78. * @brief Set seek pointer value
  79. *
  80. * @param position
  81. * @param from_start
  82. * @return true on success
  83. */
  84. bool seek(uint64_t position, bool from_start);
  85. /**
  86. * @brief Write data to file.
  87. *
  88. * @param buffer
  89. * @param bytes_to_write
  90. * @return true on success
  91. */
  92. bool write(const void* buffer, uint16_t bytes_to_write = 1);
  93. /**
  94. * @brief Write data to file in hexadecimal space-delimited format. For example [175, 255] in a write buffer - "AF FF" in a file.
  95. *
  96. * @param buffer
  97. * @param bytes_to_write
  98. * @return true on success
  99. */
  100. bool write_hex(const uint8_t* buffer, uint16_t bytes_to_write = 1);
  101. /**
  102. * @brief Show system file error message
  103. *
  104. * @param error_text
  105. */
  106. void show_error(const char* error_text);
  107. /**
  108. * @brief Show system file select widget
  109. *
  110. * @param path
  111. * @param extension
  112. * @param result
  113. * @param result_size
  114. * @param selected_filename
  115. * @return true if file was selected
  116. */
  117. bool file_select(
  118. const char* path,
  119. const char* extension,
  120. char* result,
  121. uint8_t result_size,
  122. char* selected_filename);
  123. private:
  124. File file;
  125. bool silent;
  126. bool check_common_errors();
  127. void show_error_internal(const char* error_text);
  128. bool read_internal(void* buffer, uint16_t bytes_to_read = 1);
  129. bool write_internal(const void* buffer, uint16_t bytes_to_write = 1);
  130. bool tell_internal(uint64_t* position);
  131. bool seek_internal(uint64_t position, bool from_start);
  132. };