mirror of
https://github.com/CTCaer/RetroArch.git
synced 2024-12-23 19:24:46 +00:00
(LibretroDB) Style nits
This commit is contained in:
parent
a2d8e0f148
commit
fc49dbf141
@ -191,14 +191,16 @@ error:
|
||||
static int libretrodb_find_index(libretrodb_t *db, const char *index_name,
|
||||
libretrodb_index_t *idx)
|
||||
{
|
||||
off_t eof = lseek(db->fd, 0, SEEK_END);
|
||||
off_t eof = lseek(db->fd, 0, SEEK_END);
|
||||
off_t offset = lseek(db->fd, db->first_index_offset, SEEK_SET);
|
||||
|
||||
while (offset < eof)
|
||||
{
|
||||
libretrodb_read_index_header(db->fd, idx);
|
||||
|
||||
if (strncmp(index_name, idx->name, strlen(idx->name)) == 0)
|
||||
return 0;
|
||||
|
||||
offset = lseek(db->fd, idx->next, SEEK_CUR);
|
||||
}
|
||||
|
||||
@ -223,9 +225,11 @@ static int binsearch(const void * buff, const void * item,
|
||||
*offset = *(uint64_t *)(current + field_size);
|
||||
return 0;
|
||||
}
|
||||
else if (count == 0)
|
||||
|
||||
if (count == 0)
|
||||
return -1;
|
||||
else if (rv > 0)
|
||||
|
||||
if (rv > 0)
|
||||
return binsearch(buff, item, mid, field_size, offset);
|
||||
|
||||
return binsearch(current + item_size, item,
|
||||
@ -270,10 +274,10 @@ int libretrodb_find_entry(libretrodb_t *db, const char *index_name,
|
||||
lseek(db->fd, offset, SEEK_SET);
|
||||
|
||||
rv = rmsgpack_dom_read(db->fd, out);
|
||||
|
||||
if (rv < 0)
|
||||
return rv;
|
||||
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
@ -375,9 +379,10 @@ int libretrodb_cursor_open(libretrodb_t *db, libretrodb_cursor_t *cursor,
|
||||
|
||||
static int node_iter(void * value, void * ctx)
|
||||
{
|
||||
struct node_iter_ctx * nictx = (struct node_iter_ctx *)ctx;
|
||||
struct node_iter_ctx *nictx = (struct node_iter_ctx*)ctx;
|
||||
|
||||
if (write(nictx->db->fd, value, nictx->idx->key_size + sizeof(uint64_t)) > 0)
|
||||
if (write(nictx->db->fd, value,
|
||||
nictx->idx->key_size + sizeof(uint64_t)) > 0)
|
||||
return 0;
|
||||
|
||||
return -1;
|
||||
@ -415,8 +420,10 @@ int libretrodb_create_index(libretrodb_t *db,
|
||||
|
||||
key.type = RDT_STRING;
|
||||
key.string.len = strlen(field_name);
|
||||
|
||||
/* We know we aren't going to change it */
|
||||
key.string.buff = (char *) field_name;
|
||||
|
||||
while (libretrodb_cursor_read_item(&cur, &item) == 0)
|
||||
{
|
||||
if (item.type != RDT_MAP)
|
||||
@ -466,7 +473,9 @@ int libretrodb_create_index(libretrodb_t *db,
|
||||
}
|
||||
|
||||
memcpy(buff, field->binary.buff, field_size);
|
||||
|
||||
buff_u64 = (uint64_t *)buff + field_size;
|
||||
|
||||
memcpy(buff_u64, &item_loc, sizeof(uint64_t));
|
||||
|
||||
if (bintree_insert(&tree, buff) != 0)
|
||||
|
@ -128,7 +128,7 @@ int rmsgpack_write_string(int fd, const char *s, uint32_t len)
|
||||
if (write(fd, &fixlen, sizeof(int8_t)) == -1)
|
||||
return -errno;
|
||||
}
|
||||
else if (len < 1<<8)
|
||||
else if (len < (1 << 8))
|
||||
{
|
||||
if (write(fd, &MPF_STR8, sizeof(MPF_STR8)) == -1)
|
||||
return -errno;
|
||||
@ -136,7 +136,7 @@ int rmsgpack_write_string(int fd, const char *s, uint32_t len)
|
||||
return -errno;
|
||||
written += sizeof(uint8_t);
|
||||
}
|
||||
else if (len < 1<<16)
|
||||
else if (len < (1 << 16))
|
||||
{
|
||||
if (write(fd, &MPF_STR16, sizeof(MPF_STR16)) == -1)
|
||||
return -errno;
|
||||
@ -166,6 +166,7 @@ int rmsgpack_write_bin(int fd, const void *s, uint32_t len)
|
||||
uint16_t tmp_i16;
|
||||
uint32_t tmp_i32;
|
||||
int written = sizeof(int8_t);
|
||||
|
||||
if (len == (uint8_t)len)
|
||||
{
|
||||
if (write(fd, &MPF_BIN8, sizeof(MPF_BIN8)) == -1)
|
||||
@ -212,11 +213,10 @@ int rmsgpack_write_bool(int fd, int value)
|
||||
if (write(fd, &MPF_TRUE, sizeof(MPF_TRUE)) == -1)
|
||||
return -errno;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (write(fd, &MPF_FALSE, sizeof(MPF_FALSE)) == -1)
|
||||
return -errno;
|
||||
}
|
||||
|
||||
if (write(fd, &MPF_FALSE, sizeof(MPF_FALSE)) == -1)
|
||||
return -errno;
|
||||
|
||||
return sizeof(uint8_t);
|
||||
}
|
||||
|
||||
@ -359,6 +359,7 @@ static int read_int(int fd, int64_t *out, size_t size)
|
||||
uint16_t tmp16;
|
||||
uint32_t tmp32;
|
||||
uint64_t tmp64;
|
||||
|
||||
if (read(fd, &tmp64, size) == -1)
|
||||
return -errno;
|
||||
|
||||
@ -385,56 +386,48 @@ static int read_int(int fd, int64_t *out, size_t size)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int read_buff(
|
||||
int fd,
|
||||
size_t size,
|
||||
char ** pbuff,
|
||||
uint64_t * len
|
||||
){
|
||||
uint64_t tmp_len = 0;
|
||||
if (read_uint(fd, &tmp_len, size) == -1)
|
||||
return -errno;
|
||||
static int read_buff(int fd, size_t size, char **pbuff, uint64_t *len)
|
||||
{
|
||||
uint64_t tmp_len = 0;
|
||||
|
||||
*pbuff = (char *)calloc(tmp_len + 1, sizeof(char));
|
||||
if (read(fd, *pbuff, tmp_len) == -1)
|
||||
if (read_uint(fd, &tmp_len, size) == -1)
|
||||
return -errno;
|
||||
|
||||
*pbuff = (char *)calloc(tmp_len + 1, sizeof(char));
|
||||
|
||||
if (read(fd, *pbuff, tmp_len) == -1)
|
||||
{
|
||||
free(*pbuff);
|
||||
return -errno;
|
||||
}
|
||||
*len = tmp_len;
|
||||
return 0;
|
||||
free(*pbuff);
|
||||
return -errno;
|
||||
}
|
||||
|
||||
*len = tmp_len;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int read_map(
|
||||
int fd,
|
||||
uint32_t len,
|
||||
struct rmsgpack_read_callbacks * callbacks,
|
||||
void * data
|
||||
){
|
||||
int rv;
|
||||
unsigned i;
|
||||
static int read_map(int fd, uint32_t len,
|
||||
struct rmsgpack_read_callbacks *callbacks, void *data)
|
||||
{
|
||||
int rv;
|
||||
unsigned i;
|
||||
|
||||
if (callbacks->read_map_start &&
|
||||
(rv = callbacks->read_map_start(len, data)) < 0)
|
||||
return rv;
|
||||
if (callbacks->read_map_start &&
|
||||
(rv = callbacks->read_map_start(len, data)) < 0)
|
||||
return rv;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
if ((rv = rmsgpack_read(fd, callbacks, data)) < 0)
|
||||
return rv;
|
||||
if ((rv = rmsgpack_read(fd, callbacks, data)) < 0)
|
||||
return rv;
|
||||
}
|
||||
if ((rv = rmsgpack_read(fd, callbacks, data)) < 0)
|
||||
return rv;
|
||||
if ((rv = rmsgpack_read(fd, callbacks, data)) < 0)
|
||||
return rv;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int read_array(
|
||||
int fd,
|
||||
uint32_t len,
|
||||
struct rmsgpack_read_callbacks * callbacks,
|
||||
void * data
|
||||
)
|
||||
static int read_array(int fd, uint32_t len,
|
||||
struct rmsgpack_read_callbacks *callbacks, void *data)
|
||||
{
|
||||
int rv;
|
||||
unsigned i;
|
||||
@ -452,15 +445,16 @@ static int read_array(
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rmsgpack_read(int fd, struct rmsgpack_read_callbacks * callbacks,
|
||||
void * data)
|
||||
int rmsgpack_read(int fd,
|
||||
struct rmsgpack_read_callbacks *callbacks, void *data)
|
||||
{
|
||||
int rv;
|
||||
uint64_t tmp_len = 0;
|
||||
uint64_t tmp_len = 0;
|
||||
uint64_t tmp_uint = 0;
|
||||
int64_t tmp_int = 0;
|
||||
uint8_t type = 0;
|
||||
char * buff = NULL;
|
||||
int64_t tmp_int = 0;
|
||||
uint8_t type = 0;
|
||||
char *buff = NULL;
|
||||
|
||||
if (read(fd, &type, sizeof(uint8_t)) == -1)
|
||||
return -errno;
|
||||
|
||||
@ -523,7 +517,8 @@ int rmsgpack_read(int fd, struct rmsgpack_read_callbacks * callbacks,
|
||||
case 0xc4:
|
||||
case 0xc5:
|
||||
case 0xc6:
|
||||
if ((rv = read_buff(fd, 1<<(type - 0xc4), &buff, &tmp_len)) < 0)
|
||||
if ((rv = read_buff(fd, 1<<(type - 0xc4),
|
||||
&buff, &tmp_len)) < 0)
|
||||
return rv;
|
||||
|
||||
if (callbacks->read_bin)
|
||||
|
@ -14,20 +14,19 @@
|
||||
struct dom_reader_state
|
||||
{
|
||||
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(
|
||||
struct dom_reader_state * s)
|
||||
static struct rmsgpack_dom_value *dom_reader_state_pop(
|
||||
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--;
|
||||
return v;
|
||||
}
|
||||
|
||||
static int dom_reader_state_push(struct dom_reader_state * s,
|
||||
struct rmsgpack_dom_value * v)
|
||||
static int dom_reader_state_push(struct dom_reader_state *s,
|
||||
struct rmsgpack_dom_value *v)
|
||||
{
|
||||
if ((s->i + 1) == MAX_DEPTH)
|
||||
return -ENOMEM;
|
||||
@ -36,20 +35,21 @@ static int dom_reader_state_push(struct dom_reader_state * s,
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dom_read_nil(void * data)
|
||||
static int dom_read_nil(void *data)
|
||||
{
|
||||
struct dom_reader_state * dom_state = (struct dom_reader_state *)data;
|
||||
struct rmsgpack_dom_value * v =
|
||||
(struct rmsgpack_dom_value *)dom_reader_state_pop(dom_state);
|
||||
struct dom_reader_state *dom_state = (struct dom_reader_state *)data;
|
||||
struct rmsgpack_dom_value *v =
|
||||
(struct rmsgpack_dom_value*)dom_reader_state_pop(dom_state);
|
||||
v->type = RDT_NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dom_read_bool(int value, void * data)
|
||||
static int dom_read_bool(int value, void *data)
|
||||
{
|
||||
struct dom_reader_state * dom_state = (struct dom_reader_state *)data;
|
||||
struct rmsgpack_dom_value * v =
|
||||
(struct rmsgpack_dom_value *)dom_reader_state_pop(dom_state);
|
||||
struct dom_reader_state *dom_state = (struct dom_reader_state *)data;
|
||||
struct rmsgpack_dom_value *v =
|
||||
(struct rmsgpack_dom_value*)dom_reader_state_pop(dom_state);
|
||||
|
||||
v->type = RDT_BOOL;
|
||||
v->bool_ = value;
|
||||
return 0;
|
||||
@ -57,19 +57,21 @@ static int dom_read_bool(int value, void * data)
|
||||
|
||||
static int dom_read_int(int64_t value, void *data)
|
||||
{
|
||||
struct dom_reader_state * dom_state = (struct dom_reader_state *)data;
|
||||
struct rmsgpack_dom_value * v =
|
||||
(struct rmsgpack_dom_value *)dom_reader_state_pop(dom_state);
|
||||
struct dom_reader_state *dom_state = (struct dom_reader_state *)data;
|
||||
struct rmsgpack_dom_value *v =
|
||||
(struct rmsgpack_dom_value*)dom_reader_state_pop(dom_state);
|
||||
|
||||
v->type = RDT_INT;
|
||||
v->int_ = value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dom_read_uint(uint64_t value, void * data)
|
||||
static int dom_read_uint(uint64_t value, void *data)
|
||||
{
|
||||
struct dom_reader_state * dom_state = (struct dom_reader_state *)data;
|
||||
struct rmsgpack_dom_value * v =
|
||||
(struct rmsgpack_dom_value *)dom_reader_state_pop(dom_state);
|
||||
struct dom_reader_state *dom_state = (struct dom_reader_state *)data;
|
||||
struct rmsgpack_dom_value *v =
|
||||
(struct rmsgpack_dom_value*)dom_reader_state_pop(dom_state);
|
||||
|
||||
v->type = RDT_UINT;
|
||||
v->uint_ = value;
|
||||
return 0;
|
||||
@ -77,9 +79,10 @@ 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 *)dom_reader_state_pop(dom_state);
|
||||
struct dom_reader_state *dom_state = (struct dom_reader_state *)data;
|
||||
struct rmsgpack_dom_value *v =
|
||||
(struct rmsgpack_dom_value*)dom_reader_state_pop(dom_state);
|
||||
|
||||
v->type = RDT_STRING;
|
||||
v->string.len = len;
|
||||
v->string.buff = value;
|
||||
@ -90,7 +93,8 @@ static int dom_read_bin(void *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 *)dom_reader_state_pop(dom_state);
|
||||
(struct rmsgpack_dom_value*)dom_reader_state_pop(dom_state);
|
||||
|
||||
v->type = RDT_BINARY;
|
||||
v->binary.len = len;
|
||||
v->binary.buff = (char *)value;
|
||||
@ -100,9 +104,9 @@ static int dom_read_bin(void *value, uint32_t len, void *data)
|
||||
static int dom_read_map_start(uint32_t len, void *data)
|
||||
{
|
||||
unsigned i;
|
||||
struct rmsgpack_dom_pair * items = NULL;
|
||||
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_pair *items = NULL;
|
||||
struct dom_reader_state *dom_state = (struct dom_reader_state *)data;
|
||||
struct rmsgpack_dom_value *v = dom_reader_state_pop(dom_state);
|
||||
|
||||
v->type = RDT_MAP;
|
||||
v->map.len = len;
|
||||
@ -115,6 +119,7 @@ static int dom_read_map_start(uint32_t len, void *data)
|
||||
return -ENOMEM;
|
||||
|
||||
v->map.items = items;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
if (dom_reader_state_push(dom_state, &items[i].value) < 0)
|
||||
@ -122,17 +127,16 @@ static int dom_read_map_start(uint32_t len, void *data)
|
||||
if (dom_reader_state_push(dom_state, &items[i].key) < 0)
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dom_read_array_start(
|
||||
uint32_t len,
|
||||
void * data
|
||||
){
|
||||
static int dom_read_array_start(uint32_t len, void *data)
|
||||
{
|
||||
unsigned i;
|
||||
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 * items = NULL;
|
||||
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 *items = NULL;
|
||||
|
||||
v->type = RDT_ARRAY;
|
||||
v->array.len = len;
|
||||
@ -145,10 +149,12 @@ static int dom_read_array_start(
|
||||
|
||||
v->array.items = items;
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
if (dom_reader_state_push(dom_state, &items[i]) < 0)
|
||||
return -ENOMEM;
|
||||
}
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
if (dom_reader_state_push(dom_state, &items[i]) < 0)
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -163,223 +169,241 @@ static struct rmsgpack_read_callbacks dom_reader_callbacks = {
|
||||
dom_read_array_start
|
||||
};
|
||||
|
||||
void rmsgpack_dom_value_free(struct rmsgpack_dom_value * v){
|
||||
unsigned i;
|
||||
void rmsgpack_dom_value_free(struct rmsgpack_dom_value *v)
|
||||
{
|
||||
unsigned i;
|
||||
|
||||
switch (v->type) {
|
||||
case RDT_STRING:
|
||||
free(v->string.buff);
|
||||
break;
|
||||
case RDT_BINARY:
|
||||
free(v->binary.buff);
|
||||
break;
|
||||
case RDT_MAP:
|
||||
for (i = 0; i < v->map.len; i++) {
|
||||
rmsgpack_dom_value_free(&v->map.items[i].key);
|
||||
rmsgpack_dom_value_free(&v->map.items[i].value);
|
||||
}
|
||||
free(v->map.items);
|
||||
break;
|
||||
case RDT_ARRAY:
|
||||
for (i = 0; i < v->array.len; i++)
|
||||
rmsgpack_dom_value_free(&v->array.items[i]);
|
||||
free(v->array.items);
|
||||
break;
|
||||
case RDT_NULL:
|
||||
case RDT_INT:
|
||||
case RDT_BOOL:
|
||||
case RDT_UINT:
|
||||
// Do nothing
|
||||
break;
|
||||
}
|
||||
switch (v->type)
|
||||
{
|
||||
case RDT_STRING:
|
||||
free(v->string.buff);
|
||||
break;
|
||||
case RDT_BINARY:
|
||||
free(v->binary.buff);
|
||||
break;
|
||||
case RDT_MAP:
|
||||
for (i = 0; i < v->map.len; i++)
|
||||
{
|
||||
rmsgpack_dom_value_free(&v->map.items[i].key);
|
||||
rmsgpack_dom_value_free(&v->map.items[i].value);
|
||||
}
|
||||
free(v->map.items);
|
||||
break;
|
||||
case RDT_ARRAY:
|
||||
for (i = 0; i < v->array.len; i++)
|
||||
rmsgpack_dom_value_free(&v->array.items[i]);
|
||||
free(v->array.items);
|
||||
break;
|
||||
case RDT_NULL:
|
||||
case RDT_INT:
|
||||
case RDT_BOOL:
|
||||
case RDT_UINT:
|
||||
// Do nothing
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
struct rmsgpack_dom_value * rmsgpack_dom_value_map_value(
|
||||
const struct rmsgpack_dom_value * map,
|
||||
const struct rmsgpack_dom_value * key
|
||||
){
|
||||
unsigned i;
|
||||
if (map->type != RDT_MAP)
|
||||
return NULL;
|
||||
struct rmsgpack_dom_value *rmsgpack_dom_value_map_value(
|
||||
const struct rmsgpack_dom_value *map,
|
||||
const struct rmsgpack_dom_value *key)
|
||||
{
|
||||
unsigned i;
|
||||
if (map->type != RDT_MAP)
|
||||
return NULL;
|
||||
|
||||
for (i = 0; i < map->map.len; i++) {
|
||||
if (rmsgpack_dom_value_cmp(key, &map->map.items[i].key) == 0)
|
||||
return &map->map.items[i].value;
|
||||
}
|
||||
return NULL;
|
||||
for (i = 0; i < map->map.len; i++)
|
||||
{
|
||||
if (rmsgpack_dom_value_cmp(key, &map->map.items[i].key) == 0)
|
||||
return &map->map.items[i].value;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int rmsgpack_dom_value_cmp(
|
||||
const struct rmsgpack_dom_value * a,
|
||||
const struct rmsgpack_dom_value * b
|
||||
){
|
||||
int rv;
|
||||
unsigned i;
|
||||
const struct rmsgpack_dom_value *a,
|
||||
const struct rmsgpack_dom_value *b
|
||||
)
|
||||
{
|
||||
int rv;
|
||||
unsigned i;
|
||||
|
||||
if (a == b)
|
||||
return 1;
|
||||
if (a == b)
|
||||
return 1;
|
||||
|
||||
if (a->type != b->type)
|
||||
return 1;
|
||||
if (a->type != b->type)
|
||||
return 1;
|
||||
|
||||
switch (a->type) {
|
||||
case RDT_NULL:
|
||||
return 0;
|
||||
case RDT_BOOL:
|
||||
return a->bool_ == b->bool_ ? 0 : 1;
|
||||
case RDT_INT:
|
||||
return a->int_ == b->int_ ? 0 : 1;
|
||||
case RDT_UINT:
|
||||
return a->uint_ == b->uint_ ? 0 : 1;
|
||||
case RDT_STRING:
|
||||
if (a->string.len != b->string.len)
|
||||
return 1;
|
||||
return strncmp(a->string.buff, b->string.buff, a->string.len);
|
||||
case RDT_BINARY:
|
||||
if (a->binary.len != b->binary.len)
|
||||
return 1;
|
||||
return memcmp(a->binary.buff, b->binary.buff, a->binary.len);
|
||||
case RDT_MAP:
|
||||
if (a->map.len != b->map.len)
|
||||
return 1;
|
||||
for (i = 0; i < a->map.len; i++) {
|
||||
if ((rv = rmsgpack_dom_value_cmp(&a->map.items[i].key, &b->map.items[i].key)) != 0)
|
||||
return rv;
|
||||
if ((rv = rmsgpack_dom_value_cmp(&a->map.items[i].value, &b->map.items[i].value)) != 0)
|
||||
return rv;
|
||||
}
|
||||
break;
|
||||
case RDT_ARRAY:
|
||||
if (a->array.len != b->array.len)
|
||||
return 1;
|
||||
for (i = 0; i < a->array.len; i++) {
|
||||
if ((rv = rmsgpack_dom_value_cmp(&a->array.items[i], &b->array.items[i])) != 0)
|
||||
return rv;
|
||||
}
|
||||
break;
|
||||
}
|
||||
switch (a->type)
|
||||
{
|
||||
case RDT_NULL:
|
||||
return 0;
|
||||
case RDT_BOOL:
|
||||
return a->bool_ == b->bool_ ? 0 : 1;
|
||||
case RDT_INT:
|
||||
return a->int_ == b->int_ ? 0 : 1;
|
||||
case RDT_UINT:
|
||||
return a->uint_ == b->uint_ ? 0 : 1;
|
||||
case RDT_STRING:
|
||||
if (a->string.len != b->string.len)
|
||||
return 1;
|
||||
return strncmp(a->string.buff, b->string.buff, a->string.len);
|
||||
case RDT_BINARY:
|
||||
if (a->binary.len != b->binary.len)
|
||||
return 1;
|
||||
return memcmp(a->binary.buff, b->binary.buff, a->binary.len);
|
||||
case RDT_MAP:
|
||||
if (a->map.len != b->map.len)
|
||||
return 1;
|
||||
for (i = 0; i < a->map.len; i++)
|
||||
{
|
||||
if ((rv = rmsgpack_dom_value_cmp(&a->map.items[i].key,
|
||||
&b->map.items[i].key)) != 0)
|
||||
return rv;
|
||||
if ((rv = rmsgpack_dom_value_cmp(&a->map.items[i].value,
|
||||
&b->map.items[i].value)) != 0)
|
||||
return rv;
|
||||
}
|
||||
break;
|
||||
case RDT_ARRAY:
|
||||
if (a->array.len != b->array.len)
|
||||
return 1;
|
||||
for (i = 0; i < a->array.len; i++)
|
||||
{
|
||||
if ((rv = rmsgpack_dom_value_cmp(&a->array.items[i],
|
||||
&b->array.items[i])) != 0)
|
||||
return rv;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void rmsgpack_dom_value_print(struct rmsgpack_dom_value * obj){
|
||||
unsigned i;
|
||||
void rmsgpack_dom_value_print(struct rmsgpack_dom_value *obj)
|
||||
{
|
||||
unsigned i;
|
||||
|
||||
switch (obj->type) {
|
||||
case RDT_NULL:
|
||||
printf("nil");
|
||||
break;
|
||||
case RDT_BOOL:
|
||||
if (obj->bool_)
|
||||
printf("true");
|
||||
else
|
||||
printf("false");
|
||||
break;
|
||||
case RDT_INT:
|
||||
switch (obj->type)
|
||||
{
|
||||
case RDT_NULL:
|
||||
printf("nil");
|
||||
break;
|
||||
case RDT_BOOL:
|
||||
if (obj->bool_)
|
||||
printf("true");
|
||||
else
|
||||
printf("false");
|
||||
break;
|
||||
case RDT_INT:
|
||||
#ifdef _WIN32
|
||||
printf("%I64d", (signed long long)obj->int_);
|
||||
printf("%I64d", (signed long long)obj->int_);
|
||||
#else
|
||||
printf("%lld", (signed long long)obj->int_);
|
||||
printf("%lld", (signed long long)obj->int_);
|
||||
#endif
|
||||
break;
|
||||
case RDT_UINT:
|
||||
break;
|
||||
case RDT_UINT:
|
||||
#ifdef _WIN32
|
||||
printf("%I64u", (unsigned long long)obj->uint_);
|
||||
printf("%I64u", (unsigned long long)obj->uint_);
|
||||
#else
|
||||
printf("%llu", (unsigned long long)obj->uint_);
|
||||
printf("%llu", (unsigned long long)obj->uint_);
|
||||
#endif
|
||||
break;
|
||||
case RDT_STRING:
|
||||
printf("\"%s\"", obj->string.buff);
|
||||
break;
|
||||
case RDT_BINARY:
|
||||
printf("\"");
|
||||
for (i = 0; i < obj->binary.len; i++)
|
||||
printf("%02X", (unsigned char) obj->binary.buff[i]);
|
||||
printf("\"");
|
||||
break;
|
||||
case RDT_MAP:
|
||||
printf("{");
|
||||
for (i = 0; i < obj->map.len; i++) {
|
||||
rmsgpack_dom_value_print(&obj->map.items[i].key);
|
||||
printf(": ");
|
||||
rmsgpack_dom_value_print(&obj->map.items[i].value);
|
||||
if (i < (obj->map.len - 1))
|
||||
printf(", ");
|
||||
}
|
||||
printf("}");
|
||||
break;
|
||||
case RDT_ARRAY:
|
||||
printf("[");
|
||||
for (i = 0; i < obj->array.len; i++) {
|
||||
rmsgpack_dom_value_print(&obj->array.items[i]);
|
||||
if (i < (obj->array.len - 1))
|
||||
printf(", ");
|
||||
}
|
||||
printf("]");
|
||||
}
|
||||
break;
|
||||
case RDT_STRING:
|
||||
printf("\"%s\"", obj->string.buff);
|
||||
break;
|
||||
case RDT_BINARY:
|
||||
printf("\"");
|
||||
for (i = 0; i < obj->binary.len; i++)
|
||||
printf("%02X", (unsigned char) obj->binary.buff[i]);
|
||||
printf("\"");
|
||||
break;
|
||||
case RDT_MAP:
|
||||
printf("{");
|
||||
for (i = 0; i < obj->map.len; i++)
|
||||
{
|
||||
rmsgpack_dom_value_print(&obj->map.items[i].key);
|
||||
printf(": ");
|
||||
rmsgpack_dom_value_print(&obj->map.items[i].value);
|
||||
if (i < (obj->map.len - 1))
|
||||
printf(", ");
|
||||
}
|
||||
printf("}");
|
||||
break;
|
||||
case RDT_ARRAY:
|
||||
printf("[");
|
||||
for (i = 0; i < obj->array.len; i++)
|
||||
{
|
||||
rmsgpack_dom_value_print(&obj->array.items[i]);
|
||||
if (i < (obj->array.len - 1))
|
||||
printf(", ");
|
||||
}
|
||||
printf("]");
|
||||
}
|
||||
}
|
||||
int rmsgpack_dom_write(
|
||||
int fd,
|
||||
const struct rmsgpack_dom_value * obj
|
||||
){
|
||||
unsigned i;
|
||||
int rv = 0;
|
||||
int written = 0;
|
||||
int rmsgpack_dom_write(int fd, const struct rmsgpack_dom_value *obj)
|
||||
{
|
||||
unsigned i;
|
||||
int rv = 0;
|
||||
int written = 0;
|
||||
|
||||
switch (obj->type) {
|
||||
case RDT_NULL:
|
||||
return rmsgpack_write_nil(fd);
|
||||
case RDT_BOOL:
|
||||
return rmsgpack_write_bool(fd, obj->bool_);
|
||||
case RDT_INT:
|
||||
return rmsgpack_write_int(fd, obj->int_);
|
||||
case RDT_UINT:
|
||||
return rmsgpack_write_uint(fd, obj->uint_);
|
||||
case RDT_STRING:
|
||||
return rmsgpack_write_string(fd, obj->string.buff, obj->string.len);
|
||||
case RDT_BINARY:
|
||||
return rmsgpack_write_bin(fd, obj->binary.buff, obj->binary.len);
|
||||
case RDT_MAP:
|
||||
if ((rv = rmsgpack_write_map_header(fd, obj->map.len)) < 0)
|
||||
return rv;
|
||||
written += rv;
|
||||
for (i = 0; i < obj->map.len; i++) {
|
||||
if ((rv = rmsgpack_dom_write(fd, &obj->map.items[i].key)) < 0)
|
||||
return rv;
|
||||
written += rv;
|
||||
if ((rv = rmsgpack_dom_write(fd, &obj->map.items[i].value)) < 0)
|
||||
return rv;
|
||||
written += rv;
|
||||
}
|
||||
break;
|
||||
case RDT_ARRAY:
|
||||
if ((rv = rmsgpack_write_array_header(fd, obj->array.len)) < 0)
|
||||
return rv;
|
||||
written += rv;
|
||||
for (i = 0; i < obj->array.len; i++) {
|
||||
if ((rv = rmsgpack_dom_write(fd, &obj->array.items[i])) < 0)
|
||||
return rv;
|
||||
written += rv;
|
||||
}
|
||||
}
|
||||
return written;
|
||||
switch (obj->type)
|
||||
{
|
||||
case RDT_NULL:
|
||||
return rmsgpack_write_nil(fd);
|
||||
case RDT_BOOL:
|
||||
return rmsgpack_write_bool(fd, obj->bool_);
|
||||
case RDT_INT:
|
||||
return rmsgpack_write_int(fd, obj->int_);
|
||||
case RDT_UINT:
|
||||
return rmsgpack_write_uint(fd, obj->uint_);
|
||||
case RDT_STRING:
|
||||
return rmsgpack_write_string(fd, obj->string.buff, obj->string.len);
|
||||
case RDT_BINARY:
|
||||
return rmsgpack_write_bin(fd, obj->binary.buff, obj->binary.len);
|
||||
case RDT_MAP:
|
||||
if ((rv = rmsgpack_write_map_header(fd, obj->map.len)) < 0)
|
||||
return rv;
|
||||
written += rv;
|
||||
|
||||
for (i = 0; i < obj->map.len; i++)
|
||||
{
|
||||
if ((rv = rmsgpack_dom_write(fd, &obj->map.items[i].key)) < 0)
|
||||
return rv;
|
||||
written += rv;
|
||||
if ((rv = rmsgpack_dom_write(fd, &obj->map.items[i].value)) < 0)
|
||||
return rv;
|
||||
written += rv;
|
||||
}
|
||||
break;
|
||||
case RDT_ARRAY:
|
||||
if ((rv = rmsgpack_write_array_header(fd, obj->array.len)) < 0)
|
||||
return rv;
|
||||
written += rv;
|
||||
|
||||
for (i = 0; i < obj->array.len; i++)
|
||||
{
|
||||
if ((rv = rmsgpack_dom_write(fd, &obj->array.items[i])) < 0)
|
||||
return rv;
|
||||
written += rv;
|
||||
}
|
||||
}
|
||||
return written;
|
||||
}
|
||||
|
||||
int rmsgpack_dom_read(
|
||||
int fd,
|
||||
struct rmsgpack_dom_value * out
|
||||
){
|
||||
int rv = 0;
|
||||
struct dom_reader_state s;
|
||||
s.i = 0;
|
||||
s.stack[0] = out;
|
||||
rv = rmsgpack_read(fd, &dom_reader_callbacks, &s);
|
||||
int rmsgpack_dom_read(int fd, struct rmsgpack_dom_value *out)
|
||||
{
|
||||
struct dom_reader_state s;
|
||||
int rv = 0;
|
||||
|
||||
if (rv < 0)
|
||||
rmsgpack_dom_value_free(out);
|
||||
s.i = 0;
|
||||
s.stack[0] = out;
|
||||
|
||||
return rv;
|
||||
rv = rmsgpack_read(fd, &dom_reader_callbacks, &s);
|
||||
|
||||
if (rv < 0)
|
||||
rmsgpack_dom_value_free(out);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
int rmsgpack_dom_read_into(int fd, ...)
|
||||
@ -387,18 +411,18 @@ int rmsgpack_dom_read_into(int fd, ...)
|
||||
va_list ap;
|
||||
struct rmsgpack_dom_value map;
|
||||
int rv;
|
||||
const char * key_name;
|
||||
const char *key_name;
|
||||
struct rmsgpack_dom_value key;
|
||||
struct rmsgpack_dom_value * value;
|
||||
int64_t * int_value;
|
||||
uint64_t * uint_value;
|
||||
int * bool_value;
|
||||
char * buff_value;
|
||||
struct rmsgpack_dom_value *value;
|
||||
int64_t *int_value;
|
||||
uint64_t *uint_value;
|
||||
int *bool_value;
|
||||
char *buff_value;
|
||||
uint64_t min_len;
|
||||
|
||||
int value_type = 0;
|
||||
|
||||
va_start(ap, fd);
|
||||
|
||||
rv = rmsgpack_dom_read(fd, &map);
|
||||
|
||||
(void)value_type;
|
||||
@ -418,14 +442,15 @@ int rmsgpack_dom_read_into(int fd, ...)
|
||||
while (1)
|
||||
{
|
||||
key_name = va_arg(ap, const char *);
|
||||
if (key_name == NULL)
|
||||
|
||||
if (!key_name)
|
||||
{
|
||||
rv = 0;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
key.type = RDT_STRING;
|
||||
key.string.len = strlen(key_name);
|
||||
key.type = RDT_STRING;
|
||||
key.string.len = strlen(key_name);
|
||||
key.string.buff = (char *) key_name;
|
||||
|
||||
value = rmsgpack_dom_value_map_value(&map, &key);
|
||||
@ -433,39 +458,34 @@ int rmsgpack_dom_read_into(int fd, ...)
|
||||
switch (value->type)
|
||||
{
|
||||
case RDT_INT:
|
||||
int_value = va_arg(ap, int64_t *);
|
||||
*int_value = value->int_;
|
||||
int_value = va_arg(ap, int64_t *);
|
||||
*int_value = value->int_;
|
||||
break;
|
||||
case RDT_BOOL:
|
||||
bool_value = va_arg(ap, int *);
|
||||
bool_value = va_arg(ap, int *);
|
||||
*bool_value = value->bool_;
|
||||
break;
|
||||
case RDT_UINT:
|
||||
uint_value = va_arg(ap, uint64_t *);
|
||||
uint_value = va_arg(ap, uint64_t *);
|
||||
*uint_value = value->uint_;
|
||||
break;
|
||||
case RDT_BINARY:
|
||||
buff_value = va_arg(ap, char *);
|
||||
uint_value = va_arg(ap, uint64_t *);
|
||||
buff_value = va_arg(ap, char *);
|
||||
uint_value = va_arg(ap, uint64_t *);
|
||||
*uint_value = value->binary.len;
|
||||
min_len = value->binary.len > *uint_value ? *uint_value : value->binary.len;
|
||||
memcpy(
|
||||
buff_value,
|
||||
value->binary.buff,
|
||||
min_len
|
||||
);
|
||||
min_len = (value->binary.len > *uint_value) ?
|
||||
*uint_value : value->binary.len;
|
||||
|
||||
memcpy(buff_value, value->binary.buff, min_len);
|
||||
break;
|
||||
case RDT_STRING:
|
||||
buff_value = va_arg(ap, char *);
|
||||
uint_value = va_arg(ap, uint64_t *);
|
||||
min_len = value->string.len + 1 > *uint_value ? *uint_value : value->string.len + 1;
|
||||
min_len = (value->string.len + 1 > *uint_value) ?
|
||||
*uint_value : value->string.len + 1;
|
||||
*uint_value = min_len;
|
||||
memcpy(
|
||||
buff_value,
|
||||
value->string.buff,
|
||||
min_len
|
||||
);
|
||||
|
||||
memcpy(buff_value, value->string.buff, min_len);
|
||||
break;
|
||||
default:
|
||||
rv = -1;
|
||||
|
Loading…
Reference in New Issue
Block a user