Fixed file reading (WRITE BROKEN)
This commit is contained in:
parent
2b34f8cbc7
commit
fb3a0351c9
2 changed files with 19 additions and 9 deletions
|
@ -3,7 +3,7 @@
|
|||
#include <flipper_format.h>
|
||||
|
||||
// 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
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue