2021-06-30 15:02:46 +03:00
|
|
|
#include "path.h"
|
2022-06-09 10:09:52 +03:00
|
|
|
#include <stddef.h>
|
2021-06-30 15:02:46 +03:00
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
void path_extract_filename_no_ext(const char* path, FuriString* filename) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(path);
|
|
|
|
furi_check(filename);
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_set(filename, path);
|
2021-06-30 15:02:46 +03:00
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
size_t start_position = furi_string_search_rchar(filename, '/');
|
|
|
|
size_t end_position = furi_string_search_rchar(filename, '.');
|
2021-06-30 15:02:46 +03:00
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
if(start_position == FURI_STRING_FAILURE) {
|
2021-06-30 15:02:46 +03:00
|
|
|
start_position = 0;
|
|
|
|
} else {
|
|
|
|
start_position += 1;
|
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
if(end_position == FURI_STRING_FAILURE) {
|
|
|
|
end_position = furi_string_size(filename);
|
2021-06-30 15:02:46 +03:00
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_mid(filename, start_position, end_position - start_position);
|
2021-06-30 15:02:46 +03:00
|
|
|
}
|
2022-04-13 23:50:25 +03:00
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
void path_extract_filename(FuriString* path, FuriString* name, bool trim_ext) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(path);
|
|
|
|
furi_check(name);
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
size_t filename_start = furi_string_search_rchar(path, '/');
|
2022-05-27 14:19:21 +03:00
|
|
|
if(filename_start > 0) {
|
|
|
|
filename_start++;
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_set_n(name, path, filename_start, furi_string_size(path) - filename_start);
|
2022-05-27 14:19:21 +03:00
|
|
|
}
|
|
|
|
if(trim_ext) {
|
2022-10-05 18:15:23 +03:00
|
|
|
size_t dot = furi_string_search_rchar(name, '.');
|
2022-05-27 14:19:21 +03:00
|
|
|
if(dot > 0) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_left(name, dot);
|
2022-05-27 14:19:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
void path_extract_extension(FuriString* path, char* ext, size_t ext_len_max) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(path);
|
|
|
|
furi_check(ext);
|
2024-06-30 13:38:48 +03:00
|
|
|
furi_check(ext_len_max > 0);
|
2024-03-19 17:43:52 +03:00
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
size_t dot = furi_string_search_rchar(path, '.');
|
|
|
|
size_t filename_start = furi_string_search_rchar(path, '/');
|
2022-06-09 10:09:52 +03:00
|
|
|
|
2022-11-12 14:55:42 +03:00
|
|
|
if((dot != FURI_STRING_FAILURE) && (filename_start < dot)) {
|
2022-10-05 18:15:23 +03:00
|
|
|
strlcpy(ext, &(furi_string_get_cstr(path))[dot], ext_len_max);
|
2022-06-09 10:09:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
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);
|
2022-04-13 23:50:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
void path_extract_basename(const char* path, FuriString* basename) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(path);
|
|
|
|
furi_check(basename);
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_set(basename, path);
|
2022-04-13 23:50:25 +03:00
|
|
|
path_cleanup(basename);
|
2022-10-05 18:15:23 +03:00
|
|
|
size_t pos = furi_string_search_rchar(basename, '/');
|
|
|
|
if(pos != FURI_STRING_FAILURE) {
|
|
|
|
furi_string_right(basename, pos + 1);
|
2022-04-13 23:50:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
void path_extract_dirname(const char* path, FuriString* dirname) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(path);
|
|
|
|
furi_check(dirname);
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_set(dirname, path);
|
2022-04-13 23:50:25 +03:00
|
|
|
path_cleanup(dirname);
|
2022-10-05 18:15:23 +03:00
|
|
|
size_t pos = furi_string_search_rchar(dirname, '/');
|
|
|
|
if(pos != FURI_STRING_FAILURE) {
|
|
|
|
furi_string_left(dirname, pos);
|
2022-04-13 23:50:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
void path_append(FuriString* path, const char* suffix) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(path);
|
|
|
|
furi_check(suffix);
|
|
|
|
|
2022-04-13 23:50:25 +03:00
|
|
|
path_cleanup(path);
|
2022-10-05 18:15:23 +03:00
|
|
|
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);
|
2022-04-13 23:50:25 +03:00
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
void path_concat(const char* path, const char* suffix, FuriString* out_path) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(path);
|
|
|
|
furi_check(suffix);
|
|
|
|
furi_check(out_path);
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_set(out_path, path);
|
2022-04-13 23:50:25 +03:00
|
|
|
path_append(out_path, suffix);
|
|
|
|
}
|
2022-06-26 15:00:03 +03:00
|
|
|
|
|
|
|
bool path_contains_only_ascii(const char* path) {
|
2022-08-09 18:57:11 +03:00
|
|
|
if(!path) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-26 15:00:03 +03:00
|
|
|
const char* name_pos = strrchr(path, '/');
|
|
|
|
if(name_pos == NULL) {
|
|
|
|
name_pos = path;
|
|
|
|
} else {
|
|
|
|
name_pos++;
|
|
|
|
}
|
|
|
|
|
2022-11-28 22:28:51 +03:00
|
|
|
for(; *name_pos; ++name_pos) {
|
|
|
|
const char c = *name_pos;
|
2022-06-26 15:00:03 +03:00
|
|
|
|
2022-11-28 22:28:51 +03:00
|
|
|
// 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;
|
|
|
|
}
|
2022-06-26 15:00:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2022-07-25 14:23:47 +03:00
|
|
|
}
|