(libretro-db) Get rid of anon union

This commit is contained in:
twinaphex 2015-06-26 20:33:40 +02:00
parent 6e1fb7b94d
commit 0292f5c9b3
5 changed files with 179 additions and 167 deletions

View File

@ -237,100 +237,100 @@ static int database_cursor_iterate(libretrodb_cursor_t *cur,
db_info->analog_supported = -1;
db_info->rumble_supported = -1;
for (i = 0; i < item.map.len; i++)
for (i = 0; i < item.val.map.len; i++)
{
uint32_t value = 0;
struct rmsgpack_dom_value *key = &item.map.items[i].key;
struct rmsgpack_dom_value *val = &item.map.items[i].value;
struct rmsgpack_dom_value *key = &item.val.map.items[i].key;
struct rmsgpack_dom_value *val = &item.val.map.items[i].value;
if (!key || !val)
continue;
str = key->string.buff;
str = key->val.string.buff;
value = djb2_calculate(str);
switch (value)
{
case DB_CURSOR_SERIAL:
db_info->serial = strdup(val->string.buff);
db_info->serial = strdup(val->val.string.buff);
break;
case DB_CURSOR_ROM_NAME:
db_info->rom_name = strdup(val->string.buff);
db_info->rom_name = strdup(val->val.string.buff);
break;
case DB_CURSOR_NAME:
db_info->name = strdup(val->string.buff);
db_info->name = strdup(val->val.string.buff);
break;
case DB_CURSOR_DESCRIPTION:
db_info->description = strdup(val->string.buff);
db_info->description = strdup(val->val.string.buff);
break;
case DB_CURSOR_PUBLISHER:
db_info->publisher = strdup(val->string.buff);
db_info->publisher = strdup(val->val.string.buff);
break;
case DB_CURSOR_DEVELOPER:
db_info->developer = string_split(val->string.buff, "|");
db_info->developer = string_split(val->val.string.buff, "|");
break;
case DB_CURSOR_ORIGIN:
db_info->origin = strdup(val->string.buff);
db_info->origin = strdup(val->val.string.buff);
break;
case DB_CURSOR_FRANCHISE:
db_info->franchise = strdup(val->string.buff);
db_info->franchise = strdup(val->val.string.buff);
break;
case DB_CURSOR_BBFC_RATING:
db_info->bbfc_rating = strdup(val->string.buff);
db_info->bbfc_rating = strdup(val->val.string.buff);
break;
case DB_CURSOR_ESRB_RATING:
db_info->esrb_rating = strdup(val->string.buff);
db_info->esrb_rating = strdup(val->val.string.buff);
break;
case DB_CURSOR_ELSPA_RATING:
db_info->elspa_rating = strdup(val->string.buff);
db_info->elspa_rating = strdup(val->val.string.buff);
break;
case DB_CURSOR_CERO_RATING:
db_info->cero_rating = strdup(val->string.buff);
db_info->cero_rating = strdup(val->val.string.buff);
break;
case DB_CURSOR_PEGI_RATING:
db_info->pegi_rating = strdup(val->string.buff);
db_info->pegi_rating = strdup(val->val.string.buff);
break;
case DB_CURSOR_ENHANCEMENT_HW:
db_info->enhancement_hw = strdup(val->string.buff);
db_info->enhancement_hw = strdup(val->val.string.buff);
break;
case DB_CURSOR_EDGE_MAGAZINE_REVIEW:
db_info->edge_magazine_review = strdup(val->string.buff);
db_info->edge_magazine_review = strdup(val->val.string.buff);
break;
case DB_CURSOR_EDGE_MAGAZINE_RATING:
db_info->edge_magazine_rating = val->uint_;
db_info->edge_magazine_rating = val->val.uint_;
break;
case DB_CURSOR_EDGE_MAGAZINE_ISSUE:
db_info->edge_magazine_issue = val->uint_;
db_info->edge_magazine_issue = val->val.uint_;
break;
case DB_CURSOR_FAMITSU_MAGAZINE_RATING:
db_info->famitsu_magazine_rating = val->uint_;
db_info->famitsu_magazine_rating = val->val.uint_;
break;
case DB_CURSOR_MAX_USERS:
db_info->max_users = val->uint_;
db_info->max_users = val->val.uint_;
break;
case DB_CURSOR_RELEASEDATE_MONTH:
db_info->releasemonth = val->uint_;
db_info->releasemonth = val->val.uint_;
break;
case DB_CURSOR_RELEASEDATE_YEAR:
db_info->releaseyear = val->uint_;
db_info->releaseyear = val->val.uint_;
break;
case DB_CURSOR_RUMBLE_SUPPORTED:
db_info->rumble_supported = val->uint_;
db_info->rumble_supported = val->val.uint_;
break;
case DB_CURSOR_ANALOG_SUPPORTED:
db_info->analog_supported = val->uint_;
db_info->analog_supported = val->val.uint_;
break;
case DB_CURSOR_SIZE:
db_info->size = val->uint_;
db_info->size = val->val.uint_;
break;
case DB_CURSOR_CHECKSUM_CRC32:
db_info->crc32 = swap_if_little32(*(uint32_t*)val->binary.buff);
db_info->crc32 = swap_if_little32(*(uint32_t*)val->val.binary.buff);
break;
case DB_CURSOR_CHECKSUM_SHA1:
db_info->sha1 = bin_to_hex_alloc((uint8_t*)val->binary.buff, val->binary.len);
db_info->sha1 = bin_to_hex_alloc((uint8_t*)val->val.binary.buff, val->val.binary.len);
break;
case DB_CURSOR_CHECKSUM_MD5:
db_info->md5 = bin_to_hex_alloc((uint8_t*)val->binary.buff, val->binary.len);
db_info->md5 = bin_to_hex_alloc((uint8_t*)val->val.binary.buff, val->val.binary.len);
break;
default:
RARCH_LOG("Unknown key: %s\n", str);

View File

@ -56,18 +56,18 @@ static int validate_document(const struct rmsgpack_dom_value * doc)
if (doc->type != RDT_MAP)
return -EINVAL;
for (i = 0; i < doc->map.len; i++)
for (i = 0; i < doc->val.map.len; i++)
{
key = doc->map.items[i].key;
value = doc->map.items[i].value;
key = doc->val.map.items[i].key;
value = doc->val.map.items[i].value;
if (key.type != RDT_STRING)
return -EINVAL;
if (key.string.len <= 0)
if (key.val.string.len <= 0)
return -EINVAL;
if (key.string.buff[0] == '$')
if (key.val.string.buff[0] == '$')
return -EINVAL;
if (value.type != RDT_MAP)
@ -428,10 +428,10 @@ int libretrodb_create_index(libretrodb_t *db,
}
key.type = RDT_STRING;
key.string.len = strlen(field_name);
key.val.string.len = strlen(field_name);
/* We know we aren't going to change it */
key.string.buff = (char*)field_name;
key.val.string.buff = (char*)field_name;
while (libretrodb_cursor_read_item(&cur, &item) == 0)
{
@ -458,7 +458,7 @@ int libretrodb_create_index(libretrodb_t *db,
goto clean;
}
if (field->binary.len == 0)
if (field->val.binary.len == 0)
{
rv = -EINVAL;
printf("field is empty\n");
@ -466,8 +466,8 @@ int libretrodb_create_index(libretrodb_t *db,
}
if (field_size == 0)
field_size = field->binary.len;
else if (field->binary.len != field_size)
field_size = field->val.binary.len;
else if (field->val.binary.len != field_size)
{
rv = -EINVAL;
printf("field is not of correct size\n");
@ -481,7 +481,7 @@ int libretrodb_create_index(libretrodb_t *db,
goto clean;
}
memcpy(buff, field->binary.buff, field_size);
memcpy(buff, field->val.binary.buff, field_size);
buff_u64 = (uint64_t *)buff + field_size;

View File

@ -190,12 +190,12 @@ static struct rmsgpack_dom_value is_true(struct rmsgpack_dom_value input,
memset(&res, 0, sizeof(res));
res.type = RDT_BOOL;
res.bool_ = 0;
res.val.bool_ = 0;
if (argc > 0 || input.type != RDT_BOOL)
res.bool_ = 0;
res.val.bool_ = 0;
else
res.bool_ = input.bool_;
res.val.bool_ = input.val.bool_;
return res;
}
@ -210,21 +210,21 @@ static struct rmsgpack_dom_value equals(struct rmsgpack_dom_value input,
res.type = RDT_BOOL;
if (argc != 1)
res.bool_ = 0;
res.val.bool_ = 0;
else
{
arg = argv[0];
if (arg.type != AT_VALUE)
res.bool_ = 0;
res.val.bool_ = 0;
else
{
if (input.type == RDT_UINT && arg.value.type == RDT_INT)
{
arg.value.type = RDT_UINT;
arg.value.uint_ = arg.value.int_;
arg.value.val.uint_ = arg.value.val.int_;
}
res.bool_ = (rmsgpack_dom_value_cmp(&input, &arg.value) == 0);
res.val.bool_ = (rmsgpack_dom_value_cmp(&input, &arg.value) == 0);
}
}
return res;
@ -238,7 +238,7 @@ static struct rmsgpack_dom_value operator_or(struct rmsgpack_dom_value input,
memset(&res, 0, sizeof(res));
res.type = RDT_BOOL;
res.bool_ = 0;
res.val.bool_ = 0;
for (i = 0; i < argc; i++)
{
@ -252,7 +252,7 @@ static struct rmsgpack_dom_value operator_or(struct rmsgpack_dom_value input,
), 0, NULL);
}
if (res.bool_)
if (res.val.bool_)
return res;
}
@ -268,7 +268,7 @@ static struct rmsgpack_dom_value between(struct rmsgpack_dom_value input,
memset(&res, 0, sizeof(res));
res.type = RDT_BOOL;
res.bool_ = 0;
res.val.bool_ = 0;
(void)i;
@ -282,10 +282,10 @@ static struct rmsgpack_dom_value between(struct rmsgpack_dom_value input,
switch (input.type)
{
case RDT_INT:
res.bool_ = ((input.int_ >= argv[0].value.int_) && (input.int_ <= argv[1].value.int_));
res.val.bool_ = ((input.val.int_ >= argv[0].value.val.int_) && (input.val.int_ <= argv[1].value.val.int_));
break;
case RDT_UINT:
res.bool_ = (((unsigned)input.int_ >= argv[0].value.uint_) && (input.int_ <= argv[1].value.int_));
res.val.bool_ = (((unsigned)input.val.int_ >= argv[0].value.val.uint_) && (input.val.int_ <= argv[1].value.val.int_));
break;
default:
return res;
@ -302,7 +302,7 @@ static struct rmsgpack_dom_value operator_and(struct rmsgpack_dom_value input,
memset(&res, 0, sizeof(res));
res.type = RDT_BOOL;
res.bool_ = 0;
res.val.bool_ = 0;
for (i = 0; i < argc; i++)
{
@ -318,7 +318,7 @@ static struct rmsgpack_dom_value operator_and(struct rmsgpack_dom_value input,
0, NULL);
}
if (!res.bool_)
if (!res.val.bool_)
return res;
}
return res;
@ -332,7 +332,7 @@ static struct rmsgpack_dom_value q_glob(struct rmsgpack_dom_value input,
memset(&res, 0, sizeof(res));
res.type = RDT_BOOL;
res.bool_ = 0;
res.val.bool_ = 0;
(void)i;
@ -342,9 +342,9 @@ static struct rmsgpack_dom_value q_glob(struct rmsgpack_dom_value input,
return res;
if (input.type != RDT_STRING)
return res;
res.bool_ = rl_fnmatch(
argv[0].value.string.buff,
input.string.buff,
res.val.bool_ = rl_fnmatch(
argv[0].value.val.string.buff,
input.val.string.buff,
0
) == 0;
return res;
@ -362,11 +362,11 @@ static struct rmsgpack_dom_value all_map(struct rmsgpack_dom_value input,
nil_value.type = RDT_NULL;
res.type = RDT_BOOL;
res.bool_ = 1;
res.val.bool_ = 1;
if (argc % 2 != 0)
{
res.bool_ = 0;
res.val.bool_ = 0;
return res;
}
@ -378,7 +378,7 @@ static struct rmsgpack_dom_value all_map(struct rmsgpack_dom_value input,
arg = argv[i];
if (arg.type != AT_VALUE)
{
res.bool_ = 0;
res.val.bool_ = 0;
goto clean;
}
value = rmsgpack_dom_value_map_value(&input, &arg.value);
@ -396,7 +396,7 @@ static struct rmsgpack_dom_value all_map(struct rmsgpack_dom_value input,
), 0, NULL);
value = NULL;
}
if (!res.bool_)
if (!res.val.bool_)
break;
}
clean:
@ -520,20 +520,20 @@ static struct buffer parse_string(struct buffer buff,
{
size_t count;
value->type = is_binstr ? RDT_BINARY : RDT_STRING;
value->string.len = (buff.data + buff.offset) - str_start - 1;
value->val.string.len = (buff.data + buff.offset) - str_start - 1;
count = is_binstr ? (value->string.len + 1) / 2 : (value->string.len + 1);
value->string.buff = (char*)calloc(count, sizeof(char));
count = is_binstr ? (value->val.string.len + 1) / 2 : (value->val.string.len + 1);
value->val.string.buff = (char*)calloc(count, sizeof(char));
if (!value->string.buff)
if (!value->val.string.buff)
raise_enomem(error);
else if (is_binstr)
{
unsigned i, j;
unsigned i;
const char *tok = str_start;
unsigned j = 0;
j = 0;
for (i = 0; i < value->string.len; i += 2)
for (i = 0; i < value->val.string.len; i += 2)
{
uint8_t hi, lo;
char hic = tok[i];
@ -549,13 +549,13 @@ static struct buffer parse_string(struct buffer buff,
else
lo = (loc - 'A') + 10;
value->string.buff[j++] = hi * 16 + lo;
value->val.string.buff[j++] = hi * 16 + lo;
}
value->string.len = j;
value->val.string.len = j;
}
else
memcpy(value->string.buff, str_start, value->string.len);
memcpy(value->val.string.buff, str_start, value->val.string.len);
}
return buff;
}
@ -571,7 +571,7 @@ static struct buffer parse_integer(struct buffer buff,
#else
"%lld",
#endif
(signed long long*)&value->int_) == 0)
(signed long long*)&value->val.int_) == 0)
raise_expected_number(buff.offset, error);
else
{
@ -595,13 +595,13 @@ static struct buffer parse_value(struct buffer buff,
{
buff.offset += strlen("true");
value->type = RDT_BOOL;
value->bool_ = 1;
value->val.bool_ = 1;
}
else if (peek(buff, "false"))
{
buff.offset += strlen("false");
value->type = RDT_BOOL;
value->bool_ = 0;
value->val.bool_ = 0;
}
else if (peek(buff, "b") || peek(buff, "\"") || peek(buff, "'"))
buff = parse_string(buff, value, error);
@ -768,17 +768,17 @@ static struct buffer parse_table(struct buffer buff,
if (!*error)
{
args[argi].value.type = RDT_STRING;
args[argi].value.string.len = ident_len;
args[argi].value.string.buff = (char*)calloc(
args[argi].value.val.string.len = ident_len;
args[argi].value.val.string.buff = (char*)calloc(
ident_len + 1,
sizeof(char)
);
if (!args[argi].value.string.buff)
if (!args[argi].value.val.string.buff)
goto clean;
strncpy(
args[argi].value.string.buff,
args[argi].value.val.string.buff,
ident_name,
ident_len
);
@ -952,5 +952,5 @@ int libretrodb_query_filter(libretrodb_query_t *q,
{
struct invocation inv = ((struct query *)q)->root;
struct rmsgpack_dom_value res = inv.func(*v, inv.argc, inv.argv);
return (res.type == RDT_BOOL && res.bool_);
return (res.type == RDT_BOOL && res.val.bool_);
}

View File

@ -99,7 +99,7 @@ static int dom_read_bool(int value, void *data)
(struct rmsgpack_dom_value*)dom_reader_state_pop(dom_state);
v->type = RDT_BOOL;
v->bool_ = value;
v->val.bool_ = value;
return 0;
}
@ -110,7 +110,7 @@ static int dom_read_int(int64_t value, void *data)
(struct rmsgpack_dom_value*)dom_reader_state_pop(dom_state);
v->type = RDT_INT;
v->int_ = value;
v->val.int_ = value;
return 0;
}
@ -121,7 +121,7 @@ static int dom_read_uint(uint64_t value, void *data)
(struct rmsgpack_dom_value*)dom_reader_state_pop(dom_state);
v->type = RDT_UINT;
v->uint_ = value;
v->val.uint_ = value;
return 0;
}
@ -132,8 +132,8 @@ static int dom_read_string(char *value, uint32_t len, void *data)
(struct rmsgpack_dom_value*)dom_reader_state_pop(dom_state);
v->type = RDT_STRING;
v->string.len = len;
v->string.buff = value;
v->val.string.len = len;
v->val.string.buff = value;
return 0;
}
@ -144,8 +144,8 @@ static int dom_read_bin(void *value, uint32_t len, void *data)
(struct rmsgpack_dom_value*)dom_reader_state_pop(dom_state);
v->type = RDT_BINARY;
v->binary.len = len;
v->binary.buff = (char *)value;
v->val.binary.len = len;
v->val.binary.buff = (char *)value;
return 0;
}
@ -157,8 +157,8 @@ static int dom_read_map_start(uint32_t len, void *data)
struct rmsgpack_dom_value *v = dom_reader_state_pop(dom_state);
v->type = RDT_MAP;
v->map.len = len;
v->map.items = NULL;
v->val.map.len = len;
v->val.map.items = NULL;
items = (struct rmsgpack_dom_pair *)calloc(len,
sizeof(struct rmsgpack_dom_pair));
@ -166,7 +166,7 @@ static int dom_read_map_start(uint32_t len, void *data)
if (!items)
return -ENOMEM;
v->map.items = items;
v->val.map.items = items;
for (i = 0; i < len; i++)
{
@ -186,16 +186,16 @@ static int dom_read_array_start(uint32_t len, void *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;
v->array.items = NULL;
v->type = RDT_ARRAY;
v->val.array.len = len;
v->val.array.items = NULL;
items = (struct rmsgpack_dom_value *)calloc(len, sizeof(struct rmsgpack_dom_pair));
if (!items)
return -ENOMEM;
v->array.items = items;
v->val.array.items = items;
for (i = 0; i < len; i++)
{
@ -224,23 +224,27 @@ void rmsgpack_dom_value_free(struct rmsgpack_dom_value *v)
switch (v->type)
{
case RDT_STRING:
free(v->string.buff);
free(v->val.string.buff);
v->val.string.buff = NULL;
break;
case RDT_BINARY:
free(v->binary.buff);
free(v->val.binary.buff);
v->val.binary.buff = NULL;
break;
case RDT_MAP:
for (i = 0; i < v->map.len; i++)
for (i = 0; i < v->val.map.len; i++)
{
rmsgpack_dom_value_free(&v->map.items[i].key);
rmsgpack_dom_value_free(&v->map.items[i].value);
rmsgpack_dom_value_free(&v->val.map.items[i].key);
rmsgpack_dom_value_free(&v->val.map.items[i].value);
}
free(v->map.items);
free(v->val.map.items);
v->val.map.items = NULL;
break;
case RDT_ARRAY:
for (i = 0; i < v->array.len; i++)
rmsgpack_dom_value_free(&v->array.items[i]);
free(v->array.items);
for (i = 0; i < v->val.array.len; i++)
rmsgpack_dom_value_free(&v->val.array.items[i]);
free(v->val.array.items);
v->val.array.items = NULL;
break;
case RDT_NULL:
case RDT_INT:
@ -259,10 +263,10 @@ struct rmsgpack_dom_value *rmsgpack_dom_value_map_value(
if (map->type != RDT_MAP)
return NULL;
for (i = 0; i < map->map.len; i++)
for (i = 0; i < map->val.map.len; i++)
{
if (rmsgpack_dom_value_cmp(key, &map->map.items[i].key) == 0)
return &map->map.items[i].value;
if (rmsgpack_dom_value_cmp(key, &map->val.map.items[i].key) == 0)
return &map->val.map.items[i].value;
}
return NULL;
}
@ -286,39 +290,39 @@ int rmsgpack_dom_value_cmp(
case RDT_NULL:
return 0;
case RDT_BOOL:
return (a->bool_ == b->bool_) ? 0 : 1;
return (a->val.bool_ == b->val.bool_) ? 0 : 1;
case RDT_INT:
return (a->int_ == b->int_) ? 0 : 1;
return (a->val.int_ == b->val.int_) ? 0 : 1;
case RDT_UINT:
return (a->uint_ == b->uint_) ? 0 : 1;
return (a->val.uint_ == b->val.uint_) ? 0 : 1;
case RDT_STRING:
if (a->string.len != b->string.len)
if (a->val.string.len != b->val.string.len)
return 1;
return strncmp(a->string.buff, b->string.buff, a->string.len);
return strncmp(a->val.string.buff, b->val.string.buff, a->val.string.len);
case RDT_BINARY:
if (a->binary.len != b->binary.len)
if (a->val.binary.len != b->val.binary.len)
return 1;
return memcmp(a->binary.buff, b->binary.buff, a->binary.len);
return memcmp(a->val.binary.buff, b->val.binary.buff, a->val.binary.len);
case RDT_MAP:
if (a->map.len != b->map.len)
if (a->val.map.len != b->val.map.len)
return 1;
for (i = 0; i < a->map.len; i++)
for (i = 0; i < a->val.map.len; i++)
{
if ((rv = rmsgpack_dom_value_cmp(&a->map.items[i].key,
&b->map.items[i].key)) != 0)
if ((rv = rmsgpack_dom_value_cmp(&a->val.map.items[i].key,
&b->val.map.items[i].key)) != 0)
return rv;
if ((rv = rmsgpack_dom_value_cmp(&a->map.items[i].value,
&b->map.items[i].value)) != 0)
if ((rv = rmsgpack_dom_value_cmp(&a->val.map.items[i].value,
&b->val.map.items[i].value)) != 0)
return rv;
}
break;
case RDT_ARRAY:
if (a->array.len != b->array.len)
if (a->val.array.len != b->val.array.len)
return 1;
for (i = 0; i < a->array.len; i++)
for (i = 0; i < a->val.array.len; i++)
{
if ((rv = rmsgpack_dom_value_cmp(&a->array.items[i],
&b->array.items[i])) != 0)
if ((rv = rmsgpack_dom_value_cmp(&a->val.array.items[i],
&b->val.array.items[i])) != 0)
return rv;
}
break;
@ -337,44 +341,44 @@ void rmsgpack_dom_value_print(struct rmsgpack_dom_value *obj)
printf("nil");
break;
case RDT_BOOL:
if (obj->bool_)
if (obj->val.bool_)
printf("true");
else
printf("false");
break;
case RDT_INT:
puts_i64(obj -> int_);
puts_i64(obj->val.int_);
break;
case RDT_UINT:
puts_u64(obj -> uint_);
puts_u64(obj->val.uint_);
break;
case RDT_STRING:
printf("\"%s\"", obj->string.buff);
printf("\"%s\"", obj->val.string.buff);
break;
case RDT_BINARY:
printf("\"");
for (i = 0; i < obj->binary.len; i++)
printf("%02X", (unsigned char) obj->binary.buff[i]);
for (i = 0; i < obj->val.binary.len; i++)
printf("%02X", (unsigned char) obj->val.binary.buff[i]);
printf("\"");
break;
case RDT_MAP:
printf("{");
for (i = 0; i < obj->map.len; i++)
for (i = 0; i < obj->val.map.len; i++)
{
rmsgpack_dom_value_print(&obj->map.items[i].key);
rmsgpack_dom_value_print(&obj->val.map.items[i].key);
printf(": ");
rmsgpack_dom_value_print(&obj->map.items[i].value);
if (i < (obj->map.len - 1))
rmsgpack_dom_value_print(&obj->val.map.items[i].value);
if (i < (obj->val.map.len - 1))
printf(", ");
}
printf("}");
break;
case RDT_ARRAY:
printf("[");
for (i = 0; i < obj->array.len; i++)
for (i = 0; i < obj->val.array.len; i++)
{
rmsgpack_dom_value_print(&obj->array.items[i]);
if (i < (obj->array.len - 1))
rmsgpack_dom_value_print(&obj->val.array.items[i]);
if (i < (obj->val.array.len - 1))
printf(", ");
}
printf("]");
@ -391,38 +395,38 @@ int rmsgpack_dom_write(FILE *fp, const struct rmsgpack_dom_value *obj)
case RDT_NULL:
return rmsgpack_write_nil(fp);
case RDT_BOOL:
return rmsgpack_write_bool(fp, obj->bool_);
return rmsgpack_write_bool(fp, obj->val.bool_);
case RDT_INT:
return rmsgpack_write_int(fp, obj->int_);
return rmsgpack_write_int(fp, obj->val.int_);
case RDT_UINT:
return rmsgpack_write_uint(fp, obj->uint_);
return rmsgpack_write_uint(fp, obj->val.uint_);
case RDT_STRING:
return rmsgpack_write_string(fp, obj->string.buff, obj->string.len);
return rmsgpack_write_string(fp, obj->val.string.buff, obj->val.string.len);
case RDT_BINARY:
return rmsgpack_write_bin(fp, obj->binary.buff, obj->binary.len);
return rmsgpack_write_bin(fp, obj->val.binary.buff, obj->val.binary.len);
case RDT_MAP:
if ((rv = rmsgpack_write_map_header(fp, obj->map.len)) < 0)
if ((rv = rmsgpack_write_map_header(fp, obj->val.map.len)) < 0)
return rv;
written += rv;
for (i = 0; i < obj->map.len; i++)
for (i = 0; i < obj->val.map.len; i++)
{
if ((rv = rmsgpack_dom_write(fp, &obj->map.items[i].key)) < 0)
if ((rv = rmsgpack_dom_write(fp, &obj->val.map.items[i].key)) < 0)
return rv;
written += rv;
if ((rv = rmsgpack_dom_write(fp, &obj->map.items[i].value)) < 0)
if ((rv = rmsgpack_dom_write(fp, &obj->val.map.items[i].value)) < 0)
return rv;
written += rv;
}
break;
case RDT_ARRAY:
if ((rv = rmsgpack_write_array_header(fp, obj->array.len)) < 0)
if ((rv = rmsgpack_write_array_header(fp, obj->val.array.len)) < 0)
return rv;
written += rv;
for (i = 0; i < obj->array.len; i++)
for (i = 0; i < obj->val.array.len; i++)
{
if ((rv = rmsgpack_dom_write(fp, &obj->array.items[i])) < 0)
if ((rv = rmsgpack_dom_write(fp, &obj->val.array.items[i])) < 0)
return rv;
written += rv;
}
@ -489,8 +493,8 @@ int rmsgpack_dom_read_into(FILE *fp, ...)
}
key.type = RDT_STRING;
key.string.len = strlen(key_name);
key.string.buff = (char *) key_name;
key.val.string.len = strlen(key_name);
key.val.string.buff = (char *) key_name;
value = rmsgpack_dom_value_map_value(&map, &key);
@ -498,33 +502,33 @@ int rmsgpack_dom_read_into(FILE *fp, ...)
{
case RDT_INT:
int_value = va_arg(ap, int64_t *);
*int_value = value->int_;
*int_value = value->val.int_;
break;
case RDT_BOOL:
bool_value = va_arg(ap, int *);
*bool_value = value->bool_;
*bool_value = value->val.bool_;
break;
case RDT_UINT:
uint_value = va_arg(ap, uint64_t *);
*uint_value = value->uint_;
*uint_value = value->val.uint_;
break;
case RDT_BINARY:
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;
*uint_value = value->val.binary.len;
min_len = (value->val.binary.len > *uint_value) ?
*uint_value : value->val.binary.len;
memcpy(buff_value, value->binary.buff, min_len);
memcpy(buff_value, value->val.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->val.string.len + 1 > *uint_value) ?
*uint_value : value->val.string.len + 1;
*uint_value = min_len;
memcpy(buff_value, value->string.buff, min_len);
memcpy(buff_value, value->val.string.buff, min_len);
break;
default:
rv = -1;

View File

@ -24,24 +24,32 @@ struct rmsgpack_dom_value {
union {
uint64_t uint_;
int64_t int_;
struct {
struct
{
uint32_t len;
char * buff;
} string;
struct {
struct
{
uint32_t len;
char * buff;
} binary;
int bool_;
struct {
struct
{
uint32_t len;
struct rmsgpack_dom_pair * items;
} map;
struct {
struct
{
uint32_t len;
struct rmsgpack_dom_value * items;
} array;
};
} val;
};
struct rmsgpack_dom_pair {