1
0
mirror of https://github.com/libretro/RetroArch synced 2024-07-08 12:15:49 +00:00

No more anon unions

This commit is contained in:
twinaphex 2015-09-17 06:39:17 +02:00
parent b32b17f4ae
commit 139be32aaa
5 changed files with 179 additions and 173 deletions

View File

@ -50,18 +50,18 @@ static int validate_document(const struct rmsgpack_dom_value * doc)
if (doc->type != RDT_MAP) if (doc->type != RDT_MAP)
return -EINVAL; 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; key = doc->val.map.items[i].key;
value = doc->map.items[i].value; value = doc->val.map.items[i].value;
if (key.type != RDT_STRING) if (key.type != RDT_STRING)
return -EINVAL; return -EINVAL;
if (key.string.len <= 0) if (key.val.string.len <= 0)
return -EINVAL; return -EINVAL;
if (key.string.buff[0] == '$') if (key.val.string.buff[0] == '$')
return -EINVAL; return -EINVAL;
if (value.type != RDT_MAP) if (value.type != RDT_MAP)
@ -419,10 +419,10 @@ int libretrodb_create_index(libretrodb_t *db,
} }
key.type = RDT_STRING; 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 */ /* 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) while (libretrodb_cursor_read_item(&cur, &item) == 0)
{ {
@ -449,7 +449,7 @@ int libretrodb_create_index(libretrodb_t *db,
goto clean; goto clean;
} }
if (field->binary.len == 0) if (field->val.binary.len == 0)
{ {
rv = -EINVAL; rv = -EINVAL;
printf("field is empty\n"); printf("field is empty\n");
@ -457,8 +457,8 @@ int libretrodb_create_index(libretrodb_t *db,
} }
if (field_size == 0) if (field_size == 0)
field_size = field->binary.len; field_size = field->val.binary.len;
else if (field->binary.len != field_size) else if (field->val.binary.len != field_size)
{ {
rv = -EINVAL; rv = -EINVAL;
printf("field is not of correct size\n"); printf("field is not of correct size\n");
@ -472,7 +472,7 @@ int libretrodb_create_index(libretrodb_t *db,
goto clean; goto clean;
} }
memcpy(buff, field->binary.buff, field_size); memcpy(buff, field->val.binary.buff, field_size);
buff_u64 = (uint64_t *)buff + field_size; buff_u64 = (uint64_t *)buff + field_size;

View File

@ -21,39 +21,39 @@ int libretrodb_lua_to_rmsgpack_value(
lua_Number tmp_num; lua_Number tmp_num;
out->type = RDT_MAP; out->type = RDT_MAP;
out->map.len = 0; out->val.map.len = 0;
out->map.items = calloc(MAX_FIELDS, sizeof(struct rmsgpack_dom_pair)); out->val.map.items = calloc(MAX_FIELDS, sizeof(struct rmsgpack_dom_pair));
lua_pushnil(L); lua_pushnil(L);
while (lua_next(L, index - 1) != 0) { while (lua_next(L, index - 1) != 0) {
if (out->map.len > MAX_FIELDS) { if (out->val.map.len > MAX_FIELDS) {
printf("skipping due to too many keys\n"); printf("skipping due to too many keys\n");
} else if (!lua_isstring(L, key_idx)) { } else if (!lua_isstring(L, key_idx)) {
printf("skipping non string key\n"); printf("skipping non string key\n");
} else if (lua_isnil(L, value_idx)) { } else if (lua_isnil(L, value_idx)) {
// Skipping nil value fields to save disk space // Skipping nil value fields to save disk space
} else { } else {
i = out->map.len; i = out->val.map.len;
tmp_buff = strdup(lua_tostring(L, key_idx)); tmp_buff = strdup(lua_tostring(L, key_idx));
out->map.items[i].key.type = RDT_STRING; out->val.map.items[i].key.type = RDT_STRING;
out->map.items[i].key.string.len = strlen(tmp_buff); out->val.map.items[i].key.val.string.len = strlen(tmp_buff);
out->map.items[i].key.string.buff = tmp_buff; out->val.map.items[i].key.val.string.buff = tmp_buff;
tmp_value = &out->map.items[i].value; tmp_value = &out->val.map.items[i].value;
switch (lua_type(L, value_idx)) { switch (lua_type(L, value_idx)) {
case LUA_TNUMBER: case LUA_TNUMBER:
tmp_num = lua_tonumber(L, value_idx); tmp_num = lua_tonumber(L, value_idx);
tmp_value->type = RDT_INT; tmp_value->type = RDT_INT;
tmp_value->int_ = tmp_num; tmp_value->val.int_ = tmp_num;
break; break;
case LUA_TBOOLEAN: case LUA_TBOOLEAN:
tmp_value->type = RDT_BOOL; tmp_value->type = RDT_BOOL;
tmp_value->bool_ = lua_toboolean(L, value_idx); tmp_value->val.bool_ = lua_toboolean(L, value_idx);
break; break;
case LUA_TSTRING: case LUA_TSTRING:
tmp_buff = strdup(lua_tostring(L, value_idx)); tmp_buff = strdup(lua_tostring(L, value_idx));
tmp_value->type = RDT_STRING; tmp_value->type = RDT_STRING;
tmp_value->string.len = strlen(tmp_buff); tmp_value->val.string.len = strlen(tmp_buff);
tmp_value->string.buff = tmp_buff; tmp_value->val.string.buff = tmp_buff;
break; break;
case LUA_TTABLE: case LUA_TTABLE:
lua_getfield(L, value_idx, "binary"); lua_getfield(L, value_idx, "binary");
@ -66,7 +66,7 @@ int libretrodb_lua_to_rmsgpack_value(
} else { } else {
tmp_num = lua_tonumber(L, -1); tmp_num = lua_tonumber(L, -1);
tmp_value->type = RDT_UINT; tmp_value->type = RDT_UINT;
tmp_value->uint_ = tmp_num; tmp_value->val.uint_ = tmp_num;
lua_pop(L, 1); lua_pop(L, 1);
} }
} else { } else {
@ -74,8 +74,8 @@ int libretrodb_lua_to_rmsgpack_value(
tmp_buff = malloc(tmp_len); tmp_buff = malloc(tmp_len);
memcpy(tmp_buff, tmp_string, tmp_len); memcpy(tmp_buff, tmp_string, tmp_len);
tmp_value->type = RDT_BINARY; tmp_value->type = RDT_BINARY;
tmp_value->binary.len = tmp_len; tmp_value->val.binary.len = tmp_len;
tmp_value->binary.buff = tmp_buff; tmp_value->val.binary.buff = tmp_buff;
lua_pop(L, 1); lua_pop(L, 1);
} }
break; break;
@ -83,7 +83,7 @@ int libretrodb_lua_to_rmsgpack_value(
set_nil: set_nil:
tmp_value->type = RDT_NULL; tmp_value->type = RDT_NULL;
} }
out->map.len++; out->val.map.len++;
} }
lua_pop(L, 1); lua_pop(L, 1);
} }

View File

@ -187,13 +187,13 @@ static struct rmsgpack_dom_value is_true(struct rmsgpack_dom_value input,
{ {
struct rmsgpack_dom_value res; struct rmsgpack_dom_value res;
res.type = RDT_BOOL; res.type = RDT_BOOL;
res.bool_ = 0; res.val.bool_ = 0;
if (argc > 0 || input.type != RDT_BOOL) if (argc > 0 || input.type != RDT_BOOL)
res.bool_ = 0; res.val.bool_ = 0;
else else
res.bool_ = input.bool_; res.val.bool_ = input.val.bool_;
return res; return res;
} }
@ -207,21 +207,21 @@ static struct rmsgpack_dom_value equals(struct rmsgpack_dom_value input,
res.type = RDT_BOOL; res.type = RDT_BOOL;
if (argc != 1) if (argc != 1)
res.bool_ = 0; res.val.bool_ = 0;
else else
{ {
arg = argv[0]; arg = argv[0];
if (arg.type != AT_VALUE) if (arg.type != AT_VALUE)
res.bool_ = 0; res.val.bool_ = 0;
else else
{ {
if (input.type == RDT_UINT && arg.value.type == RDT_INT) if (input.type == RDT_UINT && arg.value.type == RDT_INT)
{ {
arg.value.type = RDT_UINT; 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; return res;
@ -233,8 +233,8 @@ static struct rmsgpack_dom_value operator_or(struct rmsgpack_dom_value input,
struct rmsgpack_dom_value res; struct rmsgpack_dom_value res;
unsigned i; unsigned i;
res.type = RDT_BOOL; res.type = RDT_BOOL;
res.bool_ = 0; res.val.bool_ = 0;
for (i = 0; i < argc; i++) for (i = 0; i < argc; i++)
{ {
@ -248,7 +248,7 @@ static struct rmsgpack_dom_value operator_or(struct rmsgpack_dom_value input,
), 0, NULL); ), 0, NULL);
} }
if (res.bool_) if (res.val.bool_)
return res; return res;
} }
@ -259,10 +259,10 @@ 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; struct rmsgpack_dom_value res;
unsigned i = 0; unsigned i = 0;
res.type = RDT_BOOL; res.type = RDT_BOOL;
res.bool_ = 0; res.val.bool_ = 0;
(void)i; (void)i;
@ -276,10 +276,10 @@ static struct rmsgpack_dom_value between(struct rmsgpack_dom_value input,
switch (input.type) switch (input.type)
{ {
case RDT_INT: 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; break;
case RDT_UINT: case RDT_UINT:
res.bool_ = input.int_ >= argv[0].value.uint_ && input.int_ <= argv[1].value.int_; res.val.bool_ = input.val.int_ >= argv[0].value.val.uint_ && input.val.int_ <= argv[1].value.val.int_;
break; break;
default: default:
return res; return res;
@ -294,8 +294,8 @@ static struct rmsgpack_dom_value operator_and(struct rmsgpack_dom_value input,
struct rmsgpack_dom_value res; struct rmsgpack_dom_value res;
unsigned i; unsigned i;
res.type = RDT_BOOL; res.type = RDT_BOOL;
res.bool_ = 0; res.val.bool_ = 0;
for (i = 0; i < argc; i++) for (i = 0; i < argc; i++)
{ {
@ -311,7 +311,7 @@ static struct rmsgpack_dom_value operator_and(struct rmsgpack_dom_value input,
0, NULL); 0, NULL);
} }
if (!res.bool_) if (!res.val.bool_)
return res; return res;
} }
return res; return res;
@ -321,10 +321,10 @@ static struct rmsgpack_dom_value q_glob(struct rmsgpack_dom_value input,
unsigned argc, const struct argument * argv) unsigned argc, const struct argument * argv)
{ {
struct rmsgpack_dom_value res; struct rmsgpack_dom_value res;
unsigned i = 0; unsigned i = 0;
res.type = RDT_BOOL; res.type = RDT_BOOL;
res.bool_ = 0; res.val.bool_ = 0;
(void)i; (void)i;
@ -334,9 +334,9 @@ static struct rmsgpack_dom_value q_glob(struct rmsgpack_dom_value input,
return res; return res;
if (input.type != RDT_STRING) if (input.type != RDT_STRING)
return res; return res;
res.bool_ = rl_fnmatch( res.val.bool_ = rl_fnmatch(
argv[0].value.string.buff, argv[0].value.val.string.buff,
input.string.buff, input.val.string.buff,
0 0
) == 0; ) == 0;
return res; return res;
@ -353,11 +353,11 @@ static struct rmsgpack_dom_value all_map(struct rmsgpack_dom_value input,
nil_value.type = RDT_NULL; nil_value.type = RDT_NULL;
res.type = RDT_BOOL; res.type = RDT_BOOL;
res.bool_ = 1; res.val.bool_ = 1;
if (argc % 2 != 0) if (argc % 2 != 0)
{ {
res.bool_ = 0; res.val.bool_ = 0;
return res; return res;
} }
@ -369,7 +369,7 @@ static struct rmsgpack_dom_value all_map(struct rmsgpack_dom_value input,
arg = argv[i]; arg = argv[i];
if (arg.type != AT_VALUE) if (arg.type != AT_VALUE)
{ {
res.bool_ = 0; res.val.bool_ = 0;
goto clean; goto clean;
} }
value = rmsgpack_dom_value_map_value(&input, &arg.value); value = rmsgpack_dom_value_map_value(&input, &arg.value);
@ -387,7 +387,7 @@ static struct rmsgpack_dom_value all_map(struct rmsgpack_dom_value input,
), 0, NULL); ), 0, NULL);
value = NULL; value = NULL;
} }
if (!res.bool_) if (!res.val.bool_)
break; break;
} }
clean: clean:
@ -505,16 +505,16 @@ static struct buffer parse_string(struct buffer buff,
if (!*error) if (!*error)
{ {
value->type = RDT_STRING; value->type = RDT_STRING;
value->string.len = (buff.data + buff.offset) - str_start - 1; value->val.string.len = (buff.data + buff.offset) - str_start - 1;
value->string.buff = (char*)calloc(value->string.len + 1, sizeof(char)); value->val.string.buff = (char*)calloc(value->val.string.len + 1, sizeof(char));
if (!value->string.buff) if (!value->val.string.buff)
raise_enomem(error); raise_enomem(error);
else else
memcpy( memcpy(
value->string.buff, value->val.string.buff,
str_start, str_start,
value->string.len value->val.string.len
); );
} }
return buff; return buff;
@ -531,7 +531,7 @@ static struct buffer parse_integer(struct buffer buff,
#else #else
"%lld", "%lld",
#endif #endif
(signed long long*)&value->int_) == 0) (signed long long*)&value->val.int_) == 0)
raise_expected_number(buff.offset, error); raise_expected_number(buff.offset, error);
else else
{ {
@ -555,13 +555,13 @@ static struct buffer parse_value(struct buffer buff,
{ {
buff.offset += strlen("true"); buff.offset += strlen("true");
value->type = RDT_BOOL; value->type = RDT_BOOL;
value->bool_ = 1; value->val.bool_ = 1;
} }
else if (peek(buff, "false")) else if (peek(buff, "false"))
{ {
buff.offset += strlen("false"); buff.offset += strlen("false");
value->type = RDT_BOOL; value->type = RDT_BOOL;
value->bool_ = 0; value->val.bool_ = 0;
} }
else if (peek(buff, "\"") || peek(buff, "'")) else if (peek(buff, "\"") || peek(buff, "'"))
buff = parse_string(buff, value, error); buff = parse_string(buff, value, error);
@ -730,17 +730,17 @@ static struct buffer parse_table(struct buffer buff,
if (!*error) if (!*error)
{ {
args[argi].value.type = RDT_STRING; args[argi].value.type = RDT_STRING;
args[argi].value.string.len = ident_len; args[argi].value.val.string.len = ident_len;
args[argi].value.string.buff = (char*)calloc( args[argi].value.val.string.buff = (char*)calloc(
ident_len + 1, ident_len + 1,
sizeof(char) sizeof(char)
); );
if (!args[argi].value.string.buff) if (!args[argi].value.val.string.buff)
goto clean; goto clean;
strncpy( strncpy(
args[argi].value.string.buff, args[argi].value.val.string.buff,
ident_name, ident_name,
ident_len ident_len
); );
@ -912,5 +912,5 @@ int libretrodb_query_filter(libretrodb_query_t *q,
{ {
struct invocation inv = ((struct query *)q)->root; struct invocation inv = ((struct query *)q)->root;
struct rmsgpack_dom_value res = inv.func(*v, inv.argc, inv.argv); 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

@ -49,7 +49,7 @@ static int dom_read_bool(int value, void *data)
(struct rmsgpack_dom_value*)dom_reader_state_pop(dom_state); (struct rmsgpack_dom_value*)dom_reader_state_pop(dom_state);
v->type = RDT_BOOL; v->type = RDT_BOOL;
v->bool_ = value; v->val.bool_ = value;
return 0; return 0;
} }
@ -60,7 +60,7 @@ static int dom_read_int(int64_t value, void *data)
(struct rmsgpack_dom_value*)dom_reader_state_pop(dom_state); (struct rmsgpack_dom_value*)dom_reader_state_pop(dom_state);
v->type = RDT_INT; v->type = RDT_INT;
v->int_ = value; v->val.int_ = value;
return 0; return 0;
} }
@ -71,7 +71,7 @@ static int dom_read_uint(uint64_t value, void *data)
(struct rmsgpack_dom_value*)dom_reader_state_pop(dom_state); (struct rmsgpack_dom_value*)dom_reader_state_pop(dom_state);
v->type = RDT_UINT; v->type = RDT_UINT;
v->uint_ = value; v->val.uint_ = value;
return 0; return 0;
} }
@ -82,8 +82,8 @@ static int dom_read_string(char *value, uint32_t len, void *data)
(struct rmsgpack_dom_value*)dom_reader_state_pop(dom_state); (struct rmsgpack_dom_value*)dom_reader_state_pop(dom_state);
v->type = RDT_STRING; v->type = RDT_STRING;
v->string.len = len; v->val.string.len = len;
v->string.buff = value; v->val.string.buff = value;
return 0; return 0;
} }
@ -94,8 +94,8 @@ static int dom_read_bin(void *value, uint32_t len, void *data)
(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->val.binary.len = len;
v->binary.buff = (char *)value; v->val.binary.buff = (char *)value;
return 0; return 0;
} }
@ -107,8 +107,8 @@ static int dom_read_map_start(uint32_t len, void *data)
struct rmsgpack_dom_value *v = dom_reader_state_pop(dom_state); struct rmsgpack_dom_value *v = dom_reader_state_pop(dom_state);
v->type = RDT_MAP; v->type = RDT_MAP;
v->map.len = len; v->val.map.len = len;
v->map.items = NULL; v->val.map.items = NULL;
items = (struct rmsgpack_dom_pair *)calloc(len, items = (struct rmsgpack_dom_pair *)calloc(len,
sizeof(struct rmsgpack_dom_pair)); sizeof(struct rmsgpack_dom_pair));
@ -116,7 +116,7 @@ static int dom_read_map_start(uint32_t len, void *data)
if (!items) if (!items)
return -ENOMEM; return -ENOMEM;
v->map.items = items; v->val.map.items = items;
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {
@ -137,15 +137,15 @@ static int dom_read_array_start(uint32_t len, void *data)
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->val.array.len = len;
v->array.items = NULL; v->val.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->val.array.items = items;
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {
@ -174,23 +174,23 @@ void rmsgpack_dom_value_free(struct rmsgpack_dom_value *v)
switch (v->type) switch (v->type)
{ {
case RDT_STRING: case RDT_STRING:
free(v->string.buff); free(v->val.string.buff);
break; break;
case RDT_BINARY: case RDT_BINARY:
free(v->binary.buff); free(v->val.binary.buff);
break; break;
case RDT_MAP: 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->val.map.items[i].key);
rmsgpack_dom_value_free(&v->map.items[i].value); rmsgpack_dom_value_free(&v->val.map.items[i].value);
} }
free(v->map.items); free(v->val.map.items);
break; break;
case RDT_ARRAY: case RDT_ARRAY:
for (i = 0; i < v->array.len; i++) for (i = 0; i < v->val.array.len; i++)
rmsgpack_dom_value_free(&v->array.items[i]); rmsgpack_dom_value_free(&v->val.array.items[i]);
free(v->array.items); free(v->val.array.items);
break; break;
case RDT_NULL: case RDT_NULL:
case RDT_INT: case RDT_INT:
@ -209,10 +209,10 @@ struct rmsgpack_dom_value *rmsgpack_dom_value_map_value(
if (map->type != RDT_MAP) if (map->type != RDT_MAP)
return NULL; 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) if (rmsgpack_dom_value_cmp(key, &map->val.map.items[i].key) == 0)
return &map->map.items[i].value; return &map->val.map.items[i].value;
} }
return NULL; return NULL;
} }
@ -236,39 +236,39 @@ int rmsgpack_dom_value_cmp(
case RDT_NULL: case RDT_NULL:
return 0; return 0;
case RDT_BOOL: case RDT_BOOL:
return a->bool_ == b->bool_ ? 0 : 1; return a->val.bool_ == b->val.bool_ ? 0 : 1;
case RDT_INT: case RDT_INT:
return a->int_ == b->int_ ? 0 : 1; return a->val.int_ == b->val.int_ ? 0 : 1;
case RDT_UINT: case RDT_UINT:
return a->uint_ == b->uint_ ? 0 : 1; return a->val.uint_ == b->val.uint_ ? 0 : 1;
case RDT_STRING: case RDT_STRING:
if (a->string.len != b->string.len) if (a->val.string.len != b->val.string.len)
return 1; 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: case RDT_BINARY:
if (a->binary.len != b->binary.len) if (a->val.binary.len != b->val.binary.len)
return 1; 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: case RDT_MAP:
if (a->map.len != b->map.len) if (a->val.map.len != b->val.map.len)
return 1; 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, if ((rv = rmsgpack_dom_value_cmp(&a->val.map.items[i].key,
&b->map.items[i].key)) != 0) &b->val.map.items[i].key)) != 0)
return rv; return rv;
if ((rv = rmsgpack_dom_value_cmp(&a->map.items[i].value, if ((rv = rmsgpack_dom_value_cmp(&a->val.map.items[i].value,
&b->map.items[i].value)) != 0) &b->val.map.items[i].value)) != 0)
return rv; return rv;
} }
break; break;
case RDT_ARRAY: case RDT_ARRAY:
if (a->array.len != b->array.len) if (a->val.array.len != b->val.array.len)
return 1; 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], if ((rv = rmsgpack_dom_value_cmp(&a->val.array.items[i],
&b->array.items[i])) != 0) &b->val.array.items[i])) != 0)
return rv; return rv;
} }
break; break;
@ -287,52 +287,52 @@ void rmsgpack_dom_value_print(struct rmsgpack_dom_value *obj)
printf("nil"); printf("nil");
break; break;
case RDT_BOOL: case RDT_BOOL:
if (obj->bool_) if (obj->val.bool_)
printf("true"); printf("true");
else else
printf("false"); printf("false");
break; break;
case RDT_INT: case RDT_INT:
#ifdef _WIN32 #ifdef _WIN32
printf("%I64d", (signed long long)obj->int_); printf("%I64d", (signed long long)obj->val.int_);
#else #else
printf("%lld", (signed long long)obj->int_); printf("%lld", (signed long long)obj->val.int_);
#endif #endif
break; break;
case RDT_UINT: case RDT_UINT:
#ifdef _WIN32 #ifdef _WIN32
printf("%I64u", (unsigned long long)obj->uint_); printf("%I64u", (unsigned long long)obj->val.uint_);
#else #else
printf("%llu", (unsigned long long)obj->uint_); printf("%llu", (unsigned long long)obj->val.uint_);
#endif #endif
break; break;
case RDT_STRING: case RDT_STRING:
printf("\"%s\"", obj->string.buff); printf("\"%s\"", obj->val.string.buff);
break; break;
case RDT_BINARY: case RDT_BINARY:
printf("\""); printf("\"");
for (i = 0; i < obj->binary.len; i++) for (i = 0; i < obj->val.binary.len; i++)
printf("%02X", (unsigned char) obj->binary.buff[i]); printf("%02X", (unsigned char) obj->val.binary.buff[i]);
printf("\""); printf("\"");
break; break;
case RDT_MAP: case RDT_MAP:
printf("{"); 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(": "); printf(": ");
rmsgpack_dom_value_print(&obj->map.items[i].value); rmsgpack_dom_value_print(&obj->val.map.items[i].value);
if (i < (obj->map.len - 1)) if (i < (obj->val.map.len - 1))
printf(", "); printf(", ");
} }
printf("}"); printf("}");
break; break;
case RDT_ARRAY: case RDT_ARRAY:
printf("["); 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]); rmsgpack_dom_value_print(&obj->val.array.items[i]);
if (i < (obj->array.len - 1)) if (i < (obj->val.array.len - 1))
printf(", "); printf(", ");
} }
printf("]"); printf("]");
@ -349,38 +349,38 @@ int rmsgpack_dom_write(int fd, const struct rmsgpack_dom_value *obj)
case RDT_NULL: case RDT_NULL:
return rmsgpack_write_nil(fd); return rmsgpack_write_nil(fd);
case RDT_BOOL: case RDT_BOOL:
return rmsgpack_write_bool(fd, obj->bool_); return rmsgpack_write_bool(fd, obj->val.bool_);
case RDT_INT: case RDT_INT:
return rmsgpack_write_int(fd, obj->int_); return rmsgpack_write_int(fd, obj->val.int_);
case RDT_UINT: case RDT_UINT:
return rmsgpack_write_uint(fd, obj->uint_); return rmsgpack_write_uint(fd, obj->val.uint_);
case RDT_STRING: case RDT_STRING:
return rmsgpack_write_string(fd, obj->string.buff, obj->string.len); return rmsgpack_write_string(fd, obj->val.string.buff, obj->val.string.len);
case RDT_BINARY: case RDT_BINARY:
return rmsgpack_write_bin(fd, obj->binary.buff, obj->binary.len); return rmsgpack_write_bin(fd, obj->val.binary.buff, obj->val.binary.len);
case RDT_MAP: case RDT_MAP:
if ((rv = rmsgpack_write_map_header(fd, obj->map.len)) < 0) if ((rv = rmsgpack_write_map_header(fd, obj->val.map.len)) < 0)
return rv; return rv;
written += 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(fd, &obj->map.items[i].key)) < 0) if ((rv = rmsgpack_dom_write(fd, &obj->val.map.items[i].key)) < 0)
return rv; return rv;
written += rv; written += rv;
if ((rv = rmsgpack_dom_write(fd, &obj->map.items[i].value)) < 0) if ((rv = rmsgpack_dom_write(fd, &obj->val.map.items[i].value)) < 0)
return rv; return rv;
written += rv; written += rv;
} }
break; break;
case RDT_ARRAY: case RDT_ARRAY:
if ((rv = rmsgpack_write_array_header(fd, obj->array.len)) < 0) if ((rv = rmsgpack_write_array_header(fd, obj->val.array.len)) < 0)
return rv; return rv;
written += 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(fd, &obj->array.items[i])) < 0) if ((rv = rmsgpack_dom_write(fd, &obj->val.array.items[i])) < 0)
return rv; return rv;
written += rv; written += rv;
} }
@ -448,8 +448,8 @@ int rmsgpack_dom_read_into(int fd, ...)
} }
key.type = RDT_STRING; key.type = RDT_STRING;
key.string.len = strlen(key_name); key.val.string.len = strlen(key_name);
key.string.buff = (char *) key_name; key.val.string.buff = (char *) key_name;
value = rmsgpack_dom_value_map_value(&map, &key); value = rmsgpack_dom_value_map_value(&map, &key);
@ -457,33 +457,33 @@ int rmsgpack_dom_read_into(int fd, ...)
{ {
case RDT_INT: case RDT_INT:
int_value = va_arg(ap, int64_t *); int_value = va_arg(ap, int64_t *);
*int_value = value->int_; *int_value = value->val.int_;
break; break;
case RDT_BOOL: case RDT_BOOL:
bool_value = va_arg(ap, int *); bool_value = va_arg(ap, int *);
*bool_value = value->bool_; *bool_value = value->val.bool_;
break; break;
case RDT_UINT: case RDT_UINT:
uint_value = va_arg(ap, uint64_t *); uint_value = va_arg(ap, uint64_t *);
*uint_value = value->uint_; *uint_value = value->val.uint_;
break; break;
case RDT_BINARY: case RDT_BINARY:
buff_value = va_arg(ap, char *); buff_value = va_arg(ap, char *);
uint_value = va_arg(ap, uint64_t *); uint_value = va_arg(ap, uint64_t *);
*uint_value = value->binary.len; *uint_value = value->val.binary.len;
min_len = (value->binary.len > *uint_value) ? min_len = (value->val.binary.len > *uint_value) ?
*uint_value : value->binary.len; *uint_value : value->val.binary.len;
memcpy(buff_value, value->binary.buff, min_len); memcpy(buff_value, value->val.binary.buff, min_len);
break; break;
case RDT_STRING: case RDT_STRING:
buff_value = va_arg(ap, char *); buff_value = va_arg(ap, char *);
uint_value = va_arg(ap, uint64_t *); uint_value = va_arg(ap, uint64_t *);
min_len = (value->string.len + 1 > *uint_value) ? min_len = (value->val.string.len + 1 > *uint_value) ?
*uint_value : value->string.len + 1; *uint_value : value->val.string.len + 1;
*uint_value = min_len; *uint_value = min_len;
memcpy(buff_value, value->string.buff, min_len); memcpy(buff_value, value->val.string.buff, min_len);
break; break;
default: default:
rv = -1; rv = -1;

View File

@ -18,29 +18,35 @@ enum rmsgpack_dom_type {
RDT_ARRAY RDT_ARRAY
}; };
struct rmsgpack_dom_value { struct rmsgpack_dom_value
enum rmsgpack_dom_type type; {
union { enum rmsgpack_dom_type type;
uint64_t uint_; union
int64_t int_; {
struct { uint64_t uint_;
uint32_t len; int64_t int_;
char * buff; struct
} string; {
struct { uint32_t len;
uint32_t len; char * buff;
char * buff; } string;
} binary; struct
int bool_; {
struct { uint32_t len;
uint32_t len; char * buff;
struct rmsgpack_dom_pair * items; } binary;
} map; int bool_;
struct { struct
uint32_t len; {
struct rmsgpack_dom_value * items; uint32_t len;
} array; struct rmsgpack_dom_pair * items;
}; } map;
struct
{
uint32_t len;
struct rmsgpack_dom_value * items;
} array;
} val;
}; };
struct rmsgpack_dom_pair { struct rmsgpack_dom_pair {