flipper_file.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. * Int32: 1 2 -3 4
  21. * Uint32: 1 2 3 4
  22. * Float: 1.0 1234.654
  23. * Hex: A4 B3 C2 D1 12 FF
  24. * ~~~~~~~~~~~~~~~~~~~~~
  25. *
  26. * End of line is LF when writing, but CR is supported when reading.
  27. *
  28. * 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.
  29. *
  30. * File example:
  31. *
  32. * ~~~~~~~~~~~~~~~~~~~~~
  33. * Filetype: Flipper Test File
  34. * Version: 1
  35. * # Just test file
  36. * String: String value
  37. * UINT: 1234
  38. * Hex: 00 01 FF A3
  39. * ~~~~~~~~~~~~~~~~~~~~~
  40. *
  41. * Writing:
  42. *
  43. * ~~~~~~~~~~~~~~~~~~~~~
  44. * FlipperFile file = flipper_file_alloc(storage);
  45. *
  46. * do {
  47. * const uint32_t version = 1;
  48. * const char* string_value = "String value";
  49. * const uint32_t uint32_value = 1234;
  50. * const uint16_t array_size = 4;
  51. * const uint8_t* array[array_size] = {0x00, 0x01, 0xFF, 0xA3};
  52. *
  53. * if(!flipper_file_open_new(file, "/ext/flipper_file_test")) break;
  54. * if(!flipper_file_write_header_cstr(file, "Flipper Test File", version)) break;
  55. * if(!flipper_file_write_comment_cstr(file, "Just test file")) break;
  56. * if(!flipper_file_write_string_cstr(file, "String", string_value)) break;
  57. * if(!flipper_file_flipper_file_write_uint32(file, "UINT", &uint32_value, 1)) break;
  58. * if(!flipper_file_write_hex(file, "Hex Array", array, array_size)) break;
  59. *
  60. * // signal that the file was written successfully
  61. * } while(0);
  62. *
  63. * flipper_file_close(file);
  64. * flipper_file_free(file);
  65. * ~~~~~~~~~~~~~~~~~~~~~
  66. *
  67. * Reading:
  68. *
  69. * ~~~~~~~~~~~~~~~~~~~~~
  70. * FlipperFile file = flipper_file_alloc(storage);
  71. *
  72. * do {
  73. * uint32_t version = 1;
  74. * string_t file_type;
  75. * string_t string_value;
  76. * uint32_t uint32_value = 1;
  77. * uint16_t array_size = 4;
  78. * uint8_t* array[array_size] = {0};
  79. * string_init(file_type);
  80. * string_init(string_value);
  81. *
  82. * if(!flipper_file_open_existing(file, "/ext/flipper_file_test")) break;
  83. * if(!flipper_file_read_header(file, file_type, &version)) break;
  84. * if(!flipper_file_read_string(file, "String", string_value)) break;
  85. * if(!flipper_file_read_uint32(file, "UINT", &uint32_value, 1)) break;
  86. * if(!flipper_file_read_hex(file, "Hex Array", array, array_size)) break;
  87. *
  88. * // signal that the file was read successfully
  89. * } while(0);
  90. *
  91. * flipper_file_close(file);
  92. * flipper_file_free(file);
  93. * ~~~~~~~~~~~~~~~~~~~~~
  94. *
  95. */
  96. #pragma once
  97. #include <stdint.h>
  98. #include <mlib/m-string.h>
  99. #include <storage/storage.h>
  100. #ifdef __cplusplus
  101. extern "C" {
  102. #endif
  103. /** FlipperFile type anonymous structure. */
  104. typedef struct FlipperFile FlipperFile;
  105. /**
  106. * Allocate FlipperFile.
  107. * @param storage storage api
  108. * @return FlipperFile* Pointer to a FlipperFile instance
  109. */
  110. FlipperFile* flipper_file_alloc(Storage* storage);
  111. /**
  112. * Free FlipperFile.
  113. * @param flipper_file Pointer to a FlipperFile instance
  114. */
  115. void flipper_file_free(FlipperFile* flipper_file);
  116. /**
  117. * Free FlipperFile.
  118. * @param flipper_file Pointer to a FlipperFile instance
  119. * @param strict_mode True obligates not to skip valid fields. False by default.
  120. */
  121. void flipper_file_set_strict_mode(FlipperFile* flipper_file, bool strict_mode);
  122. /**
  123. * Open existing file.
  124. * @param flipper_file Pointer to a FlipperFile instance
  125. * @param filename File name and path
  126. * @return True on success
  127. */
  128. bool flipper_file_open_existing(FlipperFile* flipper_file, const char* filename);
  129. /**
  130. * Open existing file for writing and add values to the end of file.
  131. * @param flipper_file Pointer to a FlipperFile instance
  132. * @param filename File name and path
  133. * @return True on success
  134. */
  135. bool flipper_file_open_append(FlipperFile* flipper_file, const char* filename);
  136. /**
  137. * Open file. Creates a new file, or deletes the contents of the file if it already exists.
  138. * @param flipper_file Pointer to a FlipperFile instance
  139. * @param filename File name and path
  140. * @return True on success
  141. */
  142. bool flipper_file_open_always(FlipperFile* flipper_file, const char* filename);
  143. /**
  144. * Open file. Creates a new file, fails if file already exists.
  145. * @param flipper_file Pointer to a FlipperFile instance
  146. * @param filename File name and path
  147. * @return True on success
  148. */
  149. bool flipper_file_open_new(FlipperFile* flipper_file, const char* filename);
  150. /**
  151. * Close the file.
  152. * @param flipper_file Pointer to a FlipperFile instance
  153. * @return True on success
  154. */
  155. bool flipper_file_close(FlipperFile* flipper_file);
  156. /**
  157. * Rewind the file RW pointer.
  158. * @param flipper_file Pointer to a FlipperFile instance
  159. * @return True on success
  160. */
  161. bool flipper_file_rewind(FlipperFile* flipper_file);
  162. /**
  163. * Read the header (file type and version) from the file.
  164. * @param flipper_file Pointer to a FlipperFile instance
  165. * @param filetype File type string
  166. * @param version Version Value
  167. * @return True on success
  168. */
  169. bool flipper_file_read_header(FlipperFile* flipper_file, string_t filetype, uint32_t* version);
  170. /**
  171. * Write the header (file type and version) to the file.
  172. * @param flipper_file Pointer to a FlipperFile instance
  173. * @param filetype File type string
  174. * @param version Version Value
  175. * @return True on success
  176. */
  177. bool flipper_file_write_header(
  178. FlipperFile* flipper_file,
  179. string_t filetype,
  180. const uint32_t version);
  181. /**
  182. * Write the header (file type and version) to the file. Plain C string version.
  183. * @param flipper_file Pointer to a FlipperFile instance
  184. * @param filetype File type string
  185. * @param version Version Value
  186. * @return True on success
  187. */
  188. bool flipper_file_write_header_cstr(
  189. FlipperFile* flipper_file,
  190. const char* filetype,
  191. const uint32_t version);
  192. /**
  193. * Get the count of values by key
  194. * @param flipper_file
  195. * @param key
  196. * @param count
  197. * @return bool
  198. */
  199. bool flipper_file_get_value_count(FlipperFile* flipper_file, const char* key, uint32_t* count);
  200. /**
  201. * Read a string from a file by Key
  202. * @param flipper_file Pointer to a FlipperFile instance
  203. * @param key Key
  204. * @param data Value
  205. * @return True on success
  206. */
  207. bool flipper_file_read_string(FlipperFile* flipper_file, const char* key, string_t data);
  208. /**
  209. * Write key and string to file.
  210. * @param flipper_file Pointer to a FlipperFile instance
  211. * @param key Key
  212. * @param data Value
  213. * @return True on success
  214. */
  215. bool flipper_file_write_string(FlipperFile* flipper_file, const char* key, string_t data);
  216. /**
  217. * Write key and string to file. Plain C string version.
  218. * @param flipper_file Pointer to a FlipperFile instance
  219. * @param key Key
  220. * @param data Value
  221. * @return True on success
  222. */
  223. bool flipper_file_write_string_cstr(FlipperFile* flipper_file, const char* key, const char* data);
  224. /**
  225. * Read array of uint32 from a file by Key
  226. * @param flipper_file Pointer to a FlipperFile instance
  227. * @param key Key
  228. * @param data Value
  229. * @param data_size Values count
  230. * @return True on success
  231. */
  232. bool flipper_file_read_uint32(
  233. FlipperFile* flipper_file,
  234. const char* key,
  235. uint32_t* data,
  236. const uint16_t data_size);
  237. /**
  238. * Write key and array of uint32 to file.
  239. * @param flipper_file Pointer to a FlipperFile instance
  240. * @param key Key
  241. * @param data Value
  242. * @param data_size Values count
  243. * @return True on success
  244. */
  245. bool flipper_file_write_uint32(
  246. FlipperFile* flipper_file,
  247. const char* key,
  248. const uint32_t* data,
  249. const uint16_t data_size);
  250. /**
  251. * Read array of int32 from a file by Key
  252. * @param flipper_file Pointer to a FlipperFile instance
  253. * @param key Key
  254. * @param data Value
  255. * @param data_size Values count
  256. * @return True on success
  257. */
  258. bool flipper_file_read_int32(
  259. FlipperFile* flipper_file,
  260. const char* key,
  261. int32_t* data,
  262. const uint16_t data_size);
  263. /**
  264. * Write key and array of int32 to file.
  265. * @param flipper_file Pointer to a FlipperFile instance
  266. * @param key Key
  267. * @param data Value
  268. * @param data_size Values count
  269. * @return True on success
  270. */
  271. bool flipper_file_write_int32(
  272. FlipperFile* flipper_file,
  273. const char* key,
  274. const int32_t* data,
  275. const uint16_t data_size);
  276. /**
  277. * Read array of float from a file by Key
  278. * @param flipper_file Pointer to a FlipperFile instance
  279. * @param key Key
  280. * @param data Value
  281. * @param data_size Values count
  282. * @return True on success
  283. */
  284. bool flipper_file_read_float(
  285. FlipperFile* flipper_file,
  286. const char* key,
  287. float* data,
  288. const uint16_t data_size);
  289. /**
  290. * Write key and array of float to file.
  291. * @param flipper_file Pointer to a FlipperFile instance
  292. * @param key Key
  293. * @param data Value
  294. * @param data_size Values count
  295. * @return True on success
  296. */
  297. bool flipper_file_write_float(
  298. FlipperFile* flipper_file,
  299. const char* key,
  300. const float* data,
  301. const uint16_t data_size);
  302. /**
  303. * Read hex array from a file by Key
  304. * @param flipper_file Pointer to a FlipperFile instance
  305. * @param key Key
  306. * @param data Value
  307. * @param data_size Value size
  308. * @return True on success
  309. */
  310. bool flipper_file_read_hex(
  311. FlipperFile* flipper_file,
  312. const char* key,
  313. uint8_t* data,
  314. const uint16_t data_size);
  315. /**
  316. * Write key and hex array to file.
  317. * @param flipper_file Pointer to a FlipperFile instance
  318. * @param key Key
  319. * @param data Value
  320. * @param data_size Values count
  321. * @return True on success
  322. */
  323. bool flipper_file_write_hex(
  324. FlipperFile* flipper_file,
  325. const char* key,
  326. const uint8_t* data,
  327. const uint16_t data_size);
  328. /**
  329. * Write comment to file.
  330. * @param flipper_file Pointer to a FlipperFile instance
  331. * @param data Comment text
  332. * @return True on success
  333. */
  334. bool flipper_file_write_comment(FlipperFile* flipper_file, string_t data);
  335. /**
  336. * Write comment to file. Plain C string version.
  337. * @param flipper_file Pointer to a FlipperFile instance
  338. * @param data Comment text
  339. * @return True on success
  340. */
  341. bool flipper_file_write_comment_cstr(FlipperFile* flipper_file, const char* data);
  342. /**
  343. * Removes the first matching key and its value from the file. Changes the RW pointer to an undefined position.
  344. * @param flipper_file Pointer to a FlipperFile instance
  345. * @param key Key
  346. * @return True on success
  347. */
  348. bool flipper_file_delete_key(FlipperFile* flipper_file, const char* key);
  349. /**
  350. * Updates the value of the first matching key to a string value. Changes the RW pointer to an undefined position.
  351. * @param flipper_file Pointer to a FlipperFile instance
  352. * @param key Key
  353. * @param data Value
  354. * @return True on success
  355. */
  356. bool flipper_file_update_string(FlipperFile* flipper_file, const char* key, string_t data);
  357. /**
  358. * Updates the value of the first matching key to a string value. Plain C version. Changes the RW pointer to an undefined position.
  359. * @param flipper_file Pointer to a FlipperFile instance
  360. * @param key Key
  361. * @param data Value
  362. * @return True on success
  363. */
  364. bool flipper_file_update_string_cstr(FlipperFile* flipper_file, const char* key, const char* data);
  365. /**
  366. * Updates the value of the first matching key to a uint32 array value. Changes the RW pointer to an undefined position.
  367. * @param flipper_file Pointer to a FlipperFile instance
  368. * @param key Key
  369. * @param data Value
  370. * @param data_size Values count
  371. * @return True on success
  372. */
  373. bool flipper_file_update_uint32(
  374. FlipperFile* flipper_file,
  375. const char* key,
  376. const uint32_t* data,
  377. const uint16_t data_size);
  378. /**
  379. * Updates the value of the first matching key to a int32 array value. Changes the RW pointer to an undefined position.
  380. * @param flipper_file Pointer to a FlipperFile instance
  381. * @param key Key
  382. * @param data Value
  383. * @param data_size Values count
  384. * @return True on success
  385. */
  386. bool flipper_file_update_int32(
  387. FlipperFile* flipper_file,
  388. const char* key,
  389. const int32_t* data,
  390. const uint16_t data_size);
  391. /**
  392. * Updates the value of the first matching key to a float array value. Changes the RW pointer to an undefined position.
  393. * @param flipper_file Pointer to a FlipperFile instance
  394. * @param key Key
  395. * @param data Value
  396. * @param data_size Values count
  397. * @return True on success
  398. */
  399. bool flipper_file_update_float(
  400. FlipperFile* flipper_file,
  401. const char* key,
  402. const float* data,
  403. const uint16_t data_size);
  404. /**
  405. * Updates the value of the first matching key to a hex array value. Changes the RW pointer to an undefined position.
  406. * @param flipper_file Pointer to a FlipperFile instance
  407. * @param key Key
  408. * @param data Value
  409. * @param data_size Values count
  410. * @return True on success
  411. */
  412. bool flipper_file_update_hex(
  413. FlipperFile* flipper_file,
  414. const char* key,
  415. const uint8_t* data,
  416. const uint16_t data_size);
  417. /** Get file descriptor.
  418. *
  419. * We higly don't recommend to use it.
  420. * This instance is owned by FlipperFile.
  421. * @param flipper_file
  422. * @return File*
  423. */
  424. File* flipper_file_get_file(FlipperFile* flipper_file);
  425. #ifdef __cplusplus
  426. }
  427. #endif