add single instance mode
[uwplayer.git] / application / json.h
1 /*
2  * Copyright 2022 Olaf Wintermann
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a 
5  * copy of this software and associated documentation files (the "Software"), 
6  * to deal in the Software without restriction, including without limitation 
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
8  * and/or sell copies of the Software, and to permit persons to whom the 
9  * Software is furnished to do so, subject to the following conditions:
10  * 
11  * The above copyright notice and this permission notice shall be included in 
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
20  * DEALINGS IN THE SOFTWARE.
21  */
22
23 #ifndef JSON_H
24 #define JSON_H
25
26 #include <stdlib.h>
27 #include <inttypes.h>
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 typedef struct JSONParser    JSONParser;
34 typedef struct JSONToken     JSONToken;
35
36 typedef struct JSONValue     JSONValue;
37
38 typedef enum JSONTokenType   JSONTokenType;
39 typedef enum JSONValueType   JSONValueType;
40 typedef enum JSONLiteralType JSONLiteralType;
41 typedef enum JSONReaderType  JSONReaderType;
42
43 typedef struct JSONArray JSONArray;
44 typedef struct JSONObject JSONObject;
45 typedef struct JSONString JSONString;
46 typedef struct JSONInteger JSONInteger;
47 typedef struct JSONNumber JSONNumber;
48 typedef struct JSONLiteral JSONLiteral;
49
50 typedef struct JSONObjValue JSONObjValue;
51
52 typedef struct JSONReadValueStack JSONReadValueStack;
53
54 enum JSONTokenType {
55     JSON_NO_TOKEN = 0,
56     JSON_TOKEN_ERROR,
57     JSON_TOKEN_BEGIN_ARRAY,
58     JSON_TOKEN_BEGIN_OBJECT,
59     JSON_TOKEN_END_ARRAY,
60     JSON_TOKEN_END_OBJECT,
61     JSON_TOKEN_NAME_SEPARATOR,
62     JSON_TOKEN_VALUE_SEPARATOR,
63     JSON_TOKEN_STRING,
64     JSON_TOKEN_INTEGER,
65     JSON_TOKEN_NUMBER,
66     JSON_TOKEN_LITERAL,
67     JSON_TOKEN_SPACE
68 };
69
70 enum JSONValueType {
71     JSON_OBJECT = 0,
72     JSON_ARRAY,
73     JSON_STRING,
74     JSON_INTEGER,
75     JSON_NUMBER,
76     JSON_LITERAL
77 };
78
79 enum JSONLiteralType {
80     JSON_NULL = 0,
81     JSON_TRUE,
82     JSON_FALSE
83 };
84
85 enum JSONReaderType {
86     JSON_READER_OBJECT_BEGIN,
87     JSON_READER_OBJECT_END,
88     JSON_READER_ARRAY_BEGIN,
89     JSON_READER_ARRAY_END,
90     JSON_READER_STRING,
91     JSON_READER_INTEGER,
92     JSON_READER_NUMBER,
93     JSON_READER_LITERAL
94 };
95
96 struct JSONToken {
97     JSONTokenType tokentype;
98     const char *content;
99     size_t length;
100     size_t alloc;
101 };
102
103 struct JSONParser {
104     const char *buffer;
105     size_t size;
106     size_t pos;
107     
108     JSONToken uncompleted;
109     int tokenizer_escape;
110     
111     int *states;
112     int nstates;
113     int states_alloc;
114     
115     JSONToken reader_token;
116     JSONReaderType reader_type;
117     int value_ready;
118     char *value_name;
119     size_t value_name_len;
120     char *value_str;
121     size_t value_str_len;
122     int64_t value_int;
123     double value_double;
124     
125     JSONValue **readvalue_stack;
126     int readvalue_nelm;
127     int readvalue_alloc;
128     JSONValue *read_value;
129     int readvalue_initialized;
130     
131     int reader_array_alloc;
132     
133     int error;
134 };
135
136 struct JSONArray {
137     JSONValue **array;
138     size_t alloc;
139     size_t size;
140 };
141
142 struct JSONObject {
143     JSONObjValue *values;
144     size_t alloc;
145     size_t size;
146 };
147
148 struct JSONObjValue {
149     char *name;
150     JSONValue *value;
151 };
152
153 struct JSONString {
154     char *string;
155     size_t length;
156 };
157
158 struct JSONInteger {
159     int64_t value;
160 };
161
162 struct JSONNumber {
163     double value;
164 };
165
166 struct JSONLiteral {
167     JSONLiteralType literal;
168 };
169
170
171 struct JSONValue {
172     JSONValueType type;
173     union {
174         JSONArray   array;
175         JSONObject  object;
176         JSONString  string;
177         JSONInteger integer;
178         JSONNumber  number;
179         JSONLiteral literal;
180     } value;
181 };
182
183
184 JSONParser* json_parser_new(void);
185 void json_parser_free(JSONParser *p);
186
187 void json_parser_fill(JSONParser *p, const char *buf, size_t size);
188
189 JSONToken json_parser_next_token(JSONParser *p);
190
191 int json_read(JSONParser *p);
192
193 JSONReaderType json_reader_type(JSONParser *p);
194 const char* json_reader_name(JSONParser *p, size_t *opt_len);
195 const char* json_reader_string(JSONParser *p, size_t *opt_len);
196 int64_t json_reader_int(JSONParser *p);
197 double json_reader_double(JSONParser *p);
198 int json_reader_isnull(JSONParser *p);
199 JSONLiteralType json_reader_literal(JSONParser *p);
200 int json_reader_bool(JSONParser *p);
201
202 int json_read_value(JSONParser *p, JSONValue **value);
203
204 JSONValue* json_obj_get(JSONObject *obj, const char *name);
205
206 JSONValue* json_array_get(JSONArray *array, size_t i);
207
208 void json_value_free(JSONValue *value);
209
210 int json_strcmp(JSONValue *jsstr, const char *str);
211 int json_strncmp(JSONValue *jsstr, const char *str, size_t slen);
212
213 #ifdef __cplusplus
214 }
215 #endif
216
217 #endif /* JSON_H */
218