summaryrefslogtreecommitdiff
path: root/src/nbt
diff options
context:
space:
mode:
Diffstat (limited to 'src/nbt')
-rw-r--r--src/nbt/print.c6
-rw-r--r--src/nbt/read.c32
2 files changed, 16 insertions, 22 deletions
diff --git a/src/nbt/print.c b/src/nbt/print.c
index 4eac31f..3f687dc 100644
--- a/src/nbt/print.c
+++ b/src/nbt/print.c
@@ -64,9 +64,11 @@ static bool nbt_print_list(const tagdata_t *data, const stream_t *stream) {
}
static bool nbt_print_compound(const tagdata_t *data, const stream_t *stream) {
- for (int32_t i = 0; i < data->compound.size; i++)
- if (nbt_print(&data->compound.tags[i], stream) == false)
+ for (uint32_t i = 0; i < data->compound.capacity; i++) {
+ if (data->compound.entries[i].name == NULL) continue;
+ if (nbt_print(&data->compound.entries[i], stream) == false)
return false;
+ }
if (stream_write_i8(stream, TAG_END) == false)
return false;
return true;
diff --git a/src/nbt/read.c b/src/nbt/read.c
index cef04ca..38f8b14 100644
--- a/src/nbt/read.c
+++ b/src/nbt/read.c
@@ -118,44 +118,36 @@ static bool nbt_read_list(tagdata_t *data, const stream_t *stream) {
}
static bool nbt_read_compound(tagdata_t *data, const stream_t *stream) {
- int32_t size = 0;
- int32_t capacity = 8;
- tag_t *tags = xalloc(capacity * sizeof(tag_t));
+
+ map_t map;
+ map_init(&map);
while (1) {
tag_t tag;
if (nbt_read_header(&tag, stream, true) == false) {
- free(tags);
+ map_free(&map);
return false;
}
-
+
+ if (tag.type == TAG_END)
+ break;
+
if (tag.name_len < 1) {
- free(tags);
+ map_free(&map);
return false;
}
- if (tag.type == TAG_END)
- break;
-
if (nbt_read_data(&tag, stream) == false) {
- free(tags);
+ map_free(&map);
return false;
}
-
- if (size == capacity) {
- capacity *= 2;
- tags = xrealloc(tags, capacity * sizeof(tag_t));
- }
- tags[size++] = tag;
+ map_put(&map, &tag);
}
- data->compound.size = size;
- data->compound.tags = xalloc(size * sizeof(tag_t));
- memcpy(data->compound.tags, tags, size * sizeof(tag_t));
- free(tags);
+ data->compound = map;
return true;
}