flipper_file.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. * Open existing file.
  118. * @param flipper_file Pointer to a FlipperFile instance
  119. * @param filename File name and path
  120. * @return True on success
  121. */
  122. bool flipper_file_open_existing(FlipperFile* flipper_file, const char* filename);
  123. /**
  124. * Open existing file for writing and add values to the end of file.
  125. * @param flipper_file Pointer to a FlipperFile instance
  126. * @param filename File name and path
  127. * @return True on success
  128. */
  129. bool flipper_file_open_append(FlipperFile* flipper_file, const char* filename);
  130. /**
  131. * Open file. Creates a new file, or deletes the contents of the file if it already exists.
  132. * @param flipper_file Pointer to a FlipperFile instance
  133. * @param filename File name and path
  134. * @return True on success
  135. */
  136. bool flipper_file_open_always(FlipperFile* flipper_file, const char* filename);
  137. /**
  138. * Open file. Creates a new file, fails if file already exists.
  139. * @param flipper_file Pointer to a FlipperFile instance
  140. * @param filename File name and path
  141. * @return True on success
  142. */
  143. bool flipper_file_open_new(FlipperFile* flipper_file, const char* filename);
  144. /**
  145. * Close the file.
  146. * @param flipper_file Pointer to a FlipperFile instance
  147. * @return True on success
  148. */
  149. bool flipper_file_close(FlipperFile* flipper_file);
  150. /**
  151. * Rewind the file RW pointer.
  152. * @param flipper_file Pointer to a FlipperFile instance
  153. * @return True on success
  154. */
  155. bool flipper_file_rewind(FlipperFile* flipper_file);
  156. /**
  157. * Read the header (file type and version) from the file.
  158. * @param flipper_file Pointer to a FlipperFile instance
  159. * @param filetype File type string
  160. * @param version Version Value
  161. * @return True on success
  162. */
  163. bool flipper_file_read_header(FlipperFile* flipper_file, string_t filetype, uint32_t* version);
  164. /**
  165. * Write the header (file type and version) to the file.
  166. * @param flipper_file Pointer to a FlipperFile instance
  167. * @param filetype File type string
  168. * @param version Version Value
  169. * @return True on success
  170. */
  171. bool flipper_file_write_header(
  172. FlipperFile* flipper_file,
  173. string_t filetype,
  174. const uint32_t version);
  175. /**
  176. * Write the header (file type and version) to the file. Plain C string version.
  177. * @param flipper_file Pointer to a FlipperFile instance
  178. * @param filetype File type string
  179. * @param version Version Value
  180. * @return True on success
  181. */
  182. bool flipper_file_write_header_cstr(
  183. FlipperFile* flipper_file,
  184. const char* filetype,
  185. const uint32_t version);
  186. /**
  187. * Get the count of values by key
  188. * @param flipper_file
  189. * @param key
  190. * @param count
  191. * @return bool
  192. */
  193. bool flipper_file_get_value_count(FlipperFile* flipper_file, const char* key, uint32_t* count);
  194. /**
  195. * Read a string from a file by Key
  196. * @param flipper_file Pointer to a FlipperFile instance
  197. * @param key Key
  198. * @param data Value
  199. * @return True on success
  200. */
  201. bool flipper_file_read_string(FlipperFile* flipper_file, const char* key, string_t data);
  202. /**
  203. * Write key and string to file.
  204. * @param flipper_file Pointer to a FlipperFile instance
  205. * @param key Key
  206. * @param data Value
  207. * @return True on success
  208. */
  209. bool flipper_file_write_string(FlipperFile* flipper_file, const char* key, string_t data);
  210. /**
  211. * Write key and string to file. Plain C string version.
  212. * @param flipper_file Pointer to a FlipperFile instance
  213. * @param key Key
  214. * @param data Value
  215. * @return True on success
  216. */
  217. bool flipper_file_write_string_cstr(FlipperFile* flipper_file, const char* key, const char* data);
  218. /**
  219. * Read array of uint32 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 Values count
  224. * @return True on success
  225. */
  226. bool flipper_file_read_uint32(
  227. FlipperFile* flipper_file,
  228. const char* key,
  229. uint32_t* data,
  230. const uint16_t data_size);
  231. /**
  232. * Write key and array of uint32 to file.
  233. * @param flipper_file Pointer to a FlipperFile instance
  234. * @param key Key
  235. * @param data Value
  236. * @param data_size Values count
  237. * @return True on success
  238. */
  239. bool flipper_file_write_uint32(
  240. FlipperFile* flipper_file,
  241. const char* key,
  242. const uint32_t* data,
  243. const uint16_t data_size);
  244. /**
  245. * Read array of int32 from a file by Key
  246. * @param flipper_file Pointer to a FlipperFile instance
  247. * @param key Key
  248. * @param data Value
  249. * @param data_size Values count
  250. * @return True on success
  251. */
  252. bool flipper_file_read_int32(
  253. FlipperFile* flipper_file,
  254. const char* key,
  255. int32_t* data,
  256. const uint16_t data_size);
  257. /**
  258. * Write key and array of int32 to file.
  259. * @param flipper_file Pointer to a FlipperFile instance
  260. * @param key Key
  261. * @param data Value
  262. * @param data_size Values count
  263. * @return True on success
  264. */
  265. bool flipper_file_write_int32(
  266. FlipperFile* flipper_file,
  267. const char* key,
  268. const int32_t* data,
  269. const uint16_t data_size);
  270. /**
  271. * Read array of float from a file by Key
  272. * @param flipper_file Pointer to a FlipperFile instance
  273. * @param key Key
  274. * @param data Value
  275. * @param data_size Values count
  276. * @return True on success
  277. */
  278. bool flipper_file_read_float(
  279. FlipperFile* flipper_file,
  280. const char* key,
  281. float* data,
  282. const uint16_t data_size);
  283. /**
  284. * Write key and array of float to file.
  285. * @param flipper_file Pointer to a FlipperFile instance
  286. * @param key Key
  287. * @param data Value
  288. * @param data_size Values count
  289. * @return True on success
  290. */
  291. bool flipper_file_write_float(
  292. FlipperFile* flipper_file,
  293. const char* key,
  294. const float* data,
  295. const uint16_t data_size);
  296. /**
  297. * Read hex array from a file by Key
  298. * @param flipper_file Pointer to a FlipperFile instance
  299. * @param key Key
  300. * @param data Value
  301. * @param data_size Value size
  302. * @return True on success
  303. */
  304. bool flipper_file_read_hex(
  305. FlipperFile* flipper_file,
  306. const char* key,
  307. uint8_t* data,
  308. const uint16_t data_size);
  309. /**
  310. * Write key and hex array to file.
  311. * @param flipper_file Pointer to a FlipperFile instance
  312. * @param key Key
  313. * @param data Value
  314. * @param data_size Values count
  315. * @return True on success
  316. */
  317. bool flipper_file_write_hex(
  318. FlipperFile* flipper_file,
  319. const char* key,
  320. const uint8_t* data,
  321. const uint16_t data_size);
  322. /**
  323. * Write comment to file.
  324. * @param flipper_file Pointer to a FlipperFile instance
  325. * @param data Comment text
  326. * @return True on success
  327. */
  328. bool flipper_file_write_comment(FlipperFile* flipper_file, string_t data);
  329. /**
  330. * Write comment to file. Plain C string version.
  331. * @param flipper_file Pointer to a FlipperFile instance
  332. * @param data Comment text
  333. * @return True on success
  334. */
  335. bool flipper_file_write_comment_cstr(FlipperFile* flipper_file, const char* data);
  336. /**
  337. * Removes the first matching key and its value from the file. Changes the RW pointer to an undefined position.
  338. * @param flipper_file Pointer to a FlipperFile instance
  339. * @param key Key
  340. * @return True on success
  341. */
  342. bool flipper_file_delete_key(FlipperFile* flipper_file, const char* key);
  343. /**
  344. * Updates the value of the first matching key to a string value. Changes the RW pointer to an undefined position.
  345. * @param flipper_file Pointer to a FlipperFile instance
  346. * @param key Key
  347. * @param data Value
  348. * @return True on success
  349. */
  350. bool flipper_file_update_string(FlipperFile* flipper_file, const char* key, string_t data);
  351. /**
  352. * Updates the value of the first matching key to a string value. Plain C version. Changes the RW pointer to an undefined position.
  353. * @param flipper_file Pointer to a FlipperFile instance
  354. * @param key Key
  355. * @param data Value
  356. * @return True on success
  357. */
  358. bool flipper_file_update_string_cstr(FlipperFile* flipper_file, const char* key, const char* data);
  359. /**
  360. * Updates the value of the first matching key to a uint32 array value. Changes the RW pointer to an undefined position.
  361. * @param flipper_file Pointer to a FlipperFile instance
  362. * @param key Key
  363. * @param data Value
  364. * @param data_size Values count
  365. * @return True on success
  366. */
  367. bool flipper_file_update_uint32(
  368. FlipperFile* flipper_file,
  369. const char* key,
  370. const uint32_t* data,
  371. const uint16_t data_size);
  372. /**
  373. * Updates the value of the first matching key to a int32 array value. Changes the RW pointer to an undefined position.
  374. * @param flipper_file Pointer to a FlipperFile instance
  375. * @param key Key
  376. * @param data Value
  377. * @param data_size Values count
  378. * @return True on success
  379. */
  380. bool flipper_file_update_int32(
  381. FlipperFile* flipper_file,
  382. const char* key,
  383. const int32_t* data,
  384. const uint16_t data_size);
  385. /**
  386. * Updates the value of the first matching key to a float array value. Changes the RW pointer to an undefined position.
  387. * @param flipper_file Pointer to a FlipperFile instance
  388. * @param key Key
  389. * @param data Value
  390. * @param data_size Values count
  391. * @return True on success
  392. */
  393. bool flipper_file_update_float(
  394. FlipperFile* flipper_file,
  395. const char* key,
  396. const float* data,
  397. const uint16_t data_size);
  398. /**
  399. * Updates the value of the first matching key to a hex array value. Changes the RW pointer to an undefined position.
  400. * @param flipper_file Pointer to a FlipperFile instance
  401. * @param key Key
  402. * @param data Value
  403. * @param data_size Values count
  404. * @return True on success
  405. */
  406. bool flipper_file_update_hex(
  407. FlipperFile* flipper_file,
  408. const char* key,
  409. const uint8_t* data,
  410. const uint16_t data_size);
  411. /** Get file descriptor.
  412. *
  413. * We higly don't recommend to use it.
  414. * This instance is owned by FlipperFile.
  415. * @param flipper_file
  416. * @return File*
  417. */
  418. File* flipper_file_get_file(FlipperFile* flipper_file);
  419. #ifdef __cplusplus
  420. }
  421. #endif