unleashed-firmware/lib/toolbox/path.c
hedger fcbcb6b5a8
Updater: resource compression (#3716)
* toolbox: compress: moved decompressor implementation to separate func
* toolbox: compress: callback-based api; cli: storage unpack command
* toolbox: compress: separate r/w contexts for stream api
* targets: f18: sync API
* compress: naming fixes & cleanup
* toolbox: compress: using hs buffer size for stream buffers
* toolbox: tar: heatshrink stream mode
* toolbox: compress: docs & small cleanup
* toolbox: tar: header support for .hs; updater: now uses .hs for resources; .hs.tar: now rewindable
* toolbox: compress: fixed hs stream tail handling
* updater: reworked progress for resources cleanup; rebalanced stage weights
* updater: single-pass decompression; scripts: print resources compression ratio
* updater: fixed warnings
* toolbox: tar: doxygen
* docs: update
* docs: info or tarhs format; scripts: added standalone compression/decompression tool for heatshrink-formatted streams
* scripts: tarhs: fixed parameter handling
* cli: storage extract command; toolbox: tar: guess type based on extension
* unit_tests: added test for streamed raw hs decompressor `compress_decode_streamed`
* unit_tests: compress: added extraction test for .tar.hs
* rpc: autodetect compressed archives
* scripts: minor cleanup of common parts
* scripts: update: now using in-memory intermediate tar stream
* scripts: added hs.py wrapper for heatshrink-related ops (single object and directory-as-tar compression)
* scripts: naming fixes
* Toolbox: export compress_config_heatshrink_default as const symbol
* Toolbox: fix various types naming
* Toolbox: more of types naming fixes
* Toolbox: use size_t in compress io callbacks and structures
* UnitTests: update to match new compress API
* Toolbox: proper path_extract_extension usage

Co-authored-by: あく <alleteam@gmail.com>
2024-06-30 11:38:48 +01:00

136 lines
3.6 KiB
C

#include "path.h"
#include <stddef.h>
void path_extract_filename_no_ext(const char* path, FuriString* filename) {
furi_check(path);
furi_check(filename);
furi_string_set(filename, path);
size_t start_position = furi_string_search_rchar(filename, '/');
size_t end_position = furi_string_search_rchar(filename, '.');
if(start_position == FURI_STRING_FAILURE) {
start_position = 0;
} else {
start_position += 1;
}
if(end_position == FURI_STRING_FAILURE) {
end_position = furi_string_size(filename);
}
furi_string_mid(filename, start_position, end_position - start_position);
}
void path_extract_filename(FuriString* path, FuriString* name, bool trim_ext) {
furi_check(path);
furi_check(name);
size_t filename_start = furi_string_search_rchar(path, '/');
if(filename_start > 0) {
filename_start++;
furi_string_set_n(name, path, filename_start, furi_string_size(path) - filename_start);
}
if(trim_ext) {
size_t dot = furi_string_search_rchar(name, '.');
if(dot > 0) {
furi_string_left(name, dot);
}
}
}
void path_extract_extension(FuriString* path, char* ext, size_t ext_len_max) {
furi_check(path);
furi_check(ext);
furi_check(ext_len_max > 0);
size_t dot = furi_string_search_rchar(path, '.');
size_t filename_start = furi_string_search_rchar(path, '/');
if((dot != FURI_STRING_FAILURE) && (filename_start < dot)) {
strlcpy(ext, &(furi_string_get_cstr(path))[dot], ext_len_max);
}
}
static inline void path_cleanup(FuriString* path) {
furi_string_trim(path);
while(furi_string_end_with(path, "/")) {
furi_string_left(path, furi_string_size(path) - 1);
}
}
void path_extract_basename(const char* path, FuriString* basename) {
furi_check(path);
furi_check(basename);
furi_string_set(basename, path);
path_cleanup(basename);
size_t pos = furi_string_search_rchar(basename, '/');
if(pos != FURI_STRING_FAILURE) {
furi_string_right(basename, pos + 1);
}
}
void path_extract_dirname(const char* path, FuriString* dirname) {
furi_check(path);
furi_check(dirname);
furi_string_set(dirname, path);
path_cleanup(dirname);
size_t pos = furi_string_search_rchar(dirname, '/');
if(pos != FURI_STRING_FAILURE) {
furi_string_left(dirname, pos);
}
}
void path_append(FuriString* path, const char* suffix) {
furi_check(path);
furi_check(suffix);
path_cleanup(path);
FuriString* suffix_str;
suffix_str = furi_string_alloc_set(suffix);
furi_string_trim(suffix_str);
furi_string_trim(suffix_str, "/");
furi_string_cat_printf(path, "/%s", furi_string_get_cstr(suffix_str));
furi_string_free(suffix_str);
}
void path_concat(const char* path, const char* suffix, FuriString* out_path) {
furi_check(path);
furi_check(suffix);
furi_check(out_path);
furi_string_set(out_path, path);
path_append(out_path, suffix);
}
bool path_contains_only_ascii(const char* path) {
if(!path) {
return false;
}
const char* name_pos = strrchr(path, '/');
if(name_pos == NULL) {
name_pos = path;
} else {
name_pos++;
}
for(; *name_pos; ++name_pos) {
const char c = *name_pos;
// Regular ASCII characters from 0x20 to 0x7e
const bool is_out_of_range = (c < ' ') || (c > '~');
// Cross-platform forbidden character set
const bool is_forbidden = strchr("\\<>*|\":?", c);
if(is_out_of_range || is_forbidden) {
return false;
}
}
return true;
}