summaryrefslogtreecommitdiff
path: root/src/json/read.c
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2023-12-16 11:47:59 -0500
committerFreya Murphy <freya@freyacat.org>2023-12-16 11:47:59 -0500
commit85ba301f52b1c7e1ad928803b14b6c70776f2235 (patch)
tree28902147b9bf6c393497e21b0cb21c8b957aa3e2 /src/json/read.c
parentfix hang on incomplete json ident (diff)
downloadnbtvis-85ba301f52b1c7e1ad928803b14b6c70776f2235.tar.gz
nbtvis-85ba301f52b1c7e1ad928803b14b6c70776f2235.tar.bz2
nbtvis-85ba301f52b1c7e1ad928803b14b6c70776f2235.zip
use hashmap for compound tag
Diffstat (limited to '')
-rw-r--r--src/json/read.c41
1 files changed, 14 insertions, 27 deletions
diff --git a/src/json/read.c b/src/json/read.c
index ea6e106..50a5fd1 100644
--- a/src/json/read.c
+++ b/src/json/read.c
@@ -498,7 +498,10 @@ static bool json_read_list(tagdata_t *data, const stream_t *stream) {
}
static bool json_read_compound(tagdata_t *data, const stream_t *stream) {
-
+
+ map_t map;
+ map_init(&map);
+
token_t next = {0};
if (json_next_token(&next, stream) == false) {
json_token_free(&next);
@@ -506,19 +509,14 @@ static bool json_read_compound(tagdata_t *data, const stream_t *stream) {
}
if (next.type == TOK_RBRACE) {
- data->compound.tags = NULL;
- data->compound.size = 0;
+ data->compound = map;
return true;
}
- int capacity = 8;
- int len = 0;
- tag_t *tags = xalloc(capacity * sizeof(tag_t));
-
while (1) {
if (next.type != TOK_STRING) {
- free(tags);
+ map_free(&map);
json_token_free(&next);
return false;
}
@@ -527,20 +525,20 @@ static bool json_read_compound(tagdata_t *data, const stream_t *stream) {
int name_len = next.data.string.len;
if (name_len < 1) {
- free(tags);
+ map_free(&map);
free(name);
return false;
}
if (json_next_token(&next, stream) == false || next.type != TOK_COLON) {
- free(tags);
+ map_free(&map);
free(name);
return false;
}
tag_t value;
if (json_read_value(&value, stream, NULL) == false) {
- free(tags);
+ map_free(&map);
free(name);
return false;
}
@@ -548,42 +546,31 @@ static bool json_read_compound(tagdata_t *data, const stream_t *stream) {
value.name = name;
value.name_len = name_len;
- if (len == capacity) {
- capacity *= 2;
- tags = xrealloc(tags, capacity * sizeof(tag_t));
- }
-
- tags[len++] = value;
+ map_put(&map, &value);
if (json_next_token(&next, stream) == false) {
- free(tags);
- free(value.name);
+ map_free(&map);
json_token_free(&next);
return false;
}
if (next.type == TOK_COMMA) {
if (json_next_token(&next, stream) == false) {
- free(tags);
- free(name);
+ map_free(&map);
return false;
}
continue;
} else if (next.type == TOK_RBRACE) {
break;
} else {
- free(tags);
- free(value.name);
+ map_free(&map);
json_token_free(&next);
return false;
}
}
- data->compound.tags = xalloc(len * sizeof(tag_t));
- data->compound.size = len;
- memcpy(data->compound.tags, tags, len * sizeof(tag_t));
- free(tags);
+ data->compound = map;
return true;
}