flipper-file.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /**
  2. * @file flipper-file.h
  3. * Flipper File Format helper library.
  4. *
  5. * Flipper File Format is a fairly simple format for storing data in a file.
  6. *
  7. * Flipper file structure:
  8. *
  9. * ~~~~~~~~~~~~~~~~~~~~~
  10. * # Commentary
  11. * Field name: field value
  12. * ~~~~~~~~~~~~~~~~~~~~~
  13. *
  14. * Lines starting with the # character are ignored (considered as comments). The separator between the name of the value and the value itself is the string ": ".
  15. *
  16. * Currently supported types:
  17. *
  18. * ~~~~~~~~~~~~~~~~~~~~~
  19. * String: text
  20. * Uint32: 1
  21. * Hex Array: A4 B3 C2 D1 12 FF
  22. * ~~~~~~~~~~~~~~~~~~~~~
  23. *
  24. * End of line is LF when writing, but CR is supported when reading.
  25. *
  26. * The library is designed in such a way that comments and field values are completely ignored when searching for keys, that is, they do not consume memory.
  27. *
  28. * File example:
  29. *
  30. * ~~~~~~~~~~~~~~~~~~~~~
  31. * Filetype: Flipper Test File
  32. * Version: 1
  33. * # Just test file
  34. * String: String value
  35. * UINT: 1234
  36. * Hex Array: 00 01 FF A3
  37. * ~~~~~~~~~~~~~~~~~~~~~
  38. *
  39. * Writing:
  40. *
  41. * ~~~~~~~~~~~~~~~~~~~~~
  42. * FlipperFile file = flipper_file_alloc(storage);
  43. *
  44. * do {
  45. * const uint32_t version = 1;
  46. * const char* string_value = "String value";
  47. * const uint32_t uint32_value = 1234;
  48. * const uint16_t array_size = 4;
  49. * const uint8_t* array[array_size] = {0x00, 0x01, 0xFF, 0xA3};
  50. *
  51. * if(!flipper_file_new_write(file, "/ext/flipper_file_test")) break;
  52. * if(!flipper_file_write_header_cstr(file, "Flipper Test File", version)) break;
  53. * if(!flipper_file_write_comment_cstr(file, "Just test file")) break;
  54. * if(!flipper_file_write_string_cstr(file, "String", string_value)) break;
  55. * if(!flipper_file_flipper_file_write_uint32(file, "UINT", uint32_value)) break;
  56. * if(!flipper_file_write_hex_array(file, "Hex Array", array, array_size)) break;
  57. *
  58. * // signal that the file was written successfully
  59. * } while(0);
  60. *
  61. * flipper_file_close(file);
  62. * flipper_file_free(file);
  63. * ~~~~~~~~~~~~~~~~~~~~~
  64. *
  65. * Reading:
  66. *
  67. * ~~~~~~~~~~~~~~~~~~~~~
  68. * FlipperFile file = flipper_file_alloc(storage);
  69. *
  70. * do {
  71. * uint32_t version = 1;
  72. * string_t file_type;
  73. * string_t string_value;
  74. * uint32_t uint32_value = 1;
  75. * uint16_t array_size = 4;
  76. * uint8_t* array[array_size] = {0};
  77. * string_init(file_type);
  78. * string_init(string_value);
  79. *
  80. * if(!flipper_file_open_read(file, "/ext/flipper_file_test")) break;
  81. * if(!flipper_file_read_header(file, file_type, &version)) break;
  82. * if(!flipper_file_read_string(file, "String", string_value)) break;
  83. * if(!flipper_file_read_uint32(file, "UINT", &uint32_value)) break;
  84. * if(!flipper_file_read_hex_array(file, "Hex Array", array, array_size)) break;
  85. *
  86. * // signal that the file was read successfully
  87. * } while(0);
  88. *
  89. * flipper_file_close(file);
  90. * flipper_file_free(file);
  91. * ~~~~~~~~~~~~~~~~~~~~~
  92. *
  93. */
  94. #pragma once
  95. #include <stdint.h>
  96. #include <mlib/m-string.h>
  97. #include <storage/storage.h>
  98. #ifdef __cplusplus
  99. extern "C" {
  100. #endif
  101. /** FlipperFile type anonymous structure. */
  102. typedef struct FlipperFile FlipperFile;
  103. /**
  104. * Allocate FlipperFile.
  105. * @param storage storage api
  106. * @return FlipperFile* Pointer to a FlipperFile instance
  107. */
  108. FlipperFile* flipper_file_alloc(Storage* storage);
  109. /**
  110. * Free FlipperFile.
  111. * @param flipper_file Pointer to a FlipperFile instance
  112. */
  113. void flipper_file_free(FlipperFile* flipper_file);
  114. /**
  115. * Open file for reading.
  116. * @param flipper_file Pointer to a FlipperFile instance
  117. * @param filename File name and path
  118. * @return True on success
  119. */
  120. bool flipper_file_open_read(FlipperFile* flipper_file, const char* filename);
  121. /**
  122. * Open file for writing. Creates a new file, or deletes the contents of the file if it already exists.
  123. * @param flipper_file Pointer to a FlipperFile instance
  124. * @param filename File name and path
  125. * @return True on success
  126. */
  127. bool flipper_file_new_write(FlipperFile* flipper_file, const char* filename);
  128. /**
  129. * Close the file.
  130. * @param flipper_file Pointer to a FlipperFile instance
  131. * @return True on success
  132. */
  133. bool flipper_file_close(FlipperFile* flipper_file);
  134. /**
  135. * Read the header (file type and version) from the file.
  136. * @param flipper_file Pointer to a FlipperFile instance
  137. * @param filetype File type string
  138. * @param version Version Value
  139. * @return True on success
  140. */
  141. bool flipper_file_read_header(FlipperFile* flipper_file, string_t filetype, uint32_t* version);
  142. /**
  143. * Write the header (file type and version) to the file.
  144. * @param flipper_file Pointer to a FlipperFile instance
  145. * @param filetype File type string
  146. * @param version Version Value
  147. * @return True on success
  148. */
  149. bool flipper_file_write_header(
  150. FlipperFile* flipper_file,
  151. string_t filetype,
  152. const uint32_t version);
  153. /**
  154. * Write the header (file type and version) to the file. Plain C string version.
  155. * @param flipper_file Pointer to a FlipperFile instance
  156. * @param filetype File type string
  157. * @param version Version Value
  158. * @return True on success
  159. */
  160. bool flipper_file_write_header_cstr(
  161. FlipperFile* flipper_file,
  162. const char* filetype,
  163. const uint32_t version);
  164. /**
  165. * Read a string from a file by Key
  166. * @param flipper_file Pointer to a FlipperFile instance
  167. * @param key Key
  168. * @param data Value
  169. * @return True on success
  170. */
  171. bool flipper_file_read_string(FlipperFile* flipper_file, const char* key, string_t data);
  172. /**
  173. * Write key and string to file.
  174. * @param flipper_file Pointer to a FlipperFile instance
  175. * @param key Key
  176. * @param data Value
  177. * @return True on success
  178. */
  179. bool flipper_file_write_string(FlipperFile* flipper_file, const char* key, string_t data);
  180. /**
  181. * Write key and string to file. Plain C string version.
  182. * @param flipper_file Pointer to a FlipperFile instance
  183. * @param key Key
  184. * @param data Value
  185. * @return True on success
  186. */
  187. bool flipper_file_write_string_cstr(FlipperFile* flipper_file, const char* key, const char* data);
  188. /**
  189. * Read uint32 from a file by Key
  190. * @param flipper_file Pointer to a FlipperFile instance
  191. * @param key Key
  192. * @param data Value
  193. * @return True on success
  194. */
  195. bool flipper_file_read_uint32(FlipperFile* flipper_file, const char* key, uint32_t* data);
  196. /**
  197. * Write key and uint32 to file.
  198. * @param flipper_file Pointer to a FlipperFile instance
  199. * @param key Key
  200. * @param data Value
  201. * @return True on success
  202. */
  203. bool flipper_file_write_uint32(FlipperFile* flipper_file, const char* key, const uint32_t data);
  204. /**
  205. * Write comment to file.
  206. * @param flipper_file Pointer to a FlipperFile instance
  207. * @param data Comment text
  208. * @return True on success
  209. */
  210. bool flipper_file_write_comment(FlipperFile* flipper_file, string_t data);
  211. /**
  212. * Write comment to file. Plain C string version.
  213. * @param flipper_file Pointer to a FlipperFile instance
  214. * @param data Comment text
  215. * @return True on success
  216. */
  217. bool flipper_file_write_comment_cstr(FlipperFile* flipper_file, const char* data);
  218. /**
  219. * Read hex array from a file by Key
  220. * @param flipper_file Pointer to a FlipperFile instance
  221. * @param key Key
  222. * @param data Value
  223. * @param data_size Value size
  224. * @return True on success
  225. */
  226. bool flipper_file_read_hex_array(
  227. FlipperFile* flipper_file,
  228. const char* key,
  229. uint8_t* data,
  230. const uint16_t data_size);
  231. /**
  232. * Write key and hex array to file.
  233. * @param flipper_file Pointer to a FlipperFile instance
  234. * @param key Key
  235. * @param data Value
  236. * @param data_size Value size
  237. * @return True on success
  238. */
  239. bool flipper_file_write_hex_array(
  240. FlipperFile* flipper_file,
  241. const char* key,
  242. const uint8_t* data,
  243. const uint16_t data_size);
  244. #ifdef __cplusplus
  245. }
  246. #endif