Refactor database_cursor_iterate

This commit is contained in:
twinaphex 2015-05-24 20:48:49 +02:00
parent bcf389edab
commit 39528bc74e
3 changed files with 109 additions and 111 deletions

View File

@ -103,85 +103,6 @@ int database_info_build_query(
return 0;
}
int database_cursor_iterate(libretrodb_cursor_t *cur, char *s, size_t len)
{
unsigned i;
struct rmsgpack_dom_value item;
if (libretrodb_cursor_read_item(cur, &item) != 0)
return -1;
if (item.type != RDT_MAP)
return 1;
for (i = 0; i < item.map.len; i++)
{
struct rmsgpack_dom_value *key = &item.map.items[i].key;
struct rmsgpack_dom_value *val = &item.map.items[i].value;
if (!key || !val)
continue;
if (!strcmp(key->string.buff, "name"))
{
strlcpy(s, val->string.buff, len);
return 0;
}
}
return 1;
}
int database_cursor_open(libretrodb_t *db,
libretrodb_cursor_t *cur, const char *query)
{
const char *error = NULL;
libretrodb_query_t *q = NULL;
if (query)
q = (libretrodb_query_t*)libretrodb_query_compile(db, query,
strlen(query), &error);
if (error)
return -1;
if ((libretrodb_cursor_open(db, cur, q)) != 0)
return -1;
return 0;
}
database_info_handle_t *database_info_init(const char *dir, enum database_type type)
{
database_info_handle_t *db = (database_info_handle_t*)calloc(1, sizeof(*db));
if (!db)
return NULL;
db->list = dir_list_new_special(dir, DIR_LIST_CORE_INFO);
if (!db->list)
goto error;
db->list_ptr = 0;
db->status = DATABASE_STATUS_ITERATE;
db->type = type;
return db;
error:
if (db)
free(db);
return NULL;
}
void database_info_free(database_info_handle_t *db)
{
if (!db)
return;
string_list_free(db->list);
}
static char *bin_to_hex_alloc(const uint8_t *data, size_t len)
{
size_t i;
@ -195,50 +116,28 @@ static char *bin_to_hex_alloc(const uint8_t *data, size_t len)
return ret;
}
database_info_list_t *database_info_list_new(const char *rdb_path, const char *query)
int database_cursor_iterate(libretrodb_cursor_t *cur,
database_info_t *db_info)
{
libretrodb_t db;
libretrodb_cursor_t cur;
unsigned i;
struct rmsgpack_dom_value item;
size_t j;
unsigned k = 0;
database_info_t *database_info = NULL;
database_info_list_t *database_info_list = NULL;
if ((libretrodb_open(rdb_path, &db)) != 0)
return NULL;
if ((database_cursor_open(&db, &cur, query) != 0))
return NULL;
if (libretrodb_cursor_read_item(cur, &item) != 0)
return -1;
database_info_list = (database_info_list_t*)calloc(1, sizeof(*database_info_list));
if (!database_info_list)
goto error;
while (libretrodb_cursor_read_item(&cur, &item) == 0)
{
database_info_t *db_info = NULL;
if (item.type != RDT_MAP)
continue;
database_info = (database_info_t*)realloc(database_info, (k+1) * sizeof(database_info_t));
if (!database_info)
goto error;
db_info = &database_info[k];
if (!db_info)
continue;
memset(db_info, 0, sizeof(*db_info));
return 1;
db_info->analog_supported = -1;
db_info->rumble_supported = -1;
for (j = 0; j < item.map.len; j++)
for (i = 0; i < item.map.len; i++)
{
struct rmsgpack_dom_value *key = &item.map.items[j].key;
struct rmsgpack_dom_value *val = &item.map.items[j].value;
struct rmsgpack_dom_value *key = &item.map.items[i].key;
struct rmsgpack_dom_value *val = &item.map.items[i].value;
if (!key || !val)
continue;
if (!strcmp(key->string.buff, "name"))
db_info->name = strdup(val->string.buff);
@ -310,8 +209,104 @@ database_info_list_t *database_info_list_new(const char *rdb_path, const char *q
if (!strcmp(key->string.buff, "md5"))
db_info->md5 = bin_to_hex_alloc((uint8_t*)val->binary.buff, val->binary.len);
}
return 0;
}
int database_cursor_open(libretrodb_t *db,
libretrodb_cursor_t *cur, const char *query)
{
const char *error = NULL;
libretrodb_query_t *q = NULL;
if (query)
q = (libretrodb_query_t*)libretrodb_query_compile(db, query,
strlen(query), &error);
if (error)
return -1;
if ((libretrodb_cursor_open(db, cur, q)) != 0)
return -1;
return 0;
}
database_info_handle_t *database_info_init(const char *dir, enum database_type type)
{
database_info_handle_t *db = (database_info_handle_t*)calloc(1, sizeof(*db));
if (!db)
return NULL;
db->list = dir_list_new_special(dir, DIR_LIST_CORE_INFO);
if (!db->list)
goto error;
db->list_ptr = 0;
db->status = DATABASE_STATUS_ITERATE;
db->type = type;
return db;
error:
if (db)
free(db);
return NULL;
}
void database_info_free(database_info_handle_t *db)
{
if (!db)
return;
string_list_free(db->list);
}
database_info_list_t *database_info_list_new(const char *rdb_path, const char *query)
{
int ret = 0;
libretrodb_t db;
libretrodb_cursor_t cur;
struct rmsgpack_dom_value item;
size_t j;
unsigned k = 0;
database_info_t *database_info = NULL;
database_info_list_t *database_info_list = NULL;
if ((libretrodb_open(rdb_path, &db)) != 0)
return NULL;
if ((database_cursor_open(&db, &cur, query) != 0))
return NULL;
database_info_list = (database_info_list_t*)calloc(1, sizeof(*database_info_list));
if (!database_info_list)
goto error;
while (ret != -1)
{
database_info_t db_info = {0};
ret = database_cursor_iterate(&cur, &db_info);
if (ret == 0)
{
database_info_t *db_ptr = NULL;
database_info = (database_info_t*)realloc(database_info, (k+1) * sizeof(database_info_t));
if (!database_info)
goto error;
db_ptr = &database_info[k];
if (!db_ptr)
continue;
memcpy(db_ptr, &db_info, sizeof(*db_ptr));
k++;
}
}
database_info_list->list = database_info;
database_info_list->count = k;

View File

@ -94,7 +94,7 @@ void database_info_list_free(database_info_list_t *list);
int database_cursor_open(libretrodb_t *db,
libretrodb_cursor_t *cur, const char *query);
int database_cursor_iterate(libretrodb_cursor_t *cur, char *s, size_t len);
int database_cursor_iterate(libretrodb_cursor_t *cur, database_info_t *db_info);
database_info_handle_t *database_info_init(const char *dir,
enum database_type type);

View File

@ -1159,11 +1159,14 @@ static int menu_database_parse_query(file_list_t *list, const char *path,
return -1;
while (ret != -1)
{
char path[PATH_MAX_LENGTH];
ret = database_cursor_iterate(&cur, path, sizeof(path));
database_info_t dbinfo = {0};
ret = database_cursor_iterate(&cur, &dbinfo);
if (ret == 0)
menu_list_push(list, path, db.path, MENU_FILE_RDB_ENTRY, 0);
{
if (dbinfo.name && dbinfo.name[0] != '\0')
menu_list_push(list, dbinfo.name, db.path, MENU_FILE_RDB_ENTRY, 0);
}
}
libretrodb_cursor_close(&cur);