mirror of
https://github.com/libretro/RetroArch
synced 2025-03-25 16:44:01 +00:00
(libretro-db) Style nits
This commit is contained in:
parent
b18efe5096
commit
3eec012af8
@ -32,7 +32,7 @@ static struct bintree_node *new_nil_node(struct bintree_node *parent)
|
|||||||
|
|
||||||
static INLINE int is_nil(const struct bintree_node *node)
|
static INLINE int is_nil(const struct bintree_node *node)
|
||||||
{
|
{
|
||||||
return (node == NULL) || (node->value == NIL_NODE);
|
return (node == NULL) || (node->value == NIL_NODE);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int insert(struct bintree *t, struct bintree_node *root,
|
static int insert(struct bintree *t, struct bintree_node *root,
|
||||||
@ -112,7 +112,7 @@ static void bintree_free_node(struct bintree_node *n)
|
|||||||
free(n);
|
free(n);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
n->value = NULL;
|
n->value = NULL;
|
||||||
bintree_free_node(n->left);
|
bintree_free_node(n->left);
|
||||||
bintree_free_node(n->right);
|
bintree_free_node(n->right);
|
||||||
|
@ -14,13 +14,13 @@
|
|||||||
|
|
||||||
static char *strndup_(const char *s, size_t n)
|
static char *strndup_(const char *s, size_t n)
|
||||||
{
|
{
|
||||||
char* buff = calloc(n, sizeof(char));
|
char* buff = calloc(n, sizeof(char));
|
||||||
|
|
||||||
if (!buff)
|
if (!buff)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
strncpy(buff, s, n);
|
strncpy(buff, s, n);
|
||||||
return buff;
|
return buff;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct rmsgpack_dom_value *get_map_value(
|
static struct rmsgpack_dom_value *get_map_value(
|
||||||
@ -50,22 +50,22 @@ static int load_string(int fd, struct rmsgpack_dom_value *out)
|
|||||||
|
|
||||||
static int load_uint(int fd, struct rmsgpack_dom_value *out)
|
static int load_uint(int fd, struct rmsgpack_dom_value *out)
|
||||||
{
|
{
|
||||||
char tok[MAX_TOKEN], *c;
|
char tok[MAX_TOKEN], *c;
|
||||||
ssize_t tok_size;
|
ssize_t tok_size;
|
||||||
uint64_t value = 0;
|
uint64_t value = 0;
|
||||||
|
|
||||||
if ((tok_size = get_token(fd, tok, MAX_TOKEN)) < 0)
|
if ((tok_size = get_token(fd, tok, MAX_TOKEN)) < 0)
|
||||||
return tok_size;
|
return tok_size;
|
||||||
|
|
||||||
for (c = tok; c < tok + tok_size; c++)
|
for (c = tok; c < tok + tok_size; c++)
|
||||||
{
|
{
|
||||||
value *= 10;
|
value *= 10;
|
||||||
value += *c - '0';
|
value += *c - '0';
|
||||||
}
|
}
|
||||||
|
|
||||||
out->type = RDT_UINT;
|
out->type = RDT_UINT;
|
||||||
out->uint_ = value;
|
out->uint_ = value;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int load_bin(int fd, struct rmsgpack_dom_value *out)
|
static int load_bin(int fd, struct rmsgpack_dom_value *out)
|
||||||
@ -259,37 +259,37 @@ failed:
|
|||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int rv = 0;
|
int rv = 0;
|
||||||
int src = -1;
|
int src = -1;
|
||||||
int dst = -1;
|
int dst = -1;
|
||||||
|
|
||||||
if (argc != 3)
|
if (argc != 3)
|
||||||
printf("Usage: %s <dat file> <output file>\n", argv[0]);
|
printf("Usage: %s <dat file> <output file>\n", argv[0]);
|
||||||
|
|
||||||
src = open(argv[1], O_RDONLY);
|
src = open(argv[1], O_RDONLY);
|
||||||
|
|
||||||
if (src == -1)
|
if (src == -1)
|
||||||
{
|
{
|
||||||
printf("Could not open source file '%s': %s\n", argv[1], strerror(errno));
|
printf("Could not open source file '%s': %s\n", argv[1], strerror(errno));
|
||||||
rv = errno;
|
rv = errno;
|
||||||
goto clean;
|
goto clean;
|
||||||
}
|
}
|
||||||
|
|
||||||
dst = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
|
dst = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
|
||||||
|
|
||||||
if (dst == -1)
|
if (dst == -1)
|
||||||
{
|
{
|
||||||
printf("Could not open destination file '%s': %s\n", argv[1], strerror(errno));
|
printf("Could not open destination file '%s': %s\n", argv[1], strerror(errno));
|
||||||
rv = errno;
|
rv = errno;
|
||||||
goto clean;
|
goto clean;
|
||||||
}
|
}
|
||||||
|
|
||||||
rv = rarchdb_create(dst, &dat_value_provider, &src);
|
rv = rarchdb_create(dst, &dat_value_provider, &src);
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
if (src != -1)
|
if (src != -1)
|
||||||
close(src);
|
close(src);
|
||||||
if (dst != -1)
|
if (dst != -1)
|
||||||
close(dst);
|
close(dst);
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
@ -22,8 +22,8 @@
|
|||||||
|
|
||||||
struct node_iter_ctx
|
struct node_iter_ctx
|
||||||
{
|
{
|
||||||
libretrodb_t *db;
|
libretrodb_t *db;
|
||||||
libretrodb_index_t *idx;
|
libretrodb_index_t *idx;
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct rmsgpack_dom_value sentinal;
|
static struct rmsgpack_dom_value sentinal;
|
||||||
@ -80,7 +80,7 @@ static int validate_document(const struct rmsgpack_dom_value * doc)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int libretrodb_create(FILE *fp, libretrodb_value_provider value_provider,
|
int libretrodb_create(FILE *fp, libretrodb_value_provider value_provider,
|
||||||
void * ctx)
|
void * ctx)
|
||||||
{
|
{
|
||||||
int rv;
|
int rv;
|
||||||
off_t root;
|
off_t root;
|
||||||
@ -135,13 +135,13 @@ static int libretrodb_read_index_header(FILE *fp, libretrodb_index_t *idx)
|
|||||||
|
|
||||||
static void libretrodb_write_index_header(FILE *fp, libretrodb_index_t * idx)
|
static void libretrodb_write_index_header(FILE *fp, libretrodb_index_t * idx)
|
||||||
{
|
{
|
||||||
rmsgpack_write_map_header(fp, 3);
|
rmsgpack_write_map_header(fp, 3);
|
||||||
rmsgpack_write_string(fp, "name", strlen("name"));
|
rmsgpack_write_string(fp, "name", strlen("name"));
|
||||||
rmsgpack_write_string(fp, idx->name, strlen(idx->name));
|
rmsgpack_write_string(fp, idx->name, strlen(idx->name));
|
||||||
rmsgpack_write_string(fp, "key_size", strlen("key_size"));
|
rmsgpack_write_string(fp, "key_size", strlen("key_size"));
|
||||||
rmsgpack_write_uint(fp, idx->key_size);
|
rmsgpack_write_uint(fp, idx->key_size);
|
||||||
rmsgpack_write_string(fp, "next", strlen("next"));
|
rmsgpack_write_string(fp, "next", strlen("next"));
|
||||||
rmsgpack_write_uint(fp, idx->next);
|
rmsgpack_write_uint(fp, idx->next);
|
||||||
}
|
}
|
||||||
|
|
||||||
void libretrodb_close(libretrodb_t *db)
|
void libretrodb_close(libretrodb_t *db)
|
||||||
@ -149,8 +149,8 @@ void libretrodb_close(libretrodb_t *db)
|
|||||||
if (!db)
|
if (!db)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
fclose(db->fp);
|
fclose(db->fp);
|
||||||
db->fp = NULL;
|
db->fp = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int libretrodb_open(const char *path, libretrodb_t *db)
|
int libretrodb_open(const char *path, libretrodb_t *db)
|
||||||
@ -245,7 +245,7 @@ static int binsearch(const void * buff, const void * item,
|
|||||||
}
|
}
|
||||||
|
|
||||||
int libretrodb_find_entry(libretrodb_t *db, const char *index_name,
|
int libretrodb_find_entry(libretrodb_t *db, const char *index_name,
|
||||||
const void *key, struct rmsgpack_dom_value *out)
|
const void *key, struct rmsgpack_dom_value *out)
|
||||||
{
|
{
|
||||||
libretrodb_index_t idx;
|
libretrodb_index_t idx;
|
||||||
int rv;
|
int rv;
|
||||||
@ -294,8 +294,8 @@ int libretrodb_find_entry(libretrodb_t *db, const char *index_name,
|
|||||||
**/
|
**/
|
||||||
int libretrodb_cursor_reset(libretrodb_cursor_t *cursor)
|
int libretrodb_cursor_reset(libretrodb_cursor_t *cursor)
|
||||||
{
|
{
|
||||||
cursor->eof = 0;
|
cursor->eof = 0;
|
||||||
return flseek(cursor->fp,
|
return flseek(cursor->fp,
|
||||||
cursor->db->root + sizeof(libretrodb_header_t),
|
cursor->db->root + sizeof(libretrodb_header_t),
|
||||||
SEEK_SET);
|
SEEK_SET);
|
||||||
}
|
}
|
||||||
@ -339,16 +339,16 @@ void libretrodb_cursor_close(libretrodb_cursor_t *cursor)
|
|||||||
if (!cursor)
|
if (!cursor)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
fclose(cursor->fp);
|
fclose(cursor->fp);
|
||||||
|
|
||||||
if (cursor->query)
|
if (cursor->query)
|
||||||
libretrodb_query_free(cursor->query);
|
libretrodb_query_free(cursor->query);
|
||||||
|
|
||||||
cursor->is_valid = 0;
|
cursor->is_valid = 0;
|
||||||
cursor->fp = NULL;
|
cursor->fp = NULL;
|
||||||
cursor->eof = 1;
|
cursor->eof = 1;
|
||||||
cursor->db = NULL;
|
cursor->db = NULL;
|
||||||
cursor->query = NULL;
|
cursor->query = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -371,9 +371,9 @@ int libretrodb_cursor_open(libretrodb_t *db, libretrodb_cursor_t *cursor,
|
|||||||
|
|
||||||
cursor->db = db;
|
cursor->db = db;
|
||||||
cursor->is_valid = 1;
|
cursor->is_valid = 1;
|
||||||
|
|
||||||
libretrodb_cursor_reset(cursor);
|
libretrodb_cursor_reset(cursor);
|
||||||
|
|
||||||
cursor->query = q;
|
cursor->query = q;
|
||||||
|
|
||||||
if (q)
|
if (q)
|
||||||
@ -401,121 +401,121 @@ static uint64_t libretrodb_tell(libretrodb_t *db)
|
|||||||
int libretrodb_create_index(libretrodb_t *db,
|
int libretrodb_create_index(libretrodb_t *db,
|
||||||
const char *name, const char *field_name)
|
const char *name, const char *field_name)
|
||||||
{
|
{
|
||||||
int rv;
|
int rv;
|
||||||
struct node_iter_ctx nictx;
|
struct node_iter_ctx nictx;
|
||||||
struct rmsgpack_dom_value key;
|
struct rmsgpack_dom_value key;
|
||||||
libretrodb_index_t idx;
|
libretrodb_index_t idx;
|
||||||
struct rmsgpack_dom_value item;
|
struct rmsgpack_dom_value item;
|
||||||
struct rmsgpack_dom_value *field;
|
struct rmsgpack_dom_value *field;
|
||||||
struct bintree tree;
|
struct bintree tree;
|
||||||
uint64_t idx_header_offset;
|
uint64_t idx_header_offset;
|
||||||
libretrodb_cursor_t cur = {0};
|
libretrodb_cursor_t cur = {0};
|
||||||
void *buff = NULL;
|
void *buff = NULL;
|
||||||
uint64_t *buff_u64 = NULL;
|
uint64_t *buff_u64 = NULL;
|
||||||
uint8_t field_size = 0;
|
uint8_t field_size = 0;
|
||||||
uint64_t item_loc = libretrodb_tell(db);
|
uint64_t item_loc = libretrodb_tell(db);
|
||||||
|
|
||||||
bintree_new(&tree, node_compare, &field_size);
|
bintree_new(&tree, node_compare, &field_size);
|
||||||
|
|
||||||
if (libretrodb_cursor_open(db, &cur, NULL) != 0)
|
if (libretrodb_cursor_open(db, &cur, NULL) != 0)
|
||||||
{
|
{
|
||||||
rv = -1;
|
rv = -1;
|
||||||
goto clean;
|
goto clean;
|
||||||
}
|
}
|
||||||
|
|
||||||
key.type = RDT_STRING;
|
key.type = RDT_STRING;
|
||||||
key.string.len = strlen(field_name);
|
key.string.len = strlen(field_name);
|
||||||
|
|
||||||
/* We know we aren't going to change it */
|
/* We know we aren't going to change it */
|
||||||
key.string.buff = (char*)field_name;
|
key.string.buff = (char*)field_name;
|
||||||
|
|
||||||
while (libretrodb_cursor_read_item(&cur, &item) == 0)
|
while (libretrodb_cursor_read_item(&cur, &item) == 0)
|
||||||
{
|
{
|
||||||
if (item.type != RDT_MAP)
|
if (item.type != RDT_MAP)
|
||||||
{
|
{
|
||||||
rv = -EINVAL;
|
rv = -EINVAL;
|
||||||
printf("Only map keys are supported\n");
|
printf("Only map keys are supported\n");
|
||||||
goto clean;
|
goto clean;
|
||||||
}
|
}
|
||||||
|
|
||||||
field = rmsgpack_dom_value_map_value(&item, &key);
|
field = rmsgpack_dom_value_map_value(&item, &key);
|
||||||
|
|
||||||
if (!field)
|
if (!field)
|
||||||
{
|
{
|
||||||
rv = -EINVAL;
|
rv = -EINVAL;
|
||||||
printf("field not found in item\n");
|
printf("field not found in item\n");
|
||||||
goto clean;
|
goto clean;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (field->type != RDT_BINARY)
|
if (field->type != RDT_BINARY)
|
||||||
{
|
{
|
||||||
rv = -EINVAL;
|
rv = -EINVAL;
|
||||||
printf("field is not binary\n");
|
printf("field is not binary\n");
|
||||||
goto clean;
|
goto clean;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (field->binary.len == 0)
|
if (field->binary.len == 0)
|
||||||
{
|
{
|
||||||
rv = -EINVAL;
|
rv = -EINVAL;
|
||||||
printf("field is empty\n");
|
printf("field is empty\n");
|
||||||
goto clean;
|
goto clean;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (field_size == 0)
|
if (field_size == 0)
|
||||||
field_size = field->binary.len;
|
field_size = field->binary.len;
|
||||||
else if (field->binary.len != field_size)
|
else if (field->binary.len != field_size)
|
||||||
{
|
{
|
||||||
rv = -EINVAL;
|
rv = -EINVAL;
|
||||||
printf("field is not of correct size\n");
|
printf("field is not of correct size\n");
|
||||||
goto clean;
|
goto clean;
|
||||||
}
|
}
|
||||||
|
|
||||||
buff = malloc(field_size + sizeof(uint64_t));
|
buff = malloc(field_size + sizeof(uint64_t));
|
||||||
if (!buff)
|
if (!buff)
|
||||||
{
|
{
|
||||||
rv = -ENOMEM;
|
rv = -ENOMEM;
|
||||||
goto clean;
|
goto clean;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(buff, field->binary.buff, field_size);
|
memcpy(buff, field->binary.buff, field_size);
|
||||||
|
|
||||||
buff_u64 = (uint64_t *)buff + field_size;
|
buff_u64 = (uint64_t *)buff + field_size;
|
||||||
|
|
||||||
memcpy(buff_u64, &item_loc, sizeof(uint64_t));
|
memcpy(buff_u64, &item_loc, sizeof(uint64_t));
|
||||||
|
|
||||||
if (bintree_insert(&tree, buff) != 0)
|
if (bintree_insert(&tree, buff) != 0)
|
||||||
{
|
{
|
||||||
printf("Value is not unique: ");
|
printf("Value is not unique: ");
|
||||||
rmsgpack_dom_value_print(field);
|
rmsgpack_dom_value_print(field);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
rv = -EINVAL;
|
rv = -EINVAL;
|
||||||
goto clean;
|
goto clean;
|
||||||
}
|
}
|
||||||
buff = NULL;
|
buff = NULL;
|
||||||
rmsgpack_dom_value_free(&item);
|
rmsgpack_dom_value_free(&item);
|
||||||
item_loc = libretrodb_tell(db);
|
item_loc = libretrodb_tell(db);
|
||||||
}
|
}
|
||||||
|
|
||||||
(void)rv;
|
(void)rv;
|
||||||
(void)idx_header_offset;
|
(void)idx_header_offset;
|
||||||
|
|
||||||
idx_header_offset = flseek(db->fp, 0, SEEK_END);
|
idx_header_offset = flseek(db->fp, 0, SEEK_END);
|
||||||
strncpy(idx.name, name, 50);
|
strncpy(idx.name, name, 50);
|
||||||
|
|
||||||
idx.name[49] = '\0';
|
idx.name[49] = '\0';
|
||||||
idx.key_size = field_size;
|
idx.key_size = field_size;
|
||||||
idx.next = db->count * (field_size + sizeof(uint64_t));
|
idx.next = db->count * (field_size + sizeof(uint64_t));
|
||||||
libretrodb_write_index_header(db->fp, &idx);
|
libretrodb_write_index_header(db->fp, &idx);
|
||||||
|
|
||||||
nictx.db = db;
|
nictx.db = db;
|
||||||
nictx.idx = &idx;
|
nictx.idx = &idx;
|
||||||
bintree_iterate(&tree, node_iter, &nictx);
|
bintree_iterate(&tree, node_iter, &nictx);
|
||||||
bintree_free(&tree);
|
bintree_free(&tree);
|
||||||
clean:
|
clean:
|
||||||
rmsgpack_dom_value_free(&item);
|
rmsgpack_dom_value_free(&item);
|
||||||
if (buff)
|
if (buff)
|
||||||
free(buff);
|
free(buff);
|
||||||
if (cur.is_valid)
|
if (cur.is_valid)
|
||||||
libretrodb_cursor_close(&cur);
|
libretrodb_cursor_close(&cur);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -78,7 +78,7 @@ static void raise_enomem(const char **error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void raise_unknown_function(off_t where, const char *name,
|
static void raise_unknown_function(off_t where, const char *name,
|
||||||
size_t len, const char **error)
|
size_t len, const char **error)
|
||||||
{
|
{
|
||||||
int n = snprintf(tmp_error_buff, MAX_ERROR_LEN,
|
int n = snprintf(tmp_error_buff, MAX_ERROR_LEN,
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@ -131,26 +131,26 @@ enum argument_type
|
|||||||
struct argument;
|
struct argument;
|
||||||
|
|
||||||
typedef struct rmsgpack_dom_value (*rarch_query_func)(
|
typedef struct rmsgpack_dom_value (*rarch_query_func)(
|
||||||
struct rmsgpack_dom_value input,
|
struct rmsgpack_dom_value input,
|
||||||
unsigned argc,
|
unsigned argc,
|
||||||
const struct argument *argv
|
const struct argument *argv
|
||||||
);
|
);
|
||||||
|
|
||||||
struct invocation
|
struct invocation
|
||||||
{
|
{
|
||||||
rarch_query_func func;
|
rarch_query_func func;
|
||||||
unsigned argc;
|
unsigned argc;
|
||||||
struct argument *argv;
|
struct argument *argv;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct argument
|
struct argument
|
||||||
{
|
{
|
||||||
enum argument_type type;
|
enum argument_type type;
|
||||||
union
|
union
|
||||||
{
|
{
|
||||||
struct rmsgpack_dom_value value;
|
struct rmsgpack_dom_value value;
|
||||||
struct invocation invocation;
|
struct invocation invocation;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
static void argument_free(struct argument *arg)
|
static void argument_free(struct argument *arg)
|
||||||
@ -169,21 +169,21 @@ static void argument_free(struct argument *arg)
|
|||||||
|
|
||||||
struct query
|
struct query
|
||||||
{
|
{
|
||||||
unsigned ref_count;
|
unsigned ref_count;
|
||||||
struct invocation root;
|
struct invocation root;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct registered_func
|
struct registered_func
|
||||||
{
|
{
|
||||||
const char *name;
|
const char *name;
|
||||||
rarch_query_func func;
|
rarch_query_func func;
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct buffer parse_argument(struct buffer buff, struct argument *arg,
|
static struct buffer parse_argument(struct buffer buff, struct argument *arg,
|
||||||
const char **error);
|
const char **error);
|
||||||
|
|
||||||
static struct rmsgpack_dom_value is_true(struct rmsgpack_dom_value input,
|
static struct rmsgpack_dom_value is_true(struct rmsgpack_dom_value input,
|
||||||
unsigned argc, const struct argument *argv)
|
unsigned argc, const struct argument *argv)
|
||||||
{
|
{
|
||||||
struct rmsgpack_dom_value res = {0};
|
struct rmsgpack_dom_value res = {0};
|
||||||
|
|
||||||
@ -199,7 +199,7 @@ static struct rmsgpack_dom_value is_true(struct rmsgpack_dom_value input,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static struct rmsgpack_dom_value equals(struct rmsgpack_dom_value input,
|
static struct rmsgpack_dom_value equals(struct rmsgpack_dom_value input,
|
||||||
unsigned argc, const struct argument * argv)
|
unsigned argc, const struct argument * argv)
|
||||||
{
|
{
|
||||||
struct argument arg;
|
struct argument arg;
|
||||||
struct rmsgpack_dom_value res = {0};
|
struct rmsgpack_dom_value res = {0};
|
||||||
@ -228,7 +228,7 @@ static struct rmsgpack_dom_value equals(struct rmsgpack_dom_value input,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static struct rmsgpack_dom_value operator_or(struct rmsgpack_dom_value input,
|
static struct rmsgpack_dom_value operator_or(struct rmsgpack_dom_value input,
|
||||||
unsigned argc, const struct argument * argv)
|
unsigned argc, const struct argument * argv)
|
||||||
{
|
{
|
||||||
unsigned i;
|
unsigned i;
|
||||||
struct rmsgpack_dom_value res = {0};
|
struct rmsgpack_dom_value res = {0};
|
||||||
@ -258,7 +258,7 @@ static struct rmsgpack_dom_value operator_or(struct rmsgpack_dom_value input,
|
|||||||
static struct rmsgpack_dom_value between(struct rmsgpack_dom_value input,
|
static struct rmsgpack_dom_value between(struct rmsgpack_dom_value input,
|
||||||
unsigned argc, const struct argument * argv)
|
unsigned argc, const struct argument * argv)
|
||||||
{
|
{
|
||||||
struct rmsgpack_dom_value res = {0};
|
struct rmsgpack_dom_value res = {0};
|
||||||
unsigned i = 0;
|
unsigned i = 0;
|
||||||
|
|
||||||
res.type = RDT_BOOL;
|
res.type = RDT_BOOL;
|
||||||
@ -395,12 +395,12 @@ clean:
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct registered_func registered_functions[100] = {
|
struct registered_func registered_functions[100] = {
|
||||||
{"is_true", is_true},
|
{"is_true", is_true},
|
||||||
{"or", operator_or},
|
{"or", operator_or},
|
||||||
{"and", operator_and},
|
{"and", operator_and},
|
||||||
{"between", between},
|
{"between", between},
|
||||||
{"glob", q_glob},
|
{"glob", q_glob},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct buffer chomp(struct buffer buff)
|
static struct buffer chomp(struct buffer buff)
|
||||||
@ -460,14 +460,14 @@ static void peek_char(struct buffer buff, char *c, const char **error)
|
|||||||
static struct buffer get_char(struct buffer buff, char * c,
|
static struct buffer get_char(struct buffer buff, char * c,
|
||||||
const char ** error)
|
const char ** error)
|
||||||
{
|
{
|
||||||
if (is_eot(buff))
|
if (is_eot(buff))
|
||||||
{
|
{
|
||||||
raise_unexpected_eof(buff.offset, error);
|
raise_unexpected_eof(buff.offset, error);
|
||||||
return buff;
|
return buff;
|
||||||
}
|
}
|
||||||
*c = buff.data[buff.offset];
|
*c = buff.data[buff.offset];
|
||||||
buff.offset++;
|
buff.offset++;
|
||||||
return buff;
|
return buff;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct buffer parse_string(struct buffer buff,
|
static struct buffer parse_string(struct buffer buff,
|
||||||
@ -574,26 +574,26 @@ static struct buffer get_ident(struct buffer buff,
|
|||||||
{
|
{
|
||||||
char c = '\0';
|
char c = '\0';
|
||||||
|
|
||||||
if (is_eot(buff))
|
if (is_eot(buff))
|
||||||
{
|
{
|
||||||
raise_unexpected_eof(buff.offset, error);
|
raise_unexpected_eof(buff.offset, error);
|
||||||
return buff;
|
return buff;
|
||||||
}
|
}
|
||||||
|
|
||||||
*ident = buff.data + buff.offset;
|
*ident = buff.data + buff.offset;
|
||||||
*len = 0;
|
*len = 0;
|
||||||
peek_char(buff, &c, error);
|
peek_char(buff, &c, error);
|
||||||
|
|
||||||
if (*error)
|
if (*error)
|
||||||
goto clean;
|
goto clean;
|
||||||
if (!isalpha(c))
|
if (!isalpha(c))
|
||||||
return buff;
|
return buff;
|
||||||
|
|
||||||
buff.offset++;
|
buff.offset++;
|
||||||
*len = *len + 1;
|
*len = *len + 1;
|
||||||
peek_char(buff, &c, error);
|
peek_char(buff, &c, error);
|
||||||
|
|
||||||
while (!*error)
|
while (!*error)
|
||||||
{
|
{
|
||||||
if (!(isalpha(c) || isdigit(c) || c == '_'))
|
if (!(isalpha(c) || isdigit(c) || c == '_'))
|
||||||
break;
|
break;
|
||||||
@ -603,7 +603,7 @@ static struct buffer get_ident(struct buffer buff,
|
|||||||
}
|
}
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
return buff;
|
return buff;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct buffer parse_method_call(struct buffer buff,
|
static struct buffer parse_method_call(struct buffer buff,
|
||||||
@ -842,15 +842,15 @@ static struct buffer parse_argument(struct buffer buff,
|
|||||||
|
|
||||||
void libretrodb_query_free(void *q)
|
void libretrodb_query_free(void *q)
|
||||||
{
|
{
|
||||||
unsigned i;
|
unsigned i;
|
||||||
struct query *real_q = (struct query*)q;
|
struct query *real_q = (struct query*)q;
|
||||||
|
|
||||||
real_q->ref_count--;
|
real_q->ref_count--;
|
||||||
if (real_q->ref_count > 0)
|
if (real_q->ref_count > 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (i = 0; i < real_q->root.argc; i++)
|
for (i = 0; i < real_q->root.argc; i++)
|
||||||
argument_free(&real_q->root.argv[i]);
|
argument_free(&real_q->root.argv[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *libretrodb_query_compile(libretrodb_t *db,
|
void *libretrodb_query_compile(libretrodb_t *db,
|
||||||
|
@ -418,7 +418,7 @@ static int read_buff(FILE *fp, size_t size, char **pbuff, uint64_t *len)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int read_map(FILE *fp, uint32_t len,
|
static int read_map(FILE *fp, uint32_t len,
|
||||||
struct rmsgpack_read_callbacks *callbacks, void *data)
|
struct rmsgpack_read_callbacks *callbacks, void *data)
|
||||||
{
|
{
|
||||||
int rv;
|
int rv;
|
||||||
unsigned i;
|
unsigned i;
|
||||||
|
@ -13,72 +13,72 @@
|
|||||||
|
|
||||||
struct dom_reader_state
|
struct dom_reader_state
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
struct rmsgpack_dom_value *stack[MAX_DEPTH];
|
struct rmsgpack_dom_value *stack[MAX_DEPTH];
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct rmsgpack_dom_value *dom_reader_state_pop(
|
static struct rmsgpack_dom_value *dom_reader_state_pop(
|
||||||
struct dom_reader_state *s)
|
struct dom_reader_state *s)
|
||||||
{
|
{
|
||||||
struct rmsgpack_dom_value *v = s->stack[s->i];
|
struct rmsgpack_dom_value *v = s->stack[s->i];
|
||||||
s->i--;
|
s->i--;
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void puts_i64(int64_t dec)
|
static void puts_i64(int64_t dec)
|
||||||
{
|
{
|
||||||
signed char digits[19 + 1]; /* max i64: 9,223,372,036,854,775,807 */
|
signed char digits[19 + 1]; /* max i64: 9,223,372,036,854,775,807 */
|
||||||
int i;
|
int i;
|
||||||
uint64_t decimal = (dec < 0) ? (uint64_t)-dec : (uint64_t)+dec;
|
uint64_t decimal = (dec < 0) ? (uint64_t)-dec : (uint64_t)+dec;
|
||||||
|
|
||||||
digits[19] = '\0';
|
|
||||||
|
|
||||||
for (i = sizeof(digits) - 2; i >= 0; i--)
|
|
||||||
{
|
|
||||||
digits[i] = decimal % 10;
|
|
||||||
decimal /= 10;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < sizeof(digits) - 1; i++)
|
digits[19] = '\0';
|
||||||
digits[i] += '0';
|
|
||||||
for (i = 0; i < sizeof(digits) - 2; i++)
|
|
||||||
if (digits[i] != '0')
|
|
||||||
break; /* Don't print leading zeros to the console. */
|
|
||||||
|
|
||||||
if (dec < 0)
|
for (i = sizeof(digits) - 2; i >= 0; i--)
|
||||||
putchar('-');
|
{
|
||||||
fputs((char *)&digits[i], stdout);
|
digits[i] = decimal % 10;
|
||||||
|
decimal /= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < sizeof(digits) - 1; i++)
|
||||||
|
digits[i] += '0';
|
||||||
|
for (i = 0; i < sizeof(digits) - 2; i++)
|
||||||
|
if (digits[i] != '0')
|
||||||
|
break; /* Don't print leading zeros to the console. */
|
||||||
|
|
||||||
|
if (dec < 0)
|
||||||
|
putchar('-');
|
||||||
|
fputs((char *)&digits[i], stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void puts_u64(uint64_t decimal)
|
static void puts_u64(uint64_t decimal)
|
||||||
{
|
{
|
||||||
char digits[20 + 1]; /* max u64: 18,446,744,073,709,551,616 */
|
char digits[20 + 1]; /* max u64: 18,446,744,073,709,551,616 */
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
digits[20] = '\0';
|
digits[20] = '\0';
|
||||||
for (i = sizeof(digits) - 2; i >= 0; i--)
|
for (i = sizeof(digits) - 2; i >= 0; i--)
|
||||||
{
|
{
|
||||||
digits[i] = decimal % 10;
|
digits[i] = decimal % 10;
|
||||||
decimal /= 10;
|
decimal /= 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < sizeof(digits) - 1; i++)
|
for (i = 0; i < sizeof(digits) - 1; i++)
|
||||||
digits[i] += '0';
|
digits[i] += '0';
|
||||||
for (i = 0; i < sizeof(digits) - 2; i++)
|
for (i = 0; i < sizeof(digits) - 2; i++)
|
||||||
if (digits[i] != '0')
|
if (digits[i] != '0')
|
||||||
break; /* Don't print leading zeros to the console. */
|
break; /* Don't print leading zeros to the console. */
|
||||||
|
|
||||||
fputs(&digits[i], stdout);
|
fputs(&digits[i], stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int dom_reader_state_push(struct dom_reader_state *s,
|
static int dom_reader_state_push(struct dom_reader_state *s,
|
||||||
struct rmsgpack_dom_value *v)
|
struct rmsgpack_dom_value *v)
|
||||||
{
|
{
|
||||||
if ((s->i + 1) == MAX_DEPTH)
|
if ((s->i + 1) == MAX_DEPTH)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
s->i++;
|
s->i++;
|
||||||
s->stack[s->i] = v;
|
s->stack[s->i] = v;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int dom_read_nil(void *data)
|
static int dom_read_nil(void *data)
|
||||||
@ -140,7 +140,7 @@ static int dom_read_bin(void *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_BINARY;
|
v->type = RDT_BINARY;
|
||||||
v->binary.len = len;
|
v->binary.len = len;
|
||||||
v->binary.buff = (char *)value;
|
v->binary.buff = (char *)value;
|
||||||
@ -179,40 +179,40 @@ static int dom_read_map_start(uint32_t len, void *data)
|
|||||||
|
|
||||||
static int dom_read_array_start(uint32_t len, void *data)
|
static int dom_read_array_start(uint32_t len, void *data)
|
||||||
{
|
{
|
||||||
unsigned i;
|
unsigned i;
|
||||||
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 = dom_reader_state_pop(dom_state);
|
struct rmsgpack_dom_value *v = dom_reader_state_pop(dom_state);
|
||||||
struct rmsgpack_dom_value *items = NULL;
|
struct rmsgpack_dom_value *items = NULL;
|
||||||
|
|
||||||
v->type = RDT_ARRAY;
|
v->type = RDT_ARRAY;
|
||||||
v->array.len = len;
|
v->array.len = len;
|
||||||
v->array.items = NULL;
|
v->array.items = NULL;
|
||||||
|
|
||||||
items = (struct rmsgpack_dom_value *)calloc(len, sizeof(struct rmsgpack_dom_pair));
|
items = (struct rmsgpack_dom_value *)calloc(len, sizeof(struct rmsgpack_dom_pair));
|
||||||
|
|
||||||
if (!items)
|
if (!items)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
v->array.items = items;
|
v->array.items = items;
|
||||||
|
|
||||||
for (i = 0; i < len; i++)
|
for (i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
if (dom_reader_state_push(dom_state, &items[i]) < 0)
|
if (dom_reader_state_push(dom_state, &items[i]) < 0)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct rmsgpack_read_callbacks dom_reader_callbacks = {
|
static struct rmsgpack_read_callbacks dom_reader_callbacks = {
|
||||||
dom_read_nil,
|
dom_read_nil,
|
||||||
dom_read_bool,
|
dom_read_bool,
|
||||||
dom_read_int,
|
dom_read_int,
|
||||||
dom_read_uint,
|
dom_read_uint,
|
||||||
dom_read_string,
|
dom_read_string,
|
||||||
dom_read_bin,
|
dom_read_bin,
|
||||||
dom_read_map_start,
|
dom_read_map_start,
|
||||||
dom_read_array_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)
|
||||||
@ -268,7 +268,7 @@ struct rmsgpack_dom_value *rmsgpack_dom_value_map_value(
|
|||||||
int rmsgpack_dom_value_cmp(
|
int rmsgpack_dom_value_cmp(
|
||||||
const struct rmsgpack_dom_value *a,
|
const struct rmsgpack_dom_value *a,
|
||||||
const struct rmsgpack_dom_value *b
|
const struct rmsgpack_dom_value *b
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
int rv;
|
int rv;
|
||||||
unsigned i;
|
unsigned i;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user