Debian Codegraph @

libmowgli-2/src/libmowgli/ext/json-inline.h

raw
1 /*
2 * Copyright (C) 2012 Alex Iadicicco
3 * Rights to this code are as documented in COPYING.
4 *
5 * JSON inline functions, to simplify refcounting etc for other users
6 */
7
8 #ifndef MOWGLI_SRC_LIBMOWGLI_EXT_JSON_INLINE_H_INCLUDE_GUARD
9 #define MOWGLI_SRC_LIBMOWGLI_EXT_JSON_INLINE_H_INCLUDE_GUARD 1
10
11 #include "ext/json.h"
12
13 /* We don't need to include any other headers here. This is in a separate
14 file to keep clutter out of the main json.h */
15
16 /* string */
17 static inline void
18 mowgli_json_string_reset(mowgli_json_t *s)
19 {
20 mowgli_string_reset(MOWGLI_JSON_STRING(s));
21 }
22
23 static inline void
24 mowgli_json_string_append(mowgli_json_t *s, const char *src, size_t n)
25 {
26 mowgli_string_append(MOWGLI_JSON_STRING(s), src, n);
27 }
28
29 static inline void
30 mowgli_json_string_append_char(mowgli_json_t *s, const char c)
31 {
32 mowgli_string_append_char(MOWGLI_JSON_STRING(s), c);
33 }
34
35 /* array */
36 static inline size_t
37 mowgli_json_array_size(mowgli_json_t *arr)
38 {
39 return MOWGLI_JSON_ARRAY(arr)->count;
40 }
41
42 static inline void
43 mowgli_json_array_add(mowgli_json_t *arr, mowgli_json_t *data)
44 {
45 mowgli_node_add(mowgli_json_incref(data), mowgli_node_create(), MOWGLI_JSON_ARRAY(arr));
46 }
47
48 static inline void
49 mowgli_json_array_add_head(mowgli_json_t *arr, mowgli_json_t *data)
50 {
51 mowgli_node_add_head(mowgli_json_incref(data), mowgli_node_create(), MOWGLI_JSON_ARRAY(arr));
52 }
53
54 static inline void
55 mowgli_json_array_insert(mowgli_json_t *arr, mowgli_json_t *data, size_t pos)
56 {
57 mowgli_node_insert(mowgli_json_incref(data), mowgli_node_create(), MOWGLI_JSON_ARRAY(arr), pos);
58 }
59
60 static inline void
61 mowgli_json_array_delete(mowgli_json_t *arr, mowgli_json_t *data)
62 {
63 mowgli_node_t *n = mowgli_node_find(data, MOWGLI_JSON_ARRAY(arr));
64
65 if (n == NULL)
66 return;
67
68 mowgli_node_delete(n, MOWGLI_JSON_ARRAY(arr));
69 mowgli_json_decref(data);
70 }
71
72 static inline bool
73 mowgli_json_array_contains(mowgli_json_t *arr, mowgli_json_t *data)
74 {
75 return mowgli_node_find(data, MOWGLI_JSON_ARRAY(arr)) != NULL;
76 }
77
78 static inline mowgli_json_t *
79 mowgli_json_array_nth(mowgli_json_t *arr, size_t pos)
80 {
81 return (mowgli_json_t *) mowgli_node_nth_data(MOWGLI_JSON_ARRAY(arr), pos);
82 }
83
84 /* object */
85 static inline size_t
86 mowgli_json_object_size(mowgli_json_t *obj)
87 {
88 return mowgli_patricia_size(MOWGLI_JSON_OBJECT(obj));
89 }
90
91 static inline void
92 mowgli_json_object_add(mowgli_json_t *obj, const char *key, mowgli_json_t *data)
93 {
94 mowgli_patricia_add(MOWGLI_JSON_OBJECT(obj), key, mowgli_json_incref(data));
95 }
96
97 static inline mowgli_json_t *
98 mowgli_json_object_retrieve(mowgli_json_t *obj, const char *key)
99 {
100 return (mowgli_json_t *) mowgli_patricia_retrieve(MOWGLI_JSON_OBJECT(obj), key);
101 }
102
103 static inline mowgli_json_t *
104 mowgli_json_object_delete(mowgli_json_t *obj, const char *key)
105 {
106 return (mowgli_json_t *) mowgli_patricia_delete(MOWGLI_JSON_OBJECT(obj), key);
107 }
108
109 #endif /* MOWGLI_SRC_LIBMOWGLI_EXT_JSON_INLINE_H_INCLUDE_GUARD */