flipper_format.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /**
  2. * @file flipper_format.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. * FlipperFormat format = flipper_format_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_format_file_open_new(format, EXT_PATH("flipper_format_test"))) break;
  54. * if(!flipper_format_write_header_cstr(format, "Flipper Test File", version)) break;
  55. * if(!flipper_format_write_comment_cstr(format, "Just test file")) break;
  56. * if(!flipper_format_write_string_cstr(format, "String", string_value)) break;
  57. * if(!flipper_format_write_uint32(format, "UINT", &uint32_value, 1)) break;
  58. * if(!flipper_format_write_hex(format, "Hex Array", array, array_size)) break;
  59. *
  60. * // signal that the file was written successfully
  61. * } while(0);
  62. *
  63. * flipper_format_free(file);
  64. * ~~~~~~~~~~~~~~~~~~~~~
  65. *
  66. * Reading:
  67. *
  68. * ~~~~~~~~~~~~~~~~~~~~~
  69. * FlipperFormat file = flipper_format_file_alloc(storage);
  70. *
  71. * do {
  72. * uint32_t version = 1;
  73. * string_t file_type;
  74. * string_t string_value;
  75. * uint32_t uint32_value = 1;
  76. * uint16_t array_size = 4;
  77. * uint8_t* array[array_size] = {0};
  78. * string_init(file_type);
  79. * string_init(string_value);
  80. *
  81. * if(!flipper_format_file_open_existing(file, EXT_PATH("flipper_format_test"))) break;
  82. * if(!flipper_format_read_header(file, file_type, &version)) break;
  83. * if(!flipper_format_read_string(file, "String", string_value)) break;
  84. * if(!flipper_format_read_uint32(file, "UINT", &uint32_value, 1)) break;
  85. * if(!flipper_format_read_hex(file, "Hex Array", array, array_size)) break;
  86. *
  87. * // signal that the file was read successfully
  88. * } while(0);
  89. *
  90. * flipper_format_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. typedef struct FlipperFormat FlipperFormat;
  102. /**
  103. * Allocate FlipperFormat as string.
  104. * @return FlipperFormat* pointer to a FlipperFormat instance
  105. */
  106. FlipperFormat* flipper_format_string_alloc();
  107. /**
  108. * Allocate FlipperFormat as file.
  109. * @return FlipperFormat* pointer to a FlipperFormat instance
  110. */
  111. FlipperFormat* flipper_format_file_alloc(Storage* storage);
  112. /**
  113. * Allocate FlipperFormat as file, buffered mode.
  114. * @return FlipperFormat* pointer to a FlipperFormat instance
  115. */
  116. FlipperFormat* flipper_format_buffered_file_alloc(Storage* storage);
  117. /**
  118. * Open existing file.
  119. * Use only if FlipperFormat allocated as a file.
  120. * @param flipper_format Pointer to a FlipperFormat instance
  121. * @param path File path
  122. * @return True on success
  123. */
  124. bool flipper_format_file_open_existing(FlipperFormat* flipper_format, const char* path);
  125. /**
  126. * Open existing file, buffered mode.
  127. * Use only if FlipperFormat allocated as a file.
  128. * @param flipper_format Pointer to a FlipperFormat instance
  129. * @param path File path
  130. * @return True on success
  131. */
  132. bool flipper_format_buffered_file_open_existing(FlipperFormat* flipper_format, const char* path);
  133. /**
  134. * Open existing file for writing and add values to the end of file.
  135. * Use only if FlipperFormat allocated as a file.
  136. * @param flipper_format Pointer to a FlipperFormat instance
  137. * @param path File path
  138. * @return True on success
  139. */
  140. bool flipper_format_file_open_append(FlipperFormat* flipper_format, const char* path);
  141. /**
  142. * Open file. Creates a new file, or deletes the contents of the file if it already exists.
  143. * Use only if FlipperFormat allocated as a file.
  144. * @param flipper_format Pointer to a FlipperFormat instance
  145. * @param path File path
  146. * @return True on success
  147. */
  148. bool flipper_format_file_open_always(FlipperFormat* flipper_format, const char* path);
  149. /**
  150. * Open file. Creates a new file, fails if file already exists.
  151. * Use only if FlipperFormat allocated as a file.
  152. * @param flipper_format Pointer to a FlipperFormat instance
  153. * @param path File path
  154. * @return True on success
  155. */
  156. bool flipper_format_file_open_new(FlipperFormat* flipper_format, const char* path);
  157. /**
  158. * Closes the file, use only if FlipperFormat allocated as a file.
  159. * @param flipper_format
  160. * @return true
  161. * @return false
  162. */
  163. bool flipper_format_file_close(FlipperFormat* flipper_format);
  164. /**
  165. * Closes the file, use only if FlipperFormat allocated as a buffered file.
  166. * @param flipper_format
  167. * @return true
  168. * @return false
  169. */
  170. bool flipper_format_buffered_file_close(FlipperFormat* flipper_format);
  171. /**
  172. * Free FlipperFormat.
  173. * @param flipper_format Pointer to a FlipperFormat instance
  174. */
  175. void flipper_format_free(FlipperFormat* flipper_format);
  176. /**
  177. * Set FlipperFormat mode.
  178. * @param flipper_format Pointer to a FlipperFormat instance
  179. * @param strict_mode True obligates not to skip valid fields. False by default.
  180. */
  181. void flipper_format_set_strict_mode(FlipperFormat* flipper_format, bool strict_mode);
  182. /**
  183. * Rewind the RW pointer.
  184. * @param flipper_format Pointer to a FlipperFormat instance
  185. * @return True on success
  186. */
  187. bool flipper_format_rewind(FlipperFormat* flipper_format);
  188. /**
  189. * Move the RW pointer at the end. Can be useful if you want to add some data after reading.
  190. * @param flipper_format Pointer to a FlipperFormat instance
  191. * @return True on success
  192. */
  193. bool flipper_format_seek_to_end(FlipperFormat* flipper_format);
  194. /**
  195. * Check if the key exists.
  196. * @param flipper_format Pointer to a FlipperFormat instance
  197. * @param key Key
  198. * @return true key exists
  199. * @return false key is not exists
  200. */
  201. bool flipper_format_key_exist(FlipperFormat* flipper_format, const char* key);
  202. /**
  203. * Read the header (file type and version).
  204. * @param flipper_format Pointer to a FlipperFormat instance
  205. * @param filetype File type string
  206. * @param version Version Value
  207. * @return True on success
  208. */
  209. bool flipper_format_read_header(
  210. FlipperFormat* flipper_format,
  211. string_t filetype,
  212. uint32_t* version);
  213. /**
  214. * Write the header (file type and version).
  215. * @param flipper_format Pointer to a FlipperFormat instance
  216. * @param filetype File type string
  217. * @param version Version Value
  218. * @return True on success
  219. */
  220. bool flipper_format_write_header(
  221. FlipperFormat* flipper_format,
  222. string_t filetype,
  223. const uint32_t version);
  224. /**
  225. * Write the header (file type and version). Plain C string version.
  226. * @param flipper_format Pointer to a FlipperFormat instance
  227. * @param filetype File type string
  228. * @param version Version Value
  229. * @return True on success
  230. */
  231. bool flipper_format_write_header_cstr(
  232. FlipperFormat* flipper_format,
  233. const char* filetype,
  234. const uint32_t version);
  235. /**
  236. * Get the count of values by key
  237. * @param flipper_format Pointer to a FlipperFormat instance
  238. * @param key
  239. * @param count
  240. * @return bool
  241. */
  242. bool flipper_format_get_value_count(
  243. FlipperFormat* flipper_format,
  244. const char* key,
  245. uint32_t* count);
  246. /**
  247. * Read a string by key
  248. * @param flipper_format Pointer to a FlipperFormat instance
  249. * @param key Key
  250. * @param data Value
  251. * @return True on success
  252. */
  253. bool flipper_format_read_string(FlipperFormat* flipper_format, const char* key, string_t data);
  254. /**
  255. * Write key and string
  256. * @param flipper_format Pointer to a FlipperFormat instance
  257. * @param key Key
  258. * @param data Value
  259. * @return True on success
  260. */
  261. bool flipper_format_write_string(FlipperFormat* flipper_format, const char* key, string_t data);
  262. /**
  263. * Write key and string. Plain C string version.
  264. * @param flipper_format Pointer to a FlipperFormat instance
  265. * @param key Key
  266. * @param data Value
  267. * @return True on success
  268. */
  269. bool flipper_format_write_string_cstr(
  270. FlipperFormat* flipper_format,
  271. const char* key,
  272. const char* data);
  273. /**
  274. * Read array of uint64 in hex format by key
  275. * @param flipper_format Pointer to a FlipperFormat instance
  276. * @param key Key
  277. * @param data Value
  278. * @param data_size Values count
  279. * @return True on success
  280. */
  281. bool flipper_format_read_hex_uint64(
  282. FlipperFormat* flipper_format,
  283. const char* key,
  284. uint64_t* data,
  285. const uint16_t data_size);
  286. /**
  287. * Write key and array of uint64 in hex format
  288. * @param flipper_format Pointer to a FlipperFormat instance
  289. * @param key Key
  290. * @param data Value
  291. * @param data_size Values count
  292. * @return True on success
  293. */
  294. bool flipper_format_write_hex_uint64(
  295. FlipperFormat* flipper_format,
  296. const char* key,
  297. const uint64_t* data,
  298. const uint16_t data_size);
  299. /**
  300. * Read array of uint32 by key
  301. * @param flipper_format Pointer to a FlipperFormat instance
  302. * @param key Key
  303. * @param data Value
  304. * @param data_size Values count
  305. * @return True on success
  306. */
  307. bool flipper_format_read_uint32(
  308. FlipperFormat* flipper_format,
  309. const char* key,
  310. uint32_t* data,
  311. const uint16_t data_size);
  312. /**
  313. * Write key and array of uint32
  314. * @param flipper_format Pointer to a FlipperFormat instance
  315. * @param key Key
  316. * @param data Value
  317. * @param data_size Values count
  318. * @return True on success
  319. */
  320. bool flipper_format_write_uint32(
  321. FlipperFormat* flipper_format,
  322. const char* key,
  323. const uint32_t* data,
  324. const uint16_t data_size);
  325. /**
  326. * Read array of int32 by key
  327. * @param flipper_format Pointer to a FlipperFormat instance
  328. * @param key Key
  329. * @param data Value
  330. * @param data_size Values count
  331. * @return True on success
  332. */
  333. bool flipper_format_read_int32(
  334. FlipperFormat* flipper_format,
  335. const char* key,
  336. int32_t* data,
  337. const uint16_t data_size);
  338. /**
  339. * Write key and array of int32
  340. * @param flipper_format Pointer to a FlipperFormat instance
  341. * @param key Key
  342. * @param data Value
  343. * @param data_size Values count
  344. * @return True on success
  345. */
  346. bool flipper_format_write_int32(
  347. FlipperFormat* flipper_format,
  348. const char* key,
  349. const int32_t* data,
  350. const uint16_t data_size);
  351. /**
  352. * Read array of bool by key
  353. * @param flipper_format Pointer to a FlipperFormat instance
  354. * @param key Key
  355. * @param data Value
  356. * @param data_size Values count
  357. * @return True on success
  358. */
  359. bool flipper_format_read_bool(
  360. FlipperFormat* flipper_format,
  361. const char* key,
  362. bool* data,
  363. const uint16_t data_size);
  364. /**
  365. * Write key and array of bool
  366. * @param flipper_format Pointer to a FlipperFormat instance
  367. * @param key Key
  368. * @param data Value
  369. * @param data_size Values count
  370. * @return True on success
  371. */
  372. bool flipper_format_write_bool(
  373. FlipperFormat* flipper_format,
  374. const char* key,
  375. const bool* data,
  376. const uint16_t data_size);
  377. /**
  378. * Read array of float by key
  379. * @param flipper_format Pointer to a FlipperFormat instance
  380. * @param key Key
  381. * @param data Value
  382. * @param data_size Values count
  383. * @return True on success
  384. */
  385. bool flipper_format_read_float(
  386. FlipperFormat* flipper_format,
  387. const char* key,
  388. float* data,
  389. const uint16_t data_size);
  390. /**
  391. * Write key and array of float
  392. * @param flipper_format Pointer to a FlipperFormat instance
  393. * @param key Key
  394. * @param data Value
  395. * @param data_size Values count
  396. * @return True on success
  397. */
  398. bool flipper_format_write_float(
  399. FlipperFormat* flipper_format,
  400. const char* key,
  401. const float* data,
  402. const uint16_t data_size);
  403. /**
  404. * Read array of hex-formatted bytes by key
  405. * @param flipper_format Pointer to a FlipperFormat instance
  406. * @param key Key
  407. * @param data Value
  408. * @param data_size Values count
  409. * @return True on success
  410. */
  411. bool flipper_format_read_hex(
  412. FlipperFormat* flipper_format,
  413. const char* key,
  414. uint8_t* data,
  415. const uint16_t data_size);
  416. /**
  417. * Write key and array of hex-formatted bytes
  418. * @param flipper_format Pointer to a FlipperFormat instance
  419. * @param key Key
  420. * @param data Value
  421. * @param data_size Values count
  422. * @return True on success
  423. */
  424. bool flipper_format_write_hex(
  425. FlipperFormat* flipper_format,
  426. const char* key,
  427. const uint8_t* data,
  428. const uint16_t data_size);
  429. /**
  430. * Write comment
  431. * @param flipper_format Pointer to a FlipperFormat instance
  432. * @param data Comment text
  433. * @return True on success
  434. */
  435. bool flipper_format_write_comment(FlipperFormat* flipper_format, string_t data);
  436. /**
  437. * Write comment. Plain C string version.
  438. * @param flipper_format Pointer to a FlipperFormat instance
  439. * @param data Comment text
  440. * @return True on success
  441. */
  442. bool flipper_format_write_comment_cstr(FlipperFormat* flipper_format, const char* data);
  443. /**
  444. * Removes the first matching key and its value. Sets the RW pointer to a position of deleted data.
  445. * @param flipper_format Pointer to a FlipperFormat instance
  446. * @param key Key
  447. * @return True on success
  448. */
  449. bool flipper_format_delete_key(FlipperFormat* flipper_format, const char* key);
  450. /**
  451. * Updates the value of the first matching key to a string value. Sets the RW pointer to a position at the end of inserted data.
  452. * @param flipper_format Pointer to a FlipperFormat instance
  453. * @param key Key
  454. * @param data Value
  455. * @return True on success
  456. */
  457. bool flipper_format_update_string(FlipperFormat* flipper_format, const char* key, string_t data);
  458. /**
  459. * Updates the value of the first matching key to a string value. Plain C version. Sets the RW pointer to a position at the end of inserted data.
  460. * @param flipper_format Pointer to a FlipperFormat instance
  461. * @param key Key
  462. * @param data Value
  463. * @return True on success
  464. */
  465. bool flipper_format_update_string_cstr(
  466. FlipperFormat* flipper_format,
  467. const char* key,
  468. const char* data);
  469. /**
  470. * Updates the value of the first matching key to a uint32 array value. Sets the RW pointer to a position at the end of inserted data.
  471. * @param flipper_format Pointer to a FlipperFormat instance
  472. * @param key Key
  473. * @param data Value
  474. * @return True on success
  475. */
  476. bool flipper_format_update_uint32(
  477. FlipperFormat* flipper_format,
  478. const char* key,
  479. const uint32_t* data,
  480. const uint16_t data_size);
  481. /**
  482. * Updates the value of the first matching key to a int32 array value. Sets the RW pointer to a position at the end of inserted data.
  483. * @param flipper_format Pointer to a FlipperFormat instance
  484. * @param key Key
  485. * @param data Value
  486. * @return True on success
  487. */
  488. bool flipper_format_update_int32(
  489. FlipperFormat* flipper_format,
  490. const char* key,
  491. const int32_t* data,
  492. const uint16_t data_size);
  493. /**
  494. * Updates the value of the first matching key to a bool array value. Sets the RW pointer to a position at the end of inserted data.
  495. * @param flipper_format Pointer to a FlipperFormat instance
  496. * @param key Key
  497. * @param data Value
  498. * @return True on success
  499. */
  500. bool flipper_format_update_bool(
  501. FlipperFormat* flipper_format,
  502. const char* key,
  503. const bool* data,
  504. const uint16_t data_size);
  505. /**
  506. * Updates the value of the first matching key to a float array value. Sets the RW pointer to a position at the end of inserted data.
  507. * @param flipper_format Pointer to a FlipperFormat instance
  508. * @param key Key
  509. * @param data Value
  510. * @return True on success
  511. */
  512. bool flipper_format_update_float(
  513. FlipperFormat* flipper_format,
  514. const char* key,
  515. const float* data,
  516. const uint16_t data_size);
  517. /**
  518. * Updates the value of the first matching key to an array of hex-formatted bytes. Sets the RW pointer to a position at the end of inserted data.
  519. * @param flipper_format Pointer to a FlipperFormat instance
  520. * @param key Key
  521. * @param data Value
  522. * @return True on success
  523. */
  524. bool flipper_format_update_hex(
  525. FlipperFormat* flipper_format,
  526. const char* key,
  527. const uint8_t* data,
  528. const uint16_t data_size);
  529. /**
  530. * Updates the value of the first matching key to a string value, or adds the key and value if the key did not exist.
  531. * Sets the RW pointer to a position at the end of inserted data.
  532. * @param flipper_format Pointer to a FlipperFormat instance
  533. * @param key Key
  534. * @param data Value
  535. * @return True on success
  536. */
  537. bool flipper_format_insert_or_update_string(
  538. FlipperFormat* flipper_format,
  539. const char* key,
  540. string_t data);
  541. /**
  542. * Updates the value of the first matching key to a string value, or adds the key and value if the key did not exist.
  543. * Plain C version.
  544. * Sets the RW pointer to a position at the end of inserted data.
  545. * @param flipper_format Pointer to a FlipperFormat instance
  546. * @param key Key
  547. * @param data Value
  548. * @return True on success
  549. */
  550. bool flipper_format_insert_or_update_string_cstr(
  551. FlipperFormat* flipper_format,
  552. const char* key,
  553. const char* data);
  554. /**
  555. * Updates the value of the first matching key to a uint32 array value, or adds the key and value if the key did not exist.
  556. * Sets the RW pointer to a position at the end of inserted data.
  557. * @param flipper_format Pointer to a FlipperFormat instance
  558. * @param key Key
  559. * @param data Value
  560. * @return True on success
  561. */
  562. bool flipper_format_insert_or_update_uint32(
  563. FlipperFormat* flipper_format,
  564. const char* key,
  565. const uint32_t* data,
  566. const uint16_t data_size);
  567. /**
  568. * Updates the value of the first matching key to a int32 array value, or adds the key and value if the key did not exist.
  569. * Sets the RW pointer to a position at the end of inserted data.
  570. * @param flipper_format Pointer to a FlipperFormat instance
  571. * @param key Key
  572. * @param data Value
  573. * @return True on success
  574. */
  575. bool flipper_format_insert_or_update_int32(
  576. FlipperFormat* flipper_format,
  577. const char* key,
  578. const int32_t* data,
  579. const uint16_t data_size);
  580. /**
  581. * Updates the value of the first matching key to a bool array value, or adds the key and value if the key did not exist.
  582. * Sets the RW pointer to a position at the end of inserted data.
  583. * @param flipper_format Pointer to a FlipperFormat instance
  584. * @param key Key
  585. * @param data Value
  586. * @return True on success
  587. */
  588. bool flipper_format_insert_or_update_bool(
  589. FlipperFormat* flipper_format,
  590. const char* key,
  591. const bool* data,
  592. const uint16_t data_size);
  593. /**
  594. * Updates the value of the first matching key to a float array value, or adds the key and value if the key did not exist.
  595. * Sets the RW pointer to a position at the end of inserted data.
  596. * @param flipper_format Pointer to a FlipperFormat instance
  597. * @param key Key
  598. * @param data Value
  599. * @return True on success
  600. */
  601. bool flipper_format_insert_or_update_float(
  602. FlipperFormat* flipper_format,
  603. const char* key,
  604. const float* data,
  605. const uint16_t data_size);
  606. /**
  607. * Updates the value of the first matching key to an array of hex-formatted bytes, or adds the key and value if the key did not exist.
  608. *Sets the RW pointer to a position at the end of inserted data.
  609. * @param flipper_format Pointer to a FlipperFormat instance
  610. * @param key Key
  611. * @param data Value
  612. * @return True on success
  613. */
  614. bool flipper_format_insert_or_update_hex(
  615. FlipperFormat* flipper_format,
  616. const char* key,
  617. const uint8_t* data,
  618. const uint16_t data_size);
  619. #ifdef __cplusplus
  620. }
  621. #endif