diff options
| -rw-r--r-- | include/nbt.h | 10 | ||||
| -rw-r--r-- | meson.build | 7 | ||||
| -rw-r--r-- | src/libmain.c | 68 | ||||
| -rw-r--r-- | src/nbt.internal.h | 2 | ||||
| -rw-r--r-- | src/nbt/nbt.c | 35 |
5 files changed, 81 insertions, 41 deletions
diff --git a/include/nbt.h b/include/nbt.h index ba7e0a6..9df7680 100644 --- a/include/nbt.h +++ b/include/nbt.h @@ -33,13 +33,17 @@ typedef enum { NBT_TAG_COMPOUND,
NBT_TAG_INT_ARRAY,
- NBT_TAG_LONG_ARRAY
+ NBT_TAG_LONG_ARRAY,
+
+ NBT_TAG_MAX /* not a real type */
} nbt_type_t;
typedef struct nbt__tag_tag nbt_tag_t;
/* API inspired by jansson's API (it is nice to use) */
+nbt_type_t nbt_tag_type(nbt_tag_t *tag);
+
/* constructors and management functions */
nbt_tag_t *nbt_byte(nbt_byte_t b);
nbt_tag_t *nbt_short(nbt_short_t s);
@@ -151,4 +155,8 @@ void nbt_compound_clear(nbt_tag_t *compound); int nbt_compound_merge(nbt_tag_t *target, nbt_tag_t *srccomp);
+/* Returned values are shared and statically allocated. Do NOT attempt to
+ * modify or free them! */
+const char *nbt_typestr(nbt_type_t type);
+
#endif
diff --git a/meson.build b/meson.build index eaae487..5a5a8c3 100644 --- a/meson.build +++ b/meson.build @@ -4,5 +4,10 @@ xxhash_dep = dependency('libxxhash', required : true, static : true) include_folder = include_directories('include') +comp = meson.get_compiler('c') +libasan_dep = comp.find_library('clang_rt.asan_dynamic-x86_64', dirs : ['C:\\Program Files\\LLVM\\lib\\clang\\15.0.6\\lib\\windows']) + subdir('src') -executable('world', libworld_sources, include_directories : [include_folder], dependencies : [xxhash_dep], win_subsystem : 'console') +executable('world', libworld_sources, include_directories : [include_folder], + dependencies : [xxhash_dep, libasan_dep], + win_subsystem : 'console') 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 <string.h> #include <stdint.h> #include <stdio.h> - -#define HT_IMPLEMENTATIONS -#define HTGEN__TEST_CONFIG -#include "htgen.h" +#include <stdlib.h> #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 <stdlib.h>
#include <string.h>
+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];
+}
|
