From 30cc6c6a29e71f7c788d5fc9f93debb1a623b91b Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Fri, 16 Aug 2013 10:36:43 +0400 Subject: [PATCH] Add dir_list::len member to keep number of items in list. Signed-off-by: Andrew Borodin --- src/filemanager/cmd.c | 12 +- src/filemanager/dir.c | 143 +++++++++--------- src/filemanager/dir.h | 20 ++- src/filemanager/file.c | 6 +- src/filemanager/find.c | 37 +++-- src/filemanager/layout.c | 1 - src/filemanager/midnight.c | 4 +- src/filemanager/panel.c | 107 +++++++------ src/filemanager/panel.h | 2 - src/filemanager/panelize.c | 110 ++++++-------- src/filemanager/usermenu.c | 3 +- src/viewer/actions_cmd.c | 16 +- src/viewer/internal.h | 2 - src/viewer/lib.c | 8 +- .../exec_get_export_variables_ext.c | 5 +- 15 files changed, 224 insertions(+), 252 deletions(-) diff --git a/src/filemanager/cmd.c b/src/filemanager/cmd.c index d8c3f1091..736e5e58d 100644 --- a/src/filemanager/cmd.c +++ b/src/filemanager/cmd.c @@ -264,7 +264,7 @@ select_unselect_cmd (const char *title, const char *history_name, gboolean do_se search->is_entire_line = TRUE; search->is_case_sensitive = case_sens != 0; - for (i = 0; i < current_panel->count; i++) + for (i = 0; i < current_panel->dir.len; i++) { if (DIR_IS_DOTDOT (current_panel->dir.list[i].fname)) continue; @@ -356,7 +356,7 @@ compare_dir (WPanel * panel, WPanel * other, enum CompareMode mode) panel->dirs_marked = 0; /* Handle all files in the panel */ - for (i = 0; i < panel->count; i++) + for (i = 0; i < panel->dir.len; i++) { file_entry *source = &panel->dir.list[i]; @@ -368,12 +368,12 @@ compare_dir (WPanel * panel, WPanel * other, enum CompareMode mode) continue; /* Search the corresponding entry from the other panel */ - for (j = 0; j < other->count; j++) + for (j = 0; j < other->dir.len; j++) { if (strcmp (source->fname, other->dir.list[j].fname) == 0) break; } - if (j >= other->count) + if (j >= other->dir.len) /* Not found -> mark */ do_file_mark (panel, i, 1); else @@ -1058,7 +1058,7 @@ select_invert_cmd (void) int i; file_entry *file; - for (i = 0; i < current_panel->count; i++) + for (i = 0; i < current_panel->dir.len; i++) { file = ¤t_panel->dir.list[i]; if (!panels_options.reverse_files_only || !S_ISDIR (file->st.st_mode)) @@ -1665,7 +1665,7 @@ dirsizes_cmd (void) ui = compute_dir_size_create_ui (FALSE); - for (i = 0; i < panel->count; i++) + for (i = 0; i < panel->dir.len; i++) if (S_ISDIR (panel->dir.list[i].st.st_mode) && ((panel->dirs_marked && panel->dir.list[i].f.marked) || !panel->dirs_marked) && !DIR_IS_DOTDOT (panel->dir.list[i].fname)) diff --git a/src/filemanager/dir.c b/src/filemanager/dir.c index 1009a1559..ea8044e5d 100644 --- a/src/filemanager/dir.c +++ b/src/filemanager/dir.c @@ -73,7 +73,7 @@ static int case_sensitive = OS_SORT_CASE_SENSITIVE_DEFAULT; /* Are the exec_bit files top in list */ static gboolean exec_first = TRUE; -static dir_list dir_copy = { 0, 0 }; +static dir_list dir_copy = { NULL, 0, 0 }; /*** file scope functions ************************************************************************/ /* --------------------------------------------------------------------------------------------- */ @@ -224,16 +224,18 @@ alloc_dir_copy (int size) { if (dir_copy.size < size) { - if (dir_copy.list) + if (dir_copy.list != NULL) { int i; - for (i = 0; i < dir_copy.size; i++) + + for (i = 0; i < dir_copy.len; i++) g_free (dir_copy.list[i].fname); g_free (dir_copy.list); } dir_copy.list = g_new0 (file_entry, size); dir_copy.size = size; + dir_copy.len = 0; } } @@ -253,6 +255,7 @@ gboolean dir_list_grow (dir_list * list, int delta) { int size; + gboolean clear = FALSE; if (list == NULL) return FALSE; @@ -262,7 +265,10 @@ dir_list_grow (dir_list * list, int delta) size = list->size + delta; if (size <= 0) + { size = MIN_FILES; + clear = TRUE; + } if (size != list->size) { @@ -276,6 +282,8 @@ dir_list_grow (dir_list * list, int delta) list->size = size; } + list->len = clear ? 0 : min (list->len, size); + return TRUE; } @@ -450,11 +458,11 @@ sort_size (file_entry * a, file_entry * b) /* --------------------------------------------------------------------------------------------- */ void -do_sort (dir_list * list, GCompareFunc sort, int top, const dir_sort_options_t * sort_op) +do_sort (dir_list * list, GCompareFunc sort, const dir_sort_options_t * sort_op) { int dot_dot_found = 0; - if (top == 0) + if (list->len < 2) return; /* If there is an ".." entry the caller must take care to @@ -465,23 +473,25 @@ do_sort (dir_list * list, GCompareFunc sort, int top, const dir_sort_options_t * reverse = sort_op->reverse ? -1 : 1; case_sensitive = sort_op->case_sensitive ? 1 : 0; exec_first = sort_op->exec_first; - qsort (&(list->list)[dot_dot_found], top + 1 - dot_dot_found, sizeof (file_entry), sort); + qsort (&(list->list)[dot_dot_found], list->len - dot_dot_found, sizeof (file_entry), sort); - clean_sort_keys (list, dot_dot_found, top + 1 - dot_dot_found); + clean_sort_keys (list, dot_dot_found, list->len - dot_dot_found); } /* --------------------------------------------------------------------------------------------- */ void -clean_dir (dir_list * list, int count) +clean_dir (dir_list * list) { int i; - for (i = 0; i < count; i++) + for (i = 0; i < list->len; i++) { g_free (list->list[i].fname); list->list[i].fname = NULL; } + + list->len = 0; } /* --------------------------------------------------------------------------------------------- */ @@ -492,7 +502,10 @@ set_zero_dir (dir_list * list) { /* Need to grow the *list? */ if (list->size == 0 && !dir_list_grow (list, RESIZE_STEPS)) + { + list->len = 0; return FALSE; + } memset (&(list->list)[0], 0, sizeof (file_entry)); list->list[0].fnamelen = 2; @@ -502,6 +515,7 @@ set_zero_dir (dir_list * list) list->list[0].f.dir_size_computed = 0; list->list[0].f.marked = 0; list->list[0].st.st_mode = 040755; + list->len = 1; return TRUE; } @@ -552,29 +566,27 @@ handle_path (const char *path, struct stat *buf1, int *link_to_dir, int *stale_l /* --------------------------------------------------------------------------------------------- */ -int +void do_load_dir (const vfs_path_t * vpath, dir_list * list, GCompareFunc sort, const dir_sort_options_t * sort_op, const char *fltr) { DIR *dirp; struct dirent *dp; int link_to_dir, stale_link; - int next_free = 0; struct stat st; /* ".." (if any) must be the first entry in the list */ if (!set_zero_dir (list)) - return next_free; + return; if (get_dotdot_dir_stat (vpath, &st)) - list->list[next_free].st = st; - next_free++; + list->list[0].st = st; dirp = mc_opendir (vpath); if (dirp == NULL) { message (D_ERROR, MSG_ERROR, _("Cannot read directory contents")); - return next_free; + return; } tree_store_start_check (vpath); @@ -585,7 +597,7 @@ do_load_dir (const vfs_path_t * vpath, dir_list * list, GCompareFunc sort, vpath_str = vfs_path_as_str (vpath); /* Do not add a ".." entry to the root directory */ if ((vpath_str[0] == PATH_SEP) && (vpath_str[1] == '\0')) - next_free--; + list->len--; } while ((dp = mc_readdir (dirp)) != NULL) @@ -593,35 +605,32 @@ do_load_dir (const vfs_path_t * vpath, dir_list * list, GCompareFunc sort, if (!handle_dirent (dp, fltr, &st, &link_to_dir, &stale_link)) continue; /* Need to grow the *list? */ - if (next_free == list->size && !dir_list_grow (list, RESIZE_STEPS)) + if (list->len == list->size && !dir_list_grow (list, RESIZE_STEPS)) goto ret; - list->list[next_free].fnamelen = strlen (dp->d_name); - list->list[next_free].fname = g_strndup (dp->d_name, list->list[next_free].fnamelen); - list->list[next_free].f.marked = 0; - list->list[next_free].f.link_to_dir = link_to_dir; - list->list[next_free].f.stale_link = stale_link; - list->list[next_free].f.dir_size_computed = 0; - list->list[next_free].st = st; - list->list[next_free].sort_key = NULL; - list->list[next_free].second_sort_key = NULL; - next_free++; + list->list[list->len].fnamelen = strlen (dp->d_name); + list->list[list->len].fname = g_strndup (dp->d_name, list->list[list->len].fnamelen); + list->list[list->len].f.marked = 0; + list->list[list->len].f.link_to_dir = link_to_dir; + list->list[list->len].f.stale_link = stale_link; + list->list[list->len].f.dir_size_computed = 0; + list->list[list->len].st = st; + list->list[list->len].sort_key = NULL; + list->list[list->len].second_sort_key = NULL; + list->len++; - if ((next_free & 31) == 0) + if ((list->len & 31) == 0) rotate_dash (TRUE); } - if (next_free != 0) - do_sort (list, sort, next_free - 1, sort_op); + do_sort (list, sort, sort_op); ret: mc_closedir (dirp); tree_store_end_check (); rotate_dash (FALSE); - return next_free; } - /* --------------------------------------------------------------------------------------------- */ gboolean @@ -637,13 +646,12 @@ if_link_is_exe (const vfs_path_t * full_name_vpath, const file_entry * file) /* --------------------------------------------------------------------------------------------- */ /** If fltr is null, then it is a match */ -int -do_reload_dir (const vfs_path_t * vpath, dir_list * list, GCompareFunc sort, int count, +void +do_reload_dir (const vfs_path_t * vpath, dir_list * list, GCompareFunc sort, const dir_sort_options_t * sort_op, const char *fltr) { DIR *dirp; struct dirent *dp; - int next_free = 0; int i, link_to_dir, stale_link; struct stat st; int marked_cnt; @@ -654,15 +662,16 @@ do_reload_dir (const vfs_path_t * vpath, dir_list * list, GCompareFunc sort, int if (dirp == NULL) { message (D_ERROR, MSG_ERROR, _("Cannot read directory contents")); - clean_dir (list, count); - return set_zero_dir (list) ? 1 : 0; + clean_dir (list); + set_zero_dir (list); + return; } tree_store_start_check (vpath); marked_files = g_hash_table_new (g_str_hash, g_str_equal); - alloc_dir_copy (list->size); - for (marked_cnt = i = 0; i < count; i++) + alloc_dir_copy (list->len); + for (marked_cnt = i = 0; i < list->len; i++) { dir_copy.list[i].fnamelen = list->list[i].fnamelen; dir_copy.list[i].fname = list->list[i].fname; @@ -688,24 +697,20 @@ do_reload_dir (const vfs_path_t * vpath, dir_list * list, GCompareFunc sort, int { if (!set_zero_dir (list)) { - clean_dir (list, count); - clean_dir (&dir_copy, count); - return next_free; + clean_dir (&dir_copy); + return; } if (get_dotdot_dir_stat (vpath, &st)) - list->list[next_free].st = st; - - next_free++; + list->list[0].st = st; } - while ((dp = mc_readdir (dirp))) + while ((dp = mc_readdir (dirp)) != NULL) { if (!handle_dirent (dp, fltr, &st, &link_to_dir, &stale_link)) continue; - /* Need to grow the *list? */ - if (next_free == list->size && !dir_list_grow (list, RESIZE_STEPS)) + if (list->len == list->size && !dir_list_grow (list, RESIZE_STEPS)) { mc_closedir (dirp); /* Norbert (Feb 12, 1997): @@ -716,52 +721,46 @@ do_reload_dir (const vfs_path_t * vpath, dir_list * list, GCompareFunc sort, int leaks and because one 'clean_dir' would not be enough (and because I don't want to spent the time to make it working, IMHO it's not worthwhile). - clean_dir (&dir_copy, count); + clean_dir (&dir_copy); */ tree_store_end_check (); g_hash_table_destroy (marked_files); - return next_free; + return; } - list->list[next_free].f.marked = 0; + list->list[list->len].f.marked = 0; /* * If we have marked files in the copy, scan through the copy * to find matching file. Decrease number of remaining marks if * we copied one. */ - if (marked_cnt > 0) + if (marked_cnt > 0 && g_hash_table_lookup (marked_files, dp->d_name) != NULL) { - if ((g_hash_table_lookup (marked_files, dp->d_name))) - { - list->list[next_free].f.marked = 1; - marked_cnt--; - } + list->list[list->len].f.marked = 1; + marked_cnt--; } - list->list[next_free].fnamelen = strlen (dp->d_name); - list->list[next_free].fname = g_strndup (dp->d_name, list->list[next_free].fnamelen); - list->list[next_free].f.link_to_dir = link_to_dir; - list->list[next_free].f.stale_link = stale_link; - list->list[next_free].f.dir_size_computed = 0; - list->list[next_free].st = st; - list->list[next_free].sort_key = NULL; - list->list[next_free].second_sort_key = NULL; - next_free++; - if ((next_free % 16) == 0) + list->list[list->len].fnamelen = strlen (dp->d_name); + list->list[list->len].fname = g_strndup (dp->d_name, list->list[list->len].fnamelen); + list->list[list->len].f.link_to_dir = link_to_dir; + list->list[list->len].f.stale_link = stale_link; + list->list[list->len].f.dir_size_computed = 0; + list->list[list->len].st = st; + list->list[list->len].sort_key = NULL; + list->list[list->len].second_sort_key = NULL; + list->len++; + if ((list->len & 15) == 0) rotate_dash (TRUE); } mc_closedir (dirp); tree_store_end_check (); g_hash_table_destroy (marked_files); - if (next_free != 0) - do_sort (list, sort, next_free - 1, sort_op); + do_sort (list, sort, sort_op); - clean_dir (&dir_copy, count); + clean_dir (&dir_copy); rotate_dash (FALSE); - - return next_free; } /* --------------------------------------------------------------------------------------------- */ diff --git a/src/filemanager/dir.h b/src/filemanager/dir.h index 0d8a41404..bb93e70e1 100644 --- a/src/filemanager/dir.h +++ b/src/filemanager/dir.h @@ -20,10 +20,14 @@ /*** structures declarations (and typedefs of structures)*****************************************/ +/** + * A structure to represent directory content + */ typedef struct { - file_entry *list; - int size; + file_entry *list; /**< list of file_entry_t objects */ + int size; /**< number of allocated elements in list (capacity) */ + int len; /**< number of used elements in list */ } dir_list; /** @@ -42,12 +46,12 @@ typedef struct dir_sort_options_struct gboolean dir_list_grow (dir_list * list, int delta); -int do_load_dir (const vfs_path_t * vpath, dir_list * list, GCompareFunc sort, - const dir_sort_options_t * sort_op, const char *fltr); -void do_sort (dir_list * list, GCompareFunc sort, int top, const dir_sort_options_t * sort_op); -int do_reload_dir (const vfs_path_t * vpath, dir_list * list, GCompareFunc sort, int count, - const dir_sort_options_t * sort_op, const char *fltr); -void clean_dir (dir_list * list, int count); +void do_load_dir (const vfs_path_t * vpath, dir_list * list, GCompareFunc sort, + const dir_sort_options_t * sort_op, const char *fltr); +void do_sort (dir_list * list, GCompareFunc sort, const dir_sort_options_t * sort_op); +void do_reload_dir (const vfs_path_t * vpath, dir_list * list, GCompareFunc sort, + const dir_sort_options_t * sort_op, const char *fltr); +void clean_dir (dir_list * list); gboolean set_zero_dir (dir_list * list); gboolean handle_path (const char *path, struct stat *buf1, int *link_to_dir, int *stale_link); diff --git a/src/filemanager/file.c b/src/filemanager/file.c index 647edf834..23e0e73d6 100644 --- a/src/filemanager/file.c +++ b/src/filemanager/file.c @@ -1214,7 +1214,7 @@ panel_get_file (WPanel * panel) { int i; - for (i = 0; i < panel->count; i++) + for (i = 0; i < panel->dir.len; i++) if (panel->dir.list[i].f.marked) return g_strdup (panel->dir.list[i].fname); } @@ -1237,7 +1237,7 @@ panel_compute_totals (const WPanel * panel, void *ui, compute_dir_size_callback { int i; - for (i = 0; i < panel->count; i++) + for (i = 0; i < panel->dir.len; i++) { struct stat *s; @@ -2930,7 +2930,7 @@ panel_operate (void *source_panel, FileOperation operation, gboolean force_singl if (panel_operate_init_totals (panel, NULL, ctx, dialog_type) == FILE_CONT) { /* Loop for every file, perform the actual copy operation */ - for (i = 0; i < panel->count; i++) + for (i = 0; i < panel->dir.len; i++) { const char *source2; diff --git a/src/filemanager/find.c b/src/filemanager/find.c index 5f5c72fc3..a5435efec 100644 --- a/src/filemanager/find.c +++ b/src/filemanager/find.c @@ -1699,15 +1699,13 @@ do_find (const char *start_dir, ssize_t start_dir_len, const char *ignore_dirs, if (return_value == B_PANELIZE && *filename) { int link_to_dir, stale_link; - int next_free = 0; int i; struct stat st; GList *entry; dir_list *list = ¤t_panel->dir; char *name = NULL; - if (set_zero_dir (list)) - next_free++; + set_zero_dir (list); for (i = 0, entry = find_list->list; entry != NULL; i++, entry = g_list_next (entry)) { @@ -1740,40 +1738,39 @@ do_find (const char *start_dir, ssize_t start_dir_len, const char *ignore_dirs, continue; } /* Need to grow the *list? */ - if (next_free == list->size && !dir_list_grow (list, RESIZE_STEPS)) + if (list->len == list->size && !dir_list_grow (list, RESIZE_STEPS)) { g_free (name); break; } /* don't add files more than once to the panel */ - if (content_pattern != NULL && next_free > 0 - && strcmp (list->list[next_free - 1].fname, p) == 0) + if (content_pattern != NULL && list->len != 0 + && strcmp (list->list[list->len - 1].fname, p) == 0) { g_free (name); continue; } - if (next_free == 0) /* first turn i.e clean old list */ + if (list->len == 0) /* first turn i.e clean old list */ panel_clean_dir (current_panel); - list->list[next_free].fnamelen = strlen (p); - list->list[next_free].fname = g_strndup (p, list->list[next_free].fnamelen); - list->list[next_free].f.marked = 0; - list->list[next_free].f.link_to_dir = link_to_dir; - list->list[next_free].f.stale_link = stale_link; - list->list[next_free].f.dir_size_computed = 0; - list->list[next_free].st = st; - list->list[next_free].sort_key = NULL; - list->list[next_free].second_sort_key = NULL; - next_free++; + list->list[list->len].fnamelen = strlen (p); + list->list[list->len].fname = g_strndup (p, list->list[list->len].fnamelen); + list->list[list->len].f.marked = 0; + list->list[list->len].f.link_to_dir = link_to_dir; + list->list[list->len].f.stale_link = stale_link; + list->list[list->len].f.dir_size_computed = 0; + list->list[list->len].st = st; + list->list[list->len].sort_key = NULL; + list->list[list->len].second_sort_key = NULL; + list->len++; g_free (name); - if ((next_free & 15) == 0) + if ((list->len & 15) == 0) rotate_dash (TRUE); } - if (next_free) + if (list->len != 0) { - current_panel->count = next_free; current_panel->is_panelized = TRUE; /* absolute path */ diff --git a/src/filemanager/layout.c b/src/filemanager/layout.c index 2ce05aa36..70cc03e97 100644 --- a/src/filemanager/layout.c +++ b/src/filemanager/layout.c @@ -1112,7 +1112,6 @@ swap_panels (void) panelswap (active); panelswap (cwd_vpath); panelswap (lwd_vpath); - panelswap (count); panelswap (marked); panelswap (dirs_marked); panelswap (total); diff --git a/src/filemanager/midnight.c b/src/filemanager/midnight.c index eb1e745db..6ef8ee80f 100644 --- a/src/filemanager/midnight.c +++ b/src/filemanager/midnight.c @@ -821,7 +821,7 @@ put_tagged (WPanel * panel) input_disable_update (cmdline); if (panel->marked) { - for (i = 0; i < panel->count; i++) + for (i = 0; i < panel->dir.len; i++) { if (panel->dir.list[i].f.marked) command_insert (cmdline, panel->dir.list[i].fname, TRUE); @@ -1783,7 +1783,7 @@ do_nc (void) /* don't handle VFS timestamps for dirs opened in panels */ mc_event_destroy (MCEVENT_GROUP_CORE, "vfs_timestamp"); - clean_dir (&panelized_panel.list, panelized_panel.count); + clean_dir (&panelized_panel.list); } /* Program end */ diff --git a/src/filemanager/panel.c b/src/filemanager/panel.c index 8f650aa1d..0d8a34109 100644 --- a/src/filemanager/panel.c +++ b/src/filemanager/panel.c @@ -86,7 +86,7 @@ hook_t *select_file_hook = NULL; /* *INDENT-OFF* */ -panelized_panel_t panelized_panel = { {NULL, 0}, -1, NULL }; +panelized_panel_t panelized_panel = { {NULL, 0, -1}, NULL }; /* *INDENT-ON* */ static const char *string_file_name (file_entry *, int); @@ -773,7 +773,7 @@ format_file (char *dest, int limit, WPanel * panel, int file_index, int width, i (void) dest; (void) limit; length = 0; - empty_line = (file_index >= panel->count); + empty_line = (file_index >= panel->dir.len); home = (isstatus) ? panel->status_format : panel->format; fe = &panel->dir.list[file_index]; *field_lenght = 0; @@ -1032,7 +1032,7 @@ paint_dir (WPanel * panel) panel->max_shift = -1; for (i = 0; i < items; i++) { - if (i + panel->top_file >= panel->count) + if (i + panel->top_file >= panel->dir.len) color = 0; else { @@ -1342,7 +1342,7 @@ adjust_top_file (WPanel * panel) { int items = ITEMS (panel); - if (panel->count <= items) + if (panel->dir.len <= items) { /* If all files fit, show them all. */ panel->top_file = 0; @@ -1364,7 +1364,7 @@ adjust_top_file (WPanel * panel) if (panel->top_file < i) panel->top_file = i; - i = panel->count - items; + i = panel->dir.len - items; if (panel->top_file > i) panel->top_file = i; @@ -2006,7 +2006,7 @@ unselect_item (WPanel * panel) static void move_down (WPanel * panel) { - if (panel->selected + 1 == panel->count) + if (panel->selected + 1 == panel->dir.len) return; unselect_item (panel); @@ -2015,8 +2015,8 @@ move_down (WPanel * panel) { /* Scroll window half screen */ panel->top_file += ITEMS (panel) / 2; - if (panel->top_file > panel->count - ITEMS (panel)) - panel->top_file = panel->count - ITEMS (panel); + if (panel->top_file > panel->dir.len - ITEMS (panel)) + panel->top_file = panel->dir.len - ITEMS (panel); paint_dir (panel); } select_item (panel); @@ -2053,8 +2053,8 @@ move_selection (WPanel * panel, int lines) int adjust = 0; new_pos = panel->selected + lines; - if (new_pos >= panel->count) - new_pos = panel->count - 1; + if (new_pos >= panel->dir.len) + new_pos = panel->dir.len - 1; if (new_pos < 0) new_pos = 0; @@ -2190,16 +2190,16 @@ next_page (WPanel * panel) { int items; - if (panel->selected == panel->count - 1) + if (panel->selected == panel->dir.len - 1) return; unselect_item (panel); items = ITEMS (panel); - if (panel->top_file > panel->count - 2 * items) - items = panel->count - items - panel->top_file; + if (panel->top_file > panel->dir.len - 2 * items) + items = panel->dir.len - items - panel->top_file; if (panel->top_file + items < 0) items = -panel->top_file; if (!items) - panel->selected = panel->count - 1; + panel->selected = panel->dir.len - 1; else panel->selected += items; panel->top_file += items; @@ -2291,7 +2291,7 @@ move_home (WPanel * panel) static void move_end (WPanel * panel) { - if (panel->selected == panel->count - 1) + if (panel->selected == panel->dir.len - 1) return; unselect_item (panel); @@ -2312,7 +2312,7 @@ move_end (WPanel * panel) } } - panel->selected = panel->count - 1; + panel->selected = panel->dir.len - 1; paint_dir (panel); select_item (panel); } @@ -2363,7 +2363,7 @@ mark_file_right (WPanel * panel) if (state_mark < 0) state_mark = selection (panel)->f.marked ? 0 : 1; - lines = min (lines, panel->count - panel->selected - 1); + lines = min (lines, panel->dir.len - panel->selected - 1); for (; lines != 0; lines--) { do_file_mark (panel, panel->selected, state_mark); @@ -2468,7 +2468,7 @@ do_search (WPanel * panel, int c_code) sel = panel->selected; for (i = panel->selected; !wrapped || i != panel->selected; i++) { - if (i >= panel->count) + if (i >= panel->dir.len) { i = 0; if (wrapped) @@ -2510,7 +2510,7 @@ start_search (WPanel * panel) { if (panel->searching) { - if (panel->selected + 1 == panel->count) + if (panel->selected + 1 == panel->dir.len) panel->selected = 0; else move_down (panel); @@ -3063,9 +3063,8 @@ _do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_ /* Reload current panel */ panel_clean_dir (panel); - panel->count = - do_load_dir (panel->cwd_vpath, &panel->dir, panel->sort_field->sort_routine, - &panel->sort_info, panel->filter); + do_load_dir (panel->cwd_vpath, &panel->dir, panel->sort_field->sort_routine, &panel->sort_info, + panel->filter); try_to_select (panel, get_parent_dir_name (panel->cwd_vpath, olddir_vpath)); load_hint (0); @@ -3654,7 +3653,7 @@ panel_event (Gpm_Event * event, void *data) { if (is_active) { - if (panels_options.mouse_move_pages && (panel->top_file + ITEMS (panel) < panel->count)) + if (panels_options.mouse_move_pages && (panel->top_file + ITEMS (panel) < panel->dir.len)) next_page (panel); else /* We are in last page */ move_down (panel); @@ -3670,16 +3669,16 @@ panel_event (Gpm_Event * event, void *data) if (!is_active) change_panel (); - if (panel->top_file + local.y > panel->count) - my_index = panel->count - 1; + if (panel->top_file + local.y > panel->dir.len) + my_index = panel->dir.len - 1; else { my_index = panel->top_file + local.y - 1; if (panel->split && (local.x > (w->cols - 2) / 2)) my_index += llines (panel); - if (my_index >= panel->count) - my_index = panel->count - 1; + if (my_index >= panel->dir.len) + my_index = panel->dir.len - 1; } if (my_index != panel->selected) @@ -3714,7 +3713,7 @@ reload_panelized (WPanel * panel) if (panel != current_panel) (void) mc_chdir (panel->cwd_vpath); - for (i = 0, j = 0; i < panel->count; i++) + for (i = 0, j = 0; i < panel->dir.len; i++) { vfs_path_t *vpath; @@ -3729,7 +3728,7 @@ reload_panelized (WPanel * panel) do_file_mark (panel, i, 0); } vpath = vfs_path_from_str (list->list[i].fname); - if (mc_lstat (vpath, &list->list[i].st)) + if (mc_lstat (vpath, &list->list[i].st) != 0) g_free (list->list[i].fname); else { @@ -3742,9 +3741,9 @@ reload_panelized (WPanel * panel) vfs_path_free (vpath); } if (j == 0) - panel->count = set_zero_dir (list) ? 1 : 0; + set_zero_dir (list); else - panel->count = j; + list->len = j; if (panel != current_panel) (void) mc_chdir (current_panel->cwd_vpath); @@ -3841,7 +3840,7 @@ do_try_to_select (WPanel * panel, const char *name) subdir = vfs_strip_suffix_from_filename (x_basename (name)); /* Search that subdir or filename without prefix (if not panelized panel), select it if found */ - for (i = 0; i < panel->count; i++) + for (i = 0; i < panel->dir.len; i++) { if (strcmp (subdir, panel->dir.list[i].fname) == 0) { @@ -3852,8 +3851,8 @@ do_try_to_select (WPanel * panel, const char *name) } /* Try to select a file near the file that is missing */ - if (panel->selected >= panel->count) - do_select (panel, panel->count - 1); + if (panel->selected >= panel->dir.len) + do_select (panel, panel->dir.len - 1); g_free (subdir); } @@ -3895,7 +3894,7 @@ panel_save_current_file_to_clip_file (const gchar * event_group_name, const gcha gboolean first = TRUE; char *flist = NULL; - for (i = 0; i < current_panel->count; i++) + for (i = 0; i < current_panel->dir.len; i++) if (current_panel->dir.list[i].f.marked != 0) { /* Skip the unmarked ones */ if (first) @@ -3965,9 +3964,6 @@ try_to_select (WPanel * panel, const char *name) void panel_clean_dir (WPanel * panel) { - int count = panel->count; - - panel->count = 0; panel->top_file = 0; panel->selected = 0; panel->marked = 0; @@ -3979,7 +3975,7 @@ panel_clean_dir (WPanel * panel) panel->content_shift = -1; panel->max_shift = -1; - clean_dir (&panel->dir, count); + clean_dir (&panel->dir); } /* --------------------------------------------------------------------------------------------- */ @@ -4073,8 +4069,9 @@ panel_new_with_dir (const char *panel_name, const vfs_path_t * vpath) panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL); /* directories history will be get later */ - panel->dir.list = g_new (file_entry, MIN_FILES); panel->dir.size = MIN_FILES; + panel->dir.list = g_new (file_entry, panel->dir.size); + panel->dir.len = 0; panel->active = 0; panel->filter = 0; panel->split = 0; @@ -4141,9 +4138,8 @@ panel_new_with_dir (const char *panel_name, const vfs_path_t * vpath) } /* Load the default format */ - panel->count = - do_load_dir (panel->cwd_vpath, &panel->dir, panel->sort_field->sort_routine, - &panel->sort_info, panel->filter); + do_load_dir (panel->cwd_vpath, &panel->dir, panel->sort_field->sort_routine, &panel->sort_info, + panel->filter); /* Restore old right path */ if (curdir != NULL) @@ -4179,7 +4175,7 @@ panel_reload (WPanel * panel) { panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR); panel_clean_dir (panel); - panel->count = set_zero_dir (&panel->dir) ? 1 : 0; + set_zero_dir (&panel->dir); return; } @@ -4187,13 +4183,12 @@ panel_reload (WPanel * panel) memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat)); show_dir (panel); - panel->count = - do_reload_dir (panel->cwd_vpath, &panel->dir, panel->sort_field->sort_routine, - panel->count, &panel->sort_info, panel->filter); + do_reload_dir (panel->cwd_vpath, &panel->dir, panel->sort_field->sort_routine, &panel->sort_info, + panel->filter); panel->dirty = 1; - if (panel->selected >= panel->count) - do_select (panel, panel->count - 1); + if (panel->selected >= panel->dir.len) + do_select (panel, panel->dir.len - 1); recalculate_panel_summary (panel); } @@ -4311,8 +4306,8 @@ select_item (WPanel * panel) if (panel->selected < 0) panel->selected = 0; - if (panel->selected > panel->count - 1) - panel->selected = panel->count - 1; + if (panel->selected > panel->dir.len - 1) + panel->selected = panel->dir.len - 1; adjust_top_file (panel); @@ -4330,7 +4325,7 @@ unmark_files (WPanel * panel) if (!panel->marked) return; - for (i = 0; i < panel->count; i++) + for (i = 0; i < panel->dir.len; i++) file_mark (panel, i, 0); panel->dirs_marked = 0; @@ -4351,7 +4346,7 @@ recalculate_panel_summary (WPanel * panel) panel->dirs_marked = 0; panel->total = 0; - for (i = 0; i < panel->count; i++) + for (i = 0; i < panel->dir.len; i++) if (panel->dir.list[i].f.marked) { /* do_file_mark will return immediately if newmark == oldmark. @@ -4444,11 +4439,11 @@ panel_re_sort (WPanel * panel) filename = g_strdup (selection (panel)->fname); unselect_item (panel); - do_sort (&panel->dir, panel->sort_field->sort_routine, panel->count - 1, &panel->sort_info); + do_sort (&panel->dir, panel->sort_field->sort_routine, &panel->sort_info); panel->selected = -1; - for (i = panel->count; i; i--) + for (i = panel->dir.len; i != 0; i--) { - if (!strcmp (panel->dir.list[i - 1].fname, filename)) + if (strcmp (panel->dir.list[i - 1].fname, filename) == 0) { panel->selected = i - 1; break; diff --git a/src/filemanager/panel.h b/src/filemanager/panel.h index f218d27a2..355639d91 100644 --- a/src/filemanager/panel.h +++ b/src/filemanager/panel.h @@ -76,7 +76,6 @@ typedef struct panel_field_struct typedef struct { dir_list list; - int count; vfs_path_t *root_vpath; } panelized_panel_t; @@ -92,7 +91,6 @@ typedef struct WPanel GList *dir_history; /* directory history */ GList *dir_history_current; /* pointer to the current history item */ char *hist_name; /* directory history name for history file */ - int count; /* Number of files in dir structure */ int marked; /* Count of marked files */ int dirs_marked; /* Count of marked directories */ uintmax_t total; /* Bytes in marked files */ diff --git a/src/filemanager/panelize.c b/src/filemanager/panelize.c index 22415233f..f5320c0ab 100644 --- a/src/filemanager/panelize.c +++ b/src/filemanager/panelize.c @@ -312,7 +312,6 @@ static void do_external_panelize (char *command) { int link_to_dir, stale_link; - int next_free = 0; struct stat st; dir_list *list = ¤t_panel->dir; char line[MC_MAXPATHLEN]; @@ -331,10 +330,9 @@ do_external_panelize (char *command) panelize_change_root (current_panel->cwd_vpath); - if (set_zero_dir (list)) - next_free++; + set_zero_dir (list); - while (1) + while (TRUE) { clearerr (external); if (fgets (line, MC_MAXPATHLEN, external) == NULL) @@ -356,43 +354,40 @@ do_external_panelize (char *command) if (!handle_path (name, &st, &link_to_dir, &stale_link)) continue; /* Need to grow the *list? */ - if (next_free == list->size && !dir_list_grow (list, RESIZE_STEPS)) + if (list->len == list->size && !dir_list_grow (list, RESIZE_STEPS)) break; - list->list[next_free].fnamelen = strlen (name); - list->list[next_free].fname = g_strndup (name, list->list[next_free].fnamelen); - file_mark (current_panel, next_free, 0); - list->list[next_free].f.link_to_dir = link_to_dir; - list->list[next_free].f.stale_link = stale_link; - list->list[next_free].f.dir_size_computed = 0; - list->list[next_free].st = st; - list->list[next_free].sort_key = NULL; - list->list[next_free].second_sort_key = NULL; - next_free++; - if ((next_free & 32) == 0) + list->list[list->len].fnamelen = strlen (name); + list->list[list->len].fname = g_strndup (name, list->list[list->len].fnamelen); + list->list[list->len].f.link_to_dir = link_to_dir; + list->list[list->len].f.stale_link = stale_link; + list->list[list->len].f.dir_size_computed = 0; + list->list[list->len].st = st; + list->list[list->len].sort_key = NULL; + list->list[list->len].second_sort_key = NULL; + file_mark (current_panel, list->len, 0); + list->len++; + if ((list->len & 31) == 0) rotate_dash (TRUE); } current_panel->is_panelized = TRUE; - if (next_free) - { - current_panel->count = next_free; - if (list->list[0].fname[0] == PATH_SEP) - { - vfs_path_t *vpath_root; - int ret; - vpath_root = vfs_path_from_str (PATH_SEP_STR); - panel_set_cwd (current_panel, vpath_root); - ret = mc_chdir (vpath_root); - vfs_path_free (vpath_root); - (void) ret; - } - } - else + if (list->len == 0) + set_zero_dir (list); + else if (list->list[0].fname[0] == PATH_SEP) { - current_panel->count = set_zero_dir (list) ? 1 : 0; + vfs_path_t *vpath_root; + int ret; + + vpath_root = vfs_path_from_str (PATH_SEP_STR); + panel_set_cwd (current_panel, vpath_root); + ret = mc_chdir (vpath_root); + vfs_path_free (vpath_root); + + (void) ret; } + if (pclose (external) < 0) message (D_NORMAL, _("External panelize"), _("Pipe close failed")); close_error_pipe (D_NORMAL, NULL); @@ -407,29 +402,25 @@ static void do_panelize_cd (struct WPanel *panel) { int i; - dir_list *list = &panel->dir; + dir_list *list; gboolean panelized_same; - clean_dir (list, panel->count); + clean_dir (&panel->dir); if (panelized_panel.root_vpath == NULL) panelize_change_root (current_panel->cwd_vpath); - if (panelized_panel.count < 1) - { - if (set_zero_dir (&panelized_panel.list)) - panelized_panel.count = 1; - } - else if (panelized_panel.count >= list->size) - { - list->list = g_try_realloc (list->list, sizeof (file_entry) * panelized_panel.count); - list->size = panelized_panel.count; - } - panel->count = panelized_panel.count; + if (panelized_panel.list.len < 1) + set_zero_dir (&panelized_panel.list); + else if (panelized_panel.list.len > panel->dir.size) + dir_list_grow (&panel->dir, panelized_panel.list.len - panel->dir.size); + + list = &panel->dir; + list->len = panelized_panel.list.len; panel->is_panelized = TRUE; - panelized_same = (vfs_path_equal (panelized_panel.root_vpath, panel->cwd_vpath)); + panelized_same = vfs_path_equal (panelized_panel.root_vpath, panel->cwd_vpath); - for (i = 0; i < panelized_panel.count; i++) + for (i = 0; i < panelized_panel.list.len; i++) { if (panelized_same || DIR_IS_DOTDOT (panelized_panel.list.list[i].fname)) { @@ -440,13 +431,15 @@ do_panelize_cd (struct WPanel *panel) else { vfs_path_t *tmp_vpath; + const char *tmp_path; tmp_vpath = vfs_path_append_new (panelized_panel.root_vpath, panelized_panel.list.list[i].fname, NULL); - list->list[i].fname = g_strdup (vfs_path_as_str (tmp_vpath)); + tmp_path = vfs_path_as_str (tmp_vpath); + list->list[i].fnamelen = strlen (tmp_path); + list->list[i].fname = g_strndup (tmp_path, list->list[i].fnamelen); vfs_path_free (tmp_vpath); - list->list[i].fnamelen = strlen (list->list[i].fname); } list->list[i].f.link_to_dir = panelized_panel.list.list[i].f.link_to_dir; list->list[i].f.stale_link = panelized_panel.list.list[i].f.stale_link; @@ -484,19 +477,16 @@ panelize_save_panel (struct WPanel *panel) panelize_change_root (current_panel->cwd_vpath); - if (panelized_panel.count > 0) - clean_dir (&panelized_panel.list, panelized_panel.count); - if (panel->count < 1) + if (panelized_panel.list.len > 0) + clean_dir (&panelized_panel.list); + if (panel->dir.len == 0) return; - panelized_panel.count = panel->count; - if (panel->count >= panelized_panel.list.size) - { - panelized_panel.list.list = g_try_realloc (panelized_panel.list.list, - sizeof (file_entry) * panel->count); - panelized_panel.list.size = panel->count; - } - for (i = 0; i < panel->count; i++) + if (panel->dir.len > panelized_panel.list.size) + dir_list_grow (&panelized_panel.list, panel->dir.len - panelized_panel.list.size); + panelized_panel.list.len = panel->dir.len; + + for (i = 0; i < panel->dir.len; i++) { panelized_panel.list.list[i].fnamelen = list->list[i].fnamelen; panelized_panel.list.list[i].fname = diff --git a/src/filemanager/usermenu.c b/src/filemanager/usermenu.c index 2c1a8b454..867d51868 100644 --- a/src/filemanager/usermenu.c +++ b/src/filemanager/usermenu.c @@ -7,6 +7,7 @@ Written by: Slava Zanko , 2013 + Andrew Borodin , 2013 This file is part of the Midnight Commander. @@ -883,7 +884,7 @@ expand_format (struct WEdit *edit_widget, char c, gboolean do_quote) block = g_string_sized_new (16); - for (i = 0; i < panel->count; i++) + for (i = 0; i < panel->dir.len; i++) if (panel->dir.list[i].f.marked) { char *tmp; diff --git a/src/viewer/actions_cmd.c b/src/viewer/actions_cmd.c index ad6d2432b..beefd36e4 100644 --- a/src/viewer/actions_cmd.c +++ b/src/viewer/actions_cmd.c @@ -286,7 +286,6 @@ mcview_load_next_prev_init (mcview_t * view) { /* get file list from current panel. Update it each time */ view->dir = ¤t_panel->dir; - view->dir_count = ¤t_panel->count; view->dir_idx = ¤t_panel->selected; } else if (view->dir == NULL) @@ -303,17 +302,15 @@ mcview_load_next_prev_init (mcview_t * view) /* load directory where requested file is */ view->dir = g_new0 (dir_list, 1); - view->dir_count = g_new (int, 1); view->dir_idx = g_new (int, 1); - *view->dir_count = do_load_dir (view->workdir_vpath, view->dir, (GCompareFunc) sort_name, - &sort_op, NULL); + do_load_dir (view->workdir_vpath, view->dir, (GCompareFunc) sort_name, &sort_op, NULL); fname = x_basename (vfs_path_as_str (view->filename_vpath)); fname_len = strlen (fname); /* search current file in the list */ - for (i = 0; i != *view->dir_count; i++) + for (i = 0; i != view->dir->len; i++) { const file_entry *fe = &view->dir->list[i]; @@ -335,8 +332,8 @@ mcview_scan_for_file (mcview_t * view, int direction) for (i = *view->dir_idx + direction; i != *view->dir_idx; i += direction) { if (i < 0) - i = *view->dir_count - 1; - if (i == *view->dir_count) + i = view->dir->len - 1; + if (i == view->dir->len) i = 0; if (!S_ISDIR (view->dir->list[i].st.st_mode)) break; @@ -351,7 +348,7 @@ static void mcview_load_next_prev (mcview_t * view, int direction) { dir_list *dir; - int *dir_count, *dir_idx; + int *dir_idx; vfs_path_t *vfile; vfs_path_t *ext_script = NULL; @@ -360,10 +357,8 @@ mcview_load_next_prev (mcview_t * view, int direction) /* reinit view */ dir = view->dir; - dir_count = view->dir_count; dir_idx = view->dir_idx; view->dir = NULL; - view->dir_count = NULL; view->dir_idx = NULL; vfile = vfs_path_append_new (view->workdir_vpath, dir->list[*dir_idx].fname, (char *) NULL); mcview_done (view); @@ -373,7 +368,6 @@ mcview_load_next_prev (mcview_t * view, int direction) mcview_load (view, NULL, vfs_path_as_str (vfile), 0); vfs_path_free (vfile); view->dir = dir; - view->dir_count = dir_count; view->dir_idx = dir_idx; view->ext_script = ext_script; diff --git a/src/viewer/internal.h b/src/viewer/internal.h index bb536bbbf..ec0b39a73 100644 --- a/src/viewer/internal.h +++ b/src/viewer/internal.h @@ -184,8 +184,6 @@ struct mcview_struct dir_list *dir; /* List of current directory files * to handle CK_FileNext and CK_FilePrev commands */ - int *dir_count; /* Number of files in dir structure. - * Pointer is used here as reference to WPanel::count */ int *dir_idx; /* Index of current file in dir structure. * Pointer is used here as reference to WPanel::count */ vfs_path_t *ext_script; /* Temporary script file created by regex_command_for() */ diff --git a/src/viewer/lib.c b/src/viewer/lib.c index b0edcc223..1b6238812 100644 --- a/src/viewer/lib.c +++ b/src/viewer/lib.c @@ -78,7 +78,7 @@ mcview_toggle_magic_mode (mcview_t * view) { char *command; dir_list *dir; - int *dir_count, *dir_idx; + int *dir_idx; mcview_altered_magic_flag = 1; view->magic_mode = !view->magic_mode; @@ -86,16 +86,13 @@ mcview_toggle_magic_mode (mcview_t * view) /* reinit view */ command = g_strdup (view->command); dir = view->dir; - dir_count = view->dir_count; dir_idx = view->dir_idx; view->dir = NULL; - view->dir_count = NULL; view->dir_idx = NULL; mcview_done (view); mcview_init (view); mcview_load (view, command, vfs_path_as_str (view->filename_vpath), 0); view->dir = dir; - view->dir_count = dir_count; view->dir_idx = dir_idx; g_free (command); @@ -253,9 +250,8 @@ mcview_done (mcview_t * view) if (mc_global.mc_run_mode == MC_RUN_VIEWER && view->dir != NULL) { /* mcviewer is the owner of file list */ - clean_dir (view->dir, *view->dir_count); + clean_dir (view->dir); g_free (view->dir->list); - g_free (view->dir_count); g_free (view->dir_idx); g_free (view->dir); } diff --git a/tests/src/filemanager/exec_get_export_variables_ext.c b/tests/src/filemanager/exec_get_export_variables_ext.c index 086c7fa3b..a98e45b58 100644 --- a/tests/src/filemanager/exec_get_export_variables_ext.c +++ b/tests/src/filemanager/exec_get_export_variables_ext.c @@ -51,8 +51,9 @@ setup (void) mc_global.mc_run_mode = MC_RUN_FULL; current_panel = g_new0 (struct WPanel, 1); current_panel->cwd_vpath = vfs_path_from_str ("/home"); - current_panel->dir.list = g_new0 (file_entry, MIN_FILES); current_panel->dir.size = MIN_FILES; + current_panel->dir.list = g_new0 (file_entry, current_panel->dir.size); + current_panel->dir.len = 0; } static void @@ -74,12 +75,12 @@ START_TEST (sanitize_variables) const char *expected_string; current_panel->selected = 0; + current_panel->dir.len = 3; current_panel->dir.list[0].fname = (char *) "selected file.txt"; current_panel->dir.list[1].fname = (char *) "tagged file1.txt"; current_panel->dir.list[1].f.marked = TRUE; current_panel->dir.list[2].fname = (char *) "tagged file2.txt"; current_panel->dir.list[2].f.marked = TRUE; - current_panel->count = 3; /* when */ filename_vpath = vfs_path_from_str ("/tmp/blabla.txt");