// Header #include "store.h" #include // Constants #define FILE_PATH_SIZE 128 // Functions - Private void store_folder_init() { // Opening storage record Storage* storage = furi_record_open(RECORD_STORAGE); // DEBUG FURI_LOG_D(TAG, "Creating FlipperPass Folder"); // Creating folder if (storage_simply_mkdir(storage, DIR)) { FURI_LOG_D(TAG, "Created FPass Directory"); } else { FURI_LOG_D(TAG, "FPass Directory already exists"); } // Cleanup furi_record_close(RECORD_STORAGE); } // Functions char* store_load(Store_File_Path path, char* type) { // Setting file path char* file_path = malloc(sizeof(char) * FILE_PATH_SIZE); switch (path) { case Store_File_Path_Data: file_path = F_DATA; break; } // Init Folders store_folder_init(); // Allocating memory for storage struct char* result = malloc(sizeof(char)); // 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) { FURI_LOG_E(TAG, "Storage Memory Allication Issue"); return NULL; } // DEBUG FURI_LOG_D(TAG, "Opening file at %s", file_path); // Opening file if (!flipper_format_file_open_existing(_format, file_path)) { FURI_LOG_E(TAG, "Sorry, please register."); // Cleaning up furi_string_free(_data); flipper_format_free(_format); furi_record_close(RECORD_STORAGE); return NULL; } // DEBUG FURI_LOG_D(TAG, "Reading data from file..."); // Reading data from file if (!flipper_format_read_string(_format, type, _data)) { FURI_LOG_E(TAG, "Couldn't read data"); return NULL; } // DEBUG FURI_LOG_D(TAG, "Copying data into string"); // Copy data into result const char* _cstr_data = furi_string_get_cstr(_data); result = malloc(strlen(_cstr_data) + 1); strcpy(result, _cstr_data); // Free the original string furi_string_free(_data); // Cleanup flipper_format_free(_format); furi_record_close(RECORD_STORAGE); // DEBUG FURI_LOG_D(TAG, "File read successfully"); // Returning result return result; } void store_save(Store_File_Path path, char* type, char* data) { // Setting file path char* file_path = malloc(sizeof(char) * FILE_PATH_SIZE); switch (path) { case Store_File_Path_Data: file_path = F_DATA; break; } // 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_always(_format, file_path)) { // DEBUG FURI_LOG_E(TAG, "Failed to create/open file at %s", file_path); // Cleanup furi_string_free(_data); flipper_format_free(_format); furi_record_close(RECORD_STORAGE); return; } // Writing string if (!flipper_format_write_string(_format, type, _data)) { FURI_LOG_E(TAG, "Failed to write to file at %s", file_path); return; } else { FURI_LOG_D(TAG, "Successfully wrote to file at %s", file_path); } // Closing file furi_string_free(_data); flipper_format_free(_format); furi_record_close(RECORD_STORAGE); }