Fixed file reading (WRITE BROKEN)

This commit is contained in:
Maddox Werts 2024-08-28 08:45:02 -04:00
parent 2b34f8cbc7
commit fb3a0351c9
2 changed files with 19 additions and 9 deletions

View file

@ -3,7 +3,7 @@
#include <flipper_format.h> #include <flipper_format.h>
// Constructors // Constructors
FStorage* store_load() { FStorage* store_load(char* type) {
// Allocating memory for storage struct // Allocating memory for storage struct
FStorage* result = malloc(sizeof(FStorage)); FStorage* result = malloc(sizeof(FStorage));
result->valid = false; result->valid = false;
@ -34,7 +34,7 @@ FStorage* store_load() {
} }
// Reading data from file // 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"; result->data = "Couldn't read data";
FURI_LOG_E(TAG, result->data); FURI_LOG_E(TAG, result->data);
} }
@ -58,32 +58,42 @@ FStorage* store_load() {
return result; return result;
} }
void store_save(char* data) { void store_save(char* type, char* data) {
// Creating new file // Creating new file
Storage* _storage = furi_record_open(RECORD_STORAGE); Storage* _storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* _format = flipper_format_file_alloc(_storage); FlipperFormat* _format = flipper_format_file_alloc(_storage);
FuriString* _data = furi_string_alloc(); FuriString* _data = furi_string_alloc();
// Memory issue?
if (!_storage || !_format || !_data) {
FURI_LOG_E(TAG, "Memory Allocation Issue!");
return;
}
// Setting furi string data // Setting furi string data
furi_string_set_str(_data, data); furi_string_set_str(_data, data);
// Opening new file // Opening new file
if (!flipper_format_file_open_existing(_format, PATH)) { if (!flipper_format_file_open_existing(_format, PATH)) {
// New user? // 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 // Trying to create a new file
if (!flipper_format_file_open_new(_format, PATH)) { if (!flipper_format_file_open_new(_format, PATH)) {
FURI_LOG_E(TAG, "Failed to create new file at %s", 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; return;
} }
} }
// Writing string // Writing string
if (!flipper_format_write_string(_format, "Data", _data)) { if (!flipper_format_write_string(_format, type, _data)) {
FURI_LOG_E(TAG, "Failed to write to file"); FURI_LOG_E(TAG, "Failed to write to file at %s", PATH);
return; return;
} else {
FURI_LOG_I(TAG, "Successfully wrote to file at %s", PATH);
} }
// Closing file // Closing file

View file

@ -13,8 +13,8 @@ typedef struct {
} FStorage; } FStorage;
// Constructors // Constructors
FStorage* store_load(); FStorage* store_load(char* type);
void store_save(char* data); void store_save(char* type, char* data);
// Functions // Functions
void store_unload(FStorage* store); void store_unload(FStorage* store);