2013-10-07 08:56:16 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2014-01-01 00:50:59 +00: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-04-13 22:09:52 +00:00
|
|
|
#include "../../driver.h"
|
2014-03-02 01:18:37 +00:00
|
|
|
#include "../../compat/strcasestr.h"
|
|
|
|
#include "../../msvc/msvc_compat.h"
|
2013-10-07 08:56:16 +00:00
|
|
|
|
2013-11-08 03:50:33 +00:00
|
|
|
struct item_file
|
2013-10-07 08:56:16 +00:00
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
char *alt;
|
|
|
|
unsigned type;
|
|
|
|
size_t directory_ptr;
|
|
|
|
};
|
|
|
|
|
2013-12-09 15:18:58 +00:00
|
|
|
void file_list_push(file_list_t *list,
|
2013-10-07 08:56:16 +00:00
|
|
|
const char *path, unsigned type, size_t directory_ptr)
|
|
|
|
{
|
|
|
|
if (!list)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (list->size >= list->capacity)
|
|
|
|
{
|
|
|
|
list->capacity++;
|
|
|
|
list->capacity *= 2;
|
2013-11-08 03:50:33 +00:00
|
|
|
list->list = (struct item_file*)realloc(list->list, list->capacity * sizeof(struct item_file));
|
2013-10-07 08:56:16 +00:00
|
|
|
}
|
|
|
|
|
2014-04-13 22:09:52 +00:00
|
|
|
if (driver.menu_ctx && driver.menu_ctx->list_insert)
|
|
|
|
driver.menu_ctx->list_insert(list, path, list->size);
|
2014-02-28 18:07:30 +00:00
|
|
|
|
2013-10-07 08:56:16 +00:00
|
|
|
list->list[list->size].path = strdup(path);
|
|
|
|
list->list[list->size].alt = NULL;
|
|
|
|
list->list[list->size].type = type;
|
|
|
|
list->list[list->size].directory_ptr = directory_ptr;
|
|
|
|
list->size++;
|
2014-02-28 18:07:30 +00:00
|
|
|
|
2013-10-07 08:56:16 +00:00
|
|
|
}
|
|
|
|
|
2014-05-31 17:22:23 +00:00
|
|
|
size_t file_list_get_size(const file_list_t *list)
|
|
|
|
{
|
|
|
|
if (!list)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return list->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t file_list_get_directory_ptr(const file_list_t *list)
|
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
if (!list)
|
|
|
|
return 0;
|
|
|
|
size = file_list_get_size(list);
|
|
|
|
return list->list[size].directory_ptr;
|
|
|
|
}
|
|
|
|
|
2013-11-08 03:48:31 +00:00
|
|
|
void file_list_pop(file_list_t *list, size_t *directory_ptr)
|
2013-10-07 08:56:16 +00:00
|
|
|
{
|
2014-05-30 15:49:04 +00:00
|
|
|
if (!list)
|
|
|
|
return;
|
|
|
|
|
2013-10-07 08:56:16 +00:00
|
|
|
if (!(list->size == 0))
|
2014-02-28 18:07:30 +00:00
|
|
|
{
|
2014-04-13 22:09:52 +00:00
|
|
|
if (driver.menu_ctx && driver.menu_ctx->list_delete)
|
|
|
|
driver.menu_ctx->list_delete(list, list->size);
|
2013-10-07 08:56:16 +00:00
|
|
|
free(list->list[--list->size].path);
|
2014-02-28 18:07:30 +00:00
|
|
|
}
|
2013-10-07 08:56:16 +00:00
|
|
|
|
|
|
|
if (directory_ptr)
|
2014-02-28 20:47:06 +00:00
|
|
|
{
|
2013-10-07 08:56:16 +00:00
|
|
|
*directory_ptr = list->list[list->size].directory_ptr;
|
2014-03-02 20:24:58 +00:00
|
|
|
}
|
2014-04-13 22:09:52 +00:00
|
|
|
|
|
|
|
if (driver.menu_ctx && driver.menu_ctx->list_set_selection)
|
|
|
|
driver.menu_ctx->list_set_selection(list);
|
2013-10-07 08:56:16 +00:00
|
|
|
}
|
|
|
|
|
2013-11-08 03:48:31 +00:00
|
|
|
void file_list_free(file_list_t *list)
|
2013-10-07 08:56:16 +00:00
|
|
|
{
|
2013-10-22 13:08:17 +00:00
|
|
|
size_t i;
|
2014-05-30 15:49:04 +00:00
|
|
|
|
|
|
|
if (!list)
|
|
|
|
return;
|
|
|
|
|
2013-10-22 13:08:17 +00:00
|
|
|
for (i = 0; i < list->size; i++)
|
2013-10-07 08:56:16 +00:00
|
|
|
free(list->list[i].path);
|
|
|
|
free(list->list);
|
|
|
|
free(list);
|
|
|
|
}
|
|
|
|
|
2013-11-08 03:48:31 +00:00
|
|
|
void file_list_clear(file_list_t *list)
|
2013-10-07 08:56:16 +00:00
|
|
|
{
|
2013-10-22 13:08:17 +00:00
|
|
|
size_t i;
|
2014-05-30 15:49:04 +00:00
|
|
|
|
|
|
|
if (!list)
|
|
|
|
return;
|
|
|
|
|
2013-10-22 13:08:17 +00:00
|
|
|
for (i = 0; i < list->size; i++)
|
2013-10-07 08:56:16 +00:00
|
|
|
{
|
|
|
|
free(list->list[i].path);
|
|
|
|
free(list->list[i].alt);
|
|
|
|
}
|
2014-04-13 22:09:52 +00:00
|
|
|
|
|
|
|
if (driver.menu_ctx && driver.menu_ctx->list_clear)
|
|
|
|
driver.menu_ctx->list_clear(list);
|
2013-10-07 08:56:16 +00:00
|
|
|
list->size = 0;
|
|
|
|
}
|
|
|
|
|
2013-11-08 03:48:31 +00:00
|
|
|
void file_list_set_alt_at_offset(file_list_t *list, size_t index,
|
2013-10-07 08:56:16 +00:00
|
|
|
const char *alt)
|
|
|
|
{
|
2014-05-30 15:49:04 +00:00
|
|
|
if (!list)
|
|
|
|
return;
|
|
|
|
|
2013-10-07 08:56:16 +00:00
|
|
|
free(list->list[index].alt);
|
|
|
|
list->list[index].alt = strdup(alt);
|
|
|
|
}
|
|
|
|
|
2013-11-08 03:48:31 +00:00
|
|
|
void file_list_get_alt_at_offset(const file_list_t *list, size_t index,
|
2013-10-07 08:56:16 +00:00
|
|
|
const char **alt)
|
|
|
|
{
|
2014-05-30 15:49:04 +00:00
|
|
|
if (!list)
|
|
|
|
return;
|
|
|
|
|
2013-10-07 08:56:16 +00:00
|
|
|
if (alt)
|
|
|
|
*alt = list->list[index].alt ? list->list[index].alt : list->list[index].path;
|
|
|
|
}
|
|
|
|
|
2013-11-08 03:48:31 +00:00
|
|
|
static int file_list_alt_cmp(const void *a_, const void *b_)
|
2013-10-07 08:56:16 +00:00
|
|
|
{
|
2013-11-08 03:50:33 +00: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);
|
|
|
|
}
|
|
|
|
|
2013-11-08 03:48:31 +00:00
|
|
|
void file_list_sort_on_alt(file_list_t *list)
|
2013-10-07 08:56:16 +00:00
|
|
|
{
|
2014-05-30 15:49:04 +00:00
|
|
|
if (!list)
|
|
|
|
return;
|
|
|
|
|
2013-11-08 03:48:31 +00:00
|
|
|
qsort(list->list, list->size, sizeof(list->list[0]), file_list_alt_cmp);
|
2013-10-07 08:56:16 +00:00
|
|
|
}
|
|
|
|
|
2013-11-08 03:48:31 +00:00
|
|
|
void file_list_get_at_offset(const file_list_t *list, size_t index,
|
2013-10-07 08:56:16 +00:00
|
|
|
const char **path, unsigned *file_type)
|
|
|
|
{
|
2014-05-31 18:02:02 +00:00
|
|
|
if (!list || !list->list)
|
2014-05-30 15:49:04 +00:00
|
|
|
return;
|
|
|
|
|
2013-10-07 08:56:16 +00:00
|
|
|
if (path)
|
|
|
|
*path = list->list[index].path;
|
|
|
|
if (file_type)
|
|
|
|
*file_type = list->list[index].type;
|
|
|
|
}
|
|
|
|
|
2013-11-08 03:48:31 +00:00
|
|
|
void file_list_get_last(const file_list_t *list,
|
2013-10-07 08:56:16 +00:00
|
|
|
const char **path, unsigned *file_type)
|
|
|
|
{
|
2014-05-30 15:49:04 +00:00
|
|
|
if (!list)
|
|
|
|
return;
|
|
|
|
|
2013-10-07 08:56:16 +00:00
|
|
|
if (list->size)
|
2013-11-08 03:48:31 +00:00
|
|
|
file_list_get_at_offset(list, list->size - 1, path, file_type);
|
2013-10-07 08:56:16 +00:00
|
|
|
}
|
2013-12-09 15:18:58 +00:00
|
|
|
|
|
|
|
bool file_list_search(const file_list_t *list, const char *needle, size_t *index)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
const char *alt;
|
2013-12-10 19:17:53 +00:00
|
|
|
bool ret = false;
|
2014-05-30 15:49:04 +00:00
|
|
|
|
|
|
|
if (!list)
|
|
|
|
return false;
|
|
|
|
|
2013-12-09 15:18:58 +00:00
|
|
|
for (i = 0; i < list->size; i++)
|
|
|
|
{
|
|
|
|
file_list_get_alt_at_offset(list, i, &alt);
|
|
|
|
if (!alt)
|
|
|
|
continue;
|
2013-12-10 19:17:53 +00:00
|
|
|
|
|
|
|
const char *str = strcasestr(alt, needle);
|
|
|
|
if (str == alt) // Found match with first chars, best possible match.
|
|
|
|
{
|
|
|
|
*index = i;
|
|
|
|
ret = true;
|
|
|
|
break;
|
|
|
|
}
|
2013-12-12 19:12:28 +00:00
|
|
|
else if (str && !ret) // Found mid-string match, but try to find a match with first chars before we settle.
|
2013-12-09 15:18:58 +00:00
|
|
|
{
|
|
|
|
*index = i;
|
2013-12-10 19:17:53 +00:00
|
|
|
ret = true;
|
2013-12-09 15:18:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-10 19:17:53 +00:00
|
|
|
return ret;
|
2013-12-09 15:18:58 +00:00
|
|
|
}
|