// Header #include "store.h" #include // Constructors FStorage* store_load() { // Allocating memory for storage struct FStorage* result = malloc(sizeof(FStorage)); result->valid = false; // Reading storage Storage* _storage = furi_record_open(RECORD_STORAGE); FlipperFormat* _format = flipper_format_file_alloc(_storage); FuriString* _data = furi_string_alloc(); // Memory issue? if (!_format || !_data) { result->data = "Storage Memory Allication Issue"; FURI_LOG_E(TAG, result->data); return result; } // Opening file if (!flipper_format_file_open_existing(_format, PATH)) { result->data = "Sorry, please register."; FURI_LOG_E(TAG, result->data); // Cleaning up furi_string_free(_data); flipper_format_free(_format); furi_record_close(RECORD_STORAGE); return result; } // Reading data from file if (!flipper_format_read_string(_format, "Data", _data)) { result->data = "Couldn't read data"; FURI_LOG_E(TAG, result->data); } // 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); // We're valid! result->valid = true; // Returning result return result; } void store_save(char* data) { // Creating new file Storage* _storage = furi_record_open(RECORD_STORAGE); FlipperFormat* _format = flipper_format_file_alloc(_storage); FuriString* _data = furi_string_alloc(); // 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."); // 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); return; } } // Writing string if (!flipper_format_write_string(_format, "Data", _data)) { FURI_LOG_E(TAG, "Failed to write to file"); return; } // Closing file furi_string_free(_data); flipper_format_free(_format); furi_record_close(RECORD_STORAGE); } // Functions void store_unload(FStorage* store) { // Cleaning up memory free(store->data); free(store); }