2013-10-07 08:56:16 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2014-01-01 01:50:59 +01:00
|
|
|
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
|
|
|
* Copyright (C) 2011-2014 - Daniel De Matteis
|
2013-10-07 08:56:16 +00:00
|
|
|
*
|
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "file_list.h"
|
2014-07-26 18:31:34 +02:00
|
|
|
#include "compat/strcasestr.h"
|
2014-10-21 06:06:12 +02:00
|
|
|
#include "compat/msvc_compat.h"
|
2013-10-07 08:56:16 +00:00
|
|
|
|
2014-06-17 16:46:30 +02:00
|
|
|
void file_list_push(file_list_t *list,
|
2014-08-07 08:27:24 +02:00
|
|
|
const char *path, const char *label,
|
2014-08-30 01:56:12 +02:00
|
|
|
unsigned type, size_t directory_ptr)
|
2013-10-07 08:56:16 +00:00
|
|
|
{
|
|
|
|
if (list->size >= list->capacity)
|
|
|
|
{
|
|
|
|
list->capacity++;
|
|
|
|
list->capacity *= 2;
|
2014-08-20 17:23:21 +02:00
|
|
|
list->list = (struct item_file*)realloc(list->list,
|
|
|
|
list->capacity * sizeof(struct item_file));
|
2013-10-07 08:56:16 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 08:27:24 +02:00
|
|
|
list->list[list->size].label = strdup(label);
|
2014-08-30 01:56:12 +02:00
|
|
|
list->list[list->size].path = strdup(path);
|
2013-10-07 08:56:16 +00:00
|
|
|
list->list[list->size].alt = NULL;
|
|
|
|
list->list[list->size].type = type;
|
|
|
|
list->list[list->size].directory_ptr = directory_ptr;
|
|
|
|
list->size++;
|
|
|
|
}
|
|
|
|
|
2014-06-17 16:46:30 +02:00
|
|
|
size_t file_list_get_size(const file_list_t *list)
|
2014-05-31 19:22:23 +02:00
|
|
|
{
|
2014-10-17 21:41:23 +02:00
|
|
|
if (list)
|
|
|
|
return list->size;
|
|
|
|
return 0;
|
2014-05-31 19:22:23 +02:00
|
|
|
}
|
|
|
|
|
2014-06-17 16:46:30 +02:00
|
|
|
size_t file_list_get_directory_ptr(const file_list_t *list)
|
2014-05-31 19:22:23 +02:00
|
|
|
{
|
2014-07-22 07:16:26 +02:00
|
|
|
size_t size = file_list_get_size(list);
|
2014-05-31 19:22:23 +02:00
|
|
|
return list->list[size].directory_ptr;
|
|
|
|
}
|
|
|
|
|
2014-10-10 22:08:11 +02:00
|
|
|
|
2014-06-17 16:46:30 +02:00
|
|
|
void file_list_pop(file_list_t *list, size_t *directory_ptr)
|
2013-10-07 08:56:16 +00:00
|
|
|
{
|
2014-10-10 22:44:35 +02:00
|
|
|
if (list->size != 0)
|
2014-02-28 19:07:30 +01:00
|
|
|
{
|
2014-10-10 22:08:11 +02:00
|
|
|
--list->size;
|
2014-08-07 08:27:24 +02:00
|
|
|
free(list->list[list->size].path);
|
|
|
|
free(list->list[list->size].label);
|
2014-02-28 19:07:30 +01:00
|
|
|
}
|
2013-10-07 08:56:16 +00:00
|
|
|
|
|
|
|
if (directory_ptr)
|
|
|
|
*directory_ptr = list->list[list->size].directory_ptr;
|
|
|
|
}
|
|
|
|
|
2014-06-17 16:46:30 +02:00
|
|
|
void file_list_free(file_list_t *list)
|
2013-10-07 08:56:16 +00:00
|
|
|
{
|
2013-10-22 15:08:17 +02:00
|
|
|
size_t i;
|
2014-05-30 17:49:04 +02:00
|
|
|
|
2014-09-15 08:17:16 -04:00
|
|
|
if (!list)
|
|
|
|
return;
|
|
|
|
|
2013-10-22 15:08:17 +02:00
|
|
|
for (i = 0; i < list->size; i++)
|
2014-08-07 08:27:24 +02:00
|
|
|
{
|
2013-10-07 08:56:16 +00:00
|
|
|
free(list->list[i].path);
|
2014-08-07 08:27:24 +02:00
|
|
|
free(list->list[i].label);
|
|
|
|
}
|
2013-10-07 08:56:16 +00:00
|
|
|
free(list->list);
|
|
|
|
free(list);
|
|
|
|
}
|
|
|
|
|
2014-06-17 16:46:30 +02:00
|
|
|
void file_list_clear(file_list_t *list)
|
2013-10-07 08:56:16 +00:00
|
|
|
{
|
2013-10-22 15:08:17 +02:00
|
|
|
size_t i;
|
2014-05-30 17:49:04 +02:00
|
|
|
|
2013-10-22 15:08:17 +02:00
|
|
|
for (i = 0; i < list->size; i++)
|
2013-10-07 08:56:16 +00:00
|
|
|
{
|
|
|
|
free(list->list[i].path);
|
2014-10-11 00:19:11 +01:00
|
|
|
list->list[i].path = NULL;
|
2014-08-07 08:27:24 +02:00
|
|
|
free(list->list[i].label);
|
2014-10-11 00:19:11 +01:00
|
|
|
list->list[i].label = NULL;
|
2013-10-07 08:56:16 +00:00
|
|
|
free(list->list[i].alt);
|
2014-10-11 00:19:11 +01:00
|
|
|
list->list[i].alt = NULL;
|
2013-10-07 08:56:16 +00:00
|
|
|
}
|
2014-04-14 00:09:52 +02:00
|
|
|
|
2013-10-07 08:56:16 +00:00
|
|
|
list->size = 0;
|
|
|
|
}
|
|
|
|
|
2014-10-20 19:51:00 +02:00
|
|
|
void file_list_copy(file_list_t *list, file_list_t *list_old)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
list_old->size = list->size;
|
|
|
|
list_old->capacity = list->capacity;
|
|
|
|
|
|
|
|
list_old->list = (struct item_file*)realloc(list_old->list,
|
|
|
|
list_old->capacity * sizeof(struct item_file));
|
|
|
|
|
|
|
|
for (i = 0; i < list->size; i++)
|
|
|
|
{
|
|
|
|
list_old->list[i].path = strdup(list->list[i].path);
|
|
|
|
list_old->list[i].label = strdup(list->list[i].label);
|
|
|
|
if (list->list[i].alt)
|
|
|
|
list_old->list[i].alt = strdup(list->list[i].alt);
|
|
|
|
list_old->list[i].type = list->list[i].type;
|
|
|
|
list_old->list[i].directory_ptr = list->list[i].directory_ptr;
|
|
|
|
list_old->list[i].userdata = list->list[i].userdata;
|
|
|
|
list_old->list[i].actiondata = list->list[i].actiondata;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-20 22:43:17 +02:00
|
|
|
void file_list_set_label_at_offset(file_list_t *list, size_t idx,
|
2014-09-01 23:40:23 +02:00
|
|
|
const char *label)
|
|
|
|
{
|
2014-10-20 22:43:17 +02:00
|
|
|
free(list->list[idx].label);
|
|
|
|
list->list[idx].label = strdup(label);
|
2014-09-01 23:40:23 +02:00
|
|
|
}
|
|
|
|
|
2014-10-20 22:43:17 +02:00
|
|
|
void file_list_get_label_at_offset(const file_list_t *list, size_t idx,
|
2014-09-01 23:40:23 +02:00
|
|
|
const char **label)
|
|
|
|
{
|
|
|
|
if (label)
|
2014-10-20 22:43:17 +02:00
|
|
|
*label = list->list[idx].label ?
|
|
|
|
list->list[idx].label : list->list[idx].path;
|
2014-09-01 23:40:23 +02:00
|
|
|
}
|
|
|
|
|
2014-10-20 22:43:17 +02:00
|
|
|
void file_list_set_alt_at_offset(file_list_t *list, size_t idx,
|
2013-10-07 08:56:16 +00:00
|
|
|
const char *alt)
|
|
|
|
{
|
2014-10-20 22:43:17 +02:00
|
|
|
free(list->list[idx].alt);
|
|
|
|
list->list[idx].alt = strdup(alt);
|
2013-10-07 08:56:16 +00:00
|
|
|
}
|
|
|
|
|
2014-10-20 22:43:17 +02:00
|
|
|
void file_list_get_alt_at_offset(const file_list_t *list, size_t idx,
|
2013-10-07 08:56:16 +00:00
|
|
|
const char **alt)
|
|
|
|
{
|
|
|
|
if (alt)
|
2014-10-20 22:43:17 +02:00
|
|
|
*alt = list->list[idx].alt ?
|
|
|
|
list->list[idx].alt : list->list[idx].path;
|
2013-10-07 08:56:16 +00:00
|
|
|
}
|
|
|
|
|
2013-11-08 04:48:31 +01:00
|
|
|
static int file_list_alt_cmp(const void *a_, const void *b_)
|
2013-10-07 08:56:16 +00:00
|
|
|
{
|
2013-11-08 04:50:33 +01:00
|
|
|
const struct item_file *a = (const struct item_file*)a_;
|
|
|
|
const struct item_file *b = (const struct item_file*)b_;
|
2013-10-07 08:56:16 +00:00
|
|
|
const char *cmp_a = a->alt ? a->alt : a->path;
|
|
|
|
const char *cmp_b = b->alt ? b->alt : b->path;
|
|
|
|
return strcasecmp(cmp_a, cmp_b);
|
|
|
|
}
|
|
|
|
|
2014-06-17 16:46:30 +02:00
|
|
|
void file_list_sort_on_alt(file_list_t *list)
|
2013-10-07 08:56:16 +00:00
|
|
|
{
|
2013-11-08 04:48:31 +01:00
|
|
|
qsort(list->list, list->size, sizeof(list->list[0]), file_list_alt_cmp);
|
2013-10-07 08:56:16 +00:00
|
|
|
}
|
|
|
|
|
2014-10-20 22:43:17 +02:00
|
|
|
void *file_list_get_userdata_at_offset(const file_list_t *list, size_t idx)
|
2014-10-10 22:08:11 +02:00
|
|
|
{
|
2014-10-20 22:43:17 +02:00
|
|
|
return list->list[idx].userdata;
|
2014-10-10 22:08:11 +02:00
|
|
|
}
|
|
|
|
|
2014-10-20 22:43:17 +02:00
|
|
|
void *file_list_get_actiondata_at_offset(const file_list_t *list, size_t idx)
|
2014-10-12 00:10:44 +02:00
|
|
|
{
|
2014-10-17 23:32:40 +02:00
|
|
|
if (list)
|
2014-10-20 22:43:17 +02:00
|
|
|
return list->list[idx].actiondata;
|
2014-10-17 23:32:40 +02:00
|
|
|
return NULL;
|
2014-10-12 00:10:44 +02:00
|
|
|
}
|
|
|
|
|
2014-10-15 07:40:19 +02:00
|
|
|
void *file_list_get_last_actiondata(const file_list_t *list)
|
|
|
|
{
|
2014-10-17 22:53:30 +02:00
|
|
|
if (list)
|
|
|
|
return list->list[list->size - 1].actiondata;
|
|
|
|
return NULL;
|
2014-10-15 07:40:19 +02:00
|
|
|
}
|
|
|
|
|
2014-10-20 22:43:17 +02:00
|
|
|
void file_list_get_at_offset(const file_list_t *list, size_t idx,
|
2014-08-30 15:50:42 +02:00
|
|
|
const char **path, const char **label, unsigned *file_type)
|
2013-10-07 08:56:16 +00:00
|
|
|
{
|
|
|
|
if (path)
|
2014-10-20 22:43:17 +02:00
|
|
|
*path = list->list[idx].path;
|
2014-08-30 15:50:42 +02:00
|
|
|
if (label)
|
2014-10-20 22:43:17 +02:00
|
|
|
*label = list->list[idx].label;
|
2013-10-07 08:56:16 +00:00
|
|
|
if (file_type)
|
2014-10-20 22:43:17 +02:00
|
|
|
*file_type = list->list[idx].type;
|
2013-10-07 08:56:16 +00:00
|
|
|
}
|
|
|
|
|
2014-06-17 16:46:30 +02:00
|
|
|
void file_list_get_last(const file_list_t *list,
|
2014-08-30 15:50:42 +02:00
|
|
|
const char **path, const char **label,
|
|
|
|
unsigned *file_type)
|
2013-10-07 08:56:16 +00:00
|
|
|
{
|
|
|
|
if (list->size)
|
2014-08-30 15:50:42 +02:00
|
|
|
file_list_get_at_offset(list, list->size - 1, path, label, file_type);
|
2013-10-07 08:56:16 +00:00
|
|
|
}
|
2013-12-09 16:18:58 +01:00
|
|
|
|
2014-10-20 22:43:17 +02:00
|
|
|
bool file_list_search(const file_list_t *list, const char *needle, size_t *idx)
|
2013-12-09 16:18:58 +01:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
const char *alt;
|
2013-12-10 20:17:53 +01:00
|
|
|
bool ret = false;
|
2014-05-30 17:49:04 +02:00
|
|
|
|
2013-12-09 16:18:58 +01:00
|
|
|
for (i = 0; i < list->size; i++)
|
|
|
|
{
|
2014-06-01 02:08:51 +02:00
|
|
|
const char *str;
|
2013-12-09 16:18:58 +01:00
|
|
|
file_list_get_alt_at_offset(list, i, &alt);
|
|
|
|
if (!alt)
|
2014-09-01 23:40:23 +02:00
|
|
|
{
|
|
|
|
file_list_get_label_at_offset(list, i, &alt);
|
|
|
|
if (!alt)
|
|
|
|
continue;
|
|
|
|
}
|
2013-12-10 20:17:53 +01:00
|
|
|
|
2014-06-01 02:08:51 +02:00
|
|
|
str = (const char *)strcasestr(alt, needle);
|
2014-08-20 17:23:21 +02:00
|
|
|
if (str == alt)
|
2013-12-10 20:17:53 +01:00
|
|
|
{
|
2014-08-20 17:23:21 +02:00
|
|
|
/* Found match with first chars, best possible match. */
|
2014-10-20 22:43:17 +02:00
|
|
|
*idx = i;
|
2013-12-10 20:17:53 +01:00
|
|
|
ret = true;
|
|
|
|
break;
|
|
|
|
}
|
2014-08-20 17:23:21 +02:00
|
|
|
else if (str && !ret)
|
2013-12-09 16:18:58 +01:00
|
|
|
{
|
2014-08-20 17:23:21 +02:00
|
|
|
/* Found mid-string match, but try to find a match with
|
|
|
|
* first characters before we settle. */
|
2014-10-20 22:43:17 +02:00
|
|
|
*idx = i;
|
2013-12-10 20:17:53 +01:00
|
|
|
ret = true;
|
2013-12-09 16:18:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-10 20:17:53 +01:00
|
|
|
return ret;
|
2013-12-09 16:18:58 +01:00
|
|
|
}
|