static void _rtgui_filelist_view_destructor(struct rtgui_filelist_view *view) { /* delete all file items */ rtgui_filelist_view_clear(view); /* delete current directory and pattern */ if (view->current_directory != RT_NULL) { rt_free(view->current_directory); view->current_directory = RT_NULL; } if (view->pattern != RT_NULL) { rt_free(view->pattern); view->pattern = RT_NULL; } _instance_count--; RT_ASSERT(_instance_count >= 0); if (_instance_count == 0) { /* delete image */ rtgui_image_destroy(file_image); file_image = RT_NULL; rtgui_image_destroy(folder_image); folder_image = RT_NULL; } }
static void _rtgui_filelist_view_destructor(struct rtgui_filelist_view *view) { /* delete all file items */ rtgui_filelist_view_clear(view); /* delete current directory and pattern */ if (view->current_directory != RT_NULL) { rt_free(view->current_directory); view->current_directory = RT_NULL; } if (view->pattern != RT_NULL) { rt_free(view->pattern); view->pattern = RT_NULL; } /* delete image */ rtgui_image_destroy(file_image); rtgui_image_destroy(folder_image); }
void rtgui_filelist_view_set_directory(rtgui_filelist_view_t *view, const char *directory) { struct rtgui_file_item *item; RT_ASSERT(view != RT_NULL); /* clear file items firstly */ rtgui_filelist_view_clear(view); if (directory != RT_NULL) { DIR *dir; struct stat s; char *fullpath; rt_uint32_t index; struct dirent *dirent; view->items_count = 0; dir = opendir(directory); if (dir == RT_NULL) goto __return; /* current directory exists, set it */ if (view->current_directory != RT_NULL) rt_free(view->current_directory); view->current_directory = rt_strdup(directory); do { dirent = readdir(dir); if (dirent == RT_NULL) break; if (strcmp(dirent->d_name, ".") == 0) continue; /* readdir is not guaranteed to return "..". So we should deal with * it specially. */ if (strcmp(dirent->d_name, "..") == 0) continue; view->items_count++; } while (dirent != RT_NULL); closedir(dir); /* Add ".." entry. */ if (!(directory[0] == '/' && directory[1] == '\0')) view->items_count++; view->items = (struct rtgui_file_item *)rtgui_malloc(sizeof(struct rtgui_file_item) * view->items_count); if (view->items == RT_NULL) return; /* no memory */ index = 0; if (!(directory[0] == '/' && directory[1] == '\0')) { item = &(view->items[index]); item->name = rt_strdup(".."); item->type = RTGUI_FITEM_DIR; item->size = 0; index++; } /* reopen directory */ dir = opendir(directory); fullpath = (char*)rtgui_malloc(256); while (index < view->items_count) { dirent = readdir(dir); if (dirent == RT_NULL) break; if (strcmp(dirent->d_name, ".") == 0) continue; if (strcmp(dirent->d_name, "..") == 0) continue; item = &(view->items[index]); item->name = rt_strdup(dirent->d_name); rt_memset(&s, 0, sizeof(struct stat)); /* build full path for the file */ if (directory[strlen(directory) - 1] != PATH_SEPARATOR) rt_snprintf(fullpath, 256, "%s%c%s", directory, PATH_SEPARATOR, dirent->d_name); else rt_snprintf(fullpath, 256, "%s%s", directory, dirent->d_name); stat(fullpath, &s); if (s.st_mode & S_IFDIR) { item->type = RTGUI_FITEM_DIR; item->size = 0; } else { item->type = RTGUI_FITEM_FILE; item->size = s.st_size; } index ++; } rtgui_free(fullpath); closedir(dir); } view->current_item = 0; __return: /* update view */ rtgui_widget_update(RTGUI_WIDGET(view)); }