refactor file browser

This commit is contained in:
lixianjing 2024-01-01 10:59:11 +08:00
parent c247cfef39
commit 8a1a7073ac

View File

@ -96,51 +96,51 @@ file_browser_t* file_browser_create(fs_t* fs) {
return fb;
}
ret_t file_browser_set_cwd(file_browser_t* fb, const char* cwd) {
char path[MAX_PATH + 1];
char cwd_path[MAX_PATH + 1];
return_value_if_fail(fb != NULL && cwd != NULL, RET_BAD_PARAMS);
assert(strlen(cwd) <= MAX_PATH);
static ret_t file_browser_normlize_path(const char* in, char* out, uint32_t out_size) {
char path[MAX_PATH + 1] = {0};
return_value_if_fail(in != NULL && out != NULL, RET_BAD_PARAMS);
memset(path, 0x00, sizeof(path));
path_expand_vars(cwd, cwd_path, sizeof(cwd_path) -1);
cwd = cwd_path;
path_expand_vars(in, path, sizeof(path) - 1);
in = path;
if (path_is_abs(cwd)) {
path_normalize(cwd, fb->cwd, MAX_PATH);
} else {
memset(path, 0x00, sizeof(path));
return_value_if_fail(path_abs(cwd, path, MAX_PATH) == RET_OK, RET_BAD_PARAMS);
path_normalize(path, fb->cwd, MAX_PATH);
if (file_exist(in)) {
char* p = in + strlen(in);
while (p > in && *p != '/' && *p != '\\') {
p--;
}
*p = '\0';
}
path_remove_last_slash(fb->cwd);
if (path_is_abs(in)) {
path_normalize(in, out, out_size);
} else {
char abs_path[MAX_PATH + 1];
memset(abs_path, 0x00, sizeof(path));
return_value_if_fail(path_abs(in, abs_path, MAX_PATH) == RET_OK, RET_BAD_PARAMS);
path_normalize(abs_path, out, out_size);
}
path_remove_last_slash(out);
return RET_OK;
}
ret_t file_browser_set_cwd(file_browser_t* fb, const char* cwd) {
return_value_if_fail(fb != NULL && cwd != NULL, RET_BAD_PARAMS);
file_browser_normlize_path(cwd, fb->cwd, MAX_PATH);
file_browser_refresh(fb);
return RET_OK;
}
ret_t file_browser_set_top_dir(file_browser_t* fb, const char* top_dir) {
char path[MAX_PATH + 1];
char top_path[MAX_PATH + 1];
char path[MAX_PATH + 1] = {0};
return_value_if_fail(fb != NULL && top_dir != NULL, RET_BAD_PARAMS);
assert(strlen(top_dir) <= MAX_PATH);
memset(path, 0x00, sizeof(path));
path_expand_vars(top_dir, top_path, sizeof(top_path) -1);
top_dir = top_path;
if (path_is_abs(top_dir)) {
path_normalize(top_dir, path, MAX_PATH);
} else {
char abs_path[MAX_PATH + 1];
memset(abs_path, 0x00, sizeof(path));
return_value_if_fail(path_abs(top_dir, abs_path, MAX_PATH) == RET_OK, RET_BAD_PARAMS);
path_normalize(abs_path, path, MAX_PATH);
}
file_browser_normlize_path(top_dir, path, MAX_PATH);
fb->top_dir = tk_str_copy(fb->top_dir, path);
path_remove_last_slash(fb->top_dir);
return RET_OK;
}