summaryrefslogtreecommitdiffstats
path: root/include/nbt.h
blob: ba7e0a695dfcbcaff033d6e4acd2eb5847846b52 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#ifndef LIBWORLD_NBT_H_INCLUDED
#define LIBWORLD_NBT_H_INCLUDED

#include <stdint.h>
#include <stddef.h>

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