(libretro-db) Small cleanups
This commit is contained in:
parent
72afa2f932
commit
77cf0ad524
|
@ -46,11 +46,6 @@ struct bintree
|
||||||
/* TODO/FIXME - static global variable */
|
/* TODO/FIXME - static global variable */
|
||||||
static void *NIL_NODE = &NIL_NODE;
|
static void *NIL_NODE = &NIL_NODE;
|
||||||
|
|
||||||
static INLINE int bintree_is_nil(const struct bintree_node *node)
|
|
||||||
{
|
|
||||||
return !node || (node->value == NIL_NODE);
|
|
||||||
}
|
|
||||||
|
|
||||||
static struct bintree_node *bintree_new_nil_node(
|
static struct bintree_node *bintree_new_nil_node(
|
||||||
struct bintree_node *parent)
|
struct bintree_node *parent)
|
||||||
{
|
{
|
||||||
|
@ -73,7 +68,7 @@ static int bintree_insert_internal(bintree_t *t,
|
||||||
{
|
{
|
||||||
int cmp_res = 0;
|
int cmp_res = 0;
|
||||||
|
|
||||||
if (bintree_is_nil(root))
|
if (!root || (root->value == NIL_NODE))
|
||||||
{
|
{
|
||||||
root->left = bintree_new_nil_node(root);
|
root->left = bintree_new_nil_node(root);
|
||||||
root->right = bintree_new_nil_node(root);
|
root->right = bintree_new_nil_node(root);
|
||||||
|
@ -96,7 +91,7 @@ static int bintree_iterate_internal(struct bintree_node *n,
|
||||||
{
|
{
|
||||||
int rv;
|
int rv;
|
||||||
|
|
||||||
if (bintree_is_nil(n))
|
if (!n || (n->value == NIL_NODE))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if ((rv = bintree_iterate_internal(n->left, cb, ctx)) != 0)
|
if ((rv = bintree_iterate_internal(n->left, cb, ctx)) != 0)
|
||||||
|
|
|
@ -103,7 +103,6 @@ static int libretrodb_write_metadata(RFILE *fd, libretrodb_metadata_t *md)
|
||||||
static int libretrodb_validate_document(const struct rmsgpack_dom_value *doc)
|
static int libretrodb_validate_document(const struct rmsgpack_dom_value *doc)
|
||||||
{
|
{
|
||||||
unsigned i;
|
unsigned i;
|
||||||
struct rmsgpack_dom_value key, value;
|
|
||||||
int rv = 0;
|
int rv = 0;
|
||||||
|
|
||||||
if (doc->type != RDT_MAP)
|
if (doc->type != RDT_MAP)
|
||||||
|
@ -111,8 +110,8 @@ static int libretrodb_validate_document(const struct rmsgpack_dom_value *doc)
|
||||||
|
|
||||||
for (i = 0; i < doc->val.map.len; i++)
|
for (i = 0; i < doc->val.map.len; i++)
|
||||||
{
|
{
|
||||||
key = doc->val.map.items[i].key;
|
struct rmsgpack_dom_value key = doc->val.map.items[i].key;
|
||||||
value = doc->val.map.items[i].value;
|
struct rmsgpack_dom_value value = doc->val.map.items[i].value;
|
||||||
|
|
||||||
if (key.type != RDT_STRING)
|
if (key.type != RDT_STRING)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
@ -324,7 +323,7 @@ int libretrodb_find_entry(libretrodb_t *db, const char *index_name,
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
bufflen = idx.next;
|
bufflen = idx.next;
|
||||||
buff = malloc(bufflen);
|
buff = malloc(bufflen);
|
||||||
|
|
||||||
if (!buff)
|
if (!buff)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
@ -332,7 +331,7 @@ int libretrodb_find_entry(libretrodb_t *db, const char *index_name,
|
||||||
while (nread < bufflen)
|
while (nread < bufflen)
|
||||||
{
|
{
|
||||||
void *buff_ = (uint64_t *)buff + nread;
|
void *buff_ = (uint64_t *)buff + nread;
|
||||||
rv = (int)filestream_read(db->fd, buff_, bufflen - nread);
|
rv = (int)filestream_read(db->fd, buff_, bufflen - nread);
|
||||||
|
|
||||||
if (rv <= 0)
|
if (rv <= 0)
|
||||||
{
|
{
|
||||||
|
@ -433,7 +432,8 @@ void libretrodb_cursor_close(libretrodb_cursor_t *cursor)
|
||||||
*
|
*
|
||||||
* Returns: 0 if successful, otherwise negative.
|
* Returns: 0 if successful, otherwise negative.
|
||||||
**/
|
**/
|
||||||
int libretrodb_cursor_open(libretrodb_t *db, libretrodb_cursor_t *cursor,
|
int libretrodb_cursor_open(libretrodb_t *db,
|
||||||
|
libretrodb_cursor_t *cursor,
|
||||||
libretrodb_query_t *q)
|
libretrodb_query_t *q)
|
||||||
{
|
{
|
||||||
RFILE *fd = NULL;
|
RFILE *fd = NULL;
|
||||||
|
|
|
@ -136,9 +136,10 @@ static struct rmsgpack_dom_value func_equals(
|
||||||
res.val.bool_ = 0;
|
res.val.bool_ = 0;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (input.type == RDT_UINT && arg.a.value.type == RDT_INT)
|
if ( input.type == RDT_UINT &&
|
||||||
|
arg.a.value.type == RDT_INT)
|
||||||
{
|
{
|
||||||
arg.a.value.type = RDT_UINT;
|
arg.a.value.type = RDT_UINT;
|
||||||
arg.a.value.val.uint_ = arg.a.value.val.int_;
|
arg.a.value.val.uint_ = arg.a.value.val.int_;
|
||||||
}
|
}
|
||||||
res.val.bool_ = (rmsgpack_dom_value_cmp(&input, &arg.a.value) == 0);
|
res.val.bool_ = (rmsgpack_dom_value_cmp(&input, &arg.a.value) == 0);
|
||||||
|
@ -163,13 +164,11 @@ static struct rmsgpack_dom_value query_func_operator_or(
|
||||||
if (argv[i].type == AT_VALUE)
|
if (argv[i].type == AT_VALUE)
|
||||||
res = func_equals(input, 1, &argv[i]);
|
res = func_equals(input, 1, &argv[i]);
|
||||||
else
|
else
|
||||||
{
|
|
||||||
res = query_func_is_true(
|
res = query_func_is_true(
|
||||||
argv[i].a.invocation.func(input,
|
argv[i].a.invocation.func(input,
|
||||||
argv[i].a.invocation.argc,
|
argv[i].a.invocation.argc,
|
||||||
argv[i].a.invocation.argv
|
argv[i].a.invocation.argv
|
||||||
), 0, NULL);
|
), 0, NULL);
|
||||||
}
|
|
||||||
|
|
||||||
if (res.val.bool_)
|
if (res.val.bool_)
|
||||||
return res;
|
return res;
|
||||||
|
@ -193,14 +192,12 @@ static struct rmsgpack_dom_value query_func_operator_and(
|
||||||
if (argv[i].type == AT_VALUE)
|
if (argv[i].type == AT_VALUE)
|
||||||
res = func_equals(input, 1, &argv[i]);
|
res = func_equals(input, 1, &argv[i]);
|
||||||
else
|
else
|
||||||
{
|
|
||||||
res = query_func_is_true(
|
res = query_func_is_true(
|
||||||
argv[i].a.invocation.func(input,
|
argv[i].a.invocation.func(input,
|
||||||
argv[i].a.invocation.argc,
|
argv[i].a.invocation.argc,
|
||||||
argv[i].a.invocation.argv
|
argv[i].a.invocation.argv
|
||||||
),
|
),
|
||||||
0, NULL);
|
0, NULL);
|
||||||
}
|
|
||||||
|
|
||||||
if (!res.val.bool_)
|
if (!res.val.bool_)
|
||||||
return res;
|
return res;
|
||||||
|
@ -215,16 +212,16 @@ static struct rmsgpack_dom_value query_func_between(
|
||||||
struct rmsgpack_dom_value res;
|
struct rmsgpack_dom_value res;
|
||||||
unsigned i = 0;
|
unsigned i = 0;
|
||||||
|
|
||||||
res.type = RDT_BOOL;
|
res.type = RDT_BOOL;
|
||||||
res.val.bool_ = 0;
|
res.val.bool_ = 0;
|
||||||
|
|
||||||
(void)i;
|
|
||||||
|
|
||||||
if (argc != 2)
|
if (argc != 2)
|
||||||
return res;
|
return res;
|
||||||
if (argv[0].type != AT_VALUE || argv[1].type != AT_VALUE)
|
if ( argv[0].type != AT_VALUE
|
||||||
|
|| argv[1].type != AT_VALUE)
|
||||||
return res;
|
return res;
|
||||||
if (argv[0].a.value.type != RDT_INT || argv[1].a.value.type != RDT_INT)
|
if ( argv[0].a.value.type != RDT_INT
|
||||||
|
|| argv[1].a.value.type != RDT_INT)
|
||||||
return res;
|
return res;
|
||||||
|
|
||||||
switch (input.type)
|
switch (input.type)
|
||||||
|
@ -240,7 +237,7 @@ static struct rmsgpack_dom_value query_func_between(
|
||||||
&& (input.val.int_ <= argv[1].a.value.val.int_));
|
&& (input.val.int_ <= argv[1].a.value.val.int_));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return res;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
|
@ -251,7 +248,7 @@ static struct rmsgpack_dom_value query_func_glob(
|
||||||
unsigned argc, const struct argument * argv)
|
unsigned argc, const struct argument * argv)
|
||||||
{
|
{
|
||||||
struct rmsgpack_dom_value res;
|
struct rmsgpack_dom_value res;
|
||||||
unsigned i = 0;
|
unsigned i = 0;
|
||||||
|
|
||||||
res.type = RDT_BOOL;
|
res.type = RDT_BOOL;
|
||||||
res.val.bool_ = 0;
|
res.val.bool_ = 0;
|
||||||
|
|
|
@ -102,12 +102,12 @@ static int dom_read_uint(uint64_t value, void *data)
|
||||||
static int dom_read_string(char *value, uint32_t len, void *data)
|
static int dom_read_string(char *value, uint32_t len, void *data)
|
||||||
{
|
{
|
||||||
struct dom_reader_state *dom_state = (struct dom_reader_state *)data;
|
struct dom_reader_state *dom_state = (struct dom_reader_state *)data;
|
||||||
struct rmsgpack_dom_value *v =
|
struct rmsgpack_dom_value *v =
|
||||||
(struct rmsgpack_dom_value*)dom_reader_state_pop(dom_state);
|
(struct rmsgpack_dom_value*)dom_reader_state_pop(dom_state);
|
||||||
|
|
||||||
v->type = RDT_STRING;
|
v->type = RDT_STRING;
|
||||||
v->val.string.len = len;
|
v->val.string.len = len;
|
||||||
v->val.string.buff = value;
|
v->val.string.buff = value;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -181,17 +181,6 @@ static int dom_read_array_start(uint32_t len, void *data)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct rmsgpack_read_callbacks dom_reader_callbacks = {
|
|
||||||
dom_read_nil,
|
|
||||||
dom_read_bool,
|
|
||||||
dom_read_int,
|
|
||||||
dom_read_uint,
|
|
||||||
dom_read_string,
|
|
||||||
dom_read_bin,
|
|
||||||
dom_read_map_start,
|
|
||||||
dom_read_array_start
|
|
||||||
};
|
|
||||||
|
|
||||||
void rmsgpack_dom_value_free(struct rmsgpack_dom_value *v)
|
void rmsgpack_dom_value_free(struct rmsgpack_dom_value *v)
|
||||||
{
|
{
|
||||||
unsigned i;
|
unsigned i;
|
||||||
|
@ -247,12 +236,10 @@ int rmsgpack_dom_value_cmp(
|
||||||
const struct rmsgpack_dom_value *b
|
const struct rmsgpack_dom_value *b
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
int rv;
|
|
||||||
unsigned i;
|
unsigned i;
|
||||||
|
|
||||||
if (a == b)
|
if (a == b)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
if (a->type != b->type)
|
if (a->type != b->type)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
@ -281,6 +268,7 @@ int rmsgpack_dom_value_cmp(
|
||||||
return 1;
|
return 1;
|
||||||
for (i = 0; i < a->val.map.len; i++)
|
for (i = 0; i < a->val.map.len; i++)
|
||||||
{
|
{
|
||||||
|
int rv;
|
||||||
if ((rv = rmsgpack_dom_value_cmp(&a->val.map.items[i].key,
|
if ((rv = rmsgpack_dom_value_cmp(&a->val.map.items[i].key,
|
||||||
&b->val.map.items[i].key)) != 0)
|
&b->val.map.items[i].key)) != 0)
|
||||||
return rv;
|
return rv;
|
||||||
|
@ -294,6 +282,7 @@ int rmsgpack_dom_value_cmp(
|
||||||
return 1;
|
return 1;
|
||||||
for (i = 0; i < a->val.array.len; i++)
|
for (i = 0; i < a->val.array.len; i++)
|
||||||
{
|
{
|
||||||
|
int rv;
|
||||||
if ((rv = rmsgpack_dom_value_cmp(&a->val.array.items[i],
|
if ((rv = rmsgpack_dom_value_cmp(&a->val.array.items[i],
|
||||||
&b->val.array.items[i])) != 0)
|
&b->val.array.items[i])) != 0)
|
||||||
return rv;
|
return rv;
|
||||||
|
@ -407,6 +396,17 @@ int rmsgpack_dom_write(RFILE *fd, const struct rmsgpack_dom_value *obj)
|
||||||
return written;
|
return written;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static struct rmsgpack_read_callbacks dom_reader_callbacks = {
|
||||||
|
dom_read_nil,
|
||||||
|
dom_read_bool,
|
||||||
|
dom_read_int,
|
||||||
|
dom_read_uint,
|
||||||
|
dom_read_string,
|
||||||
|
dom_read_bin,
|
||||||
|
dom_read_map_start,
|
||||||
|
dom_read_array_start
|
||||||
|
};
|
||||||
|
|
||||||
int rmsgpack_dom_read(RFILE *fd, struct rmsgpack_dom_value *out)
|
int rmsgpack_dom_read(RFILE *fd, struct rmsgpack_dom_value *out)
|
||||||
{
|
{
|
||||||
struct dom_reader_state s;
|
struct dom_reader_state s;
|
||||||
|
@ -415,7 +415,7 @@ int rmsgpack_dom_read(RFILE *fd, struct rmsgpack_dom_value *out)
|
||||||
s.i = 0;
|
s.i = 0;
|
||||||
s.stack[0] = out;
|
s.stack[0] = out;
|
||||||
|
|
||||||
rv = rmsgpack_read(fd, &dom_reader_callbacks, &s);
|
rv = rmsgpack_read(fd, &dom_reader_callbacks, &s);
|
||||||
|
|
||||||
if (rv < 0)
|
if (rv < 0)
|
||||||
rmsgpack_dom_value_free(out);
|
rmsgpack_dom_value_free(out);
|
||||||
|
@ -442,8 +442,6 @@ int rmsgpack_dom_read_into(RFILE *fd, ...)
|
||||||
|
|
||||||
rv = rmsgpack_dom_read(fd, &map);
|
rv = rmsgpack_dom_read(fd, &map);
|
||||||
|
|
||||||
(void)value_type;
|
|
||||||
|
|
||||||
if (rv < 0)
|
if (rv < 0)
|
||||||
{
|
{
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
Loading…
Reference in New Issue