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:
@@ -1,7 +1,6 @@
|
||||
#include "file_browser_worker.h"
|
||||
#include <core/check.h>
|
||||
#include <core/common_defines.h>
|
||||
#include "m-string.h"
|
||||
#include "storage/filesystem_api_defines.h"
|
||||
#include <m-array.h>
|
||||
#include <stdbool.h>
|
||||
@@ -35,8 +34,8 @@ ARRAY_DEF(idx_last_array, int32_t)
|
||||
struct BrowserWorker {
|
||||
FuriThread* thread;
|
||||
|
||||
string_t filter_extension;
|
||||
string_t path_next;
|
||||
FuriString* filter_extension;
|
||||
FuriString* path_next;
|
||||
int32_t item_sel_idx;
|
||||
uint32_t load_offset;
|
||||
uint32_t load_count;
|
||||
@@ -50,11 +49,11 @@ struct BrowserWorker {
|
||||
BrowserWorkerLongLoadCallback long_load_cb;
|
||||
};
|
||||
|
||||
static bool browser_path_is_file(string_t path) {
|
||||
static bool browser_path_is_file(FuriString* path) {
|
||||
bool state = false;
|
||||
FileInfo file_info;
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
if(storage_common_stat(storage, string_get_cstr(path), &file_info) == FSE_OK) {
|
||||
if(storage_common_stat(storage, furi_string_get_cstr(path), &file_info) == FSE_OK) {
|
||||
if((file_info.flags & FSF_DIRECTORY) == 0) {
|
||||
state = true;
|
||||
}
|
||||
@@ -63,50 +62,50 @@ static bool browser_path_is_file(string_t path) {
|
||||
return state;
|
||||
}
|
||||
|
||||
static bool browser_path_trim(string_t path) {
|
||||
static bool browser_path_trim(FuriString* path) {
|
||||
bool is_root = false;
|
||||
size_t filename_start = string_search_rchar(path, '/');
|
||||
string_left(path, filename_start);
|
||||
if((string_empty_p(path)) || (filename_start == STRING_FAILURE)) {
|
||||
string_set_str(path, BROWSER_ROOT);
|
||||
size_t filename_start = furi_string_search_rchar(path, '/');
|
||||
furi_string_left(path, filename_start);
|
||||
if((furi_string_empty(path)) || (filename_start == FURI_STRING_FAILURE)) {
|
||||
furi_string_set(path, BROWSER_ROOT);
|
||||
is_root = true;
|
||||
}
|
||||
return is_root;
|
||||
}
|
||||
|
||||
static bool browser_filter_by_name(BrowserWorker* browser, string_t name, bool is_folder) {
|
||||
static bool browser_filter_by_name(BrowserWorker* browser, FuriString* name, bool is_folder) {
|
||||
if(is_folder) {
|
||||
// Skip assets folders (if enabled)
|
||||
if(browser->skip_assets) {
|
||||
return ((string_cmp_str(name, ASSETS_DIR) == 0) ? (false) : (true));
|
||||
return ((furi_string_cmp_str(name, ASSETS_DIR) == 0) ? (false) : (true));
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
// Filter files by extension
|
||||
if((string_empty_p(browser->filter_extension)) ||
|
||||
(string_cmp_str(browser->filter_extension, "*") == 0)) {
|
||||
if((furi_string_empty(browser->filter_extension)) ||
|
||||
(furi_string_cmp_str(browser->filter_extension, "*") == 0)) {
|
||||
return true;
|
||||
}
|
||||
if(string_end_with_string_p(name, browser->filter_extension)) {
|
||||
if(furi_string_end_with(name, browser->filter_extension)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool browser_folder_check_and_switch(string_t path) {
|
||||
static bool browser_folder_check_and_switch(FuriString* path) {
|
||||
FileInfo file_info;
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
bool is_root = false;
|
||||
|
||||
if(string_search_rchar(path, '/') == 0) {
|
||||
if(furi_string_search_rchar(path, '/') == 0) {
|
||||
is_root = true;
|
||||
}
|
||||
|
||||
while(1) {
|
||||
// Check if folder is existing and navigate back if not
|
||||
if(storage_common_stat(storage, string_get_cstr(path), &file_info) == FSE_OK) {
|
||||
if(storage_common_stat(storage, furi_string_get_cstr(path), &file_info) == FSE_OK) {
|
||||
if(file_info.flags & FSF_DIRECTORY) {
|
||||
break;
|
||||
}
|
||||
@@ -122,8 +121,8 @@ static bool browser_folder_check_and_switch(string_t path) {
|
||||
|
||||
static bool browser_folder_init(
|
||||
BrowserWorker* browser,
|
||||
string_t path,
|
||||
string_t filename,
|
||||
FuriString* path,
|
||||
FuriString* filename,
|
||||
uint32_t* item_cnt,
|
||||
int32_t* file_idx) {
|
||||
bool state = false;
|
||||
@@ -134,13 +133,13 @@ static bool browser_folder_init(
|
||||
File* directory = storage_file_alloc(storage);
|
||||
|
||||
char name_temp[FILE_NAME_LEN_MAX];
|
||||
string_t name_str;
|
||||
string_init(name_str);
|
||||
FuriString* name_str;
|
||||
name_str = furi_string_alloc();
|
||||
|
||||
*item_cnt = 0;
|
||||
*file_idx = -1;
|
||||
|
||||
if(storage_dir_open(directory, string_get_cstr(path))) {
|
||||
if(storage_dir_open(directory, furi_string_get_cstr(path))) {
|
||||
state = true;
|
||||
while(1) {
|
||||
if(!storage_dir_read(directory, &file_info, name_temp, FILE_NAME_LEN_MAX)) {
|
||||
@@ -148,10 +147,10 @@ static bool browser_folder_init(
|
||||
}
|
||||
if((storage_file_get_error(directory) == FSE_OK) && (name_temp[0] != '\0')) {
|
||||
total_files_cnt++;
|
||||
string_set_str(name_str, name_temp);
|
||||
furi_string_set(name_str, name_temp);
|
||||
if(browser_filter_by_name(browser, name_str, (file_info.flags & FSF_DIRECTORY))) {
|
||||
if(!string_empty_p(filename)) {
|
||||
if(string_cmp(name_str, filename) == 0) {
|
||||
if(!furi_string_empty(filename)) {
|
||||
if(furi_string_cmp(name_str, filename) == 0) {
|
||||
*file_idx = *item_cnt;
|
||||
}
|
||||
}
|
||||
@@ -167,7 +166,7 @@ static bool browser_folder_init(
|
||||
}
|
||||
}
|
||||
|
||||
string_clear(name_str);
|
||||
furi_string_free(name_str);
|
||||
|
||||
storage_dir_close(directory);
|
||||
storage_file_free(directory);
|
||||
@@ -178,20 +177,20 @@ static bool browser_folder_init(
|
||||
}
|
||||
|
||||
static bool
|
||||
browser_folder_load(BrowserWorker* browser, string_t path, uint32_t offset, uint32_t count) {
|
||||
browser_folder_load(BrowserWorker* browser, FuriString* path, uint32_t offset, uint32_t count) {
|
||||
FileInfo file_info;
|
||||
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
File* directory = storage_file_alloc(storage);
|
||||
|
||||
char name_temp[FILE_NAME_LEN_MAX];
|
||||
string_t name_str;
|
||||
string_init(name_str);
|
||||
FuriString* name_str;
|
||||
name_str = furi_string_alloc();
|
||||
|
||||
uint32_t items_cnt = 0;
|
||||
|
||||
do {
|
||||
if(!storage_dir_open(directory, string_get_cstr(path))) {
|
||||
if(!storage_dir_open(directory, furi_string_get_cstr(path))) {
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -201,7 +200,7 @@ static bool
|
||||
break;
|
||||
}
|
||||
if(storage_file_get_error(directory) == FSE_OK) {
|
||||
string_set_str(name_str, name_temp);
|
||||
furi_string_set(name_str, name_temp);
|
||||
if(browser_filter_by_name(browser, name_str, (file_info.flags & FSF_DIRECTORY))) {
|
||||
items_cnt++;
|
||||
}
|
||||
@@ -223,9 +222,9 @@ static bool
|
||||
break;
|
||||
}
|
||||
if(storage_file_get_error(directory) == FSE_OK) {
|
||||
string_set_str(name_str, name_temp);
|
||||
furi_string_set(name_str, name_temp);
|
||||
if(browser_filter_by_name(browser, name_str, (file_info.flags & FSF_DIRECTORY))) {
|
||||
string_printf(name_str, "%s/%s", string_get_cstr(path), name_temp);
|
||||
furi_string_printf(name_str, "%s/%s", furi_string_get_cstr(path), name_temp);
|
||||
if(browser->list_item_cb) {
|
||||
browser->list_item_cb(
|
||||
browser->cb_ctx, name_str, (file_info.flags & FSF_DIRECTORY), false);
|
||||
@@ -241,7 +240,7 @@ static bool
|
||||
}
|
||||
} while(0);
|
||||
|
||||
string_clear(name_str);
|
||||
furi_string_free(name_str);
|
||||
|
||||
storage_dir_close(directory);
|
||||
storage_file_free(directory);
|
||||
@@ -257,12 +256,12 @@ static int32_t browser_worker(void* context) {
|
||||
FURI_LOG_D(TAG, "Start");
|
||||
|
||||
uint32_t items_cnt = 0;
|
||||
string_t path;
|
||||
string_init_set_str(path, BROWSER_ROOT);
|
||||
FuriString* path;
|
||||
path = furi_string_alloc_set(BROWSER_ROOT);
|
||||
browser->item_sel_idx = -1;
|
||||
|
||||
string_t filename;
|
||||
string_init(filename);
|
||||
FuriString* filename;
|
||||
filename = furi_string_alloc();
|
||||
|
||||
furi_thread_flags_set(furi_thread_get_id(browser->thread), WorkerEvtConfigChange);
|
||||
|
||||
@@ -282,7 +281,7 @@ static int32_t browser_worker(void* context) {
|
||||
}
|
||||
|
||||
if(flags & WorkerEvtFolderEnter) {
|
||||
string_set(path, browser->path_next);
|
||||
furi_string_set(path, browser->path_next);
|
||||
bool is_root = browser_folder_check_and_switch(path);
|
||||
|
||||
// Push previous selected item index to history array
|
||||
@@ -293,13 +292,13 @@ static int32_t browser_worker(void* context) {
|
||||
FURI_LOG_D(
|
||||
TAG,
|
||||
"Enter folder: %s items: %u idx: %d",
|
||||
string_get_cstr(path),
|
||||
furi_string_get_cstr(path),
|
||||
items_cnt,
|
||||
file_idx);
|
||||
if(browser->folder_cb) {
|
||||
browser->folder_cb(browser->cb_ctx, items_cnt, file_idx, is_root);
|
||||
}
|
||||
string_reset(filename);
|
||||
furi_string_reset(filename);
|
||||
}
|
||||
|
||||
if(flags & WorkerEvtFolderExit) {
|
||||
@@ -313,7 +312,11 @@ static int32_t browser_worker(void* context) {
|
||||
idx_last_array_pop_back(&file_idx, browser->idx_last);
|
||||
}
|
||||
FURI_LOG_D(
|
||||
TAG, "Exit to: %s items: %u idx: %d", string_get_cstr(path), items_cnt, file_idx);
|
||||
TAG,
|
||||
"Exit to: %s items: %u idx: %d",
|
||||
furi_string_get_cstr(path),
|
||||
items_cnt,
|
||||
file_idx);
|
||||
if(browser->folder_cb) {
|
||||
browser->folder_cb(browser->cb_ctx, items_cnt, file_idx, is_root);
|
||||
}
|
||||
@@ -323,12 +326,12 @@ static int32_t browser_worker(void* context) {
|
||||
bool is_root = browser_folder_check_and_switch(path);
|
||||
|
||||
int32_t file_idx = 0;
|
||||
string_reset(filename);
|
||||
furi_string_reset(filename);
|
||||
browser_folder_init(browser, path, filename, &items_cnt, &file_idx);
|
||||
FURI_LOG_D(
|
||||
TAG,
|
||||
"Refresh folder: %s items: %u idx: %d",
|
||||
string_get_cstr(path),
|
||||
furi_string_get_cstr(path),
|
||||
items_cnt,
|
||||
browser->item_sel_idx);
|
||||
if(browser->folder_cb) {
|
||||
@@ -346,21 +349,22 @@ static int32_t browser_worker(void* context) {
|
||||
}
|
||||
}
|
||||
|
||||
string_clear(filename);
|
||||
string_clear(path);
|
||||
furi_string_free(filename);
|
||||
furi_string_free(path);
|
||||
|
||||
FURI_LOG_D(TAG, "End");
|
||||
return 0;
|
||||
}
|
||||
|
||||
BrowserWorker* file_browser_worker_alloc(string_t path, const char* filter_ext, bool skip_assets) {
|
||||
BrowserWorker*
|
||||
file_browser_worker_alloc(FuriString* path, const char* filter_ext, bool skip_assets) {
|
||||
BrowserWorker* browser = malloc(sizeof(BrowserWorker));
|
||||
|
||||
idx_last_array_init(browser->idx_last);
|
||||
|
||||
string_init_set_str(browser->filter_extension, filter_ext);
|
||||
browser->filter_extension = furi_string_alloc_set(filter_ext);
|
||||
browser->skip_assets = skip_assets;
|
||||
string_init_set(browser->path_next, path);
|
||||
browser->path_next = furi_string_alloc_set(path);
|
||||
|
||||
browser->thread = furi_thread_alloc();
|
||||
furi_thread_set_name(browser->thread, "BrowserWorker");
|
||||
@@ -379,8 +383,8 @@ void file_browser_worker_free(BrowserWorker* browser) {
|
||||
furi_thread_join(browser->thread);
|
||||
furi_thread_free(browser->thread);
|
||||
|
||||
string_clear(browser->filter_extension);
|
||||
string_clear(browser->path_next);
|
||||
furi_string_free(browser->filter_extension);
|
||||
furi_string_free(browser->path_next);
|
||||
|
||||
idx_last_array_clear(browser->idx_last);
|
||||
|
||||
@@ -422,19 +426,19 @@ void file_browser_worker_set_long_load_callback(
|
||||
|
||||
void file_browser_worker_set_config(
|
||||
BrowserWorker* browser,
|
||||
string_t path,
|
||||
FuriString* path,
|
||||
const char* filter_ext,
|
||||
bool skip_assets) {
|
||||
furi_assert(browser);
|
||||
string_set(browser->path_next, path);
|
||||
string_set_str(browser->filter_extension, filter_ext);
|
||||
furi_string_set(browser->path_next, path);
|
||||
furi_string_set(browser->filter_extension, filter_ext);
|
||||
browser->skip_assets = skip_assets;
|
||||
furi_thread_flags_set(furi_thread_get_id(browser->thread), WorkerEvtConfigChange);
|
||||
}
|
||||
|
||||
void file_browser_worker_folder_enter(BrowserWorker* browser, string_t path, int32_t item_idx) {
|
||||
void file_browser_worker_folder_enter(BrowserWorker* browser, FuriString* path, int32_t item_idx) {
|
||||
furi_assert(browser);
|
||||
string_set(browser->path_next, path);
|
||||
furi_string_set(browser->path_next, path);
|
||||
browser->item_sel_idx = item_idx;
|
||||
furi_thread_flags_set(furi_thread_get_id(browser->thread), WorkerEvtFolderEnter);
|
||||
}
|
||||
|
Reference in New Issue
Block a user