RetroArch/database_info.c

1083 lines
32 KiB
C
Raw Normal View History

2015-01-27 17:09:19 +00:00
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2017-03-22 02:09:18 +00:00
* Copyright (C) 2011-2017 - Daniel De Matteis
2019-02-22 21:31:54 +00:00
* Copyright (C) 2016-2019 - Brad Parker
*
2015-01-27 17:09:19 +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/>.
*/
2016-09-05 23:02:25 +00:00
#include <stdio.h>
2015-09-05 17:24:21 +00:00
#include <stdint.h>
2016-09-05 22:56:00 +00:00
#include <compat/strl.h>
#include <retro_endianness.h>
#include <file/file_path.h>
#include <lists/string_list.h>
#include <lists/dir_list.h>
#include <string/stdstring.h>
2015-05-05 16:43:51 +00:00
2016-09-29 08:37:31 +00:00
#include "libretro-db/libretrodb.h"
#include "core_info.h"
2015-01-27 17:09:19 +00:00
#include "database_info.h"
2016-12-20 20:19:25 +00:00
int database_info_build_query_enum(char *s, size_t len,
enum database_query_type type,
const char *path)
{
size_t _len = 0;
2016-12-20 20:19:25 +00:00
switch (type)
{
case DATABASE_QUERY_ENTRY:
s[ _len] = '{';
s[++_len] = '\'';
s[++_len] = 'n';
s[++_len] = 'a';
s[++_len] = 'm';
s[++_len] = 'e';
s[++_len] = '\'';
s[++_len] = ':';
s[++_len] = '"';
s[++_len] = '\0';
_len += strlcpy(s + _len, path, len - _len);
s[ _len] = '"';
s[++_len] = '}';
s[++_len] = '\0';
2016-12-20 20:19:25 +00:00
break;
case DATABASE_QUERY_ENTRY_PUBLISHER:
s[ _len] = '{';
s[++_len] = '\'';
s[++_len] = 'p';
s[++_len] = 'u';
s[++_len] = 'b';
s[++_len] = 'l';
s[++_len] = 'i';
s[++_len] = 's';
s[++_len] = 'h';
s[++_len] = 'e';
s[++_len] = 'r';
s[++_len] = '\'';
s[++_len] = ':';
s[++_len] = '"';
s[++_len] = '\0';
_len += strlcpy(s + _len, path, len - _len);
s[ _len] = '"';
s[++_len] = '}';
s[++_len] = '\0';
2016-12-20 20:19:25 +00:00
break;
case DATABASE_QUERY_ENTRY_DEVELOPER:
s[ _len] = '{';
s[++_len] = '\'';
s[++_len] = 'd';
s[++_len] = 'e';
s[++_len] = 'v';
s[++_len] = 'e';
s[++_len] = 'l';
s[++_len] = 'o';
s[++_len] = 'p';
s[++_len] = 'e';
s[++_len] = 'r';
s[++_len] = '\'';
s[++_len] = ':';
s[++_len] = 'g';
s[++_len] = 'l';
s[++_len] = 'o';
s[++_len] = 'b';
s[++_len] = '(';
s[++_len] = '\'';
s[++_len] = '*';
s[++_len] = '\0';
_len += strlcpy(s + _len, path, len - _len);
s[ _len] = '*';
s[++_len] = '\'';
s[++_len] = ')';
s[++_len] = '}';
s[++_len] = '\0';
2016-12-20 20:19:25 +00:00
break;
case DATABASE_QUERY_ENTRY_ORIGIN:
s[ _len] = '{';
s[++_len] = '\'';
s[++_len] = 'o';
s[++_len] = 'r';
s[++_len] = 'i';
s[++_len] = 'g';
s[++_len] = 'i';
s[++_len] = 'n';
s[++_len] = '\'';
s[++_len] = ':';
s[++_len] = '"';
s[++_len] = '\0';
_len += strlcpy(s + _len, path, len - _len);
s[ _len] = '"';
s[++_len] = '}';
s[++_len] = '\0';
2016-12-20 20:19:25 +00:00
break;
case DATABASE_QUERY_ENTRY_FRANCHISE:
s[ _len] = '{';
s[++_len] = '\'';
s[++_len] = 'f';
s[++_len] = 'r';
s[++_len] = 'a';
s[++_len] = 'n';
s[++_len] = 'c';
s[++_len] = 'h';
s[++_len] = 'i';
s[++_len] = 's';
s[++_len] = 'e';
s[++_len] = '\'';
s[++_len] = ':';
s[++_len] = '"';
s[++_len] = '\0';
_len += strlcpy(s + _len, path, len - _len);
s[ _len] = '"';
s[++_len] = '}';
s[++_len] = '\0';
2016-12-20 20:19:25 +00:00
break;
case DATABASE_QUERY_ENTRY_RATING:
s[ _len] = '{';
s[++_len] = '\'';
s[++_len] = 'e';
s[++_len] = 's';
s[++_len] = 'r';
s[++_len] = 'b';
s[++_len] = '_';
s[++_len] = 'r';
s[++_len] = 'a';
s[++_len] = 't';
s[++_len] = 'i';
s[++_len] = 'n';
s[++_len] = 'g';
s[++_len] = '\'';
s[++_len] = ':';
s[++_len] = '"';
_len += strlcpy(s + _len, path, len - _len);
s[ _len] = '"';
s[++_len] = '}';
s[++_len] = '\0';
2016-12-20 20:19:25 +00:00
break;
case DATABASE_QUERY_ENTRY_BBFC_RATING:
s[ _len] = '{';
s[++_len] = '\'';
s[++_len] = 'b';
s[++_len] = 'b';
s[++_len] = 'f';
s[++_len] = 'c';
s[++_len] = '_';
s[++_len] = 'r';
s[++_len] = 'a';
s[++_len] = 't';
s[++_len] = 'i';
s[++_len] = 'n';
s[++_len] = 'g';
s[++_len] = '\'';
s[++_len] = ':';
s[++_len] = '"';
s[++_len] = '\0';
_len += strlcpy(s + _len, path, len - _len);
s[++_len] = '"';
s[++_len] = '}';
s[++_len] = '\0';
2016-12-20 20:19:25 +00:00
break;
case DATABASE_QUERY_ENTRY_ELSPA_RATING:
s[ _len] = '{';
s[++_len] = '\'';
s[++_len] = 'e';
s[++_len] = 'l';
s[++_len] = 's';
s[++_len] = 'p';
s[++_len] = 'a';
s[++_len] = '_';
s[++_len] = 'r';
s[++_len] = 'a';
s[++_len] = 't';
s[++_len] = 'i';
s[++_len] = 'n';
s[++_len] = 'g';
s[++_len] = '\'';
s[++_len] = ':';
s[++_len] = '"';
s[++_len] = '\0';
_len += strlcpy(s + _len, path, len - _len);
s[ _len] = '"';
s[++_len] = '}';
s[++_len] = '\0';
2016-12-20 20:19:25 +00:00
break;
case DATABASE_QUERY_ENTRY_ESRB_RATING:
s[ _len] = '{';
s[++_len] = '\'';
s[++_len] = 'e';
s[++_len] = 's';
s[++_len] = 'r';
s[++_len] = 'b';
s[++_len] = '_';
s[++_len] = 'r';
s[++_len] = 'a';
s[++_len] = 't';
s[++_len] = 'i';
s[++_len] = 'n';
s[++_len] = 'g';
s[++_len] = '\'';
s[++_len] = ':';
s[++_len] = '"';
s[++_len] = '\0';
_len += strlcpy(s + _len, path, len - _len);
s[ _len] = '"';
s[++_len] = '}';
s[++_len] = '\0';
break;
2016-12-20 20:19:25 +00:00
case DATABASE_QUERY_ENTRY_PEGI_RATING:
s[ _len] = '{';
s[++_len] = '\'';
s[++_len] = 'p';
s[++_len] = 'e';
s[++_len] = 'g';
s[++_len] = 'i';
s[++_len] = '_';
s[++_len] = 'r';
s[++_len] = 'a';
s[++_len] = 't';
s[++_len] = 'i';
s[++_len] = 'n';
s[++_len] = 'g';
s[++_len] = '\'';
s[++_len] = ':';
s[++_len] = '"';
s[++_len] = '\0';
_len += strlcpy(s + _len, path, len - _len);
s[ _len] = '"';
s[++_len] = '}';
s[++_len] = '\0';
2016-12-20 20:19:25 +00:00
break;
case DATABASE_QUERY_ENTRY_CERO_RATING:
s[ _len] = '{';
s[++_len] = '\'';
s[++_len] = 'c';
s[++_len] = 'e';
s[++_len] = 'r';
s[++_len] = 'o';
s[++_len] = '_';
s[++_len] = 'r';
s[++_len] = 'a';
s[++_len] = 't';
s[++_len] = 'i';
s[++_len] = 'n';
s[++_len] = 'g';
s[++_len] = '\'';
s[++_len] = ':';
s[++_len] = '"';
s[++_len] = '\0';
_len += strlcpy(s + _len, path, len - _len);
s[ _len] = '"';
s[++_len] = '}';
s[++_len] = '\0';
2016-12-20 20:19:25 +00:00
break;
case DATABASE_QUERY_ENTRY_ENHANCEMENT_HW:
s[ _len] = '{';
s[++_len] = '\'';
s[++_len] = 'e';
s[++_len] = 'n';
s[++_len] = 'h';
s[++_len] = 'a';
s[++_len] = 'n';
s[++_len] = 'c';
s[++_len] = 'e';
s[++_len] = 'm';
s[++_len] = 'e';
s[++_len] = 'n';
s[++_len] = 't';
s[++_len] = '_';
s[++_len] = 'h';
s[++_len] = 'w';
s[++_len] = '\'';
s[++_len] = ':';
s[++_len] = '"';
s[++_len] = '\0';
_len += strlcpy(s + _len, path, len - _len);
s[ _len] = '"';
s[++_len] = '}';
s[++_len] = '\0';
2016-12-20 20:19:25 +00:00
break;
case DATABASE_QUERY_ENTRY_EDGE_MAGAZINE_RATING:
s[ _len] = '{';
s[++_len] = '\'';
s[++_len] = 'e';
s[++_len] = 'd';
s[++_len] = 'g';
s[++_len] = 'e';
s[++_len] = '_';
s[++_len] = 'r';
s[++_len] = 'a';
s[++_len] = 't';
s[++_len] = 'i';
s[++_len] = 'n';
s[++_len] = 'g';
s[++_len] = '\'';
s[++_len] = ':';
s[++_len] = '\0';
_len += strlcpy(s + _len, path, len - _len);
s[ _len] = '}';
s[++_len] = '\0';
2016-12-20 20:19:25 +00:00
break;
case DATABASE_QUERY_ENTRY_EDGE_MAGAZINE_ISSUE:
s[ _len] = '{';
s[++_len] = '\'';
s[++_len] = 'e';
s[++_len] = 'd';
s[++_len] = 'g';
s[++_len] = 'e';
s[++_len] = '_';
s[++_len] = 'i';
s[++_len] = 's';
s[++_len] = 's';
s[++_len] = 'u';
s[++_len] = 'e';
s[++_len] = '\'';
s[++_len] = ':';
s[++_len] = '\0';
_len += strlcpy(s + _len, path, len - _len);
s[ _len] = '}';
s[++_len] = '\0';
2016-12-20 20:19:25 +00:00
break;
case DATABASE_QUERY_ENTRY_FAMITSU_MAGAZINE_RATING:
s[ _len] = '{';
s[++_len] = '\'';
s[++_len] = 'f';
s[++_len] = 'a';
s[++_len] = 'm';
s[++_len] = 'i';
s[++_len] = 't';
s[++_len] = 's';
s[++_len] = 'u';
s[++_len] = '_';
s[++_len] = 'r';
s[++_len] = 'a';
s[++_len] = 't';
s[++_len] = 'i';
s[++_len] = 'n';
s[++_len] = 'g';
s[++_len] = '\'';
s[++_len] = ':';
s[++_len] = '\0';
_len += strlcpy(s + _len, path, len - _len);
s[ _len] = '}';
s[++_len] = '\0';
2016-12-20 20:19:25 +00:00
break;
case DATABASE_QUERY_ENTRY_RELEASEDATE_MONTH:
s[ _len] = '{';
s[++_len] = '\'';
s[++_len] = 'r';
s[++_len] = 'e';
s[++_len] = 'l';
s[++_len] = 'e';
s[++_len] = 'a';
s[++_len] = 's';
s[++_len] = 'e';
s[++_len] = 'm';
s[++_len] = 'o';
s[++_len] = 'n';
s[++_len] = 't';
s[++_len] = 'h';
s[++_len] = '\'';
s[++_len] = ':';
s[++_len] = '\0';
_len += strlcpy(s + _len, path, len - _len);
s[ _len] = '}';
s[++_len] = '\0';
2016-12-20 20:19:25 +00:00
break;
case DATABASE_QUERY_ENTRY_RELEASEDATE_YEAR:
s[ _len] = '{';
s[++_len] = '\'';
s[++_len] = 'r';
s[++_len] = 'e';
s[++_len] = 'l';
s[++_len] = 'e';
s[++_len] = 'a';
s[++_len] = 's';
s[++_len] = 'e';
s[++_len] = 'y';
s[++_len] = 'e';
s[++_len] = 'a';
s[++_len] = 'r';
s[++_len] = '\'';
s[++_len] = ':';
s[++_len] = '\0';
_len += strlcpy(s + _len, path, len - _len);
s[ _len] = '}';
s[++_len] = '\0';
2016-12-20 20:19:25 +00:00
break;
case DATABASE_QUERY_ENTRY_MAX_USERS:
s[ _len] = '{';
s[++_len] = '\'';
s[++_len] = 'u';
s[++_len] = 's';
s[++_len] = 'e';
s[++_len] = 'r';
s[++_len] = 's';
s[++_len] = '\'';
s[++_len] = ':';
s[++_len] = '\0';
_len += strlcpy(s + _len, path, len - _len);
s[ _len] = '}';
s[++_len] = '\0';
2016-12-20 20:19:25 +00:00
break;
case DATABASE_QUERY_NONE:
s[ _len] = '{';
s[++_len] = '\'';
s[++_len] = '\'';
s[++_len] = ':';
s[++_len] = '\'';
s[++_len] = ':';
s[++_len] = '"';
s[++_len] = '\0';
_len += strlcpy(s + _len, path, len - _len);
s[ _len] = '"';
s[++_len] = '}';
s[++_len] = '\0';
2016-12-20 20:19:25 +00:00
break;
}
return 0;
}
2015-09-28 15:18:48 +00:00
/*
* NOTE: Allocates memory, it is the caller's responsibility to free the
* memory after it is no longer required.
*/
2015-09-23 13:11:35 +00:00
char *bin_to_hex_alloc(const uint8_t *data, size_t len)
2015-05-24 18:48:49 +00:00
{
size_t i;
char *ret = (char*)malloc(len * 2 + 1);
if (len && !ret)
return NULL;
2015-05-24 18:48:49 +00:00
for (i = 0; i < len; i++)
2015-09-28 15:18:48 +00:00
snprintf(ret+i * 2, 3, "%02X", data[i]);
2015-05-24 18:48:49 +00:00
return ret;
}
static int database_cursor_iterate(libretrodb_cursor_t *cur,
2015-05-24 18:48:49 +00:00
database_info_t *db_info)
{
unsigned i;
struct rmsgpack_dom_value item;
2015-06-12 15:04:25 +00:00
const char* str = NULL;
if (libretrodb_cursor_read_item(cur, &item) != 0)
return -1;
if (item.type != RDT_MAP)
{
rmsgpack_dom_value_free(&item);
return 1;
}
2015-05-24 18:48:49 +00:00
db_info->analog_supported = -1;
db_info->rumble_supported = -1;
db_info->coop_supported = -1;
2015-05-24 18:48:49 +00:00
2015-06-26 18:33:40 +00:00
for (i = 0; i < item.val.map.len; i++)
{
2015-06-26 18:33:40 +00:00
struct rmsgpack_dom_value *key = &item.val.map.items[i].key;
struct rmsgpack_dom_value *val = &item.val.map.items[i].value;
const char *val_string = NULL;
if (!key || !val)
continue;
2018-02-25 16:43:12 +00:00
val_string = val->val.string.buff;
str = key->val.string.buff;
2015-06-03 06:22:50 +00:00
if (string_is_equal(str, "publisher"))
2018-02-25 16:43:12 +00:00
{
if (!string_is_empty(val_string))
db_info->publisher = strdup(val_string);
}
else if (string_is_equal(str, "developer"))
2018-02-25 16:43:12 +00:00
{
if (!string_is_empty(val_string))
db_info->developer = string_split(val_string, "|");
}
else if (string_is_equal(str, "serial"))
2018-02-25 16:43:12 +00:00
{
if (!string_is_empty(val_string))
db_info->serial = strdup(val_string);
}
else if (string_is_equal(str, "rom_name"))
2018-02-25 16:43:12 +00:00
{
if (!string_is_empty(val_string))
db_info->rom_name = strdup(val_string);
}
else if (string_is_equal(str, "name"))
2018-02-25 16:43:12 +00:00
{
if (!string_is_empty(val_string))
db_info->name = strdup(val_string);
}
else if (string_is_equal(str, "description"))
2018-02-25 16:43:12 +00:00
{
if (!string_is_empty(val_string))
db_info->description = strdup(val_string);
}
else if (string_is_equal(str, "genre"))
2018-02-25 16:43:12 +00:00
{
if (!string_is_empty(val_string))
db_info->genre = strdup(val_string);
}
else if (string_is_equal(str, "category"))
{
if (!string_is_empty(val_string))
db_info->category = strdup(val_string);
}
else if (string_is_equal(str, "language"))
{
if (!string_is_empty(val_string))
db_info->language = strdup(val_string);
}
else if (string_is_equal(str, "region"))
{
if (!string_is_empty(val_string))
db_info->region = strdup(val_string);
}
else if (string_is_equal(str, "score"))
{
if (!string_is_empty(val_string))
db_info->score = strdup(val_string);
}
else if (string_is_equal(str, "media"))
{
if (!string_is_empty(val_string))
db_info->media = strdup(val_string);
}
else if (string_is_equal(str, "controls"))
{
if (!string_is_empty(val_string))
db_info->controls = strdup(val_string);
}
else if (string_is_equal(str, "artstyle"))
{
if (!string_is_empty(val_string))
db_info->artstyle = strdup(val_string);
}
else if (string_is_equal(str, "gameplay"))
{
if (!string_is_empty(val_string))
db_info->gameplay = strdup(val_string);
}
else if (string_is_equal(str, "narrative"))
{
if (!string_is_empty(val_string))
db_info->narrative = strdup(val_string);
}
else if (string_is_equal(str, "pacing"))
{
if (!string_is_empty(val_string))
db_info->pacing = strdup(val_string);
}
else if (string_is_equal(str, "perspective"))
{
if (!string_is_empty(val_string))
db_info->perspective = strdup(val_string);
}
else if (string_is_equal(str, "setting"))
{
if (!string_is_empty(val_string))
db_info->setting = strdup(val_string);
}
else if (string_is_equal(str, "visual"))
{
if (!string_is_empty(val_string))
db_info->visual = strdup(val_string);
}
else if (string_is_equal(str, "vehicular"))
{
if (!string_is_empty(val_string))
db_info->vehicular = strdup(val_string);
}
else if (string_is_equal(str, "origin"))
2018-02-25 16:43:12 +00:00
{
if (!string_is_empty(val_string))
db_info->origin = strdup(val_string);
}
else if (string_is_equal(str, "franchise"))
2018-02-25 16:43:12 +00:00
{
if (!string_is_empty(val_string))
db_info->franchise = strdup(val_string);
}
2020-06-25 12:38:06 +00:00
else if (string_ends_with_size(str, "_rating",
strlen(str), STRLEN_CONST("_rating")))
2018-02-25 16:43:12 +00:00
{
2020-06-08 13:27:08 +00:00
if (string_is_equal(str, "bbfc_rating"))
{
if (!string_is_empty(val_string))
db_info->bbfc_rating = strdup(val_string);
}
else if (string_is_equal(str, "esrb_rating"))
{
if (!string_is_empty(val_string))
db_info->esrb_rating = strdup(val_string);
}
else if (string_is_equal(str, "elspa_rating"))
{
if (!string_is_empty(val_string))
db_info->elspa_rating = strdup(val_string);
}
else if (string_is_equal(str, "cero_rating"))
{
if (!string_is_empty(val_string))
db_info->cero_rating = strdup(val_string);
}
else if (string_is_equal(str, "pegi_rating"))
{
if (!string_is_empty(val_string))
db_info->pegi_rating = strdup(val_string);
}
else if (string_is_equal(str, "edge_rating"))
db_info->edge_magazine_rating = (unsigned)val->val.uint_;
else if (string_is_equal(str, "famitsu_rating"))
db_info->famitsu_magazine_rating = (unsigned)val->val.uint_;
else if (string_is_equal(str, "tgdb_rating"))
db_info->tgdb_rating = (unsigned)val->val.uint_;
2018-02-25 16:43:12 +00:00
}
else if (string_is_equal(str, "enhancement_hw"))
2018-02-25 16:43:12 +00:00
{
if (!string_is_empty(val_string))
db_info->enhancement_hw = strdup(val_string);
}
else if (string_is_equal(str, "edge_review"))
2018-02-25 16:43:12 +00:00
{
if (!string_is_empty(val_string))
db_info->edge_magazine_review = strdup(val_string);
}
else if (string_is_equal(str, "edge_issue"))
2018-02-25 16:43:12 +00:00
db_info->edge_magazine_issue = (unsigned)val->val.uint_;
else if (string_is_equal(str, "users"))
2018-02-25 16:43:12 +00:00
db_info->max_users = (unsigned)val->val.uint_;
else if (string_is_equal(str, "releasemonth"))
2018-02-25 16:43:12 +00:00
db_info->releasemonth = (unsigned)val->val.uint_;
else if (string_is_equal(str, "releaseyear"))
2018-02-25 16:43:12 +00:00
db_info->releaseyear = (unsigned)val->val.uint_;
else if (string_is_equal(str, "rumble"))
2018-02-25 16:43:12 +00:00
db_info->rumble_supported = (int)val->val.uint_;
else if (string_is_equal(str, "achievements"))
db_info->achievements = (int)val->val.uint_;
else if (string_is_equal(str, "console_exclusive"))
db_info->console_exclusive = (int)val->val.uint_;
else if (string_is_equal(str, "platform_exclusive"))
db_info->platform_exclusive = (int)val->val.uint_;
else if (string_is_equal(str, "coop"))
2018-02-25 16:43:12 +00:00
db_info->coop_supported = (int)val->val.uint_;
else if (string_is_equal(str, "analog"))
2018-02-25 16:43:12 +00:00
db_info->analog_supported = (int)val->val.uint_;
else if (string_is_equal(str, "size"))
2018-02-25 16:43:12 +00:00
db_info->size = (unsigned)val->val.uint_;
else if (string_is_equal(str, "crc"))
{
switch (val->val.binary.len)
{
case 1:
db_info->crc32 = *(uint8_t*)val->val.binary.buff;
break;
case 2:
db_info->crc32 = swap_if_little16(*(uint16_t*)val->val.binary.buff);
break;
case 4:
db_info->crc32 = swap_if_little32(*(uint32_t*)val->val.binary.buff);
break;
default:
db_info->crc32 = 0;
break;
}
}
else if (string_is_equal(str, "sha1"))
2018-02-25 16:43:12 +00:00
db_info->sha1 = bin_to_hex_alloc(
(uint8_t*)val->val.binary.buff, val->val.binary.len);
else if (string_is_equal(str, "md5"))
2018-02-25 16:43:12 +00:00
db_info->md5 = bin_to_hex_alloc(
(uint8_t*)val->val.binary.buff, val->val.binary.len);
}
rmsgpack_dom_value_free(&item);
2015-05-24 18:48:49 +00:00
return 0;
}
static int database_cursor_open(libretrodb_t *db,
2015-05-25 03:09:18 +00:00
libretrodb_cursor_t *cur, const char *path, const char *query)
2015-01-27 17:09:19 +00:00
{
const char *error = NULL;
libretrodb_query_t *q = NULL;
if ((libretrodb_open(path, db, false)) != 0)
2015-05-25 03:09:18 +00:00
return -1;
if (query)
q = (libretrodb_query_t*)libretrodb_query_compile(db, query,
2015-01-27 17:09:19 +00:00
strlen(query), &error);
2015-01-27 17:09:19 +00:00
if (error)
goto error;
2015-01-27 17:09:19 +00:00
if ((libretrodb_cursor_open(db, cur, q)) != 0)
goto error;
2015-01-27 17:09:19 +00:00
if (q)
libretrodb_query_free(q);
2015-01-27 17:09:19 +00:00
return 0;
error:
if (q)
libretrodb_query_free(q);
libretrodb_close(db);
return -1;
2015-01-27 17:09:19 +00:00
}
2018-02-25 16:47:36 +00:00
static bool type_is_prioritized(const char *path)
{
2018-02-25 16:47:36 +00:00
const char *ext = path_get_extension(path);
if (string_is_equal_noncase(ext, "cue"))
return true;
if (string_is_equal_noncase(ext, "gdi"))
return true;
return false;
}
static int dir_entry_compare(const void *left, const void *right)
{
2017-09-19 19:29:28 +00:00
const struct string_list_elem *le = (const struct string_list_elem*)left;
const struct string_list_elem *re = (const struct string_list_elem*)right;
2018-02-25 16:47:36 +00:00
bool l = type_is_prioritized(le->data);
bool r = type_is_prioritized(re->data);
return (int) r - (int) l;
}
database_info_handle_t *database_info_dir_init(const char *dir,
enum database_type type, retro_task_t *task,
bool show_hidden_files)
{
core_info_list_t *core_info_list = NULL;
struct string_list *list = NULL;
database_info_handle_t *db = (database_info_handle_t*)
2020-06-28 14:11:17 +00:00
malloc(sizeof(*db));
2015-04-14 07:27:55 +00:00
if (!db)
return NULL;
core_info_get_list(&core_info_list);
if (!(list = dir_list_new(dir, core_info_list ? core_info_list->all_ext : NULL,
false, show_hidden_files,
false, true)))
2018-02-25 20:58:52 +00:00
{
free(db);
return NULL;
}
2023-02-17 16:49:36 +00:00
/* dir list prioritize */
qsort(list->elems, list->size, sizeof(*list->elems), dir_entry_compare);
2020-06-28 14:11:17 +00:00
db->status = DATABASE_STATUS_ITERATE;
db->type = type;
db->list_ptr = 0;
db->list = list;
2015-06-09 17:01:24 +00:00
return db;
}
database_info_handle_t *database_info_file_init(const char *path,
enum database_type type, retro_task_t *task)
2015-06-09 17:01:24 +00:00
{
2017-04-20 07:21:08 +00:00
union string_list_elem_attr attr;
2018-02-25 20:58:52 +00:00
struct string_list *list = NULL;
2015-06-09 17:01:24 +00:00
database_info_handle_t *db = (database_info_handle_t*)
2020-06-28 14:11:17 +00:00
malloc(sizeof(*db));
2015-06-09 17:01:24 +00:00
if (!db)
return NULL;
if (!(list = string_list_new()))
2018-02-25 20:58:52 +00:00
{
free(db);
return NULL;
}
2015-06-09 17:01:24 +00:00
attr.i = 0;
2018-02-25 20:58:52 +00:00
string_list_append(list, path, attr);
2015-06-09 17:01:24 +00:00
2020-06-28 14:11:17 +00:00
db->status = DATABASE_STATUS_ITERATE;
db->type = type;
db->list_ptr = 0;
db->list = list;
2015-04-14 07:27:55 +00:00
return db;
}
2015-04-14 07:27:55 +00:00
void database_info_free(database_info_handle_t *db)
{
if (db)
string_list_free(db->list);
}
2015-06-03 06:22:50 +00:00
database_info_list_t *database_info_list_new(
const char *rdb_path, const char *query)
2015-01-27 17:09:19 +00:00
{
2015-06-03 06:22:50 +00:00
int ret = 0;
2015-03-22 07:04:35 +00:00
unsigned k = 0;
database_info_t *database_info = NULL;
2015-01-27 17:09:19 +00:00
database_info_list_t *database_info_list = NULL;
2015-09-17 07:46:26 +00:00
libretrodb_t *db = libretrodb_new();
libretrodb_cursor_t *cur = libretrodb_cursor_new();
2015-01-27 17:09:19 +00:00
2015-09-17 07:46:26 +00:00
if (!db || !cur)
goto end;
if ((database_cursor_open(db, cur, rdb_path, query) != 0))
goto end;
2015-01-27 17:09:19 +00:00
2015-06-03 06:22:50 +00:00
database_info_list = (database_info_list_t*)
2017-09-29 03:01:00 +00:00
malloc(sizeof(*database_info_list));
2015-06-03 06:22:50 +00:00
2015-01-27 17:09:19 +00:00
if (!database_info_list)
goto end;
2015-01-27 17:09:19 +00:00
2017-09-29 03:01:00 +00:00
database_info_list->count = 0;
database_info_list->list = NULL;
2015-05-24 18:48:49 +00:00
while (ret != -1)
2015-01-27 17:09:19 +00:00
{
2015-05-24 18:48:49 +00:00
database_info_t db_info = {0};
2015-09-17 07:46:26 +00:00
ret = database_cursor_iterate(cur, &db_info);
2015-05-24 18:30:05 +00:00
2015-05-24 18:48:49 +00:00
if (ret == 0)
2015-01-27 17:09:19 +00:00
{
2015-09-28 14:57:07 +00:00
database_info_t *db_ptr = NULL;
database_info_t *new_ptr = (database_info_t*)
2015-06-03 06:22:50 +00:00
realloc(database_info, (k+1) * sizeof(database_info_t));
2015-01-27 18:20:39 +00:00
2015-09-28 14:57:07 +00:00
if (!new_ptr)
{
2018-08-18 05:15:50 +00:00
if (db_info.bbfc_rating)
free(db_info.bbfc_rating);
if (db_info.cero_rating)
free(db_info.cero_rating);
if (db_info.description)
free(db_info.description);
if (db_info.edge_magazine_review)
free(db_info.edge_magazine_review);
if (db_info.elspa_rating)
free(db_info.elspa_rating);
if (db_info.enhancement_hw)
free(db_info.enhancement_hw);
if (db_info.esrb_rating)
free(db_info.esrb_rating);
if (db_info.franchise)
free(db_info.franchise);
if (db_info.genre)
free(db_info.genre);
if (db_info.category)
free(db_info.category);
if (db_info.language)
free(db_info.language);
if (db_info.region)
free(db_info.region);
if (db_info.score)
free(db_info.score);
if (db_info.media)
free(db_info.media);
if (db_info.controls)
free(db_info.controls);
if (db_info.artstyle)
free(db_info.artstyle);
if (db_info.gameplay)
free(db_info.gameplay);
if (db_info.narrative)
free(db_info.narrative);
if (db_info.pacing)
free(db_info.pacing);
if (db_info.perspective)
free(db_info.perspective);
if (db_info.setting)
free(db_info.setting);
if (db_info.visual)
free(db_info.visual);
if (db_info.vehicular)
free(db_info.vehicular);
2018-08-18 05:15:50 +00:00
if (db_info.name)
free(db_info.name);
if (db_info.origin)
free(db_info.origin);
if (db_info.pegi_rating)
free(db_info.pegi_rating);
if (db_info.publisher)
free(db_info.publisher);
if (db_info.rom_name)
free(db_info.rom_name);
if (db_info.serial)
free(db_info.serial);
if (db_info.md5)
free(db_info.md5);
if (db_info.sha1)
free(db_info.sha1);
db_info.name = NULL;
db_info.rom_name = NULL;
db_info.serial = NULL;
db_info.genre = NULL;
db_info.description = NULL;
db_info.publisher = NULL;
db_info.developer = NULL;
db_info.origin = NULL;
db_info.franchise = NULL;
db_info.edge_magazine_review = NULL;
db_info.cero_rating = NULL;
db_info.pegi_rating = NULL;
db_info.enhancement_hw = NULL;
db_info.elspa_rating = NULL;
db_info.esrb_rating = NULL;
db_info.bbfc_rating = NULL;
db_info.sha1 = NULL;
db_info.md5 = NULL;
database_info_list_free(database_info_list);
2016-09-25 04:14:23 +00:00
free(database_info);
free(database_info_list);
database_info_list = NULL;
goto end;
}
2015-01-27 18:20:39 +00:00
2015-09-28 14:57:07 +00:00
database_info = new_ptr;
db_ptr = &database_info[k];
2015-01-27 18:20:39 +00:00
2015-05-24 18:48:49 +00:00
memcpy(db_ptr, &db_info, sizeof(*db_ptr));
2015-01-27 18:20:39 +00:00
2015-05-24 18:48:49 +00:00
k++;
2015-01-27 17:09:19 +00:00
}
}
2015-01-27 17:09:19 +00:00
database_info_list->list = database_info;
2015-02-10 21:39:42 +00:00
database_info_list->count = k;
2015-01-27 17:09:19 +00:00
end:
2015-09-17 07:46:26 +00:00
if (db)
{
libretrodb_cursor_close(cur);
libretrodb_close(db);
2015-09-17 07:46:26 +00:00
libretrodb_free(db);
}
2015-09-17 07:46:26 +00:00
if (cur)
libretrodb_cursor_free(cur);
return database_info_list;
2015-01-27 17:09:19 +00:00
}
void database_info_list_free(database_info_list_t *database_info_list)
{
2015-01-27 22:36:20 +00:00
size_t i;
2015-01-27 17:09:19 +00:00
if (!database_info_list)
return;
for (i = 0; i < database_info_list->count; i++)
{
2015-02-10 21:19:20 +00:00
database_info_t *info = &database_info_list->list[i];
2015-01-27 17:09:19 +00:00
if (info->name)
free(info->name);
if (info->rom_name)
free(info->rom_name);
if (info->serial)
free(info->serial);
2016-05-23 22:33:12 +00:00
if (info->genre)
free(info->genre);
if (info->category)
free(info->category);
if (info->language)
free(info->language);
if (info->region)
free(info->region);
if (info->score)
free(info->score);
if (info->media)
free(info->media);
if (info->controls)
free(info->controls);
if (info->artstyle)
free(info->artstyle);
if (info->gameplay)
free(info->gameplay);
if (info->narrative)
free(info->narrative);
if (info->pacing)
free(info->pacing);
if (info->perspective)
free(info->perspective);
if (info->setting)
free(info->setting);
if (info->visual)
free(info->visual);
if (info->vehicular)
free(info->vehicular);
2015-01-27 17:09:19 +00:00
if (info->description)
free(info->description);
if (info->publisher)
free(info->publisher);
if (info->developer)
string_list_free(info->developer);
2015-01-27 17:09:19 +00:00
if (info->origin)
free(info->origin);
if (info->franchise)
free(info->franchise);
2015-01-27 18:20:39 +00:00
if (info->edge_magazine_review)
free(info->edge_magazine_review);
if (info->cero_rating)
free(info->cero_rating);
if (info->pegi_rating)
free(info->pegi_rating);
2015-01-28 10:08:44 +00:00
if (info->enhancement_hw)
free(info->enhancement_hw);
2015-01-27 18:20:39 +00:00
if (info->elspa_rating)
free(info->elspa_rating);
if (info->esrb_rating)
free(info->esrb_rating);
if (info->bbfc_rating)
free(info->bbfc_rating);
2015-02-02 03:03:46 +00:00
if (info->sha1)
free(info->sha1);
if (info->md5)
free(info->md5);
info->name = NULL;
info->rom_name = NULL;
info->serial = NULL;
info->genre = NULL;
info->description = NULL;
info->publisher = NULL;
info->developer = NULL;
info->origin = NULL;
info->franchise = NULL;
info->edge_magazine_review = NULL;
info->cero_rating = NULL;
info->pegi_rating = NULL;
info->enhancement_hw = NULL;
info->elspa_rating = NULL;
info->esrb_rating = NULL;
info->bbfc_rating = NULL;
info->sha1 = NULL;
info->md5 = NULL;
2015-01-27 17:09:19 +00:00
}
free(database_info_list->list);
}