From 962f5efdf1af9f07395c28df7a2181733e3e5125 Mon Sep 17 00:00:00 2001 From: bigfoot547 Date: Sat, 3 Feb 2024 16:58:23 -0600 Subject: add lists --- include/nbt.h | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 include/nbt.h (limited to 'include/nbt.h') diff --git a/include/nbt.h b/include/nbt.h new file mode 100644 index 0000000..ba7e0a6 --- /dev/null +++ b/include/nbt.h @@ -0,0 +1,154 @@ +#ifndef LIBWORLD_NBT_H_INCLUDED +#define LIBWORLD_NBT_H_INCLUDED + +#include +#include + +typedef size_t nbt_size_t; + +/* NBT primitives */ +typedef int8_t nbt_byte_t; +typedef int16_t nbt_short_t; +typedef int32_t nbt_int_t; +typedef int64_t nbt_long_t; + +typedef float nbt_float_t; +typedef double nbt_double_t; + +/* NBT tag types */ +typedef enum { + NBT_TAG_END = 0, + NBT_TAG_BYTE, + NBT_TAG_SHORT, + NBT_TAG_INT, + NBT_TAG_LONG, + + NBT_TAG_FLOAT, + NBT_TAG_DOUBLE, + + NBT_TAG_BYTE_ARRAY, + + NBT_TAG_STRING, + NBT_TAG_LIST, + NBT_TAG_COMPOUND, + + NBT_TAG_INT_ARRAY, + NBT_TAG_LONG_ARRAY +} nbt_type_t; + +typedef struct nbt__tag_tag nbt_tag_t; + +/* API inspired by jansson's API (it is nice to use) */ + +/* constructors and management functions */ +nbt_tag_t *nbt_byte(nbt_byte_t b); +nbt_tag_t *nbt_short(nbt_short_t s); +nbt_tag_t *nbt_int(nbt_int_t i); +nbt_tag_t *nbt_long(nbt_long_t l); + +nbt_tag_t *nbt_float(nbt_float_t f); +nbt_tag_t *nbt_double(nbt_double_t d); + +nbt_tag_t *nbt_byte_array(const nbt_byte_t *bytes, nbt_size_t length); + +nbt_tag_t *nbt_string(const char *c_str); +nbt_tag_t *nbt_stringn(const char *c_str, nbt_size_t length); + +nbt_tag_t *nbt_list(void); +nbt_tag_t *nbt_listn(nbt_size_t cap); + +nbt_tag_t *nbt_compound(void); +nbt_tag_t *nbt_compoundn(nbt_size_t cap); + +nbt_tag_t *nbt_int_array(const nbt_int_t *ints, nbt_size_t length); +nbt_tag_t *nbt_long_array(const nbt_long_t *longs, nbt_size_t length); + +void nbt_decref(nbt_tag_t *tag); +nbt_tag_t *nbt_incref(nbt_tag_t *tag); + +/* primitive functions */ + +nbt_byte_t nbt_byte_get(nbt_tag_t *tag); +nbt_short_t nbt_short_get(nbt_tag_t *tag); +nbt_int_t nbt_int_get(nbt_tag_t *tag); +nbt_long_t nbt_long_get(nbt_tag_t *tag); + +nbt_float_t nbt_float_get(nbt_tag_t *tag); +nbt_double_t nbt_double_get(nbt_tag_t *tag); + +/* array functions */ + +const nbt_byte_t *nbt_byte_array_data(nbt_tag_t *tag, nbt_size_t *length); +nbt_size_t nbt_byte_array_length(nbt_tag_t *tag); + +const char *nbt_string_data(nbt_tag_t *tag, nbt_size_t *length); +nbt_size_t nbt_string_length(nbt_tag_t *tag); + +const nbt_int_t *nbt_int_array_data(nbt_tag_t *tag, nbt_size_t *length); +nbt_size_t nbt_int_array_length(nbt_tag_t *tag); + +const nbt_long_t *nbt_long_array_data(nbt_tag_t *tag, nbt_size_t *length); +nbt_size_t nbt_long_array_length(nbt_tag_t *tag); + +/* list functions */ + +nbt_type_t nbt_list_type(nbt_tag_t *list); +nbt_size_t nbt_list_length(nbt_tag_t *list); + +nbt_tag_t *nbt_list_get(nbt_tag_t *list, nbt_size_t idx); + +int nbt_list_append(nbt_tag_t *list, nbt_tag_t *tag); +int nbt_list_append_move(nbt_tag_t *list, nbt_tag_t *tag); + +int nbt_list_insert(nbt_tag_t *list, nbt_tag_t *tag, size_t at); +int nbt_list_insert_move(nbt_tag_t *list, nbt_tag_t *tag, size_t at); + +int nbt_list_remove(nbt_tag_t *list, nbt_size_t idx); +nbt_tag_t *nbt_list_pop(nbt_tag_t *list, nbt_size_t idx); + +int nbt_list_remove_back(nbt_tag_t *list); +nbt_tag_t *nbt_list_pop_back(nbt_tag_t *list); + +void nbt_list_clear(nbt_tag_t *list); + +/* list advanced functions */ +nbt_size_t nbt_list_capacity(nbt_tag_t *list); + +/* realloc internal array down to size */ +int nbt_list_shrink(nbt_tag_t *list); + +/* realloc internal array to at least this size */ +int nbt_list_reserve(nbt_tag_t *list, nbt_size_t newsz); + +/* realloc internal array to at least the list size plus moresz + * (basically equivalent to `nbt_list_reserve(list, nbt_list_length(list) + moresz)'.) */ +int nbt_list_reserve_more(nbt_tag_t *list, nbt_size_t moresz); + +/* compound functions */ + +nbt_size_t nbt_compound_length(nbt_tag_t *compound); + +nbt_tag_t *nbt_compound_find(const char *key); +nbt_tag_t *nbt_compound_findn(const char *key, nbt_size_t keylen); + +/* returns NBT_TAG_END if there is no value present */ +nbt_type_t nbt_compound_type(const char *key); +nbt_type_t nbt_compound_typen(const char *key, nbt_size_t keylen); + +int nbt_compound_put(nbt_tag_t *compound, const char *key, nbt_tag_t *value); +int nbt_compound_putn(nbt_tag_t *compound, const char *key, nbt_size_t sz, nbt_tag_t *value); + +nbt_tag_t *nbt_compound_swap(nbt_tag_t *compound, const char *key, nbt_tag_t *value); +nbt_tag_t *nbt_compound_swapn(nbt_tag_t *compound, const char *key, nbt_size_t sz, nbt_tag_t *value); + +int nbt_compound_remove(nbt_tag_t *compound, const char *key); +int nbt_compound_removen(nbt_tag_t *compound, const char *key, nbt_size_t sz); + +nbt_tag_t *nbt_compound_pop(nbt_tag_t *compound, const char *key); +nbt_tag_t *nbt_compound_popn(nbt_tag_t *compound, const char *key, nbt_size_t sz); + +void nbt_compound_clear(nbt_tag_t *compound); + +int nbt_compound_merge(nbt_tag_t *target, nbt_tag_t *srccomp); + +#endif -- cgit v1.2.3-70-g09d2