(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 */
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(
struct bintree_node *parent)
{
@ -73,7 +68,7 @@ static int bintree_insert_internal(bintree_t *t,
{
int cmp_res = 0;
if (bintree_is_nil(root))
if (!root || (root->value == NIL_NODE))
{
root->left = 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;
if (bintree_is_nil(n))
if (!n || (n->value == NIL_NODE))
return 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)
{
unsigned i;
struct rmsgpack_dom_value key, value;
int rv = 0;
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++)
{
key = doc->val.map.items[i].key;
value = doc->val.map.items[i].value;
struct rmsgpack_dom_value key = doc->val.map.items[i].key;
struct rmsgpack_dom_value value = doc->val.map.items[i].value;
if (key.type != RDT_STRING)
return -EINVAL;
@ -324,7 +323,7 @@ int libretrodb_find_entry(libretrodb_t *db, const char *index_name,
return -1;
bufflen = idx.next;
buff = malloc(bufflen);
buff = malloc(bufflen);
if (!buff)
return -ENOMEM;
@ -332,7 +331,7 @@ int libretrodb_find_entry(libretrodb_t *db, const char *index_name,
while (nread < bufflen)
{
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)
{
@ -433,7 +432,8 @@ void libretrodb_cursor_close(libretrodb_cursor_t *cursor)
*
* 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)
{
RFILE *fd = NULL;

View File

@ -136,9 +136,10 @@ static struct rmsgpack_dom_value func_equals(
res.val.bool_ = 0;
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_;
}
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)
res = func_equals(input, 1, &argv[i]);
else
{
res = query_func_is_true(
argv[i].a.invocation.func(input,
argv[i].a.invocation.argc,
argv[i].a.invocation.argv
), 0, NULL);
}
if (res.val.bool_)
return res;
@ -193,14 +192,12 @@ static struct rmsgpack_dom_value query_func_operator_and(
if (argv[i].type == AT_VALUE)
res = func_equals(input, 1, &argv[i]);
else
{
res = query_func_is_true(
argv[i].a.invocation.func(input,
argv[i].a.invocation.argc,
argv[i].a.invocation.argv
),
0, NULL);
}
if (!res.val.bool_)
return res;
@ -215,16 +212,16 @@ static struct rmsgpack_dom_value query_func_between(
struct rmsgpack_dom_value res;
unsigned i = 0;
res.type = RDT_BOOL;
res.val.bool_ = 0;
(void)i;
res.type = RDT_BOOL;
res.val.bool_ = 0;
if (argc != 2)
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;
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;
switch (input.type)
@ -240,7 +237,7 @@ static struct rmsgpack_dom_value query_func_between(
&& (input.val.int_ <= argv[1].a.value.val.int_));
break;
default:
return res;
break;
}
return res;
@ -251,7 +248,7 @@ static struct rmsgpack_dom_value query_func_glob(
unsigned argc, const struct argument * argv)
{
struct rmsgpack_dom_value res;
unsigned i = 0;
unsigned i = 0;
res.type = RDT_BOOL;
res.val.bool_ = 0;

View File

@ -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)
{
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);
v->type = RDT_STRING;
v->val.string.len = len;
v->val.string.buff = value;
v->type = RDT_STRING;
v->val.string.len = len;
v->val.string.buff = value;
return 0;
}
@ -181,17 +181,6 @@ static int dom_read_array_start(uint32_t len, void *data)
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)
{
unsigned i;
@ -247,12 +236,10 @@ int rmsgpack_dom_value_cmp(
const struct rmsgpack_dom_value *b
)
{
int rv;
unsigned i;
if (a == b)
return 1;
if (a->type != b->type)
return 1;
@ -281,6 +268,7 @@ int rmsgpack_dom_value_cmp(
return 1;
for (i = 0; i < a->val.map.len; i++)
{
int rv;
if ((rv = rmsgpack_dom_value_cmp(&a->val.map.items[i].key,
&b->val.map.items[i].key)) != 0)
return rv;
@ -294,6 +282,7 @@ int rmsgpack_dom_value_cmp(
return 1;
for (i = 0; i < a->val.array.len; i++)
{
int rv;
if ((rv = rmsgpack_dom_value_cmp(&a->val.array.items[i],
&b->val.array.items[i])) != 0)
return rv;
@ -407,6 +396,17 @@ int rmsgpack_dom_write(RFILE *fd, const struct rmsgpack_dom_value *obj)
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)
{
struct dom_reader_state s;
@ -415,7 +415,7 @@ int rmsgpack_dom_read(RFILE *fd, struct rmsgpack_dom_value *out)
s.i = 0;
s.stack[0] = out;
rv = rmsgpack_read(fd, &dom_reader_callbacks, &s);
rv = rmsgpack_read(fd, &dom_reader_callbacks, &s);
if (rv < 0)
rmsgpack_dom_value_free(out);
@ -442,8 +442,6 @@ int rmsgpack_dom_read_into(RFILE *fd, ...)
rv = rmsgpack_dom_read(fd, &map);
(void)value_type;
if (rv < 0)
{
va_end(ap);