[FL-1926] Flipper File Format addons (#753)
* Flipper file format: remove C wrapper * Flipper file format: open append, float, uint32_t as array, delete key, value count * Flipper file format: fix scratchpad location * Flipper file format: add EOL on append * SubGHZ keystore: update encryption type read and write * Flipper File Format: enhanced version * Flipper File Format: fix naming * Flipper File Format: fix "open" subset naming * Flipper File Format: tests * Flipper File Format: file helper naming * SubGHZ keystore: merge with current state of flipper file format * Flipper File Format: update make recipe * Flipper File Format: open new file method
This commit is contained in:
204
lib/flipper_file/file_helper.c
Normal file
204
lib/flipper_file/file_helper.c
Normal file
@@ -0,0 +1,204 @@
|
||||
#include "file_helper.h"
|
||||
|
||||
const char flipper_file_eoln = '\n';
|
||||
const char flipper_file_eolr = '\r';
|
||||
|
||||
bool file_helper_seek(File* file, int32_t offset) {
|
||||
uint64_t position = storage_file_tell(file);
|
||||
return storage_file_seek(file, position + offset, true);
|
||||
}
|
||||
|
||||
bool file_helper_write_hex(File* file, const uint8_t* data, const uint16_t data_size) {
|
||||
const uint8_t byte_text_size = 3;
|
||||
char byte_text[byte_text_size];
|
||||
|
||||
bool result = true;
|
||||
uint16_t bytes_written;
|
||||
for(uint8_t i = 0; i < data_size; i++) {
|
||||
snprintf(byte_text, byte_text_size, "%02X", data[i]);
|
||||
|
||||
if(i != 0) {
|
||||
// space
|
||||
const char space = ' ';
|
||||
bytes_written = storage_file_write(file, &space, sizeof(char));
|
||||
if(bytes_written != sizeof(char)) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bytes_written = storage_file_write(file, &byte_text, strlen(byte_text));
|
||||
if(bytes_written != strlen(byte_text)) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool file_helper_read_line(File* file, string_t str_result) {
|
||||
string_clean(str_result);
|
||||
const uint8_t buffer_size = 32;
|
||||
uint8_t buffer[buffer_size];
|
||||
|
||||
do {
|
||||
uint16_t bytes_were_read = storage_file_read(file, buffer, buffer_size);
|
||||
// TODO process EOF
|
||||
if(bytes_were_read == 0) break;
|
||||
|
||||
bool result = false;
|
||||
bool error = false;
|
||||
for(uint16_t i = 0; i < bytes_were_read; i++) {
|
||||
if(buffer[i] == flipper_file_eoln) {
|
||||
if(!file_helper_seek(file, i - bytes_were_read)) {
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
|
||||
result = true;
|
||||
break;
|
||||
} else if(buffer[i] == flipper_file_eolr) {
|
||||
// Ignore
|
||||
} else {
|
||||
string_push_back(str_result, buffer[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if(result || error) {
|
||||
break;
|
||||
}
|
||||
} while(true);
|
||||
|
||||
return string_size(str_result) != 0;
|
||||
}
|
||||
|
||||
bool file_helper_seek_to_next_line(File* file) {
|
||||
const uint8_t buffer_size = 32;
|
||||
uint8_t buffer[buffer_size];
|
||||
bool result = false;
|
||||
bool error = false;
|
||||
|
||||
do {
|
||||
uint16_t bytes_were_read = storage_file_read(file, buffer, buffer_size);
|
||||
if(bytes_were_read == 0) {
|
||||
if(storage_file_eof(file)) {
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for(uint16_t i = 0; i < bytes_were_read; i++) {
|
||||
if(buffer[i] == flipper_file_eoln) {
|
||||
if(!file_helper_seek(file, i - bytes_were_read)) {
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(result || error) {
|
||||
break;
|
||||
}
|
||||
} while(true);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool file_helper_read_value(File* file, string_t value, bool* last) {
|
||||
string_clean(value);
|
||||
const uint8_t buffer_size = 32;
|
||||
uint8_t buffer[buffer_size];
|
||||
bool result = false;
|
||||
bool error = false;
|
||||
|
||||
while(true) {
|
||||
uint16_t bytes_were_read = storage_file_read(file, buffer, buffer_size);
|
||||
|
||||
if(bytes_were_read == 0) {
|
||||
// check EOF
|
||||
if(storage_file_eof(file) && string_size(value) > 0) {
|
||||
result = true;
|
||||
*last = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for(uint16_t i = 0; i < bytes_were_read; i++) {
|
||||
if(buffer[i] == flipper_file_eoln) {
|
||||
if(string_size(value) > 0) {
|
||||
if(!file_helper_seek(file, i - bytes_were_read)) {
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
|
||||
result = true;
|
||||
*last = true;
|
||||
break;
|
||||
} else {
|
||||
error = true;
|
||||
}
|
||||
} else if(buffer[i] == ' ') {
|
||||
if(string_size(value) > 0) {
|
||||
if(!file_helper_seek(file, i - bytes_were_read)) {
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
|
||||
result = true;
|
||||
*last = false;
|
||||
break;
|
||||
}
|
||||
|
||||
} else if(buffer[i] == flipper_file_eolr) {
|
||||
// Ignore
|
||||
} else {
|
||||
string_push_back(value, buffer[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if(error || result) break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool file_helper_write(File* file, const void* data, uint16_t data_size) {
|
||||
uint16_t bytes_written = storage_file_write(file, data, data_size);
|
||||
return bytes_written == data_size;
|
||||
}
|
||||
|
||||
bool file_helper_write_eol(File* file) {
|
||||
return file_helper_write(file, &flipper_file_eoln, sizeof(char));
|
||||
}
|
||||
|
||||
bool file_helper_copy(File* file_from, File* file_to, uint64_t start_offset, uint64_t stop_offset) {
|
||||
bool result = false;
|
||||
|
||||
const uint8_t buffer_size = 32;
|
||||
uint8_t buffer[buffer_size];
|
||||
uint64_t current_offset = start_offset;
|
||||
|
||||
if(storage_file_seek(file_from, start_offset, true)) {
|
||||
do {
|
||||
int32_t bytes_count = MIN(buffer_size, stop_offset - current_offset);
|
||||
if(bytes_count <= 0) {
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
|
||||
uint16_t bytes_were_read = storage_file_read(file_from, buffer, bytes_count);
|
||||
if(bytes_were_read != bytes_count) break;
|
||||
|
||||
uint16_t bytes_were_written = storage_file_write(file_to, buffer, bytes_count);
|
||||
if(bytes_were_written != bytes_count) break;
|
||||
|
||||
current_offset += bytes_count;
|
||||
} while(true);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
81
lib/flipper_file/file_helper.h
Normal file
81
lib/flipper_file/file_helper.h
Normal file
@@ -0,0 +1,81 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <mlib/m-string.h>
|
||||
#include <storage/storage.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern const char flipper_file_eoln;
|
||||
extern const char flipper_file_eolr;
|
||||
|
||||
/**
|
||||
* Negative seek helper
|
||||
* @param file
|
||||
* @param offset
|
||||
* @return bool
|
||||
*/
|
||||
bool file_helper_seek(File* file, int32_t offset);
|
||||
|
||||
/**
|
||||
* Writes data to a file as a hexadecimal array.
|
||||
* @param file
|
||||
* @param data
|
||||
* @param data_size
|
||||
* @return true on success write
|
||||
*/
|
||||
bool file_helper_write_hex(File* file, const uint8_t* data, const uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Reads data as a string from the stored rw pointer to the \\n symbol position. Ignores \r.
|
||||
* @param file
|
||||
* @param str_result
|
||||
* @return true on success read
|
||||
*/
|
||||
bool file_helper_read_line(File* file, string_t str_result);
|
||||
|
||||
/**
|
||||
* Moves the RW pointer to the beginning of the next line
|
||||
* @param file
|
||||
* @return bool
|
||||
*/
|
||||
bool file_helper_seek_to_next_line(File* file);
|
||||
|
||||
/**
|
||||
* Read one value from array-like string (separated by ' ')
|
||||
* @param file
|
||||
* @param value
|
||||
* @return bool
|
||||
*/
|
||||
bool file_helper_read_value(File* file, string_t value, bool* last);
|
||||
|
||||
/**
|
||||
* Write helper
|
||||
* @param file
|
||||
* @param data
|
||||
* @param data_size
|
||||
* @return bool
|
||||
*/
|
||||
bool file_helper_write(File* file, const void* data, uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Write EOL
|
||||
* @param file
|
||||
* @return bool
|
||||
*/
|
||||
bool file_helper_write_eol(File* file);
|
||||
|
||||
/**
|
||||
* Appends part of one file to the end of another file
|
||||
* @param file_from
|
||||
* @param file_to
|
||||
* @param start_offset
|
||||
* @param stop_offset
|
||||
* @return bool
|
||||
*/
|
||||
bool file_helper_copy(File* file_from, File* file_to, uint64_t start_offset, uint64_t stop_offset);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
395
lib/flipper_file/flipper_file.c
Normal file
395
lib/flipper_file/flipper_file.c
Normal file
@@ -0,0 +1,395 @@
|
||||
#include <furi.h>
|
||||
#include "file_helper.h"
|
||||
#include "flipper_file_helper.h"
|
||||
#include "flipper_file.h"
|
||||
#include "flipper_file_i.h"
|
||||
#include <inttypes.h>
|
||||
#include <toolbox/hex.h>
|
||||
|
||||
FlipperFile* flipper_file_alloc(Storage* storage) {
|
||||
// furi_assert(storage);
|
||||
|
||||
FlipperFile* flipper_file = malloc(sizeof(FlipperFile));
|
||||
flipper_file->storage = storage;
|
||||
flipper_file->file = storage_file_alloc(flipper_file->storage);
|
||||
|
||||
return flipper_file;
|
||||
}
|
||||
|
||||
void flipper_file_free(FlipperFile* flipper_file) {
|
||||
furi_assert(flipper_file);
|
||||
if(storage_file_is_open(flipper_file->file)) {
|
||||
storage_file_close(flipper_file->file);
|
||||
}
|
||||
storage_file_free(flipper_file->file);
|
||||
free(flipper_file);
|
||||
}
|
||||
|
||||
bool flipper_file_open_existing(FlipperFile* flipper_file, const char* filename) {
|
||||
furi_assert(flipper_file);
|
||||
bool result = storage_file_open(
|
||||
flipper_file->file, filename, FSAM_READ | FSAM_WRITE, FSOM_OPEN_EXISTING);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_open_append(FlipperFile* flipper_file, const char* filename) {
|
||||
furi_assert(flipper_file);
|
||||
|
||||
bool result =
|
||||
storage_file_open(flipper_file->file, filename, FSAM_READ | FSAM_WRITE, FSOM_OPEN_APPEND);
|
||||
|
||||
// Add EOL if it is not there
|
||||
if(storage_file_size(flipper_file->file) >= 1) {
|
||||
do {
|
||||
char last_char;
|
||||
result = false;
|
||||
|
||||
if(!file_helper_seek(flipper_file->file, -1)) break;
|
||||
|
||||
uint16_t bytes_were_read = storage_file_read(flipper_file->file, &last_char, 1);
|
||||
if(bytes_were_read != 1) break;
|
||||
|
||||
if(last_char != flipper_file_eoln) {
|
||||
if(!file_helper_write_eol(flipper_file->file)) break;
|
||||
}
|
||||
|
||||
result = true;
|
||||
} while(false);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_open_always(FlipperFile* flipper_file, const char* filename) {
|
||||
furi_assert(flipper_file);
|
||||
bool result = storage_file_open(
|
||||
flipper_file->file, filename, FSAM_READ | FSAM_WRITE, FSOM_CREATE_ALWAYS);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_open_new(FlipperFile* flipper_file, const char* filename) {
|
||||
furi_assert(flipper_file);
|
||||
bool result = storage_file_open(
|
||||
flipper_file->file, filename, FSAM_READ | FSAM_WRITE, FSOM_CREATE_NEW);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_close(FlipperFile* flipper_file) {
|
||||
furi_assert(flipper_file);
|
||||
if(storage_file_is_open(flipper_file->file)) {
|
||||
return storage_file_close(flipper_file->file);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool flipper_file_rewind(FlipperFile* flipper_file) {
|
||||
furi_assert(flipper_file);
|
||||
return storage_file_seek(flipper_file->file, 0, true);
|
||||
}
|
||||
|
||||
bool flipper_file_read_header(FlipperFile* flipper_file, string_t filetype, uint32_t* version) {
|
||||
bool result = false;
|
||||
do {
|
||||
result = flipper_file_read_string(flipper_file, flipper_file_filetype_key, filetype);
|
||||
if(!result) break;
|
||||
result = flipper_file_read_uint32(flipper_file, flipper_file_version_key, version, 1);
|
||||
if(!result) break;
|
||||
} while(false);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_write_header(
|
||||
FlipperFile* flipper_file,
|
||||
string_t filetype,
|
||||
const uint32_t version) {
|
||||
bool result = false;
|
||||
do {
|
||||
result = flipper_file_write_string(flipper_file, flipper_file_filetype_key, filetype);
|
||||
if(!result) break;
|
||||
result = flipper_file_write_uint32(flipper_file, flipper_file_version_key, &version, 1);
|
||||
if(!result) break;
|
||||
} while(false);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_write_header_cstr(
|
||||
FlipperFile* flipper_file,
|
||||
const char* filetype,
|
||||
const uint32_t version) {
|
||||
bool result = false;
|
||||
string_t value;
|
||||
string_init_set(value, filetype);
|
||||
result = flipper_file_write_header(flipper_file, value, version);
|
||||
string_clear(value);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_get_value_count(FlipperFile* flipper_file, const char* key, uint32_t* count) {
|
||||
furi_assert(flipper_file);
|
||||
bool result = false;
|
||||
bool last = false;
|
||||
|
||||
string_t value;
|
||||
string_init(value);
|
||||
|
||||
uint32_t position = storage_file_tell(flipper_file->file);
|
||||
do {
|
||||
if(!flipper_file_seek_to_key(flipper_file->file, key)) break;
|
||||
|
||||
// Balance between speed and memory consumption
|
||||
// I prefer lower speed but less memory consumption
|
||||
*count = 0;
|
||||
|
||||
result = true;
|
||||
while(true) {
|
||||
if(!file_helper_read_value(flipper_file->file, value, &last)) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
|
||||
*count = *count + 1;
|
||||
if(last) break;
|
||||
}
|
||||
|
||||
} while(true);
|
||||
|
||||
if(!storage_file_seek(flipper_file->file, position, true)) {
|
||||
result = false;
|
||||
}
|
||||
|
||||
string_clear(value);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_write_comment(FlipperFile* flipper_file, string_t data) {
|
||||
furi_assert(flipper_file);
|
||||
|
||||
bool result = false;
|
||||
do {
|
||||
const char comment_buffer[2] = {flipper_file_comment, ' '};
|
||||
result = file_helper_write(flipper_file->file, comment_buffer, sizeof(comment_buffer));
|
||||
if(!result) break;
|
||||
|
||||
result = file_helper_write(flipper_file->file, string_get_cstr(data), string_size(data));
|
||||
if(!result) break;
|
||||
|
||||
result = file_helper_write_eol(flipper_file->file);
|
||||
} while(false);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_write_comment_cstr(FlipperFile* flipper_file, const char* data) {
|
||||
bool result = false;
|
||||
string_t value;
|
||||
string_init_set(value, data);
|
||||
result = flipper_file_write_comment(flipper_file, value);
|
||||
string_clear(value);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_delete_key_and_call(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
flipper_file_cb call,
|
||||
const char* cb_key,
|
||||
const void* cb_data,
|
||||
const uint16_t cb_data_size) {
|
||||
bool result = false;
|
||||
File* scratch_file = storage_file_alloc(flipper_file->storage);
|
||||
|
||||
do {
|
||||
// get size
|
||||
uint64_t file_size = storage_file_size(flipper_file->file);
|
||||
if(file_size == 0) break;
|
||||
|
||||
if(!storage_file_seek(flipper_file->file, 0, true)) break;
|
||||
|
||||
// find key
|
||||
if(!flipper_file_seek_to_key(flipper_file->file, key)) break;
|
||||
// get key start position
|
||||
uint64_t start_position = storage_file_tell(flipper_file->file) - strlen(key);
|
||||
if(start_position >= 2) {
|
||||
start_position -= 2;
|
||||
} else {
|
||||
// something wrong
|
||||
break;
|
||||
}
|
||||
|
||||
// get value end position
|
||||
if(!file_helper_seek_to_next_line(flipper_file->file)) break;
|
||||
uint64_t end_position = storage_file_tell(flipper_file->file);
|
||||
// newline symbol
|
||||
if(end_position < file_size) {
|
||||
end_position += 1;
|
||||
}
|
||||
|
||||
// open scratchpad
|
||||
const char* scratch_name = "";
|
||||
if(!flipper_file_get_scratchpad_name(&scratch_name)) break;
|
||||
|
||||
if(!storage_file_open(
|
||||
scratch_file, scratch_name, FSAM_READ | FSAM_WRITE, FSOM_CREATE_ALWAYS))
|
||||
break;
|
||||
|
||||
// copy key file before key to scratchpad
|
||||
if(!file_helper_copy(flipper_file->file, scratch_file, 0, start_position)) break;
|
||||
|
||||
// do something in between if needed
|
||||
if(call != NULL) {
|
||||
if(!call(scratch_file, cb_key, cb_data, cb_data_size)) break;
|
||||
};
|
||||
|
||||
// copy key file after key value to scratchpad
|
||||
if(!file_helper_copy(flipper_file->file, scratch_file, end_position, file_size)) break;
|
||||
|
||||
file_size = storage_file_tell(scratch_file);
|
||||
if(file_size == 0) break;
|
||||
|
||||
if(!storage_file_seek(flipper_file->file, 0, true)) break;
|
||||
|
||||
// copy whole scratchpad file to the original file
|
||||
if(!file_helper_copy(scratch_file, flipper_file->file, 0, file_size)) break;
|
||||
|
||||
// and truncate original file
|
||||
if(!storage_file_truncate(flipper_file->file)) break;
|
||||
|
||||
// close and remove scratchpad file
|
||||
if(!storage_file_close(scratch_file)) break;
|
||||
if(storage_common_remove(flipper_file->storage, scratch_name) != FSE_OK) break;
|
||||
result = true;
|
||||
} while(false);
|
||||
|
||||
storage_file_free(scratch_file);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_delete_key(FlipperFile* flipper_file, const char* key) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_delete_key_and_call(flipper_file, key, NULL, NULL, NULL, 0);
|
||||
}
|
||||
|
||||
bool flipper_file_write_internal(
|
||||
File* file,
|
||||
const char* key,
|
||||
const void* _data,
|
||||
const uint16_t data_size,
|
||||
FlipperFileValueType type) {
|
||||
bool result = false;
|
||||
string_t value;
|
||||
string_init(value);
|
||||
|
||||
do {
|
||||
result = flipper_file_write_key(file, key);
|
||||
if(!result) break;
|
||||
|
||||
for(uint16_t i = 0; i < data_size; i++) {
|
||||
switch(type) {
|
||||
case FlipperFileValueHex: {
|
||||
const uint8_t* data = _data;
|
||||
string_printf(value, "%02X", data[i]);
|
||||
}; break;
|
||||
case FlipperFileValueFloat: {
|
||||
const float* data = _data;
|
||||
string_printf(value, "%f", data[i]);
|
||||
}; break;
|
||||
case FlipperFileValueInt32: {
|
||||
const int32_t* data = _data;
|
||||
string_printf(value, "%" PRIi32, data[i]);
|
||||
}; break;
|
||||
case FlipperFileValueUint32: {
|
||||
const uint32_t* data = _data;
|
||||
string_printf(value, "%" PRId32, data[i]);
|
||||
}; break;
|
||||
}
|
||||
|
||||
if((i + 1) < data_size) {
|
||||
string_cat(value, " ");
|
||||
}
|
||||
|
||||
result = file_helper_write(file, string_get_cstr(value), string_size(value));
|
||||
if(!result) break;
|
||||
}
|
||||
|
||||
result = file_helper_write_eol(file);
|
||||
} while(false);
|
||||
|
||||
string_clear(value);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_read_internal(
|
||||
File* file,
|
||||
const char* key,
|
||||
void* _data,
|
||||
const uint16_t data_size,
|
||||
FlipperFileValueType type) {
|
||||
bool result = false;
|
||||
string_t value;
|
||||
string_init(value);
|
||||
|
||||
if(flipper_file_seek_to_key(file, key)) {
|
||||
result = true;
|
||||
for(uint16_t i = 0; i < data_size; i++) {
|
||||
bool last = false;
|
||||
result = file_helper_read_value(file, value, &last);
|
||||
if(result) {
|
||||
int scan_values = 0;
|
||||
switch(type) {
|
||||
case FlipperFileValueHex: {
|
||||
uint8_t* data = _data;
|
||||
// sscanf "%02X" does not work here
|
||||
if(hex_chars_to_uint8(
|
||||
string_get_char(value, 0), string_get_char(value, 1), &data[i])) {
|
||||
scan_values = 1;
|
||||
}
|
||||
}; break;
|
||||
case FlipperFileValueFloat: {
|
||||
float* data = _data;
|
||||
// newlib-nano does not have sscanf for floats
|
||||
// scan_values = sscanf(string_get_cstr(value), "%f", &data[i]);
|
||||
char* end_char;
|
||||
data[i] = strtof(string_get_cstr(value), &end_char);
|
||||
if(*end_char == 0) {
|
||||
// very probably ok
|
||||
scan_values = 1;
|
||||
}
|
||||
}; break;
|
||||
case FlipperFileValueInt32: {
|
||||
int32_t* data = _data;
|
||||
scan_values = sscanf(string_get_cstr(value), "%" PRIi32, &data[i]);
|
||||
}; break;
|
||||
case FlipperFileValueUint32: {
|
||||
uint32_t* data = _data;
|
||||
scan_values = sscanf(string_get_cstr(value), "%" PRId32, &data[i]);
|
||||
}; break;
|
||||
}
|
||||
|
||||
if(scan_values != 1) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
||||
if(last && ((i + 1) != data_size)) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
string_clear(value);
|
||||
return result;
|
||||
}
|
||||
|
||||
File* flipper_file_get_file(FlipperFile* flipper_file) {
|
||||
furi_assert(flipper_file);
|
||||
furi_assert(flipper_file->file);
|
||||
|
||||
return flipper_file->file;
|
||||
}
|
458
lib/flipper_file/flipper_file.h
Normal file
458
lib/flipper_file/flipper_file.h
Normal file
@@ -0,0 +1,458 @@
|
||||
/**
|
||||
* @file flipper-file.h
|
||||
* Flipper File Format helper library.
|
||||
*
|
||||
* Flipper File Format is a fairly simple format for storing data in a file.
|
||||
*
|
||||
* Flipper file structure:
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~
|
||||
* # Commentary
|
||||
* Field name: field value
|
||||
* ~~~~~~~~~~~~~~~~~~~~~
|
||||
*
|
||||
* Lines starting with the # character are ignored (considered as comments). The separator between the name of the value and the value itself is the string ": ".
|
||||
*
|
||||
* Currently supported types:
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~
|
||||
* String: text
|
||||
* Int32: 1 2 -3 4
|
||||
* Uint32: 1 2 3 4
|
||||
* Float: 1.0 1234.654
|
||||
* Hex: A4 B3 C2 D1 12 FF
|
||||
* ~~~~~~~~~~~~~~~~~~~~~
|
||||
*
|
||||
* End of line is LF when writing, but CR is supported when reading.
|
||||
*
|
||||
* The library is designed in such a way that comments and field values are completely ignored when searching for keys, that is, they do not consume memory.
|
||||
*
|
||||
* File example:
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~
|
||||
* Filetype: Flipper Test File
|
||||
* Version: 1
|
||||
* # Just test file
|
||||
* String: String value
|
||||
* UINT: 1234
|
||||
* Hex: 00 01 FF A3
|
||||
* ~~~~~~~~~~~~~~~~~~~~~
|
||||
*
|
||||
* Writing:
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~
|
||||
* FlipperFile file = flipper_file_alloc(storage);
|
||||
*
|
||||
* do {
|
||||
* const uint32_t version = 1;
|
||||
* const char* string_value = "String value";
|
||||
* const uint32_t uint32_value = 1234;
|
||||
* const uint16_t array_size = 4;
|
||||
* const uint8_t* array[array_size] = {0x00, 0x01, 0xFF, 0xA3};
|
||||
*
|
||||
* if(!flipper_file_open_new(file, "/ext/flipper_file_test")) break;
|
||||
* if(!flipper_file_write_header_cstr(file, "Flipper Test File", version)) break;
|
||||
* if(!flipper_file_write_comment_cstr(file, "Just test file")) break;
|
||||
* if(!flipper_file_write_string_cstr(file, "String", string_value)) break;
|
||||
* if(!flipper_file_flipper_file_write_uint32(file, "UINT", &uint32_value, 1)) break;
|
||||
* if(!flipper_file_write_hex(file, "Hex Array", array, array_size)) break;
|
||||
*
|
||||
* // signal that the file was written successfully
|
||||
* } while(0);
|
||||
*
|
||||
* flipper_file_close(file);
|
||||
* flipper_file_free(file);
|
||||
* ~~~~~~~~~~~~~~~~~~~~~
|
||||
*
|
||||
* Reading:
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~
|
||||
* FlipperFile file = flipper_file_alloc(storage);
|
||||
*
|
||||
* do {
|
||||
* uint32_t version = 1;
|
||||
* string_t file_type;
|
||||
* string_t string_value;
|
||||
* uint32_t uint32_value = 1;
|
||||
* uint16_t array_size = 4;
|
||||
* uint8_t* array[array_size] = {0};
|
||||
* string_init(file_type);
|
||||
* string_init(string_value);
|
||||
*
|
||||
* if(!flipper_file_open_existing(file, "/ext/flipper_file_test")) break;
|
||||
* if(!flipper_file_read_header(file, file_type, &version)) break;
|
||||
* if(!flipper_file_read_string(file, "String", string_value)) break;
|
||||
* if(!flipper_file_read_uint32(file, "UINT", &uint32_value, 1)) break;
|
||||
* if(!flipper_file_read_hex(file, "Hex Array", array, array_size)) break;
|
||||
*
|
||||
* // signal that the file was read successfully
|
||||
* } while(0);
|
||||
*
|
||||
* flipper_file_close(file);
|
||||
* flipper_file_free(file);
|
||||
* ~~~~~~~~~~~~~~~~~~~~~
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <mlib/m-string.h>
|
||||
#include <storage/storage.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** FlipperFile type anonymous structure. */
|
||||
typedef struct FlipperFile FlipperFile;
|
||||
|
||||
/**
|
||||
* Allocate FlipperFile.
|
||||
* @param storage storage api
|
||||
* @return FlipperFile* Pointer to a FlipperFile instance
|
||||
*/
|
||||
FlipperFile* flipper_file_alloc(Storage* storage);
|
||||
|
||||
/**
|
||||
* Free FlipperFile.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
*/
|
||||
void flipper_file_free(FlipperFile* flipper_file);
|
||||
|
||||
/**
|
||||
* Open existing file.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param filename File name and path
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_open_existing(FlipperFile* flipper_file, const char* filename);
|
||||
|
||||
/**
|
||||
* Open existing file for writing and add values to the end of file.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param filename File name and path
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_open_append(FlipperFile* flipper_file, const char* filename);
|
||||
|
||||
/**
|
||||
* Open file. Creates a new file, or deletes the contents of the file if it already exists.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param filename File name and path
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_open_always(FlipperFile* flipper_file, const char* filename);
|
||||
|
||||
/**
|
||||
* Open file. Creates a new file, fails if file already exists.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param filename File name and path
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_open_new(FlipperFile* flipper_file, const char* filename);
|
||||
|
||||
/**
|
||||
* Close the file.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_close(FlipperFile* flipper_file);
|
||||
|
||||
/**
|
||||
* Rewind the file RW pointer.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_rewind(FlipperFile* flipper_file);
|
||||
|
||||
/**
|
||||
* Read the header (file type and version) from the file.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param filetype File type string
|
||||
* @param version Version Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_read_header(FlipperFile* flipper_file, string_t filetype, uint32_t* version);
|
||||
|
||||
/**
|
||||
* Write the header (file type and version) to the file.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param filetype File type string
|
||||
* @param version Version Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_write_header(
|
||||
FlipperFile* flipper_file,
|
||||
string_t filetype,
|
||||
const uint32_t version);
|
||||
|
||||
/**
|
||||
* Write the header (file type and version) to the file. Plain C string version.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param filetype File type string
|
||||
* @param version Version Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_write_header_cstr(
|
||||
FlipperFile* flipper_file,
|
||||
const char* filetype,
|
||||
const uint32_t version);
|
||||
|
||||
/**
|
||||
* Get the count of values by key
|
||||
* @param flipper_file
|
||||
* @param key
|
||||
* @param count
|
||||
* @return bool
|
||||
*/
|
||||
bool flipper_file_get_value_count(FlipperFile* flipper_file, const char* key, uint32_t* count);
|
||||
|
||||
/**
|
||||
* Read a string from a file by Key
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_read_string(FlipperFile* flipper_file, const char* key, string_t data);
|
||||
|
||||
/**
|
||||
* Write key and string to file.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_write_string(FlipperFile* flipper_file, const char* key, string_t data);
|
||||
|
||||
/**
|
||||
* Write key and string to file. Plain C string version.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_write_string_cstr(FlipperFile* flipper_file, const char* key, const char* data);
|
||||
|
||||
/**
|
||||
* Read array of uint32 from a file by Key
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @param data_size Values count
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_read_uint32(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
uint32_t* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Write key and array of uint32 to file.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @param data_size Values count
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_write_uint32(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const uint32_t* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Read array of int32 from a file by Key
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @param data_size Values count
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_read_int32(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
int32_t* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Write key and array of int32 to file.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @param data_size Values count
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_write_int32(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const int32_t* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Read array of float from a file by Key
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @param data_size Values count
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_read_float(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
float* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Write key and array of float to file.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @param data_size Values count
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_write_float(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const float* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Read hex array from a file by Key
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @param data_size Value size
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_read_hex(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
uint8_t* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Write key and hex array to file.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @param data_size Values count
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_write_hex(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const uint8_t* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Write comment to file.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param data Comment text
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_write_comment(FlipperFile* flipper_file, string_t data);
|
||||
|
||||
/**
|
||||
* Write comment to file. Plain C string version.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param data Comment text
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_write_comment_cstr(FlipperFile* flipper_file, const char* data);
|
||||
|
||||
/**
|
||||
* Removes the first matching key and its value from the file. Changes the RW pointer to an undefined position.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_delete_key(FlipperFile* flipper_file, const char* key);
|
||||
|
||||
/**
|
||||
* Updates the value of the first matching key to a string value. Changes the RW pointer to an undefined position.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_update_string(FlipperFile* flipper_file, const char* key, string_t data);
|
||||
|
||||
/**
|
||||
* Updates the value of the first matching key to a string value. Plain C version. Changes the RW pointer to an undefined position.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_update_string_cstr(FlipperFile* flipper_file, const char* key, const char* data);
|
||||
|
||||
/**
|
||||
* Updates the value of the first matching key to a uint32 array value. Changes the RW pointer to an undefined position.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @param data_size Values count
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_update_uint32(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const uint32_t* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Updates the value of the first matching key to a int32 array value. Changes the RW pointer to an undefined position.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @param data_size Values count
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_update_int32(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const int32_t* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Updates the value of the first matching key to a float array value. Changes the RW pointer to an undefined position.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @param data_size Values count
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_update_float(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const float* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Updates the value of the first matching key to a hex array value. Changes the RW pointer to an undefined position.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @param data_size Values count
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_update_hex(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const uint8_t* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
/** Get file descriptor.
|
||||
*
|
||||
* We higly don't recommend to use it.
|
||||
* This instance is owned by FlipperFile.
|
||||
* @param flipper_file
|
||||
* @return File*
|
||||
*/
|
||||
File* flipper_file_get_file(FlipperFile* flipper_file);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
42
lib/flipper_file/flipper_file_float.c
Normal file
42
lib/flipper_file/flipper_file_float.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <furi.h>
|
||||
|
||||
#include "flipper_file.h"
|
||||
#include "flipper_file_i.h"
|
||||
#include "flipper_file_helper.h"
|
||||
|
||||
static bool flipper_file_write_float_internal(
|
||||
File* file,
|
||||
const char* key,
|
||||
const void* _data,
|
||||
const uint16_t data_size) {
|
||||
return flipper_file_write_internal(file, key, _data, data_size, FlipperFileValueFloat);
|
||||
};
|
||||
|
||||
bool flipper_file_read_float(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
float* data,
|
||||
const uint16_t data_size) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_read_internal(
|
||||
flipper_file->file, key, data, data_size, FlipperFileValueFloat);
|
||||
}
|
||||
|
||||
bool flipper_file_write_float(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const float* data,
|
||||
const uint16_t data_size) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_write_float_internal(flipper_file->file, key, data, data_size);
|
||||
}
|
||||
|
||||
bool flipper_file_update_float(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const float* data,
|
||||
const uint16_t data_size) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_delete_key_and_call(
|
||||
flipper_file, key, flipper_file_write_float_internal, key, data, data_size);
|
||||
}
|
118
lib/flipper_file/flipper_file_helper.c
Normal file
118
lib/flipper_file/flipper_file_helper.c
Normal file
@@ -0,0 +1,118 @@
|
||||
#include "flipper_file_helper.h"
|
||||
|
||||
const char* flipper_file_filetype_key = "Filetype";
|
||||
const char* flipper_file_version_key = "Version";
|
||||
const char flipper_file_delimiter = ':';
|
||||
const char flipper_file_comment = '#';
|
||||
|
||||
#ifdef __linux__
|
||||
const char* flipper_file_scratchpad = ".scratch.pad";
|
||||
#else
|
||||
const char* flipper_file_scratchpad = "/any/.scratch.pad";
|
||||
#endif
|
||||
|
||||
bool flipper_file_read_valid_key(File* file, string_t key) {
|
||||
string_clean(key);
|
||||
bool found = false;
|
||||
bool error = false;
|
||||
const uint8_t buffer_size = 32;
|
||||
uint8_t buffer[buffer_size];
|
||||
bool accumulate = true;
|
||||
bool new_line = true;
|
||||
|
||||
while(true) {
|
||||
uint16_t bytes_were_read = storage_file_read(file, buffer, buffer_size);
|
||||
if(bytes_were_read == 0) break;
|
||||
|
||||
for(uint16_t i = 0; i < bytes_were_read; i++) {
|
||||
if(buffer[i] == flipper_file_eoln) {
|
||||
// EOL found, clean data, start accumulating data and set the new_line flag
|
||||
string_clean(key);
|
||||
accumulate = true;
|
||||
new_line = true;
|
||||
} else if(buffer[i] == flipper_file_eolr) {
|
||||
// Ignore
|
||||
} else if(buffer[i] == flipper_file_comment && new_line) {
|
||||
// if there is a comment character and we are at the beginning of a new line
|
||||
// do not accumulate comment data and reset the new_line flag
|
||||
accumulate = false;
|
||||
new_line = false;
|
||||
} else if(buffer[i] == flipper_file_delimiter) {
|
||||
if(new_line) {
|
||||
// we are on a "new line" and found the delimiter
|
||||
// this can only be if we have previously found some kind of key, so
|
||||
// clear the data, set the flag that we no longer want to accumulate data
|
||||
// and reset the new_line flag
|
||||
string_clean(key);
|
||||
accumulate = false;
|
||||
new_line = false;
|
||||
} else {
|
||||
// parse the delimiter only if we are accumulating data
|
||||
if(accumulate) {
|
||||
// we found the delimiter, move the rw pointer to the correct location
|
||||
// and signal that we have found something
|
||||
if(!file_helper_seek(file, i - bytes_were_read)) {
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// just new symbol, reset the new_line flag
|
||||
new_line = false;
|
||||
if(accumulate) {
|
||||
// and accumulate data if we want
|
||||
string_push_back(key, buffer[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(found || error) break;
|
||||
}
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
bool flipper_file_seek_to_key(File* file, const char* key) {
|
||||
bool found = false;
|
||||
string_t readed_key;
|
||||
|
||||
string_init(readed_key);
|
||||
|
||||
while(!storage_file_eof(file)) {
|
||||
if(flipper_file_read_valid_key(file, readed_key)) {
|
||||
if(string_cmp_str(readed_key, key) == 0) {
|
||||
if(!file_helper_seek(file, 2)) break;
|
||||
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
string_clear(readed_key);
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
bool flipper_file_write_key(File* file, const char* key) {
|
||||
bool result = false;
|
||||
|
||||
do {
|
||||
result = file_helper_write(file, key, strlen(key));
|
||||
if(!result) break;
|
||||
|
||||
const char delimiter_buffer[2] = {flipper_file_delimiter, ' '};
|
||||
result = file_helper_write(file, delimiter_buffer, sizeof(delimiter_buffer));
|
||||
} while(false);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_get_scratchpad_name(const char** name) {
|
||||
// TODO do not rewrite existing file
|
||||
*name = flipper_file_scratchpad;
|
||||
return true;
|
||||
}
|
51
lib/flipper_file/flipper_file_helper.h
Normal file
51
lib/flipper_file/flipper_file_helper.h
Normal file
@@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <mlib/m-string.h>
|
||||
#include <storage/storage.h>
|
||||
#include "file_helper.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern const char* flipper_file_filetype_key;
|
||||
extern const char* flipper_file_version_key;
|
||||
extern const char flipper_file_delimiter;
|
||||
extern const char flipper_file_comment;
|
||||
|
||||
/**
|
||||
* Reads a valid key from a file as a string.
|
||||
* After reading, the rw pointer will be on the flipper_file_delimiter symbol.
|
||||
* Optimized not to read comments and values into RAM.
|
||||
* @param file
|
||||
* @param key
|
||||
* @return true on success read
|
||||
*/
|
||||
bool flipper_file_read_valid_key(File* file, string_t key);
|
||||
|
||||
/**
|
||||
* Sets rw pointer to the data after the key
|
||||
* @param file
|
||||
* @param key
|
||||
* @return true if key was found
|
||||
*/
|
||||
bool flipper_file_seek_to_key(File* file, const char* key);
|
||||
|
||||
/**
|
||||
* Write key and key delimiter
|
||||
* @param file
|
||||
* @param key
|
||||
* @return bool
|
||||
*/
|
||||
bool flipper_file_write_key(File* file, const char* key);
|
||||
|
||||
/**
|
||||
* Get scratchpad name and path
|
||||
* @param name
|
||||
* @return bool
|
||||
*/
|
||||
bool flipper_file_get_scratchpad_name(const char** name);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
42
lib/flipper_file/flipper_file_hex.c
Normal file
42
lib/flipper_file/flipper_file_hex.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <furi.h>
|
||||
|
||||
#include "flipper_file.h"
|
||||
#include "flipper_file_i.h"
|
||||
#include "flipper_file_helper.h"
|
||||
|
||||
static bool flipper_file_write_hex_internal(
|
||||
File* file,
|
||||
const char* key,
|
||||
const void* _data,
|
||||
const uint16_t data_size) {
|
||||
return flipper_file_write_internal(file, key, _data, data_size, FlipperFileValueHex);
|
||||
};
|
||||
|
||||
bool flipper_file_write_hex(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const uint8_t* data,
|
||||
const uint16_t data_size) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_write_hex_internal(flipper_file->file, key, data, data_size);
|
||||
}
|
||||
|
||||
bool flipper_file_read_hex(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
uint8_t* data,
|
||||
const uint16_t data_size) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_read_internal(
|
||||
flipper_file->file, key, data, data_size, FlipperFileValueHex);
|
||||
}
|
||||
|
||||
bool flipper_file_update_hex(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const uint8_t* data,
|
||||
const uint16_t data_size) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_delete_key_and_call(
|
||||
flipper_file, key, flipper_file_write_hex_internal, key, data, data_size);
|
||||
}
|
72
lib/flipper_file/flipper_file_i.h
Normal file
72
lib/flipper_file/flipper_file_i.h
Normal file
@@ -0,0 +1,72 @@
|
||||
#include <stdint.h>
|
||||
#include <storage/storage.h>
|
||||
|
||||
struct FlipperFile {
|
||||
File* file;
|
||||
Storage* storage;
|
||||
};
|
||||
|
||||
/**
|
||||
* Value write type callback
|
||||
*/
|
||||
typedef bool (*flipper_file_cb)(File* file, const char* key, const void* data, uint16_t data_size);
|
||||
|
||||
/**
|
||||
*
|
||||
* @param flipper_file
|
||||
* @param key
|
||||
* @param cb
|
||||
* @param cb_key
|
||||
* @param cb_data
|
||||
* @param cb_data_size
|
||||
* @return bool
|
||||
*/
|
||||
bool flipper_file_delete_key_and_call(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
flipper_file_cb cb,
|
||||
const char* cb_key,
|
||||
const void* cb_data,
|
||||
const uint16_t cb_data_size);
|
||||
|
||||
/**
|
||||
* Value types
|
||||
*/
|
||||
typedef enum {
|
||||
FlipperFileValueHex,
|
||||
FlipperFileValueFloat,
|
||||
FlipperFileValueInt32,
|
||||
FlipperFileValueUint32,
|
||||
} FlipperFileValueType;
|
||||
|
||||
/**
|
||||
* Internal write values function
|
||||
* @param file
|
||||
* @param key
|
||||
* @param _data
|
||||
* @param data_size
|
||||
* @param type
|
||||
* @return bool
|
||||
*/
|
||||
bool flipper_file_write_internal(
|
||||
File* file,
|
||||
const char* key,
|
||||
const void* _data,
|
||||
const uint16_t data_size,
|
||||
FlipperFileValueType type);
|
||||
|
||||
/**
|
||||
* Internal read values function
|
||||
* @param file
|
||||
* @param key
|
||||
* @param _data
|
||||
* @param data_size
|
||||
* @param type
|
||||
* @return bool
|
||||
*/
|
||||
bool flipper_file_read_internal(
|
||||
File* file,
|
||||
const char* key,
|
||||
void* _data,
|
||||
const uint16_t data_size,
|
||||
FlipperFileValueType type);
|
42
lib/flipper_file/flipper_file_int32.c
Normal file
42
lib/flipper_file/flipper_file_int32.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <furi.h>
|
||||
|
||||
#include "flipper_file.h"
|
||||
#include "flipper_file_i.h"
|
||||
#include "flipper_file_helper.h"
|
||||
|
||||
static bool flipper_file_write_int32_internal(
|
||||
File* file,
|
||||
const char* key,
|
||||
const void* _data,
|
||||
const uint16_t data_size) {
|
||||
return flipper_file_write_internal(file, key, _data, data_size, FlipperFileValueInt32);
|
||||
};
|
||||
|
||||
bool flipper_file_read_int32(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
int32_t* data,
|
||||
const uint16_t data_size) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_read_internal(
|
||||
flipper_file->file, key, data, data_size, FlipperFileValueInt32);
|
||||
}
|
||||
|
||||
bool flipper_file_write_int32(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const int32_t* data,
|
||||
const uint16_t data_size) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_write_int32_internal(flipper_file->file, key, data, data_size);
|
||||
}
|
||||
|
||||
bool flipper_file_update_int32(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const int32_t* data,
|
||||
const uint16_t data_size) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_delete_key_and_call(
|
||||
flipper_file, key, flipper_file_write_int32_internal, key, data, data_size);
|
||||
}
|
67
lib/flipper_file/flipper_file_string.c
Normal file
67
lib/flipper_file/flipper_file_string.c
Normal file
@@ -0,0 +1,67 @@
|
||||
#include <furi.h>
|
||||
|
||||
#include "flipper_file.h"
|
||||
#include "flipper_file_i.h"
|
||||
#include "flipper_file_helper.h"
|
||||
|
||||
static bool flipper_file_write_string_internal(
|
||||
File* file,
|
||||
const char* key,
|
||||
const void* data,
|
||||
const uint16_t data_size) {
|
||||
bool result = false;
|
||||
(void)data_size;
|
||||
|
||||
do {
|
||||
result = flipper_file_write_key(file, key);
|
||||
if(!result) break;
|
||||
|
||||
result = file_helper_write(file, string_get_cstr(data), string_size(data));
|
||||
if(!result) break;
|
||||
|
||||
result = file_helper_write_eol(file);
|
||||
} while(false);
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
bool flipper_file_read_string(FlipperFile* flipper_file, const char* key, string_t data) {
|
||||
furi_assert(flipper_file);
|
||||
|
||||
bool result = false;
|
||||
if(flipper_file_seek_to_key(flipper_file->file, key)) {
|
||||
if(file_helper_read_line(flipper_file->file, data)) {
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_write_string(FlipperFile* flipper_file, const char* key, string_t data) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_write_string_internal(flipper_file->file, key, data, 0);
|
||||
}
|
||||
|
||||
bool flipper_file_write_string_cstr(FlipperFile* flipper_file, const char* key, const char* data) {
|
||||
bool result = false;
|
||||
string_t value;
|
||||
string_init_set(value, data);
|
||||
result = flipper_file_write_string(flipper_file, key, value);
|
||||
string_clear(value);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_update_string(FlipperFile* flipper_file, const char* key, string_t data) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_delete_key_and_call(
|
||||
flipper_file, key, flipper_file_write_string_internal, key, data, 0);
|
||||
}
|
||||
|
||||
bool flipper_file_update_string_cstr(FlipperFile* flipper_file, const char* key, const char* data) {
|
||||
bool result = false;
|
||||
string_t value;
|
||||
string_init_set(value, data);
|
||||
result = flipper_file_update_string(flipper_file, key, value);
|
||||
string_clear(value);
|
||||
return result;
|
||||
}
|
42
lib/flipper_file/flipper_file_uint32.c
Normal file
42
lib/flipper_file/flipper_file_uint32.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <furi.h>
|
||||
|
||||
#include "flipper_file.h"
|
||||
#include "flipper_file_i.h"
|
||||
#include "flipper_file_helper.h"
|
||||
|
||||
static bool flipper_file_write_uint32_internal(
|
||||
File* file,
|
||||
const char* key,
|
||||
const void* _data,
|
||||
const uint16_t data_size) {
|
||||
return flipper_file_write_internal(file, key, _data, data_size, FlipperFileValueUint32);
|
||||
};
|
||||
|
||||
bool flipper_file_read_uint32(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
uint32_t* data,
|
||||
const uint16_t data_size) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_read_internal(
|
||||
flipper_file->file, key, data, data_size, FlipperFileValueUint32);
|
||||
}
|
||||
|
||||
bool flipper_file_write_uint32(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const uint32_t* data,
|
||||
const uint16_t data_size) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_write_uint32_internal(flipper_file->file, key, data, data_size);
|
||||
}
|
||||
|
||||
bool flipper_file_update_uint32(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const uint32_t* data,
|
||||
const uint16_t data_size) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_delete_key_and_call(
|
||||
flipper_file, key, flipper_file_write_uint32_internal, key, data, data_size);
|
||||
}
|
Reference in New Issue
Block a user