M*LIB: non-inlined strings, FuriString primitive (#1795)
* Quicksave 1 * Header stage complete * Source stage complete * Lint & merge fixes * Includes * Documentation step 1 * FBT: output free size considering BT STACK * Documentation step 2 * py lint * Fix music player plugin * unit test stage 1: string allocator, mem, getters, setters, appends, compare, search. * unit test: string equality * unit test: string replace * unit test: string start_with, end_with * unit test: string trim * unit test: utf-8 * Rename * Revert fw_size changes * Simplify CLI backspace handling * Simplify CLI character insert * Merge fixes * Furi: correct filenaming and spelling * Bt: remove furi string include Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
@@ -137,7 +137,7 @@ bool flipper_format_key_exist(FlipperFormat* flipper_format, const char* key) {
|
||||
|
||||
bool flipper_format_read_header(
|
||||
FlipperFormat* flipper_format,
|
||||
string_t filetype,
|
||||
FuriString* filetype,
|
||||
uint32_t* version) {
|
||||
furi_assert(flipper_format);
|
||||
return flipper_format_read_string(flipper_format, flipper_format_filetype_key, filetype) &&
|
||||
@@ -146,10 +146,11 @@ bool flipper_format_read_header(
|
||||
|
||||
bool flipper_format_write_header(
|
||||
FlipperFormat* flipper_format,
|
||||
string_t filetype,
|
||||
FuriString* filetype,
|
||||
const uint32_t version) {
|
||||
furi_assert(flipper_format);
|
||||
return flipper_format_write_header_cstr(flipper_format, string_get_cstr(filetype), version);
|
||||
return flipper_format_write_header_cstr(
|
||||
flipper_format, furi_string_get_cstr(filetype), version);
|
||||
}
|
||||
|
||||
bool flipper_format_write_header_cstr(
|
||||
@@ -171,18 +172,18 @@ bool flipper_format_get_value_count(
|
||||
flipper_format->stream, key, count, flipper_format->strict_mode);
|
||||
}
|
||||
|
||||
bool flipper_format_read_string(FlipperFormat* flipper_format, const char* key, string_t data) {
|
||||
bool flipper_format_read_string(FlipperFormat* flipper_format, const char* key, FuriString* data) {
|
||||
furi_assert(flipper_format);
|
||||
return flipper_format_stream_read_value_line(
|
||||
flipper_format->stream, key, FlipperStreamValueStr, data, 1, flipper_format->strict_mode);
|
||||
}
|
||||
|
||||
bool flipper_format_write_string(FlipperFormat* flipper_format, const char* key, string_t data) {
|
||||
bool flipper_format_write_string(FlipperFormat* flipper_format, const char* key, FuriString* data) {
|
||||
furi_assert(flipper_format);
|
||||
FlipperStreamWriteData write_data = {
|
||||
.key = key,
|
||||
.type = FlipperStreamValueStr,
|
||||
.data = string_get_cstr(data),
|
||||
.data = furi_string_get_cstr(data),
|
||||
.data_size = 1,
|
||||
};
|
||||
bool result = flipper_format_stream_write_value_line(flipper_format->stream, &write_data);
|
||||
@@ -386,9 +387,9 @@ bool flipper_format_write_hex(
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_format_write_comment(FlipperFormat* flipper_format, string_t data) {
|
||||
bool flipper_format_write_comment(FlipperFormat* flipper_format, FuriString* data) {
|
||||
furi_assert(flipper_format);
|
||||
return flipper_format_write_comment_cstr(flipper_format, string_get_cstr(data));
|
||||
return flipper_format_write_comment_cstr(flipper_format, furi_string_get_cstr(data));
|
||||
}
|
||||
|
||||
bool flipper_format_write_comment_cstr(FlipperFormat* flipper_format, const char* data) {
|
||||
@@ -409,12 +410,12 @@ bool flipper_format_delete_key(FlipperFormat* flipper_format, const char* key) {
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_format_update_string(FlipperFormat* flipper_format, const char* key, string_t data) {
|
||||
bool flipper_format_update_string(FlipperFormat* flipper_format, const char* key, FuriString* data) {
|
||||
furi_assert(flipper_format);
|
||||
FlipperStreamWriteData write_data = {
|
||||
.key = key,
|
||||
.type = FlipperStreamValueStr,
|
||||
.data = string_get_cstr(data),
|
||||
.data = furi_string_get_cstr(data),
|
||||
.data_size = 1,
|
||||
};
|
||||
bool result = flipper_format_stream_delete_key_and_write(
|
||||
@@ -522,7 +523,7 @@ bool flipper_format_update_hex(
|
||||
bool flipper_format_insert_or_update_string(
|
||||
FlipperFormat* flipper_format,
|
||||
const char* key,
|
||||
string_t data) {
|
||||
FuriString* data) {
|
||||
bool result = false;
|
||||
|
||||
if(!flipper_format_key_exist(flipper_format, key)) {
|
||||
|
@@ -70,13 +70,13 @@
|
||||
*
|
||||
* do {
|
||||
* uint32_t version = 1;
|
||||
* string_t file_type;
|
||||
* string_t string_value;
|
||||
* FuriString* file_type;
|
||||
* FuriString* 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);
|
||||
* file_type = furi_string_alloc();
|
||||
* string_value = furi_string_alloc();
|
||||
*
|
||||
* if(!flipper_format_file_open_existing(file, EXT_PATH("flipper_format_test"))) break;
|
||||
* if(!flipper_format_read_header(file, file_type, &version)) break;
|
||||
@@ -94,7 +94,6 @@
|
||||
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <mlib/m-string.h>
|
||||
#include <storage/storage.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -227,7 +226,7 @@ bool flipper_format_key_exist(FlipperFormat* flipper_format, const char* key);
|
||||
*/
|
||||
bool flipper_format_read_header(
|
||||
FlipperFormat* flipper_format,
|
||||
string_t filetype,
|
||||
FuriString* filetype,
|
||||
uint32_t* version);
|
||||
|
||||
/**
|
||||
@@ -239,7 +238,7 @@ bool flipper_format_read_header(
|
||||
*/
|
||||
bool flipper_format_write_header(
|
||||
FlipperFormat* flipper_format,
|
||||
string_t filetype,
|
||||
FuriString* filetype,
|
||||
const uint32_t version);
|
||||
|
||||
/**
|
||||
@@ -273,7 +272,7 @@ bool flipper_format_get_value_count(
|
||||
* @param data Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_format_read_string(FlipperFormat* flipper_format, const char* key, string_t data);
|
||||
bool flipper_format_read_string(FlipperFormat* flipper_format, const char* key, FuriString* data);
|
||||
|
||||
/**
|
||||
* Write key and string
|
||||
@@ -282,7 +281,7 @@ bool flipper_format_read_string(FlipperFormat* flipper_format, const char* key,
|
||||
* @param data Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_format_write_string(FlipperFormat* flipper_format, const char* key, string_t data);
|
||||
bool flipper_format_write_string(FlipperFormat* flipper_format, const char* key, FuriString* data);
|
||||
|
||||
/**
|
||||
* Write key and string. Plain C string version.
|
||||
@@ -470,7 +469,7 @@ bool flipper_format_write_hex(
|
||||
* @param data Comment text
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_format_write_comment(FlipperFormat* flipper_format, string_t data);
|
||||
bool flipper_format_write_comment(FlipperFormat* flipper_format, FuriString* data);
|
||||
|
||||
/**
|
||||
* Write comment. Plain C string version.
|
||||
@@ -495,7 +494,7 @@ bool flipper_format_delete_key(FlipperFormat* flipper_format, const char* key);
|
||||
* @param data Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_format_update_string(FlipperFormat* flipper_format, const char* key, string_t data);
|
||||
bool flipper_format_update_string(FlipperFormat* flipper_format, const char* key, FuriString* data);
|
||||
|
||||
/**
|
||||
* Updates the value of the first matching key to a string value. Plain C version. Sets the RW pointer to a position at the end of inserted data.
|
||||
@@ -585,7 +584,7 @@ bool flipper_format_update_hex(
|
||||
bool flipper_format_insert_or_update_string(
|
||||
FlipperFormat* flipper_format,
|
||||
const char* key,
|
||||
string_t data);
|
||||
FuriString* 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.
|
||||
|
@@ -26,8 +26,8 @@ bool flipper_format_stream_write_eol(Stream* stream) {
|
||||
return flipper_format_stream_write(stream, &flipper_format_eoln, 1);
|
||||
}
|
||||
|
||||
static bool flipper_format_stream_read_valid_key(Stream* stream, string_t key) {
|
||||
string_reset(key);
|
||||
static bool flipper_format_stream_read_valid_key(Stream* stream, FuriString* key) {
|
||||
furi_string_reset(key);
|
||||
const size_t buffer_size = 32;
|
||||
uint8_t buffer[buffer_size];
|
||||
|
||||
@@ -44,7 +44,7 @@ static bool flipper_format_stream_read_valid_key(Stream* stream, string_t key) {
|
||||
uint8_t data = buffer[i];
|
||||
if(data == flipper_format_eoln) {
|
||||
// EOL found, clean data, start accumulating data and set the new_line flag
|
||||
string_reset(key);
|
||||
furi_string_reset(key);
|
||||
accumulate = true;
|
||||
new_line = true;
|
||||
} else if(data == flipper_format_eolr) {
|
||||
@@ -60,7 +60,7 @@ static bool flipper_format_stream_read_valid_key(Stream* stream, string_t key) {
|
||||
// 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_reset(key);
|
||||
furi_string_reset(key);
|
||||
accumulate = false;
|
||||
new_line = false;
|
||||
} else {
|
||||
@@ -82,7 +82,7 @@ static bool flipper_format_stream_read_valid_key(Stream* stream, string_t key) {
|
||||
new_line = false;
|
||||
if(accumulate) {
|
||||
// and accumulate data if we want
|
||||
string_push_back(key, data);
|
||||
furi_string_push_back(key, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -95,13 +95,13 @@ static bool flipper_format_stream_read_valid_key(Stream* stream, string_t key) {
|
||||
|
||||
bool flipper_format_stream_seek_to_key(Stream* stream, const char* key, bool strict_mode) {
|
||||
bool found = false;
|
||||
string_t read_key;
|
||||
FuriString* read_key;
|
||||
|
||||
string_init(read_key);
|
||||
read_key = furi_string_alloc();
|
||||
|
||||
while(!stream_eof(stream)) {
|
||||
if(flipper_format_stream_read_valid_key(stream, read_key)) {
|
||||
if(string_cmp_str(read_key, key) == 0) {
|
||||
if(furi_string_cmp_str(read_key, key) == 0) {
|
||||
if(!stream_seek(stream, 2, StreamOffsetFromCurrent)) break;
|
||||
|
||||
found = true;
|
||||
@@ -112,13 +112,13 @@ bool flipper_format_stream_seek_to_key(Stream* stream, const char* key, bool str
|
||||
}
|
||||
}
|
||||
}
|
||||
string_clear(read_key);
|
||||
furi_string_free(read_key);
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
static bool flipper_format_stream_read_value(Stream* stream, string_t value, bool* last) {
|
||||
string_reset(value);
|
||||
static bool flipper_format_stream_read_value(Stream* stream, FuriString* value, bool* last) {
|
||||
furi_string_reset(value);
|
||||
const size_t buffer_size = 32;
|
||||
uint8_t buffer[buffer_size];
|
||||
bool result = false;
|
||||
@@ -129,7 +129,7 @@ static bool flipper_format_stream_read_value(Stream* stream, string_t value, boo
|
||||
|
||||
if(was_read == 0) {
|
||||
// check EOF
|
||||
if(stream_eof(stream) && string_size(value) > 0) {
|
||||
if(stream_eof(stream) && furi_string_size(value) > 0) {
|
||||
result = true;
|
||||
*last = true;
|
||||
break;
|
||||
@@ -139,7 +139,7 @@ static bool flipper_format_stream_read_value(Stream* stream, string_t value, boo
|
||||
for(uint16_t i = 0; i < was_read; i++) {
|
||||
uint8_t data = buffer[i];
|
||||
if(data == flipper_format_eoln) {
|
||||
if(string_size(value) > 0) {
|
||||
if(furi_string_size(value) > 0) {
|
||||
if(!stream_seek(stream, i - was_read, StreamOffsetFromCurrent)) {
|
||||
error = true;
|
||||
break;
|
||||
@@ -152,7 +152,7 @@ static bool flipper_format_stream_read_value(Stream* stream, string_t value, boo
|
||||
error = true;
|
||||
}
|
||||
} else if(data == ' ') {
|
||||
if(string_size(value) > 0) {
|
||||
if(furi_string_size(value) > 0) {
|
||||
if(!stream_seek(stream, i - was_read, StreamOffsetFromCurrent)) {
|
||||
error = true;
|
||||
break;
|
||||
@@ -166,7 +166,7 @@ static bool flipper_format_stream_read_value(Stream* stream, string_t value, boo
|
||||
} else if(data == flipper_format_eolr) {
|
||||
// Ignore
|
||||
} else {
|
||||
string_push_back(value, data);
|
||||
furi_string_push_back(value, data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,8 +176,8 @@ static bool flipper_format_stream_read_value(Stream* stream, string_t value, boo
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool flipper_format_stream_read_line(Stream* stream, string_t str_result) {
|
||||
string_reset(str_result);
|
||||
static bool flipper_format_stream_read_line(Stream* stream, FuriString* str_result) {
|
||||
furi_string_reset(str_result);
|
||||
const size_t buffer_size = 32;
|
||||
uint8_t buffer[buffer_size];
|
||||
|
||||
@@ -201,7 +201,7 @@ static bool flipper_format_stream_read_line(Stream* stream, string_t str_result)
|
||||
} else if(data == flipper_format_eolr) {
|
||||
// Ignore
|
||||
} else {
|
||||
string_push_back(str_result, data);
|
||||
furi_string_push_back(str_result, data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,7 +210,7 @@ static bool flipper_format_stream_read_line(Stream* stream, string_t str_result)
|
||||
}
|
||||
} while(true);
|
||||
|
||||
return string_size(str_result) != 0;
|
||||
return furi_string_size(str_result) != 0;
|
||||
}
|
||||
|
||||
static bool flipper_format_stream_seek_to_next_line(Stream* stream) {
|
||||
@@ -254,8 +254,8 @@ bool flipper_format_stream_write_value_line(Stream* stream, FlipperStreamWriteDa
|
||||
if(write_data->type == FlipperStreamValueIgnore) {
|
||||
result = true;
|
||||
} else {
|
||||
string_t value;
|
||||
string_init(value);
|
||||
FuriString* value;
|
||||
value = furi_string_alloc();
|
||||
|
||||
do {
|
||||
if(!flipper_format_stream_write_key(stream, write_data->key)) break;
|
||||
@@ -267,45 +267,45 @@ bool flipper_format_stream_write_value_line(Stream* stream, FlipperStreamWriteDa
|
||||
switch(write_data->type) {
|
||||
case FlipperStreamValueStr: {
|
||||
const char* data = write_data->data;
|
||||
string_printf(value, "%s", data);
|
||||
furi_string_printf(value, "%s", data);
|
||||
}; break;
|
||||
case FlipperStreamValueHex: {
|
||||
const uint8_t* data = write_data->data;
|
||||
string_printf(value, "%02X", data[i]);
|
||||
furi_string_printf(value, "%02X", data[i]);
|
||||
}; break;
|
||||
#ifndef FLIPPER_STREAM_LITE
|
||||
case FlipperStreamValueFloat: {
|
||||
const float* data = write_data->data;
|
||||
string_printf(value, "%f", (double)data[i]);
|
||||
furi_string_printf(value, "%f", (double)data[i]);
|
||||
}; break;
|
||||
#endif
|
||||
case FlipperStreamValueInt32: {
|
||||
const int32_t* data = write_data->data;
|
||||
string_printf(value, "%" PRIi32, data[i]);
|
||||
furi_string_printf(value, "%" PRIi32, data[i]);
|
||||
}; break;
|
||||
case FlipperStreamValueUint32: {
|
||||
const uint32_t* data = write_data->data;
|
||||
string_printf(value, "%" PRId32, data[i]);
|
||||
furi_string_printf(value, "%" PRId32, data[i]);
|
||||
}; break;
|
||||
case FlipperStreamValueHexUint64: {
|
||||
const uint64_t* data = write_data->data;
|
||||
string_printf(
|
||||
furi_string_printf(
|
||||
value, "%08lX%08lX", (uint32_t)(data[i] >> 32), (uint32_t)data[i]);
|
||||
}; break;
|
||||
case FlipperStreamValueBool: {
|
||||
const bool* data = write_data->data;
|
||||
string_printf(value, data[i] ? "true" : "false");
|
||||
furi_string_printf(value, data[i] ? "true" : "false");
|
||||
}; break;
|
||||
default:
|
||||
furi_crash("Unknown FF type");
|
||||
}
|
||||
|
||||
if((size_t)(i + 1) < write_data->data_size) {
|
||||
string_cat(value, " ");
|
||||
furi_string_cat(value, " ");
|
||||
}
|
||||
|
||||
if(!flipper_format_stream_write(
|
||||
stream, string_get_cstr(value), string_size(value))) {
|
||||
stream, furi_string_get_cstr(value), furi_string_size(value))) {
|
||||
cycle_error = true;
|
||||
break;
|
||||
}
|
||||
@@ -316,7 +316,7 @@ bool flipper_format_stream_write_value_line(Stream* stream, FlipperStreamWriteDa
|
||||
result = true;
|
||||
} while(false);
|
||||
|
||||
string_clear(value);
|
||||
furi_string_free(value);
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -335,15 +335,15 @@ bool flipper_format_stream_read_value_line(
|
||||
if(!flipper_format_stream_seek_to_key(stream, key, strict_mode)) break;
|
||||
|
||||
if(type == FlipperStreamValueStr) {
|
||||
string_ptr data = (string_ptr)_data;
|
||||
FuriString* data = (FuriString*)_data;
|
||||
if(flipper_format_stream_read_line(stream, data)) {
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
result = true;
|
||||
string_t value;
|
||||
string_init(value);
|
||||
FuriString* value;
|
||||
value = furi_string_alloc();
|
||||
|
||||
for(size_t i = 0; i < data_size; i++) {
|
||||
bool last = false;
|
||||
@@ -354,11 +354,11 @@ bool flipper_format_stream_read_value_line(
|
||||
switch(type) {
|
||||
case FlipperStreamValueHex: {
|
||||
uint8_t* data = _data;
|
||||
if(string_size(value) >= 2) {
|
||||
if(furi_string_size(value) >= 2) {
|
||||
// sscanf "%02X" does not work here
|
||||
if(hex_char_to_uint8(
|
||||
string_get_char(value, 0),
|
||||
string_get_char(value, 1),
|
||||
furi_string_get_char(value, 0),
|
||||
furi_string_get_char(value, 1),
|
||||
&data[i])) {
|
||||
scan_values = 1;
|
||||
}
|
||||
@@ -368,9 +368,9 @@ bool flipper_format_stream_read_value_line(
|
||||
case FlipperStreamValueFloat: {
|
||||
float* data = _data;
|
||||
// newlib-nano does not have sscanf for floats
|
||||
// scan_values = sscanf(string_get_cstr(value), "%f", &data[i]);
|
||||
// scan_values = sscanf(furi_string_get_cstr(value), "%f", &data[i]);
|
||||
char* end_char;
|
||||
data[i] = strtof(string_get_cstr(value), &end_char);
|
||||
data[i] = strtof(furi_string_get_cstr(value), &end_char);
|
||||
if(*end_char == 0) {
|
||||
// most likely ok
|
||||
scan_values = 1;
|
||||
@@ -379,23 +379,23 @@ bool flipper_format_stream_read_value_line(
|
||||
#endif
|
||||
case FlipperStreamValueInt32: {
|
||||
int32_t* data = _data;
|
||||
scan_values = sscanf(string_get_cstr(value), "%" PRIi32, &data[i]);
|
||||
scan_values = sscanf(furi_string_get_cstr(value), "%" PRIi32, &data[i]);
|
||||
}; break;
|
||||
case FlipperStreamValueUint32: {
|
||||
uint32_t* data = _data;
|
||||
scan_values = sscanf(string_get_cstr(value), "%" PRId32, &data[i]);
|
||||
scan_values = sscanf(furi_string_get_cstr(value), "%" PRId32, &data[i]);
|
||||
}; break;
|
||||
case FlipperStreamValueHexUint64: {
|
||||
uint64_t* data = _data;
|
||||
if(string_size(value) >= 16) {
|
||||
if(hex_chars_to_uint64(string_get_cstr(value), &data[i])) {
|
||||
if(furi_string_size(value) >= 16) {
|
||||
if(hex_chars_to_uint64(furi_string_get_cstr(value), &data[i])) {
|
||||
scan_values = 1;
|
||||
}
|
||||
}
|
||||
}; break;
|
||||
case FlipperStreamValueBool: {
|
||||
bool* data = _data;
|
||||
data[i] = !string_cmpi_str(value, "true");
|
||||
data[i] = !furi_string_cmpi(value, "true");
|
||||
scan_values = 1;
|
||||
}; break;
|
||||
default:
|
||||
@@ -416,7 +416,7 @@ bool flipper_format_stream_read_value_line(
|
||||
}
|
||||
}
|
||||
|
||||
string_clear(value);
|
||||
furi_string_free(value);
|
||||
}
|
||||
} while(false);
|
||||
|
||||
@@ -431,8 +431,8 @@ bool flipper_format_stream_get_value_count(
|
||||
bool result = false;
|
||||
bool last = false;
|
||||
|
||||
string_t value;
|
||||
string_init(value);
|
||||
FuriString* value;
|
||||
value = furi_string_alloc();
|
||||
|
||||
uint32_t position = stream_tell(stream);
|
||||
do {
|
||||
@@ -456,7 +456,7 @@ bool flipper_format_stream_get_value_count(
|
||||
result = false;
|
||||
}
|
||||
|
||||
string_clear(value);
|
||||
furi_string_free(value);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@@ -2,7 +2,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <toolbox/stream/stream.h>
|
||||
#include <mlib/m-string.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
Reference in New Issue
Block a user