summaryrefslogtreecommitdiff
path: root/src/json
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
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 'src/json')
-rw-r--r--src/json/print.c12
-rw-r--r--src/json/read.c41
2 files changed, 22 insertions, 31 deletions
diff --git a/src/json/print.c b/src/json/print.c
index 97ccce8..9f2931f 100644
--- a/src/json/print.c
+++ b/src/json/print.c
@@ -68,7 +68,7 @@ static bool json_print_long_array(const tagdata_t *data, const stream_t *stream)
}
static bool json_print_string(const tagdata_t *data, const stream_t *stream) {
- if (data->string.size > 1) {
+ if (data->string.size > 0) {
if (printi(stream, 0, "\"%.*s\"", data->string.size, data->string.data) == false)
return false;
} else {
@@ -81,10 +81,14 @@ static bool json_print_string(const tagdata_t *data, const stream_t *stream) {
static bool json_print_compound(const tagdata_t *data, const stream_t *stream, int depth) {
if (printi(stream, 0, "{\n") == false)
return false;
- for (int32_t i = 0; i < data->compound.size; i++) {
- if (i != 0 && printi(stream, 0, ",\n") == false)
+ bool first = true;
+ for (uint32_t i = 0; i < data->compound.capacity; i++) {
+ if (data->compound.entries[i].name == NULL)
+ continue;
+ if (!first && printi(stream, 0, ",\n") == false)
return false;
- if (json_print_impl(&data->compound.tags[i], stream, depth + 1) == false)
+ first = false;
+ if (json_print_impl(&data->compound.entries[i], stream, depth + 1) == false)
return false;
}
if (printi(stream, 0, "\n") == false || printi(stream, depth, "}") == false)
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;
}