Accelerator Independent Data Access / PVAccess 2.0
AIDA-PVA is the latest version of the AIDA framework. Built on top of EPICS 7 it enables client applications to programmatically access and manage any device or database on the SLAC Network using simple channel names.
Loading...
Searching...
No Matches
aida_pva_json.h
1/* vim: set et ts=3 sw=3 sts=3 ft=c:
2 * **CMS**=C_INC
3 *
4 * Copyright (C) 2012-2021 the json-parser authors All rights reserved.
5 * https://github.com/json-parser/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 aida_pva_json_h
32#define aida_pva_json_h
33
34#ifndef json_char
35#define json_char char
36#endif
37
38#ifndef json_int_t
39#ifndef _MSC_VER
40// REMOVED for compatibility with OpenVMS
41// #include <stdint.h>
42#include <limits.h>
43#define json_int_t long
44#else
45#define json_int_t __int64
46#endif
47#endif
48
49#include <stddef.h>
50
51#ifdef __cplusplus
52
53#include <string.h>
54
55extern "C"
56{
57
58#endif
59
60typedef struct
61{
62 unsigned long max_memory;
63 int settings;
64
65 /* Custom allocator support (leave null to use malloc/free)
66 */
67
68 void* (* mem_alloc)(size_t, int zero, void* user_data);
69 void (* mem_free)(void*, void* user_data);
70
71 void* user_data; /* will be passed to mem_alloc and mem_free */
72
73 size_t value_extra; /* how much extra space to allocate for values? */
74
75} json_settings;
76
77#define json_enable_comments 0x01
78
79typedef enum
80{
81 json_none,
82 json_object,
83 json_array,
84 json_integer,
85 json_double,
86 json_string,
87 json_boolean,
88 json_null
89
90} json_type;
91
92extern const struct _json_value json_value_none;
93
94typedef struct _json_object_entry
95{
96 json_char* name;
97 unsigned int name_length;
98
99 struct _json_value* value;
100
101} json_object_entry;
102
103typedef struct _json_value
104{
105 struct _json_value* parent;
106
107 json_type type;
108
109 union
110 {
111 int boolean;
112 json_int_t integer;
113 double dbl;
114
115 struct
116 {
117 unsigned int length;
118 json_char* ptr; /* null terminated */
119
120 } string;
121
122 struct
123 {
124 unsigned int length;
125
126 json_object_entry* values;
127
128#if defined(__cplusplus)
129 json_object_entry * begin () const
130 { return values;
131 }
132 json_object_entry * end () const
133 { return values + length;
134 }
135#endif
136
137 } object;
138
139 struct
140 {
141 unsigned int length;
142 struct _json_value** values;
143
144#if defined(__cplusplus)
145 _json_value ** begin () const
146 { return values;
147 }
148 _json_value ** end () const
149 { return values + length;
150 }
151#endif
152
153 } array;
154
155 } u;
156
157 union
158 {
159 struct _json_value* next_alloc;
160 void* object_mem;
161
162 } _reserved;
163
164#ifdef JSON_TRACK_SOURCE
165
166 /* Location of the value in the source JSON
167 */
168 unsigned int line, col;
169
170#endif
171
172
173 /* Some C++ operator sugar */
174
175#ifdef __cplusplus
176
177 public:
178
179 inline _json_value ()
180 { memset (this, 0, sizeof (_json_value));
181 }
182
183 inline const struct _json_value &operator [] (int index) const
184 {
185 if (type != json_array || index < 0
186 || ((unsigned int) index) >= u.array.length)
187 {
188 return json_value_none;
189 }
190
191 return *u.array.values [index];
192 }
193
194 inline const struct _json_value &operator [] (const char * index) const
195 {
196 if (type != json_object)
197 return json_value_none;
198
199 for (unsigned int i = 0; i < u.object.length; ++ i)
200 if (!strcmp (u.object.values [i].name, index))
201 return *u.object.values [i].value;
202
203 return json_value_none;
204 }
205
206 inline operator const char * () const
207 {
208 switch (type)
209 {
210 case json_string:
211 return u.string.ptr;
212
213 default:
214 return "";
215 };
216 }
217
218 inline operator json_int_t () const
219 {
220 switch (type)
221 {
222 case json_integer:
223 return u.integer;
224
225 case json_double:
226 return (json_int_t) u.dbl;
227
228 default:
229 return 0;
230 };
231 }
232
233 inline operator bool () const
234 {
235 if (type != json_boolean)
236 return false;
237
238 return u.boolean != 0;
239 }
240
241 inline operator double () const
242 {
243 switch (type)
244 {
245 case json_integer:
246 return (double) u.integer;
247
248 case json_double:
249 return u.dbl;
250
251 default:
252 return 0;
253 };
254 }
255
256#endif
257
258} json_value;
259
260json_value* json_parse(const json_char* json,
261 size_t length);
262
263#define json_error_max 128
264json_value* json_parse_ex(json_settings* settings,
265 const json_char* json,
266 size_t length,
267 char* error);
268
269void json_value_free(json_value*);
270
271/* Not usually necessary, unless you used a custom mem_alloc and now want to
272 * use a custom mem_free.
273 */
274void json_value_free_ex(json_settings* settings,
275 json_value*);
276
277unsigned int totalStingLengthOf(json_value *jsonArray);
278
279#ifdef __cplusplus
280} /* extern "C" */
281#endif
282
283#endif