-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathntv.h
343 lines (262 loc) · 12.8 KB
/
ntv.h
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/******************************************************************************
* Copyright (C) 2008 - 2016 Andreas Smas
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/
#pragma once
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "queue.h"
TAILQ_HEAD(ntv_queue, ntv);
typedef struct ntv_ns {
int refcount;
char *str;
} ntv_ns_t;
typedef enum {
NTV_XML_ATTRIBUTE = 0x1,
NTV_DONT_FREE = 0x2,
NTV_REFCOUNTED = 0x4,
NTV_NOCASE = 0x8,
NTV_ERR = 0x10,
} ntv_flags;
typedef enum {
NTV_NULL,
NTV_BOOLEAN,
NTV_STRING,
NTV_INT,
NTV_DOUBLE,
NTV_BINARY,
NTV_MAP,
NTV_LIST,
} ntv_type;
typedef struct ntv {
TAILQ_ENTRY(ntv) ntv_link;
union {
struct ntv *ntv_parent;
unsigned long *ntv_refcount;
};
char *ntv_name;
ntv_flags ntv_flags;
ntv_type ntv_type;
ntv_ns_t *ntv_namespace;
union {
int64_t ntv_s64;
char *ntv_string;
const char *ntv_cstring;
struct {
void *ntv_bin;
size_t ntv_binsize;
};
double ntv_double;
bool ntv_boolean;
};
struct ntv_queue ntv_children;
} ntv_t;
#define NTV_INT(x) &(const ntv_t){.ntv_type = NTV_INT, .ntv_s64 = x}
#define NTV_STR(x) &(const ntv_t){.ntv_type = NTV_STRING, .ntv_cstring = x}
#define NTV_INDEX(i) ((const char *)(intptr_t)(-(i+1)))
#define NTV_FOREACH(field_, msg) \
for(ntv_t *field_ = (msg) ? TAILQ_FIRST(&(msg)->ntv_children) : NULL; \
field_ != NULL; field_ = TAILQ_NEXT(field_, ntv_link))
#define NTV_FOREACH_TYPE(field_, msg, type) \
for(ntv_t *field_ = ({ \
ntv_t *_x_ = (msg) ? TAILQ_FIRST(&(msg)->ntv_children) : NULL; \
while(_x_ && _x_->ntv_type != type) \
_x_ = TAILQ_NEXT(_x_, ntv_link); \
_x_; \
}); field_ != NULL; ({ \
field_ = TAILQ_NEXT(field_, ntv_link); \
while(field_ && field_->ntv_type != type) \
field_ = TAILQ_NEXT(field_, ntv_link); \
}))
// Misc toplevel functions
ntv_t *ntv_create(ntv_type type);
ntv_t *ntv_create_map(void);
ntv_t *ntv_create_list(void);
void ntv_delete_field(const ntv_t *ntv, const char *key);
ntv_t *ntv_nocase(ntv_t *ntv);
void ntv_release(ntv_t *ntv);
ntv_t *ntv_retain(ntv_t *ntv) __attribute__ ((warn_unused_result));
void ntv_releasep(ntv_t **ntv);
void ntv_print(const ntv_t *ntv);
ntv_t *ntv_copy(const ntv_t *src);
void ntv_merge(ntv_t *dst, const ntv_t *src);
void ntv_merge_add(ntv_t *dst, const ntv_t *src);
#define NTV_MERGE_MAPS 0x1
void ntv_merge_ex(ntv_t *dst, const ntv_t *src, int flags);
int ntv_is_empty(const ntv_t *ntv);
int ntv_num_children(const ntv_t *ntv);
const ntv_t *ntv_field_from_path(const ntv_t *n, const char **path);
ntv_t *ntv_detach_field(ntv_t *n, const char *key);
void ntv_delete_nulls(ntv_t *n); // Recursively delete all NULL fields
// Namespaces
ntv_ns_t *ntv_ns_create(const char *str);
ntv_ns_t *ntv_ns_retain(ntv_ns_t *ns) __attribute__ ((warn_unused_result));
void ntv_ns_release(ntv_ns_t *ns);
// Return non-zero if 'src' and 'dst' are not equal
int ntv_cmp(const ntv_t *src, const ntv_t *dst);
int ntv_has_field(const ntv_t *ntv, const char *key);
// Get operations on maps
const ntv_t *ntv_get(const ntv_t *ntv, const char *key);
int ntv_get_int(const ntv_t *ntv, const char *key, int default_value);
int64_t ntv_get_int64(const ntv_t *ntv, const char *key, int64_t default_value);
double ntv_get_double(const ntv_t *ntv, const char *key, double default_value);
const char *ntv_get_str(const ntv_t *ntv, const char *key);
const void *ntv_get_bin(const ntv_t *ntv, const char *key, size_t *sizep);
const ntv_t *ntv_get_map(const ntv_t *ntv, const char *key);
const ntv_t *ntv_get_list(const ntv_t *ntv, const char *key);
ntv_t *ntv_get_mutable_map(ntv_t *n, const char *key);
ntv_t *ntv_get_mutable_list(ntv_t *n, const char *key);
// Get operations on lists
int ntv_idx_int(const ntv_t *ntv, int idx, int default_value);
int64_t ntv_idx_int64(const ntv_t *ntv, int idx, int64_t default_value);
double ntv_idx_double(const ntv_t *ntv, int idx, double default_value);
const char *ntv_idx_str(const ntv_t *ntv, int idx);
const ntv_t *ntv_idx_map(const ntv_t *ntv, int idx);
const ntv_t *ntv_idx_list(const ntv_t *ntv, int idx);
// Unparanted field creation
ntv_t *ntv_int(int64_t value);
ntv_t *ntv_double(double value);
ntv_t *ntv_boolean(int value);
ntv_t *ntv_null(void);
ntv_t *ntv_bin(const void *data, size_t len);
ntv_t *ntv_str(const char *str);
ntv_t *ntv_strf(const char *fmt, ...);
ntv_t *ntv_map(const char *key, ...) __attribute__((__sentinel__(0)));
ntv_t *ntv_list(ntv_t *f, ...) __attribute__((__sentinel__(0)));
// Set operations on maps
void ntv_set_int(ntv_t *ntv, const char *key, int value);
void ntv_set_int64(ntv_t *ntv, const char *key, int64_t value);
void ntv_set_double(ntv_t *ntv, const char *key, double value);
void ntv_set_null(ntv_t *ntv, const char *key);
void ntv_set_boolean(ntv_t *ntv, const char *key, bool value);
void ntv_set_str(ntv_t *ntv, const char *key, const char *value);
void ntv_set_strf(ntv_t *ntv, const char *key, const char *fmt, ...)
__attribute__ ((format (printf, 3, 4)));
void ntv_set_bin(ntv_t *ntv, const char *key, const void *data,
size_t datalen);
void ntv_set_bin_prealloc(ntv_t *ntv, const char *key, void *data,
size_t datalen);
void ntv_set_ntv(ntv_t *ntv, const char *key, struct ntv *sub);
// Add operations on maps (This is incompatible with most JSON parsers)
void ntv_add_int(ntv_t *ntv, const char *key, int value);
void ntv_add_int64(ntv_t *ntv, const char *key, int64_t value);
void ntv_add_double(ntv_t *ntv, const char *key, double value);
void ntv_add_null(ntv_t *ntv, const char *key);
void ntv_add_boolean(ntv_t *ntv, const char *key, bool value);
void ntv_add_str(ntv_t *ntv, const char *key, const char *value);
void ntv_add_strf(ntv_t *ntv, const char *key, const char *fmt, ...)
__attribute__ ((format (printf, 3, 4)));
void ntv_add_str_prealloc(ntv_t *ntv, char *key, char *value);
void ntv_add_bin(ntv_t *ntv, const char *key, const void *data,
size_t datalen);
void ntv_add_bin_prealloc(ntv_t *ntv, const char *key, void *data,
size_t datalen);
void ntv_add_ntv(ntv_t *ntv, const char *key, struct ntv *sub);
// Set operations on lists
void ntv_set_idx_int(ntv_t *ntv, int key, int value);
void ntv_set_idx_int64(ntv_t *ntv, int key, int64_t value);
void ntv_set_idx_double(ntv_t *ntv, int key, double value);
void ntv_set_idx_null(ntv_t *ntv, int key);
void ntv_set_idx_boolean(ntv_t *ntv, int key, bool value);
void ntv_set_idx_str(ntv_t *ntv, int key, const char *value);
void ntv_set_idx_ntv(ntv_t *ntv, int key, struct ntv *sub);
int ntv_copy_field(ntv_t *dst, const char *dstfieldname,
const ntv_t *src, const char *srcfieldname);
struct mbuf;
#define NTV_JSON_F_PRETTY 0x1
#define NTV_JSON_F_WIDE 0x2
#define NTV_JSON_F_MINIMAL_ESCAPE 0x4
#define NTV_JSON_F_TRAILING_LF 0x8
void ntv_json_serialize_ex(const ntv_t *msg, struct mbuf *m, int flags,
int precision);
void ntv_json_serialize(const ntv_t *msg, struct mbuf *m, int flags);
char *ntv_json_serialize_to_str(const ntv_t *msg, int pretty);
char *ntv_json_serialize_to_str_ex(const ntv_t *msg, int pretty,
int precision);
ntv_t *ntv_json_deserialize(const char *src, char *errbuf, size_t errlen);
void ntv_binary_serialize(const ntv_t *msg, struct mbuf *m);
ntv_t *ntv_binary_deserialize(const void *data, size_t length);
ntv_t *ntv_binary_deserialize_nocopy(const void *data, size_t length);
void ntv_msgpack_serialize(const ntv_t *msg, struct mbuf *m);
ntv_t *ntv_msgpack_deserialize(const void *data, size_t length,
char *errbuf, size_t errlen);
ntv_t *ntv_msgpack_deserialize_nocopy(const void *data, size_t length,
char *errbuf, size_t errlen);
void ntv_cbor_serialize(const ntv_t *msg, struct mbuf *m);
ntv_t *ntv_cbor_deserialize(const void *data, size_t length,
char *errmsg, size_t errlen);
ntv_t *ntv_cbor_deserialize_nocopy(const void *data, size_t length,
char *errmsg, size_t errlen);
ntv_t *ntv_xml_deserialize(const char *src, char *errmsg, size_t errlen);
#if __STDC_VERSION__ >= 201112L
#ifdef __APPLE__
#define ntv_set(ntv, key, val) \
_Generic(val, \
int64_t: ntv_set_int64, \
const int64_t: ntv_set_int64, \
int: ntv_set_int, \
const int: ntv_set_int, \
unsigned int: ntv_set_int, \
const unsigned int: ntv_set_int, \
float: ntv_set_double, \
const float: ntv_set_double, \
double: ntv_set_double, \
const double: ntv_set_double, \
char *: ntv_set_str, \
const char *: ntv_set_str, \
char *const: ntv_set_str, \
const char *const: ntv_set_str, \
char[sizeof( val )]: ntv_set_str, \
const char[sizeof( val )]: ntv_set_str, \
ntv_t *: ntv_set_ntv \
)(ntv, key, val)
#else
#if UINTPTR_MAX == 0xffffffffffffffff
#define ntv_set(ntv, key, val) \
_Generic(val, \
int64_t: ntv_set_int64, \
long long: ntv_set_int64, \
int: ntv_set_int, \
unsigned int: ntv_set_int, \
float: ntv_set_double, \
double: ntv_set_double, \
char *: ntv_set_str, \
const char *: ntv_set_str, \
ntv_t *: ntv_set_ntv \
)(ntv, key, val)
#else
#define ntv_set(ntv, key, val) \
_Generic(val, \
int64_t: ntv_set_int64, \
int: ntv_set_int, \
unsigned int: ntv_set_int, \
float: ntv_set_double, \
double: ntv_set_double, \
char *: ntv_set_str, \
const char *: ntv_set_str, \
ntv_t *: ntv_set_ntv \
)(ntv, key, val)
#endif
#endif
#endif
#define NTV_CLEANUP __attribute__((cleanup(ntv_releasep)))
#define scoped_ntv_t ntv_t __attribute__((cleanup(ntv_releasep)))