From b3a833b2c0dd5227df1c6321231e3ea287ea9de1 Mon Sep 17 00:00:00 2001 From: xianjimli Date: Mon, 6 Jan 2020 11:13:12 +0800 Subject: [PATCH] refactor file browser --- src/ext_widgets/file_browser/file_browser.c | 20 ++++++++++++++++--- src/ext_widgets/file_browser/file_browser.h | 10 ++++++++-- tests/file_browser_test.cc | 22 ++++++++++----------- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/ext_widgets/file_browser/file_browser.c b/src/ext_widgets/file_browser/file_browser.c index 0366204a4..75046446a 100644 --- a/src/ext_widgets/file_browser/file_browser.c +++ b/src/ext_widgets/file_browser/file_browser.c @@ -68,7 +68,7 @@ static fb_item_t* file_browser_find_item(file_browser_t* fb, const char* name) { for (i = 0; i < fb->items_size; i++) { fb_item_t* iter = fb->items + i; - if (tk_str_eq(iter->info.name, name)) { + if (tk_str_eq(iter->name, name)) { return iter; } } @@ -127,7 +127,7 @@ ret_t file_browser_create_file(file_browser_t* fb, const char* name, const void* fs_file_t* fp = NULL; char fullpath[MAX_PATH + 1]; fb_item_t* item = file_browser_find_item(fb, name); - return_value_if_fail(item == NULL || item->info.is_reg_file, RET_FOUND); + return_value_if_fail(item == NULL || item->is_reg_file, RET_FOUND); memset(fullpath, 0x00, sizeof(fullpath)); return_value_if_fail(fb != NULL && name != NULL && buff != NULL, RET_BAD_PARAMS); @@ -231,13 +231,18 @@ ret_t file_browser_paste(file_browser_t* fb) { ret_t file_browser_refresh(file_browser_t* fb) { fs_item_t info; + fs_stat_info_t st; + uint32_t len = 0; fs_dir_t* dir = NULL; + char fullpath[MAX_PATH + 1]; return_value_if_fail(fb != NULL && fb->cwd[0] != '\0', RET_BAD_PARAMS); fb->items_size = 0; dir = fs_open_dir(fb->fs, fb->cwd); return_value_if_fail(dir != NULL, RET_BAD_PARAMS); + len = strlen(fb->cwd); + memset(fullpath, 0x00, sizeof(fullpath)); while (fs_dir_read(dir, &info) == RET_OK) { fb_item_t* iter = NULL; if (tk_str_eq(info.name, ".") || tk_str_eq(info.name, "..")) { @@ -247,11 +252,20 @@ ret_t file_browser_refresh(file_browser_t* fb) { if (fb->filter != NULL && fb->filter(fb->filter_ctx, &info) == FALSE) { continue; } + + path_build(fullpath, MAX_PATH, fb->cwd, info.name, NULL); + return_value_if_fail(fs_stat(os_fs(), fullpath, &st) == RET_OK, RET_FAIL); iter = file_browser_add_item(fb); return_value_if_fail(iter != NULL, RET_OOM); - iter->info = info; + iter->size = st.size; + iter->mtime = st.mtime; + iter->ctime = st.ctime; + iter->mtime = st.mtime; + iter->is_dir = info.is_dir; + iter->is_reg_file = info.is_reg_file; + iter->name = tk_str_copy(iter->name, info.name); } fs_dir_close(dir); diff --git a/src/ext_widgets/file_browser/file_browser.h b/src/ext_widgets/file_browser/file_browser.h index f5bef8c16..bebb24d4d 100644 --- a/src/ext_widgets/file_browser/file_browser.h +++ b/src/ext_widgets/file_browser/file_browser.h @@ -34,8 +34,14 @@ BEGIN_C_DECLS * 文件或目录的信息。 */ typedef struct _fb_item_t { - fs_item_t info; - fs_stat_info_t stat; + char* name; + bool_t is_dir; + bool_t is_reg_file; + + uint64_t size; + uint64_t atime; + uint64_t mtime; + uint64_t ctime; } fb_item_t; /** diff --git a/tests/file_browser_test.cc b/tests/file_browser_test.cc index a99397545..07a457cd3 100644 --- a/tests/file_browser_test.cc +++ b/tests/file_browser_test.cc @@ -14,8 +14,8 @@ TEST(FileBrowser, dir) { item = file_browser_get_item(fb, 0); ASSERT_EQ(item != NULL, true); - ASSERT_STREQ(item->info.name, "a"); - ASSERT_EQ(item->info.is_dir, TRUE); + ASSERT_STREQ(item->name, "a"); + ASSERT_EQ(item->is_dir, TRUE); ASSERT_EQ(file_browser_remove(fb, "a"), RET_OK); ASSERT_EQ(file_browser_refresh(fb), RET_OK); ASSERT_EQ(file_browser_get_items_nr(fb), 0); @@ -36,8 +36,8 @@ TEST(FileBrowser, file) { item = file_browser_get_item(fb, 0); ASSERT_EQ(item != NULL, true); - ASSERT_STREQ(item->info.name, "a"); - ASSERT_EQ(item->info.is_reg_file, TRUE); + ASSERT_STREQ(item->name, "a"); + ASSERT_EQ(item->is_reg_file, TRUE); ASSERT_EQ(file_browser_remove(fb, "a"), RET_OK); ASSERT_EQ(file_browser_refresh(fb), RET_OK); ASSERT_EQ(file_browser_get_items_nr(fb), 0); @@ -98,17 +98,17 @@ TEST(FileBrowser, filter_funcs) { fb_item_t item; memset(&item, 0x00, sizeof(item)); - strcpy(item.info.name, "test.txt"); - item.info.is_reg_file = TRUE; + strcpy(item.name, "test.txt"); + item.is_reg_file = TRUE; ASSERT_EQ(fb_filter_files_only(NULL, &item), TRUE); - item.info.is_reg_file = FALSE; + item.is_reg_file = FALSE; ASSERT_EQ(fb_filter_files_only(NULL, &item), FALSE); - item.info.is_dir = TRUE; + item.is_dir = TRUE; ASSERT_EQ(fb_filter_directories_only(NULL, &item), TRUE); - item.info.is_dir = FALSE; + item.is_dir = FALSE; ASSERT_EQ(fb_filter_directories_only(NULL, &item), FALSE); ASSERT_EQ(fb_filter_by_ext_names((void*)".txt", &item), TRUE); @@ -129,13 +129,13 @@ TEST(FileBrowser, filter) { ASSERT_EQ(file_browser_refresh(fb), RET_OK); ASSERT_EQ(file_browser_get_items_nr(fb), 1); item = file_browser_get_item(fb, 0); - ASSERT_EQ(item->info.is_reg_file, TRUE); + ASSERT_EQ(item->is_reg_file, TRUE); ASSERT_EQ(file_browser_set_filter(fb, fb_filter_directories_only, (void*)".txt"), RET_OK); ASSERT_EQ(file_browser_refresh(fb), RET_OK); ASSERT_EQ(file_browser_get_items_nr(fb), 1); item = file_browser_get_item(fb, 0); - ASSERT_EQ(item->info.is_dir, TRUE); + ASSERT_EQ(item->is_dir, TRUE); ASSERT_EQ(file_browser_set_filter(fb, NULL, NULL), RET_OK); ASSERT_EQ(file_browser_refresh(fb), RET_OK);