From 994c432dd51217b4c5ef23680344e23df6c939aa Mon Sep 17 00:00:00 2001 From: "Yury V. Zaytsev" Date: Sun, 28 Oct 2012 12:05:27 +0100 Subject: [PATCH] Ticket #2888: code cleanup before 4.8.7 release. src/filemanager/dir.c: refactored growing of dir_list into a separate function. Signed-off-by: Yury V. Zaytsev Signed-off-by: Slava Zanko --- src/filemanager/dir.c | 58 ++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/src/filemanager/dir.c b/src/filemanager/dir.c index afc63d329..93c3029d8 100644 --- a/src/filemanager/dir.c +++ b/src/filemanager/dir.c @@ -115,7 +115,9 @@ key_collate (const char *t1, const char *t2) } /* --------------------------------------------------------------------------------------------- */ -/** clear keys, should be call after sorting is finished */ +/** + * clear keys, should be call after sorting is finished. + */ static void clean_sort_keys (dir_list * list, int start, int count) @@ -131,6 +133,30 @@ clean_sort_keys (dir_list * list, int start, int count) } } +/* --------------------------------------------------------------------------------------------- */ +/** + * Increase directory list by RESIZE_STEPS + * + * @param list directory list + * @returns FALSE = failure, TRUE = 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. @@ -183,13 +209,9 @@ handle_dirent (dir_list * list, const char *fltr, struct dirent *dp, return 0; /* Need to grow the *list? */ - if (next_free == list->size) - { - list->list = g_try_realloc (list->list, sizeof (file_entry) * (list->size + RESIZE_STEPS)); - if (list->list == NULL) - return -1; - list->size += RESIZE_STEPS; - } + if (next_free == list->size && !grow_list (list)) + return -1; + return 1; } @@ -456,14 +478,8 @@ gboolean set_zero_dir (dir_list * list) { /* Need to grow the *list? */ - if (list->size == 0) - { - list->list = g_try_realloc (list->list, sizeof (file_entry) * (list->size + RESIZE_STEPS)); - if (list->list == NULL) - return FALSE; - - list->size += RESIZE_STEPS; - } + if (list->size == 0 && !grow_list (list)) + return FALSE; memset (&(list->list)[0], 0, sizeof (file_entry)); list->list[0].fnamelen = 2; @@ -521,13 +537,9 @@ handle_path (dir_list * list, const char *path, vfs_path_free (vpath); /* Need to grow the *list? */ - if (next_free == list->size) - { - list->list = g_try_realloc (list->list, sizeof (file_entry) * (list->size + RESIZE_STEPS)); - if (list->list == NULL) - return -1; - list->size += RESIZE_STEPS; - } + if (next_free == list->size && !grow_list (list)) + return -1; + return 1; }