flipper_format.h 20 KB

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