file_worker_cpp.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #pragma once
  2. #include "file_worker.h"
  3. /**
  4. * @brief File operations helper class.
  5. * Automatically opens API records, shows error message on error.
  6. */
  7. class FileWorkerCpp {
  8. public:
  9. FileWorkerCpp(bool silent = false);
  10. ~FileWorkerCpp();
  11. /**
  12. * @brief Open file
  13. *
  14. * @param filename
  15. * @param access_mode
  16. * @param open_mode
  17. * @return true on success
  18. */
  19. bool open(const char* filename, FS_AccessMode access_mode, FS_OpenMode open_mode);
  20. /**
  21. * @brief Close file
  22. *
  23. * @return true on success
  24. */
  25. bool close();
  26. /**
  27. * @brief Creates a directory. Doesn't show error if directory exist.
  28. *
  29. * @param dirname
  30. * @return true on success
  31. */
  32. bool mkdir(const char* dirname);
  33. /**
  34. * @brief Removes the file. Doesn't show error if file doesn't exist.
  35. *
  36. * @param filename
  37. * @return true on success
  38. */
  39. bool remove(const char* filename);
  40. /**
  41. * @brief Reads data from a file.
  42. *
  43. * @param buffer
  44. * @param bytes_to_read
  45. * @return true on success
  46. */
  47. bool read(void* buffer, uint16_t bytes_to_read = 1);
  48. /**
  49. * @brief Reads data from a file until separator or EOF is found.
  50. * Moves seek pointer to the next symbol after the separator. The separator is not included in the result.
  51. *
  52. * @param result
  53. * @param separator
  54. * @return true on success
  55. */
  56. bool read_until(string_t result, char separator = '\n');
  57. /**
  58. * @brief Reads data in hexadecimal space-delimited format. For example "AF FF" in a file - [175, 255] in a read buffer.
  59. *
  60. * @param buffer
  61. * @param bytes_to_read
  62. * @return true on success
  63. */
  64. bool read_hex(uint8_t* buffer, uint16_t bytes_to_read = 1);
  65. /**
  66. * @brief Read seek pointer value
  67. *
  68. * @param position
  69. * @return true on success
  70. */
  71. bool tell(uint64_t* position);
  72. /**
  73. * @brief Set seek pointer value
  74. *
  75. * @param position
  76. * @param from_start
  77. * @return true on success
  78. */
  79. bool seek(uint64_t position, bool from_start);
  80. /**
  81. * @brief Write data to file.
  82. *
  83. * @param buffer
  84. * @param bytes_to_write
  85. * @return true on success
  86. */
  87. bool write(const void* buffer, uint16_t bytes_to_write = 1);
  88. /**
  89. * @brief Write data to file in hexadecimal space-delimited format. For example [175, 255] in a write buffer - "AF FF" in a file.
  90. *
  91. * @param buffer
  92. * @param bytes_to_write
  93. * @return true on success
  94. */
  95. bool write_hex(const uint8_t* buffer, uint16_t bytes_to_write = 1);
  96. /**
  97. * @brief Show system file error message
  98. *
  99. * @param error_text
  100. */
  101. void show_error(const char* error_text);
  102. /**
  103. * @brief Show system file select widget
  104. *
  105. * @param path
  106. * @param extension
  107. * @param result
  108. * @param result_size
  109. * @param selected_filename
  110. * @return true if file was selected
  111. */
  112. bool file_select(
  113. const char* path,
  114. const char* extension,
  115. char* result,
  116. uint8_t result_size,
  117. const char* selected_filename);
  118. /**
  119. * @brief Reads data from a file until separator or EOF is found.
  120. * Moves seek pointer to the next symbol after the separator. The separator is included in the result.
  121. *
  122. * @param result
  123. * @param file_buf
  124. * @param file_buf_cnt
  125. * @param max_length
  126. * @param separator
  127. * @return true on success
  128. */
  129. bool read_until_buffered(
  130. string_t str_result,
  131. char* file_buf,
  132. size_t* file_buf_cnt,
  133. size_t max_length,
  134. char separator = '\n');
  135. /**
  136. * @brief Gets value from key
  137. *
  138. * @param file_worker FileWorker instance
  139. * @param key key
  140. * @param delimeter key-value delimeter
  141. * @param value value for given key
  142. * @return true on success
  143. */
  144. bool get_value_from_key(string_t key, char delimiter, string_t value);
  145. /**
  146. * @brief Check whether file exist or not
  147. *
  148. * @param file_worker FileWorker instance
  149. * @param filename
  150. * @param exist - flag to show file exist
  151. * @return true on success
  152. */
  153. bool is_file_exist(const char* filename, bool* exist);
  154. /**
  155. * @brief Rename file or directory
  156. *
  157. * @param old_filename
  158. * @param new_filename
  159. * @return true on success
  160. */
  161. bool rename(const char* old_path, const char* new_path);
  162. /**
  163. * @brief Check errors
  164. *
  165. * @return true if no errors
  166. */
  167. bool check_errors();
  168. private:
  169. FileWorker* file_worker;
  170. };