flipbip_file.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "flipbip_file.h"
  2. #include "../flipbip.h"
  3. #include <storage/storage.h>
  4. #include <lib/flipper_format/flipper_format.h>
  5. bool flipbip_load_file(const char* file_path) {
  6. furi_assert(file_path);
  7. bool result = false;
  8. FuriString* temp_str;
  9. temp_str = furi_string_alloc();
  10. Storage* storage = furi_record_open(RECORD_STORAGE);
  11. FlipperFormat* file = flipper_format_file_alloc(storage);
  12. do {
  13. if(!flipper_format_file_open_existing(file, file_path)) break;
  14. uint32_t version = 0;
  15. if(!flipper_format_read_header(file, temp_str, &version)) break;
  16. if(furi_string_cmp_str(temp_str, "FlipBIP") ||
  17. (version != 1)) {
  18. break;
  19. }
  20. if(!flipper_format_read_string(file, "X", temp_str)) {
  21. break;
  22. }
  23. result = true;
  24. } while(false);
  25. furi_record_close(RECORD_STORAGE);
  26. flipper_format_free(file);
  27. furi_string_free(temp_str);
  28. return result;
  29. }
  30. bool flipbip_save_file(const char* file_path) {
  31. furi_assert(file_path);
  32. bool result = false;
  33. FuriString* temp_str;
  34. temp_str = furi_string_alloc();
  35. Storage* storage = furi_record_open(RECORD_STORAGE);
  36. FlipperFormat* file = flipper_format_file_alloc(storage);
  37. do {
  38. if(!flipper_format_file_open_existing(file, file_path) ||
  39. !flipper_format_file_open_new(file, file_path)) break;
  40. //uint32_t version = 1;
  41. //temp_str = "FlipBIP";
  42. if(!flipper_format_write_header_cstr(file, "FlipBIP", 1)) break;
  43. //temp_str = "12345abcde";
  44. if(!flipper_format_write_string_cstr(file, "X", "12345abcde")) {
  45. break;
  46. }
  47. result = true;
  48. } while(false);
  49. furi_record_close(RECORD_STORAGE);
  50. flipper_format_free(file);
  51. furi_string_free(temp_str);
  52. return result;
  53. }