flipper_format.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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 uint32 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_uint32(
  262. FlipperFormat* flipper_format,
  263. const char* key,
  264. uint32_t* data,
  265. const uint16_t data_size);
  266. /**
  267. * Write key and array of uint32
  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_uint32(
  275. FlipperFormat* flipper_format,
  276. const char* key,
  277. const uint32_t* data,
  278. const uint16_t data_size);
  279. /**
  280. * Read array of int32 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_int32(
  288. FlipperFormat* flipper_format,
  289. const char* key,
  290. int32_t* data,
  291. const uint16_t data_size);
  292. /**
  293. * Write key and array of int32
  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_int32(
  301. FlipperFormat* flipper_format,
  302. const char* key,
  303. const int32_t* data,
  304. const uint16_t data_size);
  305. /**
  306. * Read array of bool 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_bool(
  314. FlipperFormat* flipper_format,
  315. const char* key,
  316. bool* data,
  317. const uint16_t data_size);
  318. /**
  319. * Write key and array of bool
  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_bool(
  327. FlipperFormat* flipper_format,
  328. const char* key,
  329. const bool* data,
  330. const uint16_t data_size);
  331. /**
  332. * Read array of float 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_float(
  340. FlipperFormat* flipper_format,
  341. const char* key,
  342. float* data,
  343. const uint16_t data_size);
  344. /**
  345. * Write key and array of float
  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_float(
  353. FlipperFormat* flipper_format,
  354. const char* key,
  355. const float* data,
  356. const uint16_t data_size);
  357. /**
  358. * Read array of hex-formatted bytes 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_hex(
  366. FlipperFormat* flipper_format,
  367. const char* key,
  368. uint8_t* data,
  369. const uint16_t data_size);
  370. /**
  371. * Write key and array of hex-formatted bytes
  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_hex(
  379. FlipperFormat* flipper_format,
  380. const char* key,
  381. const uint8_t* data,
  382. const uint16_t data_size);
  383. /**
  384. * Write comment
  385. * @param flipper_format Pointer to a FlipperFormat instance
  386. * @param data Comment text
  387. * @return True on success
  388. */
  389. bool flipper_format_write_comment(FlipperFormat* flipper_format, string_t data);
  390. /**
  391. * Write comment. Plain C string version.
  392. * @param flipper_format Pointer to a FlipperFormat instance
  393. * @param data Comment text
  394. * @return True on success
  395. */
  396. bool flipper_format_write_comment_cstr(FlipperFormat* flipper_format, const char* data);
  397. /**
  398. * Removes the first matching key and its value. Sets the RW pointer to a position of deleted data.
  399. * @param flipper_format Pointer to a FlipperFormat instance
  400. * @param key Key
  401. * @return True on success
  402. */
  403. bool flipper_format_delete_key(FlipperFormat* flipper_format, const char* key);
  404. /**
  405. * 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.
  406. * @param flipper_format Pointer to a FlipperFormat instance
  407. * @param key Key
  408. * @param data Value
  409. * @return True on success
  410. */
  411. bool flipper_format_update_string(FlipperFormat* flipper_format, const char* key, string_t data);
  412. /**
  413. * 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.
  414. * @param flipper_format Pointer to a FlipperFormat instance
  415. * @param key Key
  416. * @param data Value
  417. * @return True on success
  418. */
  419. bool flipper_format_update_string_cstr(
  420. FlipperFormat* flipper_format,
  421. const char* key,
  422. const char* data);
  423. /**
  424. * 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.
  425. * @param flipper_format Pointer to a FlipperFormat instance
  426. * @param key Key
  427. * @param data Value
  428. * @return True on success
  429. */
  430. bool flipper_format_update_uint32(
  431. FlipperFormat* flipper_format,
  432. const char* key,
  433. const uint32_t* data,
  434. const uint16_t data_size);
  435. /**
  436. * 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.
  437. * @param flipper_format Pointer to a FlipperFormat instance
  438. * @param key Key
  439. * @param data Value
  440. * @return True on success
  441. */
  442. bool flipper_format_update_int32(
  443. FlipperFormat* flipper_format,
  444. const char* key,
  445. const int32_t* data,
  446. const uint16_t data_size);
  447. /**
  448. * 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.
  449. * @param flipper_format Pointer to a FlipperFormat instance
  450. * @param key Key
  451. * @param data Value
  452. * @return True on success
  453. */
  454. bool flipper_format_update_bool(
  455. FlipperFormat* flipper_format,
  456. const char* key,
  457. const bool* data,
  458. const uint16_t data_size);
  459. /**
  460. * 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.
  461. * @param flipper_format Pointer to a FlipperFormat instance
  462. * @param key Key
  463. * @param data Value
  464. * @return True on success
  465. */
  466. bool flipper_format_update_float(
  467. FlipperFormat* flipper_format,
  468. const char* key,
  469. const float* data,
  470. const uint16_t data_size);
  471. /**
  472. * 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.
  473. * @param flipper_format Pointer to a FlipperFormat instance
  474. * @param key Key
  475. * @param data Value
  476. * @return True on success
  477. */
  478. bool flipper_format_update_hex(
  479. FlipperFormat* flipper_format,
  480. const char* key,
  481. const uint8_t* data,
  482. const uint16_t data_size);
  483. /**
  484. * Updates the value of the first matching key to a string value, or adds the key and value if the key did not exist.
  485. * Sets the RW pointer to a position at the end of inserted data.
  486. * @param flipper_format Pointer to a FlipperFormat instance
  487. * @param key Key
  488. * @param data Value
  489. * @return True on success
  490. */
  491. bool flipper_format_insert_or_update_string(
  492. FlipperFormat* flipper_format,
  493. const char* key,
  494. string_t data);
  495. /**
  496. * Updates the value of the first matching key to a string value, or adds the key and value if the key did not exist.
  497. * Plain C version.
  498. * 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_insert_or_update_string_cstr(
  505. FlipperFormat* flipper_format,
  506. const char* key,
  507. const char* data);
  508. /**
  509. * 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.
  510. * Sets the RW pointer to a position at the end of inserted data.
  511. * @param flipper_format Pointer to a FlipperFormat instance
  512. * @param key Key
  513. * @param data Value
  514. * @return True on success
  515. */
  516. bool flipper_format_insert_or_update_uint32(
  517. FlipperFormat* flipper_format,
  518. const char* key,
  519. const uint32_t* data,
  520. const uint16_t data_size);
  521. /**
  522. * 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.
  523. * Sets the RW pointer to a position at the end of inserted data.
  524. * @param flipper_format Pointer to a FlipperFormat instance
  525. * @param key Key
  526. * @param data Value
  527. * @return True on success
  528. */
  529. bool flipper_format_insert_or_update_int32(
  530. FlipperFormat* flipper_format,
  531. const char* key,
  532. const int32_t* data,
  533. const uint16_t data_size);
  534. /**
  535. * 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.
  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_bool(
  543. FlipperFormat* flipper_format,
  544. const char* key,
  545. const bool* data,
  546. const uint16_t data_size);
  547. /**
  548. * 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.
  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_float(
  556. FlipperFormat* flipper_format,
  557. const char* key,
  558. const float* data,
  559. const uint16_t data_size);
  560. /**
  561. * 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.
  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_hex(
  569. FlipperFormat* flipper_format,
  570. const char* key,
  571. const uint8_t* data,
  572. const uint16_t data_size);
  573. #ifdef __cplusplus
  574. }
  575. #endif