From 4b3a61106a7d38cd904ab021b82a4a487df61f0a Mon Sep 17 00:00:00 2001 From: bigfoot547 Date: Sat, 10 Feb 2024 14:28:38 -0500 Subject: changes --- src/libmain.c | 68 ++++++++++++++++++++++++------------------------------ src/nbt.internal.h | 2 ++ src/nbt/nbt.c | 35 +++++++++++++++++++++++++++- 3 files changed, 66 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/libmain.c b/src/libmain.c index e82635d..c6d1c29 100644 --- a/src/libmain.c +++ b/src/libmain.c @@ -1,56 +1,48 @@ -#include "xxhash.h" #include #include #include - -#define HT_IMPLEMENTATIONS -#define HTGEN__TEST_CONFIG -#include "htgen.h" +#include #include "nbt.h" -#if 1 -int main(int argc, char **argv) { - shash_t *hash = shash_create(4, 0.75f); - printf("%d\n", shash_put(hash, "test1", "value1")); - printf("%d\n", shash_put(hash, "testbro", "val2")); - shash__debug_table(hash); - - char *oldval = shash_pop(hash, "test1", NULL); - printf("%s:%s\n", "test1", oldval); - free(oldval); +void debug_list(nbt_tag_t *tag) { + if (!tag) { + printf("list: (null)\n"); + return; + } - shash__debug_table(hash); - printf("%d\n", shash_put(hash, "testbro", "val3")); - shash__debug_table(hash); + printf("list %p (%zu len, %zu cap)\n", tag, nbt_list_length(tag), nbt_list_capacity(tag)); - shash_free(hash); - - return 0; + for (size_t n = 0; n < nbt_list_length(tag); ++n) { + nbt_tag_t *t = nbt_list_get(tag, n); + printf("%zu %p: tag %p(%s)\n", n, tag, t, nbt_typestr(nbt_tag_type(tag))); + } } -#else -int main(int argc, char **argv) +void test__is_true(int i, const char *test, const char *ex, const char *file, int line, const char *func) { - ihash_t *hash = ihash_create(8, 0.75f); + if (!i) { + fprintf(stderr, "Test \"%s\" failed: %s == %d (%s:%d @ %s)\n", test, ex, i, file, line, func); + abort(); + } +} - printf("%d\n", ihash_put(hash, 1, 10)); - ihash__debug_table(hash); - printf("%d\n", ihash_put(hash, 5, 20)); - printf("%d\n", ihash_put(hash, 3, 19)); - printf("%d\n", ihash_put(hash, 56, 101)); - ihash__debug_table(hash); - printf("%d\n", ihash_put(hash, 90, 6)); - printf("%d\n", ihash_put(hash, 2000, 1)); - ihash__debug_table(hash); - printf("%d\n", ihash_put(hash, 4, 2020)); +void nbt_compound_clear(nbt_tag_t *a) { } - ihash__debug_table(hash); +#define TEST_TRUE(_e) test__is_true(_e, "is true", #_e, __FILE__, __LINE__, __func__) +#define TEST_NONNULL(_e) test__is_true(!!(_e), "is nonnull", #_e, __FILE__, __LINE__, __func__) - ihash_free(hash); +int main(int argc, char **argv) { + nbt_tag_t *list = nbt_list(); + debug_list(list); + + TEST_TRUE(nbt_list_append_move(list, nbt_byte(5)) >= 0); + TEST_TRUE(nbt_list_append_move(list, nbt_string("somestr")) >= 0); + + debug_list(list); + + nbt_decref(list); return 0; } - -#endif diff --git a/src/nbt.internal.h b/src/nbt.internal.h index 88f636b..3e60a10 100644 --- a/src/nbt.internal.h +++ b/src/nbt.internal.h @@ -75,4 +75,6 @@ typedef struct { void nbt__tag_free(nbt_tag_t *tag); +extern const char *const nbt__type_names[]; + #endif /* include guard */ diff --git a/src/nbt/nbt.c b/src/nbt/nbt.c index 5948bce..596ec1c 100644 --- a/src/nbt/nbt.c +++ b/src/nbt/nbt.c @@ -6,6 +6,29 @@ #include #include +const char *const nbt__type_names[] = { + "TAG_End", + "TAG_Byte", + "TAG_Short", + "TAG_Int", + "TAG_Long", + "TAG_Float", + "TAG_Double", + "TAG_Byte_Array", + "TAG_String", + "TAG_List", + "TAG_Compound", + "TAG_Int_Array", + "TAG_Long_Array", + NULL +}; + +nbt_type_t nbt_tag_type(nbt_tag_t *tag) +{ + if (!tag) return NBT_TAG_END; + return tag->type; +} + nbt_tag_t *nbt__new_val(nbt_type_t type) { nbt_tag_t *tag = calloc(1, sizeof(nbt_tag_t)); @@ -117,11 +140,15 @@ nbt_tag_t *nbt_listn(nbt_size_t cap) { nbt_tag_t *tag = nbt__new_val(NBT_TAG_LIST); if (!tag) return NULL; - if (!cap) cap = 16; tag->value.nbt_list.len = 0; tag->value.nbt_list.cap = cap; + if (!cap) { + tag->value.nbt_list.ptags = NULL; + return tag; + } + nbt_tag_t **tags = calloc(cap, sizeof(nbt_tag_t *)); if (!tags) goto cleanup; @@ -205,3 +232,9 @@ void nbt__tag_free(nbt_tag_t *tag) free(tag); } + +const char *nbt_typestr(nbt_type_t type) +{ + if (type < 0 || type >= NBT_TAG_MAX) return NULL; + return nbt__type_names[type]; +} -- cgit v1.2.3-70-g09d2