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