flipper_format.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. * Read the header (file type and version).
  170. * @param flipper_format Pointer to a FlipperFormat instance
  171. * @param filetype File type string
  172. * @param version Version Value
  173. * @return True on success
  174. */
  175. bool flipper_format_read_header(
  176. FlipperFormat* flipper_format,
  177. string_t filetype,
  178. uint32_t* version);
  179. /**
  180. * Write the header (file type and version).
  181. * @param flipper_format Pointer to a FlipperFormat instance
  182. * @param filetype File type string
  183. * @param version Version Value
  184. * @return True on success
  185. */
  186. bool flipper_format_write_header(
  187. FlipperFormat* flipper_format,
  188. string_t filetype,
  189. const uint32_t version);
  190. /**
  191. * Write the header (file type and version). Plain C string version.
  192. * @param flipper_format Pointer to a FlipperFormat instance
  193. * @param filetype File type string
  194. * @param version Version Value
  195. * @return True on success
  196. */
  197. bool flipper_format_write_header_cstr(
  198. FlipperFormat* flipper_format,
  199. const char* filetype,
  200. const uint32_t version);
  201. /**
  202. * Get the count of values by key
  203. * @param flipper_format Pointer to a FlipperFormat instance
  204. * @param key
  205. * @param count
  206. * @return bool
  207. */
  208. bool flipper_format_get_value_count(
  209. FlipperFormat* flipper_format,
  210. const char* key,
  211. uint32_t* count);
  212. /**
  213. * Read a string by key
  214. * @param flipper_format Pointer to a FlipperFormat instance
  215. * @param key Key
  216. * @param data Value
  217. * @return True on success
  218. */
  219. bool flipper_format_read_string(FlipperFormat* flipper_format, const char* key, string_t data);
  220. /**
  221. * Write key and string
  222. * @param flipper_format Pointer to a FlipperFormat instance
  223. * @param key Key
  224. * @param data Value
  225. * @return True on success
  226. */
  227. bool flipper_format_write_string(FlipperFormat* flipper_format, const char* key, string_t data);
  228. /**
  229. * Write key and string. Plain C string version.
  230. * @param flipper_format Pointer to a FlipperFormat instance
  231. * @param key Key
  232. * @param data Value
  233. * @return True on success
  234. */
  235. bool flipper_format_write_string_cstr(
  236. FlipperFormat* flipper_format,
  237. const char* key,
  238. const char* data);
  239. /**
  240. * Read array of uint32 by key
  241. * @param flipper_format Pointer to a FlipperFormat instance
  242. * @param key Key
  243. * @param data Value
  244. * @param data_size Values count
  245. * @return True on success
  246. */
  247. bool flipper_format_read_uint32(
  248. FlipperFormat* flipper_format,
  249. const char* key,
  250. uint32_t* data,
  251. const uint16_t data_size);
  252. /**
  253. * Write key and array of uint32
  254. * @param flipper_format Pointer to a FlipperFormat instance
  255. * @param key Key
  256. * @param data Value
  257. * @param data_size Values count
  258. * @return True on success
  259. */
  260. bool flipper_format_write_uint32(
  261. FlipperFormat* flipper_format,
  262. const char* key,
  263. const uint32_t* data,
  264. const uint16_t data_size);
  265. /**
  266. * Read array of int32 by key
  267. * @param flipper_format Pointer to a FlipperFormat instance
  268. * @param key Key
  269. * @param data Value
  270. * @param data_size Values count
  271. * @return True on success
  272. */
  273. bool flipper_format_read_int32(
  274. FlipperFormat* flipper_format,
  275. const char* key,
  276. int32_t* data,
  277. const uint16_t data_size);
  278. /**
  279. * Write key and array of int32
  280. * @param flipper_format Pointer to a FlipperFormat instance
  281. * @param key Key
  282. * @param data Value
  283. * @param data_size Values count
  284. * @return True on success
  285. */
  286. bool flipper_format_write_int32(
  287. FlipperFormat* flipper_format,
  288. const char* key,
  289. const int32_t* data,
  290. const uint16_t data_size);
  291. /**
  292. * Read array of float by key
  293. * @param flipper_format Pointer to a FlipperFormat instance
  294. * @param key Key
  295. * @param data Value
  296. * @param data_size Values count
  297. * @return True on success
  298. */
  299. bool flipper_format_read_float(
  300. FlipperFormat* flipper_format,
  301. const char* key,
  302. float* data,
  303. const uint16_t data_size);
  304. /**
  305. * Write key and array of float
  306. * @param flipper_format Pointer to a FlipperFormat instance
  307. * @param key Key
  308. * @param data Value
  309. * @param data_size Values count
  310. * @return True on success
  311. */
  312. bool flipper_format_write_float(
  313. FlipperFormat* flipper_format,
  314. const char* key,
  315. const float* data,
  316. const uint16_t data_size);
  317. /**
  318. * Read array of hex-formatted bytes by key
  319. * @param flipper_format Pointer to a FlipperFormat instance
  320. * @param key Key
  321. * @param data Value
  322. * @param data_size Values count
  323. * @return True on success
  324. */
  325. bool flipper_format_read_hex(
  326. FlipperFormat* flipper_format,
  327. const char* key,
  328. uint8_t* data,
  329. const uint16_t data_size);
  330. /**
  331. * Write key and array of hex-formatted bytes
  332. * @param flipper_format Pointer to a FlipperFormat instance
  333. * @param key Key
  334. * @param data Value
  335. * @param data_size Values count
  336. * @return True on success
  337. */
  338. bool flipper_format_write_hex(
  339. FlipperFormat* flipper_format,
  340. const char* key,
  341. const uint8_t* data,
  342. const uint16_t data_size);
  343. /**
  344. * Write comment
  345. * @param flipper_format Pointer to a FlipperFormat instance
  346. * @param data Comment text
  347. * @return True on success
  348. */
  349. bool flipper_format_write_comment(FlipperFormat* flipper_format, string_t data);
  350. /**
  351. * Write comment. Plain C string version.
  352. * @param flipper_format Pointer to a FlipperFormat instance
  353. * @param data Comment text
  354. * @return True on success
  355. */
  356. bool flipper_format_write_comment_cstr(FlipperFormat* flipper_format, const char* data);
  357. /**
  358. * Removes the first matching key and its value. Sets the RW pointer to a position of deleted data.
  359. * @param flipper_format Pointer to a FlipperFormat instance
  360. * @param key Key
  361. * @return True on success
  362. */
  363. bool flipper_format_delete_key(FlipperFormat* flipper_format, const char* key);
  364. /**
  365. * 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.
  366. * @param flipper_format Pointer to a FlipperFormat instance
  367. * @param key Key
  368. * @param data Value
  369. * @return True on success
  370. */
  371. bool flipper_format_update_string(FlipperFormat* flipper_format, const char* key, string_t data);
  372. /**
  373. * 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.
  374. * @param flipper_format Pointer to a FlipperFormat instance
  375. * @param key Key
  376. * @param data Value
  377. * @return True on success
  378. */
  379. bool flipper_format_update_string_cstr(
  380. FlipperFormat* flipper_format,
  381. const char* key,
  382. const char* data);
  383. /**
  384. * 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.
  385. * @param flipper_format Pointer to a FlipperFormat instance
  386. * @param key Key
  387. * @param data Value
  388. * @return True on success
  389. */
  390. bool flipper_format_update_uint32(
  391. FlipperFormat* flipper_format,
  392. const char* key,
  393. const uint32_t* data,
  394. const uint16_t data_size);
  395. /**
  396. * 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.
  397. * @param flipper_format Pointer to a FlipperFormat instance
  398. * @param key Key
  399. * @param data Value
  400. * @return True on success
  401. */
  402. bool flipper_format_update_int32(
  403. FlipperFormat* flipper_format,
  404. const char* key,
  405. const int32_t* data,
  406. const uint16_t data_size);
  407. /**
  408. * 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.
  409. * @param flipper_format Pointer to a FlipperFormat instance
  410. * @param key Key
  411. * @param data Value
  412. * @return True on success
  413. */
  414. bool flipper_format_update_float(
  415. FlipperFormat* flipper_format,
  416. const char* key,
  417. const float* data,
  418. const uint16_t data_size);
  419. /**
  420. * 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.
  421. * @param flipper_format Pointer to a FlipperFormat instance
  422. * @param key Key
  423. * @param data Value
  424. * @return True on success
  425. */
  426. bool flipper_format_update_hex(
  427. FlipperFormat* flipper_format,
  428. const char* key,
  429. const uint8_t* data,
  430. const uint16_t data_size);
  431. #ifdef __cplusplus
  432. }
  433. #endif