1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (file_list.c).
5 * ---------------------------------------------------------------------------------------
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include <retro_common.h>
28 #include <lists/file_list.h>
29 #include <string/stdstring.h>
30 #include <compat/strcasestr.h>
32 static bool file_list_deinitialize_internal(file_list_t *list)
35 for (i = 0; i < list->size; i++)
37 file_list_free_userdata(list, i);
38 file_list_free_actiondata(list, i);
40 if (list->list[i].path)
41 free(list->list[i].path);
42 list->list[i].path = NULL;
44 if (list->list[i].label)
45 free(list->list[i].label);
46 list->list[i].label = NULL;
48 if (list->list[i].alt)
49 free(list->list[i].alt);
50 list->list[i].alt = NULL;
58 bool file_list_reserve(file_list_t *list, size_t nitems)
60 const size_t item_size = sizeof(struct item_file);
61 struct item_file *new_data;
63 if (nitems < list->capacity || nitems > (size_t)-1/item_size)
66 if (!(new_data = (struct item_file*)realloc(list->list, nitems * item_size)))
69 memset(&new_data[list->capacity], 0, item_size * (nitems - list->capacity));
71 list->list = new_data;
72 list->capacity = nitems;
77 bool file_list_insert(file_list_t *list,
78 const char *path, const char *label,
79 unsigned type, size_t directory_ptr,
85 /* Expand file list if needed */
86 if (list->size >= list->capacity)
87 if (!file_list_reserve(list, list->capacity * 2 + 1))
90 for (i = (unsigned)list->size; i > (int)idx; i--)
92 struct item_file *copy = (struct item_file*)
93 malloc(sizeof(struct item_file));
99 copy->directory_ptr = 0;
101 copy->userdata = NULL;
102 copy->actiondata = NULL;
104 memcpy(copy, &list->list[i-1], sizeof(struct item_file));
106 memcpy(&list->list[i-1], &list->list[i], sizeof(struct item_file));
107 memcpy(&list->list[i], copy, sizeof(struct item_file));
112 list->list[idx].path = NULL;
113 list->list[idx].label = NULL;
114 list->list[idx].alt = NULL;
115 list->list[idx].type = type;
116 list->list[idx].directory_ptr = directory_ptr;
117 list->list[idx].entry_idx = entry_idx;
118 list->list[idx].userdata = NULL;
119 list->list[idx].actiondata = NULL;
122 list->list[idx].label = strdup(label);
124 list->list[idx].path = strdup(path);
131 bool file_list_append(file_list_t *list,
132 const char *path, const char *label,
133 unsigned type, size_t directory_ptr,
136 unsigned idx = (unsigned)list->size;
137 /* Expand file list if needed */
138 if (idx >= list->capacity)
139 if (!file_list_reserve(list, list->capacity * 2 + 1))
142 list->list[idx].path = NULL;
143 list->list[idx].label = NULL;
144 list->list[idx].alt = NULL;
145 list->list[idx].type = type;
146 list->list[idx].directory_ptr = directory_ptr;
147 list->list[idx].entry_idx = entry_idx;
148 list->list[idx].userdata = NULL;
149 list->list[idx].actiondata = NULL;
152 list->list[idx].label = strdup(label);
154 list->list[idx].path = strdup(path);
161 void file_list_pop(file_list_t *list, size_t *directory_ptr)
169 if (list->list[list->size].path)
170 free(list->list[list->size].path);
171 list->list[list->size].path = NULL;
173 if (list->list[list->size].label)
174 free(list->list[list->size].label);
175 list->list[list->size].label = NULL;
179 *directory_ptr = list->list[list->size].directory_ptr;
182 void file_list_free(file_list_t *list)
186 file_list_deinitialize_internal(list);
190 bool file_list_deinitialize(file_list_t *list)
194 if (!file_list_deinitialize_internal(list))
201 void file_list_clear(file_list_t *list)
208 for (i = 0; i < list->size; i++)
210 if (list->list[i].path)
211 free(list->list[i].path);
212 list->list[i].path = NULL;
214 if (list->list[i].label)
215 free(list->list[i].label);
216 list->list[i].label = NULL;
218 if (list->list[i].alt)
219 free(list->list[i].alt);
220 list->list[i].alt = NULL;
226 static void file_list_get_label_at_offset(const file_list_t *list, size_t idx,
232 *label = list->list[idx].path;
233 if (list->list[idx].label)
234 *label = list->list[idx].label;
237 void file_list_set_alt_at_offset(file_list_t *list, size_t idx,
243 if (list->list[idx].alt)
244 free(list->list[idx].alt);
245 list->list[idx].alt = NULL;
248 list->list[idx].alt = strdup(alt);
251 static int file_list_alt_cmp(const void *a_, const void *b_)
253 const struct item_file *a = (const struct item_file*)a_;
254 const struct item_file *b = (const struct item_file*)b_;
255 const char *cmp_a = a->alt ? a->alt : a->path;
256 const char *cmp_b = b->alt ? b->alt : b->path;
257 return strcasecmp(cmp_a, cmp_b);
260 static int file_list_type_cmp(const void *a_, const void *b_)
262 const struct item_file *a = (const struct item_file*)a_;
263 const struct item_file *b = (const struct item_file*)b_;
264 if (a->type < b->type)
266 if (a->type == b->type)
272 void file_list_sort_on_alt(file_list_t *list)
274 qsort(list->list, list->size, sizeof(list->list[0]), file_list_alt_cmp);
277 void file_list_sort_on_type(file_list_t *list)
279 qsort(list->list, list->size, sizeof(list->list[0]), file_list_type_cmp);
282 void *file_list_get_userdata_at_offset(const file_list_t *list, size_t idx)
286 return list->list[idx].userdata;
289 void *file_list_get_actiondata_at_offset(const file_list_t *list, size_t idx)
293 return list->list[idx].actiondata;
296 void file_list_free_actiondata(const file_list_t *list, size_t idx)
300 if (list->list[idx].actiondata)
301 free(list->list[idx].actiondata);
302 list->list[idx].actiondata = NULL;
305 void file_list_free_userdata(const file_list_t *list, size_t idx)
309 if (list->list[idx].userdata)
310 free(list->list[idx].userdata);
311 list->list[idx].userdata = NULL;
314 bool file_list_search(const file_list_t *list, const char *needle, size_t *idx)
322 for (i = 0; i < list->size; i++)
324 const char *str = NULL;
325 const char *alt = list->list[i].alt
327 : list->list[i].path;
331 file_list_get_label_at_offset(list, i, &alt);
336 if ((str = (const char *)strcasestr(alt, needle)) == alt)
338 /* Found match with first chars, best possible match. */
343 else if (str && !ret)
345 /* Found mid-string match, but try to find a match with
346 * first characters before we settle. */