flipper_format.h 19 KB

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