[FL-2312] Flipper format: insert OR update (#1009)
* Flipper format: seek_to_end, key_exist * Flipper Format: insert_or_update
This commit is contained in:
@@ -102,6 +102,20 @@ bool flipper_format_rewind(FlipperFormat* flipper_format) {
|
||||
return stream_rewind(flipper_format->stream);
|
||||
}
|
||||
|
||||
bool flipper_format_seek_to_end(FlipperFormat* flipper_format) {
|
||||
furi_assert(flipper_format);
|
||||
return stream_seek(flipper_format->stream, 0, StreamOffsetFromEnd);
|
||||
}
|
||||
|
||||
bool flipper_format_key_exist(FlipperFormat* flipper_format, const char* key) {
|
||||
size_t pos = stream_tell(flipper_format->stream);
|
||||
stream_seek(flipper_format->stream, 0, StreamOffsetFromStart);
|
||||
bool result = flipper_format_stream_seek_to_key(flipper_format->stream, key, false);
|
||||
stream_seek(flipper_format->stream, pos, StreamOffsetFromStart);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_format_read_header(
|
||||
FlipperFormat* flipper_format,
|
||||
string_t filetype,
|
||||
@@ -320,7 +334,7 @@ bool flipper_format_update_string(FlipperFormat* flipper_format, const char* key
|
||||
FlipperStreamWriteData write_data = {
|
||||
.key = key,
|
||||
.type = FlipperStreamValueStr,
|
||||
.data = data,
|
||||
.data = string_get_cstr(data),
|
||||
.data_size = 1,
|
||||
};
|
||||
bool result = flipper_format_stream_delete_key_and_write(
|
||||
@@ -408,3 +422,103 @@ bool flipper_format_update_hex(
|
||||
flipper_format->stream, &write_data, flipper_format->strict_mode);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_format_insert_or_update_string(
|
||||
FlipperFormat* flipper_format,
|
||||
const char* key,
|
||||
string_t data) {
|
||||
bool result = false;
|
||||
|
||||
if(!flipper_format_key_exist(flipper_format, key)) {
|
||||
flipper_format_seek_to_end(flipper_format);
|
||||
result = flipper_format_write_string(flipper_format, key, data);
|
||||
} else {
|
||||
result = flipper_format_update_string(flipper_format, key, data);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_format_insert_or_update_string_cstr(
|
||||
FlipperFormat* flipper_format,
|
||||
const char* key,
|
||||
const char* data) {
|
||||
bool result = false;
|
||||
|
||||
if(!flipper_format_key_exist(flipper_format, key)) {
|
||||
flipper_format_seek_to_end(flipper_format);
|
||||
result = flipper_format_write_string_cstr(flipper_format, key, data);
|
||||
} else {
|
||||
result = flipper_format_update_string_cstr(flipper_format, key, data);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_format_insert_or_update_uint32(
|
||||
FlipperFormat* flipper_format,
|
||||
const char* key,
|
||||
const uint32_t* data,
|
||||
const uint16_t data_size) {
|
||||
bool result = false;
|
||||
|
||||
if(!flipper_format_key_exist(flipper_format, key)) {
|
||||
flipper_format_seek_to_end(flipper_format);
|
||||
result = flipper_format_write_uint32(flipper_format, key, data, data_size);
|
||||
} else {
|
||||
result = flipper_format_update_uint32(flipper_format, key, data, data_size);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_format_insert_or_update_int32(
|
||||
FlipperFormat* flipper_format,
|
||||
const char* key,
|
||||
const int32_t* data,
|
||||
const uint16_t data_size) {
|
||||
bool result = false;
|
||||
|
||||
if(!flipper_format_key_exist(flipper_format, key)) {
|
||||
flipper_format_seek_to_end(flipper_format);
|
||||
result = flipper_format_write_int32(flipper_format, key, data, data_size);
|
||||
} else {
|
||||
result = flipper_format_update_int32(flipper_format, key, data, data_size);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_format_insert_or_update_float(
|
||||
FlipperFormat* flipper_format,
|
||||
const char* key,
|
||||
const float* data,
|
||||
const uint16_t data_size) {
|
||||
bool result = false;
|
||||
|
||||
if(!flipper_format_key_exist(flipper_format, key)) {
|
||||
flipper_format_seek_to_end(flipper_format);
|
||||
result = flipper_format_write_float(flipper_format, key, data, data_size);
|
||||
} else {
|
||||
result = flipper_format_update_float(flipper_format, key, data, data_size);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_format_insert_or_update_hex(
|
||||
FlipperFormat* flipper_format,
|
||||
const char* key,
|
||||
const uint8_t* data,
|
||||
const uint16_t data_size) {
|
||||
bool result = false;
|
||||
|
||||
if(!flipper_format_key_exist(flipper_format, key)) {
|
||||
flipper_format_seek_to_end(flipper_format);
|
||||
result = flipper_format_write_hex(flipper_format, key, data, data_size);
|
||||
} else {
|
||||
result = flipper_format_update_hex(flipper_format, key, data, data_size);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
@@ -179,6 +179,22 @@ void flipper_format_set_strict_mode(FlipperFormat* flipper_format, bool strict_m
|
||||
*/
|
||||
bool flipper_format_rewind(FlipperFormat* flipper_format);
|
||||
|
||||
/**
|
||||
* Move the RW pointer at the end. Can be useful if you want to add some data after reading.
|
||||
* @param flipper_format Pointer to a FlipperFormat instance
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_format_seek_to_end(FlipperFormat* flipper_format);
|
||||
|
||||
/**
|
||||
* Check if the key exists.
|
||||
* @param flipper_format Pointer to a FlipperFormat instance
|
||||
* @param key Key
|
||||
* @return true key exists
|
||||
* @return false key is not exists
|
||||
*/
|
||||
bool flipper_format_key_exist(FlipperFormat* flipper_format, const char* key);
|
||||
|
||||
/**
|
||||
* Read the header (file type and version).
|
||||
* @param flipper_format Pointer to a FlipperFormat instance
|
||||
@@ -466,6 +482,89 @@ bool flipper_format_update_hex(
|
||||
const uint8_t* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Updates the value of the first matching key to a string value, or adds the key and value if the key did not exist.
|
||||
* Sets the RW pointer to a position at the end of inserted data.
|
||||
* @param flipper_format Pointer to a FlipperFormat instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_format_insert_or_update_string(
|
||||
FlipperFormat* flipper_format,
|
||||
const char* key,
|
||||
string_t data);
|
||||
|
||||
/**
|
||||
* Updates the value of the first matching key to a string value, or adds the key and value if the key did not exist.
|
||||
* Plain C version.
|
||||
* Sets the RW pointer to a position at the end of inserted data.
|
||||
* @param flipper_format Pointer to a FlipperFormat instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_format_insert_or_update_string_cstr(
|
||||
FlipperFormat* flipper_format,
|
||||
const char* key,
|
||||
const char* data);
|
||||
|
||||
/**
|
||||
* Updates the value of the first matching key to a uint32 array value, or adds the key and value if the key did not exist.
|
||||
* Sets the RW pointer to a position at the end of inserted data.
|
||||
* @param flipper_format Pointer to a FlipperFormat instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_format_insert_or_update_uint32(
|
||||
FlipperFormat* flipper_format,
|
||||
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, or adds the key and value if the key did not exist.
|
||||
* Sets the RW pointer to a position at the end of inserted data.
|
||||
* @param flipper_format Pointer to a FlipperFormat instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_format_insert_or_update_int32(
|
||||
FlipperFormat* flipper_format,
|
||||
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, or adds the key and value if the key did not exist.
|
||||
* Sets the RW pointer to a position at the end of inserted data.
|
||||
* @param flipper_format Pointer to a FlipperFormat instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_format_insert_or_update_float(
|
||||
FlipperFormat* flipper_format,
|
||||
const char* key,
|
||||
const float* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Updates the value of the first matching key to an array of hex-formatted bytes, or adds the key and value if the key did not exist.
|
||||
*Sets the RW pointer to a position at the end of inserted data.
|
||||
* @param flipper_format Pointer to a FlipperFormat instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_format_insert_or_update_hex(
|
||||
FlipperFormat* flipper_format,
|
||||
const char* key,
|
||||
const uint8_t* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
@@ -93,7 +93,7 @@ static bool flipper_format_stream_read_valid_key(Stream* stream, string_t key) {
|
||||
return found;
|
||||
}
|
||||
|
||||
static bool flipper_format_stream_seek_to_key(Stream* stream, const char* key, bool strict_mode) {
|
||||
bool flipper_format_stream_seek_to_key(Stream* stream, const char* key, bool strict_mode) {
|
||||
bool found = false;
|
||||
string_t read_key;
|
||||
|
||||
|
@@ -18,6 +18,17 @@ extern "C" {
|
||||
*/
|
||||
bool flipper_format_stream_write_eol(Stream* stream);
|
||||
|
||||
/**
|
||||
* Seek to the key from the current position of the stream.
|
||||
* Position will be at the beginning of the value corresponding to the key, if the key is found,, or at the end of the stream.
|
||||
* @param stream
|
||||
* @param key
|
||||
* @param strict_mode
|
||||
* @return true key is found
|
||||
* @return false key is not found
|
||||
*/
|
||||
bool flipper_format_stream_seek_to_key(Stream* stream, const char* key, bool strict_mode);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
Reference in New Issue
Block a user