FlippyPass/project/backend/store.c

47 lines
No EOL
1.2 KiB
C

// Header
#include "store.h"
#include <furi.h>
#include <flipper_format.h>
// Constructors
FStorage* store_load() {
// Allocating memory for storage struct
FStorage* result = malloc(sizeof(FStorage));
// Reading storage
Storage* _storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* _format = flipper_format_file_alloc(_storage);
FuriString* _data = furi_string_alloc();
// Opening file
if (!flipper_format_file_open_existing(_format, PATH)) {
FURI_LOG_E(TAG, "Couldn't open %s", PATH);
return NULL;
} else if (!flipper_format_read_string(_format, "Data", _data)) {
FURI_LOG_E(TAG, "Couldn't read %s", PATH);
return NULL;
}
// Copy data into result
const char* _cstr_data = furi_string_get_cstr(_data);
result->data = malloc(strlen(_cstr_data) + 1);
strcpy(result->data, _cstr_data);
// Free the original string
furi_string_free(_data);
// Cleanup
flipper_format_free(_format);
furi_record_close(RECORD_STORAGE);
// Returning result
return result;
}
// Functions
void store_unload(FStorage* store) {
// Cleaning up memory
free(store->data);
free(store);
}