flipchess_file.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "flipchess_file.h"
  2. #include <storage/storage.h>
  3. #include <loader/loader.h>
  4. // #define FLIPCHESS_APP_BASE_FOLDER APP_BOARDA_PATH("flipchess")
  5. #define FLIPCHESS_APP_BASE_FOLDER EXT_PATH("apps_data/flipchess")
  6. #define FLIPCHESS_APP_BASE_FOLDER_PATH(path) FLIPCHESS_APP_BASE_FOLDER "/" path
  7. #define FLIPCHESS_BOARD_FILE_NAME "board_fen.txt"
  8. #define FLIPCHESS_BOARD_FILE_NAME_BAK "board_fen.bak"
  9. #define FLIPCHESS_BOARD_PATH FLIPCHESS_APP_BASE_FOLDER_PATH(FLIPCHESS_BOARD_FILE_NAME)
  10. #define FLIPCHESS_BOARD_PATH_BAK FLIPCHESS_APP_BASE_FOLDER_PATH(FLIPCHESS_BOARD_FILE_NAME_BAK)
  11. #define FILE_MAX_PATH_LEN 48
  12. #define FILE_MAX_CHARS 94
  13. bool flipchess_has_file(const FlipChessFile file_type, const char* file_name, const bool remove) {
  14. bool ret = false;
  15. const char* path;
  16. char path_buf[FILE_MAX_PATH_LEN] = {0};
  17. if(file_type == FlipChessFileBoard) {
  18. path = FLIPCHESS_BOARD_PATH;
  19. } else {
  20. strcpy(path_buf, FLIPCHESS_APP_BASE_FOLDER); // 22
  21. strcpy(path_buf + strlen(path_buf), "/");
  22. strcpy(path_buf + strlen(path_buf), file_name);
  23. path = path_buf;
  24. }
  25. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  26. if(remove) {
  27. ret = storage_simply_remove(fs_api, path);
  28. } else {
  29. ret = storage_file_exists(fs_api, path);
  30. }
  31. furi_record_close(RECORD_STORAGE);
  32. return ret;
  33. }
  34. bool flipchess_load_file(char* contents, const FlipChessFile file_type, const char* file_name) {
  35. bool ret = false;
  36. const char* path;
  37. char path_buf[FILE_MAX_PATH_LEN] = {0};
  38. if(file_type == FlipChessFileBoard) {
  39. path = FLIPCHESS_BOARD_PATH;
  40. } else {
  41. strcpy(path_buf, FLIPCHESS_APP_BASE_FOLDER); // 22
  42. strcpy(path_buf + strlen(path_buf), "/");
  43. strcpy(path_buf + strlen(path_buf), file_name);
  44. path = path_buf;
  45. }
  46. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  47. File* settings_file = storage_file_alloc(fs_api);
  48. if(storage_file_open(settings_file, path, FSAM_READ, FSOM_OPEN_EXISTING)) {
  49. char chr;
  50. int i = 0;
  51. while((storage_file_read(settings_file, &chr, 1) == 1) &&
  52. !storage_file_eof(settings_file)) {
  53. if(i < FILE_MAX_CHARS) {
  54. contents[i] = chr;
  55. }
  56. i++;
  57. }
  58. ret = true;
  59. } else {
  60. contents[0] = '\0';
  61. ret = false;
  62. }
  63. storage_file_close(settings_file);
  64. storage_file_free(settings_file);
  65. furi_record_close(RECORD_STORAGE);
  66. if(strlen(contents) > 0) {
  67. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  68. FileInfo layout_file_info;
  69. FS_Error file_check_err = storage_common_stat(fs_api, path, &layout_file_info);
  70. furi_record_close(RECORD_STORAGE);
  71. if(file_check_err != FSE_OK) {
  72. contents[0] = '\0';
  73. ret = false;
  74. }
  75. // if(layout_file_info.size != 256) {
  76. // memzero(settings, strlen(settings));
  77. // settings[0] = '\0';
  78. // }
  79. }
  80. return ret;
  81. }
  82. bool flipchess_save_file(
  83. const char* settings,
  84. const FlipChessFile file_type,
  85. const char* file_name,
  86. const bool append,
  87. const bool overwrite) {
  88. bool ret = false;
  89. const char* path;
  90. const char* path_bak;
  91. char path_buf[FILE_MAX_PATH_LEN] = {0};
  92. if(file_type == FlipChessFileBoard) {
  93. path = FLIPCHESS_BOARD_PATH;
  94. path_bak = FLIPCHESS_BOARD_PATH_BAK;
  95. } else {
  96. strcpy(path_buf, FLIPCHESS_APP_BASE_FOLDER); // 22
  97. strcpy(path_buf + strlen(path_buf), "/");
  98. strcpy(path_buf + strlen(path_buf), file_name);
  99. path = path_buf;
  100. path_bak = NULL;
  101. }
  102. int open_mode = FSOM_OPEN_ALWAYS;
  103. if(append) {
  104. open_mode = FSOM_OPEN_APPEND;
  105. }
  106. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  107. // try to create the folder
  108. storage_simply_mkdir(fs_api, FLIPCHESS_APP_BASE_FOLDER);
  109. if(overwrite) {
  110. storage_simply_remove(fs_api, path);
  111. }
  112. File* settings_file = storage_file_alloc(fs_api);
  113. if(storage_file_open(settings_file, path, FSAM_WRITE, open_mode)) {
  114. storage_file_write(settings_file, settings, strlen(settings));
  115. storage_file_write(settings_file, "\n", 1);
  116. ret = true;
  117. }
  118. storage_file_close(settings_file);
  119. storage_file_free(settings_file);
  120. if(path_bak != NULL) {
  121. if(overwrite) {
  122. storage_simply_remove(fs_api, path_bak);
  123. }
  124. File* settings_file_bak = storage_file_alloc(fs_api);
  125. if(storage_file_open(settings_file_bak, path_bak, FSAM_WRITE, open_mode)) {
  126. storage_file_write(settings_file_bak, settings, strlen(settings));
  127. storage_file_write(settings_file_bak, "\n", 1);
  128. }
  129. storage_file_close(settings_file_bak);
  130. storage_file_free(settings_file_bak);
  131. }
  132. furi_record_close(RECORD_STORAGE);
  133. return ret;
  134. }