From fb3a0351c975c88f7e8017f5de5d4ac567cf7bbf Mon Sep 17 00:00:00 2001 From: Maddox Werts Date: Wed, 28 Aug 2024 08:45:02 -0400 Subject: [PATCH] Fixed file reading (WRITE BROKEN) --- project/backend/store.c | 24 +++++++++++++++++------- project/backend/store.h | 4 ++-- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/project/backend/store.c b/project/backend/store.c index 8b662c6..6336a85 100644 --- a/project/backend/store.c +++ b/project/backend/store.c @@ -3,7 +3,7 @@ #include // Constructors -FStorage* store_load() { +FStorage* store_load(char* type) { // Allocating memory for storage struct FStorage* result = malloc(sizeof(FStorage)); result->valid = false; @@ -34,7 +34,7 @@ FStorage* store_load() { } // Reading data from file - if (!flipper_format_read_string(_format, "Data", _data)) { + if (!flipper_format_read_string(_format, type, _data)) { result->data = "Couldn't read data"; FURI_LOG_E(TAG, result->data); } @@ -58,32 +58,42 @@ FStorage* store_load() { return result; } -void store_save(char* data) { +void store_save(char* type, char* data) { // Creating new file Storage* _storage = furi_record_open(RECORD_STORAGE); FlipperFormat* _format = flipper_format_file_alloc(_storage); - FuriString* _data = furi_string_alloc(); + // Memory issue? + if (!_storage || !_format || !_data) { + FURI_LOG_E(TAG, "Memory Allocation Issue!"); + return; + } + // Setting furi string data furi_string_set_str(_data, data); // Opening new file if (!flipper_format_file_open_existing(_format, PATH)) { // New user? - FURI_LOG_I(TAG, "Creating new user."); + FURI_LOG_I(TAG, "File not found, creating user at %s", PATH); // Trying to create a new file if (!flipper_format_file_open_new(_format, PATH)) { FURI_LOG_E(TAG, "Failed to create new file at %s", PATH); + furi_string_free(_data); + flipper_format_free(_format); + furi_record_close(RECORD_STORAGE); return; } } // Writing string - if (!flipper_format_write_string(_format, "Data", _data)) { - FURI_LOG_E(TAG, "Failed to write to file"); + if (!flipper_format_write_string(_format, type, _data)) { + FURI_LOG_E(TAG, "Failed to write to file at %s", PATH); return; + } else { + FURI_LOG_I(TAG, "Successfully wrote to file at %s", PATH); } // Closing file diff --git a/project/backend/store.h b/project/backend/store.h index 267774c..5279cc4 100644 --- a/project/backend/store.h +++ b/project/backend/store.h @@ -13,8 +13,8 @@ typedef struct { } FStorage; // Constructors -FStorage* store_load(); -void store_save(char* data); +FStorage* store_load(char* type); +void store_save(char* type, char* data); // Functions void store_unload(FStorage* store);