]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Plugson/src/Core/ventoy_json.h
Update ko_KR.txt (#2025)
[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_ITEM_INT(Item) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%d,", (Item))
156
157
158 #define VTOY_JSON_FMT_COMA() ssprintf(__uiCurPos, __pcBuf, __uiBufLen, ",")
159 #define VTOY_JSON_FMT_COMA_N(cnt) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, ",\n")
160 #define VTOY_JSON_FMT_COMA_N_CNT(cnt) if ((cnt) > 0) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, ",\n")
161
162
163 #define VTOY_JSON_FMT_APPEND_BEGIN() \
164 { \
165 if ('}' == *(__pcBuf + (__uiCurPos - 1)))\
166 {\
167 __uiCurPos -= 1;\
168 }\
169 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, ",");\
170 }
171
172 #define VTOY_JSON_FMT_APPEND_END() \
173 { \
174 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "}");\
175 }
176
177 #define VTOY_JSON_FMT_ARY_BEGIN() ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "[")
178 #define VTOY_JSON_FMT_ARY_BEGIN_N() ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "[\n")
179
180 #define VTOY_JSON_FMT_ARY_END() \
181 {\
182 if (',' == *(__pcBuf + (__uiCurPos - 1)))\
183 {\
184 __uiCurPos -= 1;\
185 }\
186 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "]");\
187 }
188 #define VTOY_JSON_FMT_ARY_ENDEX() \
189 {\
190 if (',' == *(__pcBuf + (__uiCurPos - 1)))\
191 {\
192 __uiCurPos -= 1;\
193 }\
194 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "],");\
195 }
196
197
198
199 #define VTOY_JSON_FMT_OBJ_END_L(P) \
200 {\
201 if ('\n' == *(__pcBuf + (__uiCurPos - 1)) && ',' == *(__pcBuf + (__uiCurPos - 2)))\
202 {\
203 *(__pcBuf + (__uiCurPos - 2)) = '\n';\
204 __uiCurPos -= 1;\
205 }\
206 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s}%s", P);\
207 }
208 #define VTOY_JSON_FMT_OBJ_ENDEX_L(P) \
209 {\
210 if ('\n' == *(__pcBuf + (__uiCurPos - 1)) && ',' == *(__pcBuf + (__uiCurPos - 2)))\
211 {\
212 *(__pcBuf + (__uiCurPos - 2)) = '\n';\
213 __uiCurPos -= 1;\
214 }\
215 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s},%s", P);\
216 }
217
218 #define VTOY_JSON_FMT_OBJ_END_LN(P) \
219 {\
220 if ('\n' == *(__pcBuf + (__uiCurPos - 1)) && ',' == *(__pcBuf + (__uiCurPos - 2)))\
221 {\
222 *(__pcBuf + (__uiCurPos - 2)) = '\n';\
223 __uiCurPos -= 1;\
224 }\
225 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s}\n", P);\
226 }
227 #define VTOY_JSON_FMT_OBJ_ENDEX_LN(P) \
228 {\
229 if ('\n' == *(__pcBuf + (__uiCurPos - 1)) && ',' == *(__pcBuf + (__uiCurPos - 2)))\
230 {\
231 *(__pcBuf + (__uiCurPos - 2)) = '\n';\
232 __uiCurPos -= 1;\
233 }\
234 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s},\n", P);\
235 }
236
237
238
239
240 #define VTOY_JSON_FMT_ARY_END_L(P) \
241 {\
242 if ('\n' == *(__pcBuf + (__uiCurPos - 1)) && ',' == *(__pcBuf + (__uiCurPos - 2)))\
243 {\
244 *(__pcBuf + (__uiCurPos - 2)) = '\n';\
245 __uiCurPos -= 1;\
246 }\
247 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s]", P);\
248 }
249
250 #define VTOY_JSON_FMT_ARY_END_LN(P) \
251 {\
252 if ('\n' == *(__pcBuf + (__uiCurPos - 1)) && ',' == *(__pcBuf + (__uiCurPos - 2)))\
253 {\
254 *(__pcBuf + (__uiCurPos - 2)) = '\n';\
255 __uiCurPos -= 1;\
256 }\
257 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s]\n", P);\
258 }
259
260 #define VTOY_JSON_FMT_ARY_ENDEX_L(P) \
261 {\
262 if ('\n' == *(__pcBuf + (__uiCurPos - 1)) && ',' == *(__pcBuf + (__uiCurPos - 2)))\
263 {\
264 *(__pcBuf + (__uiCurPos - 2)) = '\n';\
265 __uiCurPos -= 1;\
266 }\
267 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s],", P);\
268 }
269
270
271 #define VTOY_JSON_FMT_ARY_ENDEX_LN(P) \
272 {\
273 if ('\n' == *(__pcBuf + (__uiCurPos - 1)) && ',' == *(__pcBuf + (__uiCurPos - 2)))\
274 {\
275 *(__pcBuf + (__uiCurPos - 2)) = '\n';\
276 __uiCurPos -= 1;\
277 }\
278 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s],\n", P);\
279 }
280
281
282
283
284 #define VTOY_JSON_FMT_UINT64(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %llu,", Key, (_ull)Val)
285
286 #define VTOY_JSON_FMT_ULONG(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %lu,", Key, Val)
287 #define VTOY_JSON_FMT_LONG(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %ld,", Key, Val)
288
289 #define VTOY_JSON_FMT_UINT(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %u,", Key, Val)
290 #define VTOY_JSON_FMT_UINT_N(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %u,\n", Key, Val)
291 #define VTOY_JSON_FMT_UINT_L(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": %u,", P, Key, Val)
292 #define VTOY_JSON_FMT_UINT_LN(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": %u,\n", P, Key, Val)
293
294
295 #define VTOY_JSON_FMT_STRUINT(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": \"%u\",", Key, Val)
296 #define VTOY_JSON_FMT_STRUINT_N(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": \"%u\",\n", Key, Val)
297 #define VTOY_JSON_FMT_STRUINT_L(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": \"%u\",", P, Key, Val)
298 #define VTOY_JSON_FMT_STRUINT_LN(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": \"%u\",\n", P, Key, Val)
299
300 #define VTOY_JSON_FMT_STRSINT(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": \"%d\",", Key, Val)
301 #define VTOY_JSON_FMT_STRSINT_N(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": \"%d\",\n", Key, Val)
302 #define VTOY_JSON_FMT_STRSINT_L(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": \"%d\",", P, Key, Val)
303 #define VTOY_JSON_FMT_STRSINT_LN(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": \"%d\",\n", P, Key, Val)
304
305 #define VTOY_JSON_FMT_CTRL_INT(Prefix, Key, Field) \
306 if (def->Field != data->Field) \
307 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s{ \"%s\": \"%d\" },\n", Prefix, Key, data->Field)
308
309 #define VTOY_JSON_FMT_CTRL_STRN(P, Key, Field) \
310 if (strcmp(def->Field, data->Field)) \
311 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s{ \"%s\": \"%s\" },\n", P, Key, data->Field)
312
313 #define VTOY_JSON_FMT_CTRL_STRN_STR(P, Key, ptr) \
314 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s{ \"%s\": \"%s\" },\n", P, Key, ptr)
315
316 #define VTOY_JSON_FMT_CTRL_PUB_STRN(P, Key, Field) \
317 if (strcmp(def->Field, g_pub_path)) \
318 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s{ \"%s\": \"%s\" },\n", P, Key, g_pub_path)
319
320
321 #define VTOY_JSON_FMT_DIFF_STRN(P, Key, Field) \
322 if (strcmp(def->Field, data->Field)) \
323 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": \"%s\",\n", P, Key, data->Field)
324
325
326 #define VTOY_JSON_FMT_STRINT64(Key, Val) \
327 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": \"%llu\",", Key, Val)
328
329 #define VTOY_JSON_FMT_SINT(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %d,", Key, Val)
330 #define VTOY_JSON_FMT_SINT_N(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %d,\n", Key, Val)
331 #define VTOY_JSON_FMT_SINT_L(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": %d,", P, Key, Val)
332 #define VTOY_JSON_FMT_SINT_LN(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": %d,\n", P, Key, Val)
333
334 #define VTOY_JSON_FMT_DUBL(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %.1lf,", Key, Val)
335 #define VTOY_JSON_FMT_DUBL2(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %10.02lf,", Key, Val)
336
337 #define VTOY_JSON_FMT_STRN(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": \"%s\",", Key, Val)
338 #define VTOY_JSON_FMT_STRN_N(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": \"%s\",\n", Key, Val)
339 #define VTOY_JSON_FMT_STRN_L(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": \"%s\",", P, Key, Val)
340 #define VTOY_JSON_FMT_STRN_LN(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": \"%s\",\n", P, Key, Val)
341 #define VTOY_JSON_FMT_STRN_PATH_LN(P, Key, Val) \
342 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": \"%s\",\n", P, Key, ventoy_real_path(Val))
343
344 int vtoy_json_escape_string(char *buf, int buflen, const char *str, int newline);
345
346 #define VTOY_JSON_FMT_STRN_EX_LN(P, Key, Val) \
347 {\
348 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": ", P, Key);\
349 __uiCurPos += vtoy_json_escape_string(__pcBuf + __uiCurPos, __uiBufLen - __uiCurPos, Val, 1);\
350 }
351
352
353 #define VTOY_JSON_FMT_NULL(Key) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": null,", Key)
354
355 #define VTOY_JSON_FMT_TRUE(Key) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": true,", (Key))
356 #define VTOY_JSON_FMT_FALSE(Key) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": false,", (Key))
357
358 #define VTOY_JSON_FMT_BOOL(Key, Val) \
359 {\
360 if (0 == (Val))\
361 {\
362 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": false,", (Key));\
363 }\
364 else \
365 {\
366 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": true,", (Key));\
367 }\
368 }
369
370 typedef struct tagVTOY_JSON_PARSE
371 {
372 char *pcKey;
373 void *pDataBuf;
374 uint32_t uiBufSize;
375 }VTOY_JSON_PARSE_S;
376
377 #define JSON_SUCCESS 0
378 #define JSON_FAILED 1
379 #define JSON_NOT_FOUND 2
380
381 int vtoy_json_parse_value
382 (
383 char *pcNewStart,
384 char *pcRawStart,
385 VTOY_JSON *pstJson,
386 const char *pcData,
387 const char **ppcEnd
388 );
389 VTOY_JSON * vtoy_json_create(void);
390 int vtoy_json_parse(VTOY_JSON *pstJson, const char *szJsonData);
391 int vtoy_json_parse_ex(VTOY_JSON *pstJson, const char *szJsonData, int szLen);
392 int vtoy_json_destroy(VTOY_JSON *pstJson);
393 VTOY_JSON *vtoy_json_find_item
394 (
395 VTOY_JSON *pstJson,
396 JSON_TYPE enDataType,
397 const char *szKey
398 );
399 int vtoy_json_scan_parse
400 (
401 const VTOY_JSON *pstJson,
402 uint32_t uiParseNum,
403 VTOY_JSON_PARSE_S *pstJsonParse
404 );
405 int vtoy_json_get_int
406 (
407 VTOY_JSON *pstJson,
408 const char *szKey,
409 int *piValue
410 );
411 int vtoy_json_get_uint
412 (
413 VTOY_JSON *pstJson,
414 const char *szKey,
415 uint32_t *puiValue
416 );
417 int vtoy_json_get_uint64
418 (
419 VTOY_JSON *pstJson,
420 const char *szKey,
421 uint64_t *pui64Value
422 );
423 int vtoy_json_get_bool
424 (
425 VTOY_JSON *pstJson,
426 const char *szKey,
427 uint8_t *pbValue
428 );
429 int vtoy_json_get_string
430 (
431 VTOY_JSON *pstJson,
432 const char *szKey,
433 uint32_t uiBufLen,
434 char *pcBuf
435 );
436 const char * vtoy_json_get_string_ex(VTOY_JSON *pstJson, const char *szKey);
437
438 #endif /* __VENTOY_JSON_H__ */
439