From 6a3943fcf010dbdef2e58c7e92ffaafdabc71d37 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Sat, 17 Aug 2013 16:50:11 +0400 Subject: [PATCH] (dir_list_grow): new public API of dir_list. Signed-off-by: Andrew Borodin --- src/filemanager/dir.c | 70 ++++++++++++++++++++++++++----------------- src/filemanager/dir.h | 2 ++ 2 files changed, 45 insertions(+), 27 deletions(-) diff --git a/src/filemanager/dir.c b/src/filemanager/dir.c index f7d61f185..2e7abe5ac 100644 --- a/src/filemanager/dir.c +++ b/src/filemanager/dir.c @@ -137,30 +137,6 @@ clean_sort_keys (dir_list * list, int start, int count) } } -/* --------------------------------------------------------------------------------------------- */ -/** - * Increase directory list by RESIZE_STEPS - * - * @param list directory list - * @return FALSE on failure, TRUE on success - */ - -static gboolean -grow_list (dir_list * list) -{ - if (list == NULL) - return FALSE; - - list->list = g_try_realloc (list->list, sizeof (file_entry) * (list->size + RESIZE_STEPS)); - - if (list->list == NULL) - return FALSE; - - list->size += RESIZE_STEPS; - - return TRUE; -} - /* --------------------------------------------------------------------------------------------- */ /** * If you change handle_dirent then check also handle_path. @@ -211,7 +187,7 @@ handle_dirent (dir_list * list, const char *fltr, struct dirent *dp, return 0; /* Need to grow the *list? */ - if (next_free == list->size && !grow_list (list)) + if (next_free == list->size && !dir_list_grow (list, RESIZE_STEPS)) return -1; return 1; @@ -267,6 +243,46 @@ alloc_dir_copy (int size) /* --------------------------------------------------------------------------------------------- */ /*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ +/** + * Increase or decrease directory list size. + * + * @param list directory list + * @param delta value by increase (if positive) or decrease (if negative) list size + * + * @return FALSE on failure, TRUE on success + */ + +gboolean +dir_list_grow (dir_list * list, int delta) +{ + int size; + + if (list == NULL) + return FALSE; + + if (delta == 0) + return TRUE; + + size = list->size + delta; + if (size <= 0) + size = MIN_FILES; + + if (size != list->size) + { + file_entry *fe; + + fe = g_try_renew (file_entry, list->list, size); + if (fe == NULL) + return FALSE; + + list->list = fe; + list->size = size; + } + + return TRUE; +} + /* --------------------------------------------------------------------------------------------- */ int @@ -479,7 +495,7 @@ gboolean set_zero_dir (dir_list * list) { /* Need to grow the *list? */ - if (list->size == 0 && !grow_list (list)) + if (list->size == 0 && !dir_list_grow (list, RESIZE_STEPS)) return FALSE; memset (&(list->list)[0], 0, sizeof (file_entry)); @@ -536,7 +552,7 @@ handle_path (dir_list * list, const char *path, vfs_path_free (vpath); /* Need to grow the *list? */ - if (next_free == list->size && !grow_list (list)) + if (next_free == list->size && !dir_list_grow (list, RESIZE_STEPS)) return -1; return 1; diff --git a/src/filemanager/dir.h b/src/filemanager/dir.h index fe721fa96..4c3802b08 100644 --- a/src/filemanager/dir.h +++ b/src/filemanager/dir.h @@ -40,6 +40,8 @@ typedef struct dir_sort_options_struct /*** declarations of public functions ************************************************************/ +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);