]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - LinuxGUI/Ventoy2Disk/Core/ventoy_json.h
Fix Plugson Web update issue.
[Ventoy.git] / LinuxGUI / Ventoy2Disk / 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 #define ssprintf(curpos, buf, len, fmt, args...) \
34 curpos += snprintf(buf + curpos, len - curpos, fmt, ##args)
35
36 #define VTOY_JSON_IS_SKIPABLE(c) (((c) <= 32) ? 1 : 0)
37
38 #define VTOY_JSON_PRINT_PREFIX(uiDepth, args...) \
39 { \
40 uint32_t _uiLoop = 0; \
41 for (_uiLoop = 0; _uiLoop < (uiDepth); _uiLoop++) \
42 { \
43 ssprintf(uiCurPos, pcBuf, uiBufLen, " "); \
44 } \
45 ssprintf(uiCurPos, pcBuf, uiBufLen, ##args); \
46 }
47
48 #define VTOY_JSON_SUCCESS_RET "{ \"result\" : \"success\" }"
49 #define VTOY_JSON_FAILED_RET "{ \"result\" : \"failed\" }"
50 #define VTOY_JSON_INVALID_RET "{ \"result\" : \"invalidfmt\" }"
51 #define VTOY_JSON_TOKEN_ERR_RET "{ \"result\" : \"tokenerror\" }"
52 #define VTOY_JSON_EXIST_RET "{ \"result\" : \"exist\" }"
53 #define VTOY_JSON_TIMEOUT_RET "{ \"result\" : \"timeout\" }"
54 #define VTOY_JSON_BUSY_RET "{ \"result\" : \"busy\" }"
55 #define VTOY_JSON_INUSE_RET "{ \"result\" : \"inuse\" }"
56 #define VTOY_JSON_NOTFOUND_RET "{ \"result\" : \"notfound\" }"
57 #define VTOY_JSON_NOTRUNNING_RET "{ \"result\" : \"notrunning\" }"
58 #define VTOY_JSON_NOT_READY_RET "{ \"result\" : \"notready\" }"
59 #define VTOY_JSON_NOT_SUPPORT_RET "{ \"result\" : \"notsupport\" }"
60 #define VTOY_JSON_MBR_2TB_RET "{ \"result\" : \"mbr2tb\" }"
61 #define VTOY_JSON_INVALID_RSV_RET "{ \"result\" : \"reserve_invalid\" }"
62 #define VTOY_JSON_FILE_NOT_FOUND_RET "{ \"result\" : \"file_not_found\" }"
63
64 typedef enum tagJSON_TYPE
65 {
66 JSON_TYPE_NUMBER = 0,
67 JSON_TYPE_STRING,
68 JSON_TYPE_BOOL,
69 JSON_TYPE_ARRAY,
70 JSON_TYPE_OBJECT,
71 JSON_TYPE_NULL,
72 JSON_TYPE_BUTT
73 }JSON_TYPE;
74
75 typedef struct tagVTOY_JSON
76 {
77 struct tagVTOY_JSON *pstPrev;
78 struct tagVTOY_JSON *pstNext;
79 struct tagVTOY_JSON *pstChild;
80
81 JSON_TYPE enDataType;
82 union
83 {
84 char *pcStrVal;
85 int iNumVal;
86 uint64_t lValue;
87 }unData;
88
89 char *pcName;
90 }VTOY_JSON;
91
92 #define VTOY_JSON_FMT_BEGIN(uiCurPos, pcBuf, uiBufLen) \
93 {\
94 uint32_t __uiCurPos = (uiCurPos);\
95 uint32_t __uiBufLen = (uiBufLen);\
96 char *__pcBuf = (pcBuf);
97
98 #define VTOY_JSON_FMT_END(uiCurPos) \
99 (uiCurPos) = __uiCurPos;\
100 }
101
102 #define VTOY_JSON_FMT_OBJ_BEGIN() ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "{")
103
104 #define VTOY_JSON_FMT_OBJ_END() \
105 {\
106 if (',' == *(__pcBuf + (__uiCurPos - 1)))\
107 {\
108 __uiCurPos -= 1;\
109 }\
110 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "}");\
111 }
112
113 #define VTOY_JSON_FMT_OBJ_ENDEX() \
114 {\
115 if (',' == *(__pcBuf + (__uiCurPos - 1)))\
116 {\
117 __uiCurPos -= 1;\
118 }\
119 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "},");\
120 }
121
122 #define VTOY_JSON_FMT_KEY(Key) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":", (Key))
123
124 #define VTOY_JSON_FMT_ITEM(Item) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\",", (Item))
125
126 #define VTOY_JSON_FMT_COMA() ssprintf(__uiCurPos, __pcBuf, __uiBufLen, ",");
127
128 #define VTOY_JSON_FMT_APPEND_BEGIN() \
129 { \
130 if ('}' == *(__pcBuf + (__uiCurPos - 1)))\
131 {\
132 __uiCurPos -= 1;\
133 }\
134 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, ",");\
135 }
136
137 #define VTOY_JSON_FMT_APPEND_END() \
138 { \
139 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "}");\
140 }
141
142 #define VTOY_JSON_FMT_ARY_BEGIN() ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "[")
143
144 #define VTOY_JSON_FMT_ARY_END() \
145 {\
146 if (',' == *(__pcBuf + (__uiCurPos - 1)))\
147 {\
148 __uiCurPos -= 1;\
149 }\
150 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "]");\
151 }
152
153 #define VTOY_JSON_FMT_ARY_ENDEX() \
154 {\
155 if (',' == *(__pcBuf + (__uiCurPos - 1)))\
156 {\
157 __uiCurPos -= 1;\
158 }\
159 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "],");\
160 }
161
162 #define VTOY_JSON_FMT_UINT64(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":%llu,", Key, (_ull)Val)
163
164 #define VTOY_JSON_FMT_ULONG(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":%lu,", Key, Val)
165 #define VTOY_JSON_FMT_LONG(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":%ld,", Key, Val)
166
167 #define VTOY_JSON_FMT_UINT(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":%u,", Key, Val)
168
169 #define VTOY_JSON_FMT_STRINT(Key, Val) \
170 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":\"%u\",", Key, Val)
171
172 #define VTOY_JSON_FMT_STRINT64(Key, Val) \
173 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":\"%llu\",", Key, Val)
174
175 #define VTOY_JSON_FMT_SINT(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":%d,", Key, Val)
176
177 #define VTOY_JSON_FMT_DUBL(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":%.1lf,", Key, Val)
178 #define VTOY_JSON_FMT_DUBL2(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":%10.02lf,", Key, Val)
179
180 #define VTOY_JSON_FMT_STRN(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":\"%s\",", Key, Val)
181
182 #define VTOY_JSON_FMT_NULL(Key) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":null,", Key)
183
184 #define VTOY_JSON_FMT_TRUE(Key) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":true,", (Key))
185 #define VTOY_JSON_FMT_FALSE(Key) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":false,", (Key))
186
187 #define VTOY_JSON_FMT_BOOL(Key, Val) \
188 {\
189 if (0 == (Val))\
190 {\
191 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":false,", (Key));\
192 }\
193 else \
194 {\
195 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":true,", (Key));\
196 }\
197 }
198
199 typedef struct tagVTOY_JSON_PARSE
200 {
201 char *pcKey;
202 void *pDataBuf;
203 uint32_t uiBufSize;
204 }VTOY_JSON_PARSE_S;
205
206 #define JSON_SUCCESS 0
207 #define JSON_FAILED 1
208 #define JSON_NOT_FOUND 2
209
210 int vtoy_json_parse_value
211 (
212 char *pcNewStart,
213 char *pcRawStart,
214 VTOY_JSON *pstJson,
215 const char *pcData,
216 const char **ppcEnd
217 );
218 VTOY_JSON * vtoy_json_create(void);
219 int vtoy_json_parse(VTOY_JSON *pstJson, const char *szJsonData);
220 int vtoy_json_destroy(VTOY_JSON *pstJson);
221 VTOY_JSON *vtoy_json_find_item
222 (
223 VTOY_JSON *pstJson,
224 JSON_TYPE enDataType,
225 const char *szKey
226 );
227 int vtoy_json_scan_parse
228 (
229 const VTOY_JSON *pstJson,
230 uint32_t uiParseNum,
231 VTOY_JSON_PARSE_S *pstJsonParse
232 );
233 int vtoy_json_get_int
234 (
235 VTOY_JSON *pstJson,
236 const char *szKey,
237 int *piValue
238 );
239 int vtoy_json_get_uint
240 (
241 VTOY_JSON *pstJson,
242 const char *szKey,
243 uint32_t *puiValue
244 );
245 int vtoy_json_get_uint64
246 (
247 VTOY_JSON *pstJson,
248 const char *szKey,
249 uint64_t *pui64Value
250 );
251 int vtoy_json_get_bool
252 (
253 VTOY_JSON *pstJson,
254 const char *szKey,
255 uint8_t *pbValue
256 );
257 int vtoy_json_get_string
258 (
259 VTOY_JSON *pstJson,
260 const char *szKey,
261 uint32_t uiBufLen,
262 char *pcBuf
263 );
264 const char * vtoy_json_get_string_ex(VTOY_JSON *pstJson, const char *szKey);
265
266 #endif /* __VENTOY_JSON_H__ */
267