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