flipper_format.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 float 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_float(
  314. FlipperFormat* flipper_format,
  315. const char* key,
  316. float* data,
  317. const uint16_t data_size);
  318. /**
  319. * Write key and array of float
  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_float(
  327. FlipperFormat* flipper_format,
  328. const char* key,
  329. const float* data,
  330. const uint16_t data_size);
  331. /**
  332. * Read array of hex-formatted bytes 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_hex(
  340. FlipperFormat* flipper_format,
  341. const char* key,
  342. uint8_t* data,
  343. const uint16_t data_size);
  344. /**
  345. * Write key and array of hex-formatted bytes
  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_hex(
  353. FlipperFormat* flipper_format,
  354. const char* key,
  355. const uint8_t* data,
  356. const uint16_t data_size);
  357. /**
  358. * Write comment
  359. * @param flipper_format Pointer to a FlipperFormat instance
  360. * @param data Comment text
  361. * @return True on success
  362. */
  363. bool flipper_format_write_comment(FlipperFormat* flipper_format, string_t data);
  364. /**
  365. * Write comment. Plain C string version.
  366. * @param flipper_format Pointer to a FlipperFormat instance
  367. * @param data Comment text
  368. * @return True on success
  369. */
  370. bool flipper_format_write_comment_cstr(FlipperFormat* flipper_format, const char* data);
  371. /**
  372. * Removes the first matching key and its value. Sets the RW pointer to a position of deleted data.
  373. * @param flipper_format Pointer to a FlipperFormat instance
  374. * @param key Key
  375. * @return True on success
  376. */
  377. bool flipper_format_delete_key(FlipperFormat* flipper_format, const char* key);
  378. /**
  379. * 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.
  380. * @param flipper_format Pointer to a FlipperFormat instance
  381. * @param key Key
  382. * @param data Value
  383. * @return True on success
  384. */
  385. bool flipper_format_update_string(FlipperFormat* flipper_format, const char* key, string_t data);
  386. /**
  387. * 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.
  388. * @param flipper_format Pointer to a FlipperFormat instance
  389. * @param key Key
  390. * @param data Value
  391. * @return True on success
  392. */
  393. bool flipper_format_update_string_cstr(
  394. FlipperFormat* flipper_format,
  395. const char* key,
  396. const char* data);
  397. /**
  398. * 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.
  399. * @param flipper_format Pointer to a FlipperFormat instance
  400. * @param key Key
  401. * @param data Value
  402. * @return True on success
  403. */
  404. bool flipper_format_update_uint32(
  405. FlipperFormat* flipper_format,
  406. const char* key,
  407. const uint32_t* data,
  408. const uint16_t data_size);
  409. /**
  410. * 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.
  411. * @param flipper_format Pointer to a FlipperFormat instance
  412. * @param key Key
  413. * @param data Value
  414. * @return True on success
  415. */
  416. bool flipper_format_update_int32(
  417. FlipperFormat* flipper_format,
  418. const char* key,
  419. const int32_t* data,
  420. const uint16_t data_size);
  421. /**
  422. * 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.
  423. * @param flipper_format Pointer to a FlipperFormat instance
  424. * @param key Key
  425. * @param data Value
  426. * @return True on success
  427. */
  428. bool flipper_format_update_float(
  429. FlipperFormat* flipper_format,
  430. const char* key,
  431. const float* data,
  432. const uint16_t data_size);
  433. /**
  434. * 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.
  435. * @param flipper_format Pointer to a FlipperFormat instance
  436. * @param key Key
  437. * @param data Value
  438. * @return True on success
  439. */
  440. bool flipper_format_update_hex(
  441. FlipperFormat* flipper_format,
  442. const char* key,
  443. const uint8_t* data,
  444. const uint16_t data_size);
  445. /**
  446. * Updates the value of the first matching key to a string value, or adds the key and value if the key did not exist.
  447. * Sets the RW pointer to a position at the end of inserted data.
  448. * @param flipper_format Pointer to a FlipperFormat instance
  449. * @param key Key
  450. * @param data Value
  451. * @return True on success
  452. */
  453. bool flipper_format_insert_or_update_string(
  454. FlipperFormat* flipper_format,
  455. const char* key,
  456. string_t data);
  457. /**
  458. * Updates the value of the first matching key to a string value, or adds the key and value if the key did not exist.
  459. * Plain C version.
  460. * 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_insert_or_update_string_cstr(
  467. FlipperFormat* flipper_format,
  468. const char* key,
  469. const char* data);
  470. /**
  471. * 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.
  472. * 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_insert_or_update_uint32(
  479. FlipperFormat* flipper_format,
  480. const char* key,
  481. const uint32_t* data,
  482. const uint16_t data_size);
  483. /**
  484. * 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.
  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_int32(
  492. FlipperFormat* flipper_format,
  493. const char* key,
  494. const int32_t* data,
  495. const uint16_t data_size);
  496. /**
  497. * 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.
  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_float(
  505. FlipperFormat* flipper_format,
  506. const char* key,
  507. const float* data,
  508. const uint16_t data_size);
  509. /**
  510. * 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.
  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_hex(
  518. FlipperFormat* flipper_format,
  519. const char* key,
  520. const uint8_t* data,
  521. const uint16_t data_size);
  522. #ifdef __cplusplus
  523. }
  524. #endif