This repository has been archived on 2026-04-25. You can view files and clone it, but cannot push or open issues or pull requests.
FlippyPass/project/backend/store.c

127 lines
3.2 KiB
C
Raw Normal View History

2024-08-27 22:33:59 -05:00
// Header
#include "store.h"
#include <flipper_format.h>
2024-08-28 08:17:57 -05:00
// Functions - Private
void store_folder_init() {
// Opening storage record
Storage* storage = furi_record_open(RECORD_STORAGE);
// DEBUG
2024-09-05 15:42:59 -05:00
FURI_LOG_D(TAG, "Creating FlipperPass Folder");
2024-08-28 08:17:57 -05:00
// Creating folder
if (storage_simply_mkdir(storage, DIR)) {
2024-09-05 15:42:59 -05:00
FURI_LOG_D(TAG, "Created FPass Directory");
2024-08-28 08:17:57 -05:00
} else {
2024-09-05 15:42:59 -05:00
FURI_LOG_D(TAG, "FPass Directory already exists");
2024-08-28 08:17:57 -05:00
}
// Cleanup
furi_record_close(RECORD_STORAGE);
}
2024-08-30 08:06:47 -05:00
// Functions
char* store_load(char* type) {
2024-08-28 08:17:57 -05:00
// Init Folders
store_folder_init();
2024-08-27 22:33:59 -05:00
// Allocating memory for storage struct
2024-08-30 08:06:47 -05:00
char* result = malloc(sizeof(char));
2024-08-27 22:33:59 -05:00
// Reading storage
Storage* _storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* _format = flipper_format_file_alloc(_storage);
FuriString* _data = furi_string_alloc();
2024-08-27 23:43:31 -05:00
// Memory issue?
if (!_format || !_data) {
2024-08-30 08:06:47 -05:00
FURI_LOG_E(TAG, "Storage Memory Allication Issue");
return NULL;
2024-08-27 23:43:31 -05:00
}
2024-09-05 15:42:59 -05:00
// DEBUG
FURI_LOG_D(TAG, "Opening file at %s", PATH);
2024-08-27 22:33:59 -05:00
// Opening file
if (!flipper_format_file_open_existing(_format, PATH)) {
2024-08-30 08:06:47 -05:00
FURI_LOG_E(TAG, "Sorry, please register.");
2024-08-27 23:43:31 -05:00
// Cleaning up
furi_string_free(_data);
flipper_format_free(_format);
furi_record_close(RECORD_STORAGE);
2024-08-30 08:06:47 -05:00
return NULL;
2024-08-27 23:43:31 -05:00
}
2024-09-05 15:42:59 -05:00
// DEBUG
FURI_LOG_D(TAG, "Reading data from file...");
2024-08-27 23:43:31 -05:00
// Reading data from file
2024-08-28 07:45:02 -05:00
if (!flipper_format_read_string(_format, type, _data)) {
2024-08-30 08:06:47 -05:00
FURI_LOG_E(TAG, "Couldn't read data");
return NULL;
2024-08-27 22:33:59 -05:00
}
2024-09-05 15:42:59 -05:00
// DEBUG
FURI_LOG_D(TAG, "Copying data into string");
2024-08-27 22:33:59 -05:00
// Copy data into result
const char* _cstr_data = furi_string_get_cstr(_data);
2024-08-30 08:06:47 -05:00
result = malloc(strlen(_cstr_data) + 1);
strcpy(result, _cstr_data);
2024-08-27 22:33:59 -05:00
// Free the original string
furi_string_free(_data);
// Cleanup
flipper_format_free(_format);
furi_record_close(RECORD_STORAGE);
2024-09-05 15:42:59 -05:00
// DEBUG
FURI_LOG_D(TAG, "File read successfully");
2024-08-27 22:33:59 -05:00
// Returning result
return result;
}
2024-08-28 07:45:02 -05:00
void store_save(char* type, char* data) {
2024-08-27 23:43:31 -05:00
// Creating new file
Storage* _storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* _format = flipper_format_file_alloc(_storage);
FuriString* _data = furi_string_alloc();
2024-08-28 07:45:02 -05:00
// Memory issue?
if (!_storage || !_format || !_data) {
FURI_LOG_E(TAG, "Memory Allocation Issue!");
return;
}
2024-08-27 23:43:31 -05:00
// Setting furi string data
furi_string_set_str(_data, data);
// Opening new file
2024-08-28 08:17:57 -05:00
if (!flipper_format_file_open_always(_format, PATH)) {
// DEBUG
FURI_LOG_E(TAG, "Failed to create/open file at %s", PATH);
// Cleanup
furi_string_free(_data);
flipper_format_free(_format);
furi_record_close(RECORD_STORAGE);
return;
2024-08-27 23:43:31 -05:00
}
// Writing string
2024-08-28 07:45:02 -05:00
if (!flipper_format_write_string(_format, type, _data)) {
FURI_LOG_E(TAG, "Failed to write to file at %s", PATH);
2024-08-27 23:43:31 -05:00
return;
2024-08-28 07:45:02 -05:00
} else {
2024-09-05 15:42:59 -05:00
FURI_LOG_D(TAG, "Successfully wrote to file at %s", PATH);
2024-08-27 23:43:31 -05:00
}
// Closing file
furi_string_free(_data);
flipper_format_free(_format);
furi_record_close(RECORD_STORAGE);
2024-08-27 22:33:59 -05:00
}