(file_list.c) Cleanups/optimizations

This commit is contained in:
twinaphex 2019-04-27 07:32:40 +02:00
parent 890e1eb064
commit e1b11593fc
5 changed files with 43 additions and 105 deletions

View File

@ -101,8 +101,6 @@ void file_list_pop(file_list_t *list, size_t *directory_ptr);
void file_list_clear(file_list_t *list);
void file_list_copy(const file_list_t *src, file_list_t *dst);
void file_list_get_last(const file_list_t *list,
const char **path, const char **label,
unsigned *type, size_t *entry_idx);

View File

@ -50,28 +50,6 @@ bool file_list_reserve(file_list_t *list, size_t nitems)
return new_data != NULL;
}
static void file_list_add(file_list_t *list, unsigned idx,
const char *path, const char *label,
unsigned type, size_t directory_ptr,
size_t entry_idx)
{
list->list[idx].path = NULL;
list->list[idx].label = NULL;
list->list[idx].alt = NULL;
list->list[idx].type = type;
list->list[idx].directory_ptr = directory_ptr;
list->list[idx].entry_idx = entry_idx;
list->list[idx].userdata = NULL;
list->list[idx].actiondata = NULL;
if (label)
list->list[idx].label = strdup(label);
if (path)
list->list[idx].path = strdup(path);
list->size++;
}
bool file_list_prepend(file_list_t *list,
const char *path, const char *label,
unsigned type, size_t directory_ptr,
@ -110,8 +88,21 @@ bool file_list_insert(file_list_t *list,
free(copy);
}
file_list_add(list, (unsigned)idx, path, label, type,
directory_ptr, entry_idx);
list->list[idx].path = NULL;
list->list[idx].label = NULL;
list->list[idx].alt = NULL;
list->list[idx].type = type;
list->list[idx].directory_ptr = directory_ptr;
list->list[idx].entry_idx = entry_idx;
list->list[idx].userdata = NULL;
list->list[idx].actiondata = NULL;
if (label)
list->list[idx].label = strdup(label);
if (path)
list->list[idx].path = strdup(path);
list->size++;
return true;
}
@ -121,13 +112,27 @@ bool file_list_append(file_list_t *list,
unsigned type, size_t directory_ptr,
size_t entry_idx)
{
unsigned idx = (unsigned)list->size;
/* Expand file list if needed */
if (list->size >= list->capacity)
if (idx >= list->capacity)
if (!file_list_reserve(list, list->capacity * 2 + 1))
return false;
file_list_add(list, (unsigned)list->size, path, label, type,
directory_ptr, entry_idx);
list->list[idx].path = NULL;
list->list[idx].label = NULL;
list->list[idx].alt = NULL;
list->list[idx].type = type;
list->list[idx].directory_ptr = directory_ptr;
list->list[idx].entry_idx = entry_idx;
list->list[idx].userdata = NULL;
list->list[idx].actiondata = NULL;
if (label)
list->list[idx].label = strdup(label);
if (path)
list->list[idx].path = strdup(path);
list->size++;
return true;
}
@ -221,61 +226,6 @@ void file_list_clear(file_list_t *list)
list->size = 0;
}
void file_list_copy(const file_list_t *src, file_list_t *dst)
{
struct item_file *item = NULL;
if (!src || !dst)
return;
if (dst->list)
{
for (item = dst->list; item < &dst->list[dst->size]; ++item)
{
if (!item)
continue;
if (item->path)
free(item->path);
item->path = NULL;
if (item->label)
free(item->label);
item->label = NULL;
if (item->alt)
free(item->alt);
item->alt = NULL;
}
free(dst->list);
dst->list = NULL;
}
dst->size = 0;
dst->capacity = 0;
dst->list = (struct item_file*)malloc(src->size * sizeof(struct item_file));
if (!dst->list)
return;
dst->size = dst->capacity = src->size;
memcpy(dst->list, src->list, dst->size * sizeof(struct item_file));
for (item = dst->list; item < &dst->list[dst->size]; ++item)
{
if (item->path)
item->path = strdup(item->path);
if (item->label)
item->label = strdup(item->label);
if (item->alt)
item->alt = strdup(item->alt);
}
}
void file_list_set_label_at_offset(file_list_t *list, size_t idx,
const char *label)
{
@ -318,12 +268,10 @@ void file_list_set_alt_at_offset(file_list_t *list, size_t idx,
void file_list_get_alt_at_offset(const file_list_t *list, size_t idx,
const char **alt)
{
if (!list)
return;
if (alt)
*alt = list->list[idx].alt ?
list->list[idx].alt : list->list[idx].path;
if (list && alt)
*alt = list->list[idx].alt
? list->list[idx].alt
: list->list[idx].path;
}
static int file_list_alt_cmp(const void *a_, const void *b_)
@ -366,16 +314,14 @@ void *file_list_get_userdata_at_offset(const file_list_t *list, size_t idx)
void file_list_set_userdata(const file_list_t *list, size_t idx, void *ptr)
{
if (!list || !ptr)
return;
list->list[idx].userdata = ptr;
if (list && ptr)
list->list[idx].userdata = ptr;
}
void file_list_set_actiondata(const file_list_t *list, size_t idx, void *ptr)
{
if (!list || !ptr)
return;
list->list[idx].actiondata = ptr;
if (list && ptr)
list->list[idx].actiondata = ptr;
}
void *file_list_get_actiondata_at_offset(const file_list_t *list, size_t idx)
@ -431,10 +377,7 @@ void file_list_get_last(const file_list_t *list,
const char **path, const char **label,
unsigned *file_type, size_t *entry_idx)
{
if (!list)
return;
if (list->size)
if (list && list->size)
file_list_get_at_offset(list, list->size - 1, path, label, file_type, entry_idx);
}
@ -464,7 +407,7 @@ bool file_list_search(const file_list_t *list, const char *needle, size_t *idx)
{
/* Found match with first chars, best possible match. */
*idx = i;
ret = true;
ret = true;
break;
}
else if (str && !ret)
@ -472,7 +415,7 @@ bool file_list_search(const file_list_t *list, const char *needle, size_t *idx)
/* Found mid-string match, but try to find a match with
* first characters before we settle. */
*idx = i;
ret = true;
ret = true;
}
}

View File

@ -1971,7 +1971,6 @@ static void ozone_list_deep_copy(const file_list_t *src, file_list_t *dst,
menu_animation_kill_by_tag(&tag);
/* use true here because file_list_copy() doesn't free actiondata */
ozone_free_list_nodes(dst, true);
file_list_clear(dst);

View File

@ -3831,7 +3831,6 @@ static void stripes_list_deep_copy(const file_list_t *src, file_list_t *dst,
menu_animation_kill_by_tag(&tag);
/* use true here because file_list_copy() doesn't free actiondata */
stripes_free_list_nodes(dst, true);
file_list_clear(dst);

View File

@ -5067,7 +5067,6 @@ static void xmb_list_deep_copy(const file_list_t *src, file_list_t *dst,
menu_animation_kill_by_tag(&tag);
/* use true here because file_list_copy() doesn't free actiondata */
xmb_free_list_nodes(dst, true);
file_list_clear(dst);