Skip to content

Commit

Permalink
file
Browse files Browse the repository at this point in the history
  • Loading branch information
xtruan committed Mar 6, 2023
1 parent 7507a66 commit 5aa6379
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 65 deletions.
115 changes: 55 additions & 60 deletions helpers/flipbip_file.c
Original file line number Diff line number Diff line change
@@ -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;
}
5 changes: 2 additions & 3 deletions helpers/flipbip_file.h
Original file line number Diff line number Diff line change
@@ -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);
4 changes: 2 additions & 2 deletions views/flipbip_scene_1.c
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 5aa6379

Please sign in to comment.