Debian Codegraph @

texk/web2c/mfluadir/otfcc/include/dep/json.h

raw
1
2 /* vim: set et ts=3 sw=3 sts=3 ft=c:
3 *
4 * Copyright (C) 2012, 2013, 2014 James McLaughlin et al. All rights reserved.
5 * https://github.com/udp/json-parser
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #ifndef _JSON_H
32 #define _JSON_H
33
34 #ifndef json_char
35 #define json_char char
36 #endif
37
38 #ifdef _CARYLL_USE_PRE_SERIALIZED
39 #define CARYLL_USE_PRE_SERIALIZED
40 #endif
41
42 #ifndef json_int_t
43 #include <stdint.h>
44 #ifndef _MSC_VER
45 #include <inttypes.h>
46 #define json_int_t int64_t
47 #else
48 #define json_int_t __int64
49 #endif
50 #endif
51
52 #include <stdlib.h>
53
54 #ifdef __cplusplus
55
56 #include <string.h>
57
58 extern "C"
59 {
60
61 #endif
62
63 typedef struct
64 {
65 unsigned long max_memory;
66 int settings;
67
68 /* Custom allocator support (leave null to use malloc/free)
69 */
70
71 void * (* mem_alloc) (size_t, int zero, void * user_data);
72 void (* mem_free) (void *, void * user_data);
73
74 void * user_data; /* will be passed to mem_alloc and mem_free */
75
76 size_t value_extra; /* how much extra space to allocate for values? */
77
78 } json_settings;
79
80 #define json_enable_comments 0x01
81
82 typedef enum
83 {
84 json_none,
85 json_object,
86 json_array,
87 json_integer,
88 json_double,
89 json_string,
90 json_boolean,
91 json_null
92 #ifdef CARYLL_USE_PRE_SERIALIZED
93 ,
94 json_pre_serialized
95 #endif
96 } json_type;
97
98 extern const struct _json_value json_value_none;
99
100 typedef struct _json_object_entry
101 {
102 json_char * name;
103 unsigned int name_length;
104
105 struct _json_value * value;
106
107 } json_object_entry;
108
109 typedef struct _json_value
110 {
111 struct _json_value * parent;
112
113 json_type type;
114
115 union
116 {
117 int boolean;
118 json_int_t integer;
119 double dbl;
120
121 struct
122 {
123 unsigned int length;
124 json_char * ptr; /* null terminated */
125
126 } string;
127
128 struct
129 {
130 unsigned int length;
131
132 json_object_entry * values;
133
134 #if defined(__cplusplus) && __cplusplus >= 201103L
135 decltype(values) begin () const
136 { return values;
137 }
138 decltype(values) end () const
139 { return values + length;
140 }
141 #endif
142
143 } object;
144
145 struct
146 {
147 unsigned int length;
148 struct _json_value ** values;
149
150 #if defined(__cplusplus) && __cplusplus >= 201103L
151 decltype(values) begin () const
152 { return values;
153 }
154 decltype(values) end () const
155 { return values + length;
156 }
157 #endif
158
159 } array;
160
161 } u;
162
163 union
164 {
165 struct _json_value * next_alloc;
166 void * object_mem;
167
168 } _reserved;
169
170 #ifdef JSON_TRACK_SOURCE
171
172 /* Location of the value in the source JSON
173 */
174 unsigned int line, col;
175
176 #endif
177
178
179 /* Some C++ operator sugar */
180
181 #ifdef __cplusplus
182
183 public:
184
185 inline _json_value ()
186 { memset (this, 0, sizeof (_json_value));
187 }
188
189 inline const struct _json_value &operator [] (int index) const
190 {
191 if (type != json_array || index < 0
192 || ((unsigned int) index) >= u.array.length)
193 {
194 return json_value_none;
195 }
196
197 return *u.array.values [index];
198 }
199
200 inline const struct _json_value &operator [] (const char * index) const
201 {
202 if (type != json_object)
203 return json_value_none;
204
205 for (unsigned int i = 0; i < u.object.length; ++ i)
206 if (!strcmp (u.object.values [i].name, index))
207 return *u.object.values [i].value;
208
209 return json_value_none;
210 }
211
212 inline operator const char * () const
213 {
214 switch (type)
215 {
216 case json_string:
217 return u.string.ptr;
218
219 default:
220 return "";
221 };
222 }
223
224 inline operator json_int_t () const
225 {
226 switch (type)
227 {
228 case json_integer:
229 return u.integer;
230
231 case json_double:
232 return (json_int_t) u.dbl;
233
234 default:
235 return 0;
236 };
237 }
238
239 inline operator bool () const
240 {
241 if (type != json_boolean)
242 return false;
243
244 return u.boolean != 0;
245 }
246
247 inline operator double () const
248 {
249 switch (type)
250 {
251 case json_integer:
252 return (double) u.integer;
253
254 case json_double:
255 return u.dbl;
256
257 default:
258 return 0;
259 };
260 }
261
262 #endif
263
264 } json_value;
265
266 json_value * json_parse (const json_char * json,
267 size_t length);
268
269 #define json_error_max 128
270 json_value * json_parse_ex (json_settings * settings,
271 const json_char * json,
272 size_t length,
273 char * error);
274
275 void json_value_free (json_value *);
276
277
278 /* Not usually necessary, unless you used a custom mem_alloc and now want to
279 * use a custom mem_free.
280 */
281 void json_value_free_ex (json_settings * settings,
282 json_value *);
283
284
285 #ifdef __cplusplus
286 } /* extern "C" */
287 #endif
288
289 #endif
290
291