]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Plugson/src/Core/ventoy_json.h
upate issue template
[Ventoy.git] / Plugson / src / Core / ventoy_json.h
1 /******************************************************************************
2 * ventoy_json.h
3 *
4 * Copyright (c) 2021, longpanda <admin@ventoy.net>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20 #ifndef __VENTOY_JSON_H__
21 #define __VENTOY_JSON_H__
22
23 #define JSON_NEW_ITEM(pstJson, ret) \
24 { \
25 (pstJson) = (VTOY_JSON *)zalloc(sizeof(VTOY_JSON)); \
26 if (NULL == (pstJson)) \
27 { \
28 vdebug("Failed to alloc memory for json."); \
29 return (ret); \
30 } \
31 }
32
33 #if defined(_MSC_VER) || defined(WIN32)
34 #define ssprintf(curpos, buf, len, fmt, ...) \
35 curpos += scnprintf(buf + curpos, len - curpos, fmt, ##__VA_ARGS__)
36
37 #define VTOY_JSON_IS_SKIPABLE(c) (((c) <= 32) ? 1 : 0)
38
39 #define VTOY_JSON_PRINT_PREFIX(uiDepth, ...) \
40 { \
41 uint32_t _uiLoop = 0; \
42 for (_uiLoop = 0; _uiLoop < (uiDepth); _uiLoop++) \
43 { \
44 ssprintf(uiCurPos, pcBuf, uiBufLen, " "); \
45 } \
46 ssprintf(uiCurPos, pcBuf, uiBufLen, ##__VA_ARGS__); \
47 }
48 #else
49
50 #define ssprintf(curpos, buf, len, fmt, args...) \
51 curpos += scnprintf(buf + curpos, len - curpos, fmt, ##args)
52
53 #define VTOY_JSON_IS_SKIPABLE(c) (((c) <= 32) ? 1 : 0)
54
55 #define VTOY_JSON_PRINT_PREFIX(uiDepth, args...) \
56 { \
57 uint32_t _uiLoop = 0; \
58 for (_uiLoop = 0; _uiLoop < (uiDepth); _uiLoop++) \
59 { \
60 ssprintf(uiCurPos, pcBuf, uiBufLen, " "); \
61 } \
62 ssprintf(uiCurPos, pcBuf, uiBufLen, ##args); \
63 }
64 #endif
65
66
67 #define VTOY_JSON_SUCCESS_RET "{ \"result\" : \"success\" }"
68 #define VTOY_JSON_FAILED_RET "{ \"result\" : \"failed\" }"
69 #define VTOY_JSON_INVALID_RET "{ \"result\" : \"invalidfmt\" }"
70 #define VTOY_JSON_TOKEN_ERR_RET "{ \"result\" : \"tokenerror\" }"
71 #define VTOY_JSON_EXIST_RET "{ \"result\" : \"exist\" }"
72 #define VTOY_JSON_TIMEOUT_RET "{ \"result\" : \"timeout\" }"
73 #define VTOY_JSON_BUSY_RET "{ \"result\" : \"busy\" }"
74 #define VTOY_JSON_INUSE_RET "{ \"result\" : \"inuse\" }"
75 #define VTOY_JSON_NOTFOUND_RET "{ \"result\" : \"notfound\" }"
76 #define VTOY_JSON_NOTRUNNING_RET "{ \"result\" : \"notrunning\" }"
77 #define VTOY_JSON_NOT_READY_RET "{ \"result\" : \"notready\" }"
78 #define VTOY_JSON_NOT_SUPPORT_RET "{ \"result\" : \"notsupport\" }"
79 #define VTOY_JSON_MBR_2TB_RET "{ \"result\" : \"mbr2tb\" }"
80 #define VTOY_JSON_INVALID_RSV_RET "{ \"result\" : \"reserve_invalid\" }"
81 #define VTOY_JSON_FILE_NOT_FOUND_RET "{ \"result\" : \"file_not_found\" }"
82
83 typedef enum tagJSON_TYPE
84 {
85 JSON_TYPE_NUMBER = 0,
86 JSON_TYPE_STRING,
87 JSON_TYPE_BOOL,
88 JSON_TYPE_ARRAY,
89 JSON_TYPE_OBJECT,
90 JSON_TYPE_NULL,
91 JSON_TYPE_BUTT
92 }JSON_TYPE;
93
94 typedef struct tagVTOY_JSON
95 {
96 struct tagVTOY_JSON *pstPrev;
97 struct tagVTOY_JSON *pstNext;
98 struct tagVTOY_JSON *pstChild;
99
100 JSON_TYPE enDataType;
101 union
102 {
103 char *pcStrVal;
104 int iNumVal;
105 uint64_t lValue;
106 }unData;
107
108 char *pcName;
109 }VTOY_JSON;
110
111 #define VTOY_JSON_FMT_BEGIN(uiCurPos, pcBuf, uiBufLen) \
112 {\
113 uint32_t __uiCurPos = (uiCurPos);\
114 uint32_t __uiBufLen = (uiBufLen);\
115 char *__pcBuf = (pcBuf);
116
117 #define VTOY_JSON_FMT_END(uiCurPos) \
118 (uiCurPos) = __uiCurPos;\
119 }
120
121 #define VTOY_JSON_FMT_OBJ_BEGIN() ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "{")
122 #define VTOY_JSON_FMT_OBJ_BEGIN_L(P) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s{", P)
123 #define VTOY_JSON_FMT_OBJ_BEGIN_N() ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "{\n")
124 #define VTOY_JSON_FMT_OBJ_BEGIN_LN(P) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s{\n", P)
125
126 #define VTOY_JSON_FMT_OBJ_END() \
127 {\
128 if (',' == *(__pcBuf + (__uiCurPos - 1)))\
129 {\
130 __uiCurPos -= 1;\
131 }\
132 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "}");\
133 }
134
135 #define VTOY_JSON_FMT_OBJ_ENDEX() \
136 {\
137 if (',' == *(__pcBuf + (__uiCurPos - 1)))\
138 {\
139 __uiCurPos -= 1;\
140 }\
141 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "},");\
142 }
143
144
145
146 #define VTOY_JSON_FMT_KEY(Key) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":", (Key))
147 #define VTOY_JSON_FMT_KEY_L(P, Key) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\":", P, (Key))
148
149
150 #define VTOY_JSON_FMT_ITEM(Item) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\",", (Item))
151 #define VTOY_JSON_FMT_ITEM_L(P, Item) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\",", P, (Item))
152 #define VTOY_JSON_FMT_ITEM_LN(P, Item) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\",\n", P, (Item))
153 #define VTOY_JSON_FMT_ITEM_PATH_LN(P, Item) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\",\n", P, ventoy_real_path(Item))
154
155 #define VTOY_JSON_FMT_COMA() ssprintf(__uiCurPos, __pcBuf, __uiBufLen, ",")
156 #define VTOY_JSON_FMT_COMA_N(cnt) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, ",\n")
157 #define VTOY_JSON_FMT_COMA_N_CNT(cnt) if ((cnt) > 0) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, ",\n")
158
159
160 #define VTOY_JSON_FMT_APPEND_BEGIN() \
161 { \
162 if ('}' == *(__pcBuf + (__uiCurPos - 1)))\
163 {\
164 __uiCurPos -= 1;\
165 }\
166 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, ",");\
167 }
168
169 #define VTOY_JSON_FMT_APPEND_END() \
170 { \
171 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "}");\
172 }
173
174 #define VTOY_JSON_FMT_ARY_BEGIN() ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "[")
175 #define VTOY_JSON_FMT_ARY_BEGIN_N() ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "[\n")
176
177 #define VTOY_JSON_FMT_ARY_END() \
178 {\
179 if (',' == *(__pcBuf + (__uiCurPos - 1)))\
180 {\
181 __uiCurPos -= 1;\
182 }\
183 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "]");\
184 }
185 #define VTOY_JSON_FMT_ARY_ENDEX() \
186 {\
187 if (',' == *(__pcBuf + (__uiCurPos - 1)))\
188 {\
189 __uiCurPos -= 1;\
190 }\
191 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "],");\
192 }
193
194
195
196 #define VTOY_JSON_FMT_OBJ_END_L(P) \
197 {\
198 if ('\n' == *(__pcBuf + (__uiCurPos - 1)) && ',' == *(__pcBuf + (__uiCurPos - 2)))\
199 {\
200 *(__pcBuf + (__uiCurPos - 2)) = '\n';\
201 __uiCurPos -= 1;\
202 }\
203 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s}%s", P);\
204 }
205 #define VTOY_JSON_FMT_OBJ_ENDEX_L(P) \
206 {\
207 if ('\n' == *(__pcBuf + (__uiCurPos - 1)) && ',' == *(__pcBuf + (__uiCurPos - 2)))\
208 {\
209 *(__pcBuf + (__uiCurPos - 2)) = '\n';\
210 __uiCurPos -= 1;\
211 }\
212 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s},%s", P);\
213 }
214
215 #define VTOY_JSON_FMT_OBJ_END_LN(P) \
216 {\
217 if ('\n' == *(__pcBuf + (__uiCurPos - 1)) && ',' == *(__pcBuf + (__uiCurPos - 2)))\
218 {\
219 *(__pcBuf + (__uiCurPos - 2)) = '\n';\
220 __uiCurPos -= 1;\
221 }\
222 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s}\n", P);\
223 }
224 #define VTOY_JSON_FMT_OBJ_ENDEX_LN(P) \
225 {\
226 if ('\n' == *(__pcBuf + (__uiCurPos - 1)) && ',' == *(__pcBuf + (__uiCurPos - 2)))\
227 {\
228 *(__pcBuf + (__uiCurPos - 2)) = '\n';\
229 __uiCurPos -= 1;\
230 }\
231 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s},\n", P);\
232 }
233
234
235
236
237 #define VTOY_JSON_FMT_ARY_END_L(P) \
238 {\
239 if ('\n' == *(__pcBuf + (__uiCurPos - 1)) && ',' == *(__pcBuf + (__uiCurPos - 2)))\
240 {\
241 *(__pcBuf + (__uiCurPos - 2)) = '\n';\
242 __uiCurPos -= 1;\
243 }\
244 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s]", P);\
245 }
246
247 #define VTOY_JSON_FMT_ARY_END_LN(P) \
248 {\
249 if ('\n' == *(__pcBuf + (__uiCurPos - 1)) && ',' == *(__pcBuf + (__uiCurPos - 2)))\
250 {\
251 *(__pcBuf + (__uiCurPos - 2)) = '\n';\
252 __uiCurPos -= 1;\
253 }\
254 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s]\n", P);\
255 }
256
257 #define VTOY_JSON_FMT_ARY_ENDEX_L(P) \
258 {\
259 if ('\n' == *(__pcBuf + (__uiCurPos - 1)) && ',' == *(__pcBuf + (__uiCurPos - 2)))\
260 {\
261 *(__pcBuf + (__uiCurPos - 2)) = '\n';\
262 __uiCurPos -= 1;\
263 }\
264 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s],", P);\
265 }
266
267
268 #define VTOY_JSON_FMT_ARY_ENDEX_LN(P) \
269 {\
270 if ('\n' == *(__pcBuf + (__uiCurPos - 1)) && ',' == *(__pcBuf + (__uiCurPos - 2)))\
271 {\
272 *(__pcBuf + (__uiCurPos - 2)) = '\n';\
273 __uiCurPos -= 1;\
274 }\
275 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s],\n", P);\
276 }
277
278
279
280
281 #define VTOY_JSON_FMT_UINT64(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %llu,", Key, (_ull)Val)
282
283 #define VTOY_JSON_FMT_ULONG(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %lu,", Key, Val)
284 #define VTOY_JSON_FMT_LONG(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %ld,", Key, Val)
285
286 #define VTOY_JSON_FMT_UINT(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %u,", Key, Val)
287 #define VTOY_JSON_FMT_UINT_N(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %u,\n", Key, Val)
288 #define VTOY_JSON_FMT_UINT_L(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": %u,", P, Key, Val)
289 #define VTOY_JSON_FMT_UINT_LN(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": %u,\n", P, Key, Val)
290
291
292 #define VTOY_JSON_FMT_STRUINT(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": \"%u\",", Key, Val)
293 #define VTOY_JSON_FMT_STRUINT_N(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": \"%u\",\n", Key, Val)
294 #define VTOY_JSON_FMT_STRUINT_L(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": \"%u\",", P, Key, Val)
295 #define VTOY_JSON_FMT_STRUINT_LN(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": \"%u\",\n", P, Key, Val)
296
297 #define VTOY_JSON_FMT_STRSINT(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": \"%d\",", Key, Val)
298 #define VTOY_JSON_FMT_STRSINT_N(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": \"%d\",\n", Key, Val)
299 #define VTOY_JSON_FMT_STRSINT_L(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": \"%d\",", P, Key, Val)
300 #define VTOY_JSON_FMT_STRSINT_LN(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": \"%d\",\n", P, Key, Val)
301
302 #define VTOY_JSON_FMT_CTRL_INT(Prefix, Key, Field) \
303 if (def->Field != data->Field) \
304 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s{ \"%s\": \"%d\" },\n", Prefix, Key, data->Field)
305
306 #define VTOY_JSON_FMT_CTRL_STRN(P, Key, Field) \
307 if (strcmp(def->Field, data->Field)) \
308 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s{ \"%s\": \"%s\" },\n", P, Key, data->Field)
309
310 #define VTOY_JSON_FMT_CTRL_STRN_STR(P, Key, ptr) \
311 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s{ \"%s\": \"%s\" },\n", P, Key, ptr)
312
313 #define VTOY_JSON_FMT_CTRL_PUB_STRN(P, Key, Field) \
314 if (strcmp(def->Field, g_pub_path)) \
315 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s{ \"%s\": \"%s\" },\n", P, Key, g_pub_path)
316
317
318 #define VTOY_JSON_FMT_DIFF_STRN(P, Key, Field) \
319 if (strcmp(def->Field, data->Field)) \
320 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": \"%s\",\n", P, Key, data->Field)
321
322
323 #define VTOY_JSON_FMT_STRINT64(Key, Val) \
324 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": \"%llu\",", Key, Val)
325
326 #define VTOY_JSON_FMT_SINT(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %d,", Key, Val)
327 #define VTOY_JSON_FMT_SINT_N(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %d,\n", Key, Val)
328 #define VTOY_JSON_FMT_SINT_L(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": %d,", P, Key, Val)
329 #define VTOY_JSON_FMT_SINT_LN(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": %d,\n", P, Key, Val)
330
331 #define VTOY_JSON_FMT_DUBL(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %.1lf,", Key, Val)
332 #define VTOY_JSON_FMT_DUBL2(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %10.02lf,", Key, Val)
333
334 #define VTOY_JSON_FMT_STRN(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": \"%s\",", Key, Val)
335 #define VTOY_JSON_FMT_STRN_N(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": \"%s\",\n", Key, Val)
336 #define VTOY_JSON_FMT_STRN_L(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": \"%s\",", P, Key, Val)
337 #define VTOY_JSON_FMT_STRN_LN(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": \"%s\",\n", P, Key, Val)
338 #define VTOY_JSON_FMT_STRN_PATH_LN(P, Key, Val) \
339 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": \"%s\",\n", P, Key, ventoy_real_path(Val))
340
341 int vtoy_json_escape_string(char *buf, int buflen, const char *str, int newline);
342
343 #define VTOY_JSON_FMT_STRN_EX_LN(P, Key, Val) \
344 {\
345 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": ", P, Key);\
346 __uiCurPos += vtoy_json_escape_string(__pcBuf + __uiCurPos, __uiBufLen - __uiCurPos, Val, 1);\
347 }
348
349
350 #define VTOY_JSON_FMT_NULL(Key) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": null,", Key)
351
352 #define VTOY_JSON_FMT_TRUE(Key) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": true,", (Key))
353 #define VTOY_JSON_FMT_FALSE(Key) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": false,", (Key))
354
355 #define VTOY_JSON_FMT_BOOL(Key, Val) \
356 {\
357 if (0 == (Val))\
358 {\
359 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": false,", (Key));\
360 }\
361 else \
362 {\
363 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": true,", (Key));\
364 }\
365 }
366
367 typedef struct tagVTOY_JSON_PARSE
368 {
369 char *pcKey;
370 void *pDataBuf;
371 uint32_t uiBufSize;
372 }VTOY_JSON_PARSE_S;
373
374 #define JSON_SUCCESS 0
375 #define JSON_FAILED 1
376 #define JSON_NOT_FOUND 2
377
378 int vtoy_json_parse_value
379 (
380 char *pcNewStart,
381 char *pcRawStart,
382 VTOY_JSON *pstJson,
383 const char *pcData,
384 const char **ppcEnd
385 );
386 VTOY_JSON * vtoy_json_create(void);
387 int vtoy_json_parse(VTOY_JSON *pstJson, const char *szJsonData);
388 int vtoy_json_parse_ex(VTOY_JSON *pstJson, const char *szJsonData, int szLen);
389 int vtoy_json_destroy(VTOY_JSON *pstJson);
390 VTOY_JSON *vtoy_json_find_item
391 (
392 VTOY_JSON *pstJson,
393 JSON_TYPE enDataType,
394 const char *szKey
395 );
396 int vtoy_json_scan_parse
397 (
398 const VTOY_JSON *pstJson,
399 uint32_t uiParseNum,
400 VTOY_JSON_PARSE_S *pstJsonParse
401 );
402 int vtoy_json_get_int
403 (
404 VTOY_JSON *pstJson,
405 const char *szKey,
406 int *piValue
407 );
408 int vtoy_json_get_uint
409 (
410 VTOY_JSON *pstJson,
411 const char *szKey,
412 uint32_t *puiValue
413 );
414 int vtoy_json_get_uint64
415 (
416 VTOY_JSON *pstJson,
417 const char *szKey,
418 uint64_t *pui64Value
419 );
420 int vtoy_json_get_bool
421 (
422 VTOY_JSON *pstJson,
423 const char *szKey,
424 uint8_t *pbValue
425 );
426 int vtoy_json_get_string
427 (
428 VTOY_JSON *pstJson,
429 const char *szKey,
430 uint32_t uiBufLen,
431 char *pcBuf
432 );
433 const char * vtoy_json_get_string_ex(VTOY_JSON *pstJson, const char *szKey);
434
435 #endif /* __VENTOY_JSON_H__ */
436