(libretro-db) Small cleanups

This commit is contained in:
twinaphex 2020-08-03 17:13:54 +02:00
parent 72afa2f932
commit 77cf0ad524
4 changed files with 37 additions and 47 deletions

View File

@ -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)

View File

@ -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;
@ -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;

View File

@ -136,7 +136,8 @@ 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_;
@ -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;
@ -218,13 +215,13 @@ static struct rmsgpack_dom_value query_func_between(
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;

View File

@ -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;
@ -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);