Struan Clark %!s(int64=2) %!d(string=hai) anos
pai
achega
5aa637975b
Modificáronse 3 ficheiros con 59 adicións e 65 borrados
  1. 55 60
      helpers/flipbip_file.c
  2. 2 3
      helpers/flipbip_file.h
  3. 2 2
      views/flipbip_scene_1.c

+ 55 - 60
helpers/flipbip_file.c

@@ -1,73 +1,68 @@
 #include "flipbip_file.h"
-
 #include "../flipbip.h"
 
-#include <storage/storage.h>
-#include <lib/flipper_format/flipper_format.h>
-
-bool flipbip_load_file(const char* file_path) {
-    furi_assert(file_path);
-
-    bool result = false;
-    FuriString* temp_str;
-    temp_str = furi_string_alloc();
-
-    Storage* storage = furi_record_open(RECORD_STORAGE);
-    FlipperFormat* file = flipper_format_file_alloc(storage);
+#include "../crypto/memzero.h"
 
-    do {
-        if(!flipper_format_file_open_existing(file, file_path)) break;
-
-        uint32_t version = 0;
-        if(!flipper_format_read_header(file, temp_str, &version)) break;
-        if(furi_string_cmp_str(temp_str, "FlipBIP") ||
-           (version != 1)) {
-            break;
-        }
+#include <storage/storage.h>
 
-        if(!flipper_format_read_string(file, "X", temp_str)) {
-            break;
+#define FLIPBIP_APP_BASE_FOLDER EXT_PATH("apps_data/flipbip")
+// #define FLIPBIP_SETTINGS_FILE_NAME ".flipbip.dat"
+#define FLIPBIP_SETTINGS_FILE_NAME ".flipbip.txt"
+#define FLIPBIP_SETTINGS_PATH FLIPBIP_APP_BASE_FOLDER "/" FLIPBIP_SETTINGS_FILE_NAME
+
+bool flipbip_load_settings(char* settings) {
+    Storage *fs_api = furi_record_open(RECORD_STORAGE);
+    File* settings_file = storage_file_alloc(fs_api);
+    if(storage_file_open(settings_file, FLIPBIP_SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
+        char chr;
+        int i = 0;
+        while((storage_file_read(settings_file, &chr, 1) == 1) &&
+              !storage_file_eof(settings_file) && !isspace(chr)) {
+            settings[i] = chr;
+            i++;
         }
-
-        result = true;
-    } while(false);
-
+    } else {
+        memzero(settings, strlen(settings));
+        settings[0] = '\0';
+    }
+    storage_file_close(settings_file);
+    storage_file_free(settings_file);
     furi_record_close(RECORD_STORAGE);
-    flipper_format_free(file);
-    furi_string_free(temp_str);
 
-    return result;
+    // if(!strlen(settings) == 0) {
+    //     Storage* fs_api = furi_record_open(RECORD_STORAGE);
+    //     FileInfo layout_file_info;
+    //     FS_Error file_check_err = storage_common_stat(
+    //         fs_api, FLIPBIP_SETTINGS_PATH, &layout_file_info);
+    //     furi_record_close(RECORD_STORAGE);
+    //     if(file_check_err != FSE_OK) {
+    //         memzero(settings, strlen(settings));
+    //         settings[0] = '\0';
+    //         return;
+    //     }
+    //     if(layout_file_info.size != 256) {
+    //         memzero(settings, strlen(settings));
+    //         settings[0] = '\0';
+    //     }
+    // }
+
+    return true;
 }
 
-bool flipbip_save_file(const char* file_path) {
-    furi_assert(file_path);
-
-    bool result = false;
-    FuriString* temp_str;
-    temp_str = furi_string_alloc();
-
-    Storage* storage = furi_record_open(RECORD_STORAGE);
-    FlipperFormat* file = flipper_format_file_alloc(storage);
-
-    do {
-        if(!flipper_format_file_open_existing(file, file_path) ||
-           !flipper_format_file_open_new(file, file_path)) break;
-
-        //uint32_t version = 1;
-        //temp_str = "FlipBIP";
-        if(!flipper_format_write_header_cstr(file, "FlipBIP", 1)) break;
-
-        //temp_str = "12345abcde";
-        if(!flipper_format_write_string_cstr(file, "X", "12345abcde")) {
-            break;
-        }
-
-        result = true;
-    } while(false);
-
+bool flipbip_save_settings(const char* settings) {
+    Storage* fs_api = furi_record_open(RECORD_STORAGE);
+    File* settings_file = storage_file_alloc(fs_api);
+    storage_common_mkdir(fs_api, FLIPBIP_APP_BASE_FOLDER);
+    if(storage_file_open(settings_file, FLIPBIP_SETTINGS_PATH, FSAM_WRITE, FSOM_OPEN_ALWAYS)) {
+        storage_file_write(
+            settings_file,
+            settings,
+            strlen(settings));
+        storage_file_write(settings_file, "\n", 1);
+    }
+    storage_file_close(settings_file);
+    storage_file_free(settings_file);
     furi_record_close(RECORD_STORAGE);
-    flipper_format_free(file);
-    furi_string_free(temp_str);
 
-    return result;
+    return true;
 }

+ 2 - 3
helpers/flipbip_file.h

@@ -1,5 +1,4 @@
 #include <stdbool.h>
-#include <stdint.h>
 
-bool flipbip_load_file(const char* file_path);
-bool flipbip_save_file(const char* file_path);
+bool flipbip_load_settings(char* settings);
+bool flipbip_save_settings(const char* settings);

+ 2 - 2
views/flipbip_scene_1.c

@@ -271,8 +271,8 @@ static void flipbip_scene_1_model_init(FlipBipScene1Model* const model, const in
     model->strength = strength;
     model->mnemonic = mnemonic_generate(model->strength);
 
-    flipbip_save_file(EXT_PATH("flipbip.dat"));
-    flipbip_load_file(EXT_PATH("flipbip.dat"));
+    flipbip_save_settings("123456beep");
+    // flipbip_load_file(EXT_PATH("flipbip.dat"));
     
     // test mnemonic
     //model->mnemonic = "wealth budget salt video delay obey neutral tail sure soda hold rubber joy movie boat raccoon tornado noise off inmate payment patch group topple";