]>
glassweightruler.freedombox.rocks Git - Ventoy.git/blob - LinuxGUI/Ventoy2Disk/Core/ventoy_json.h
1 /******************************************************************************
4 * Copyright (c) 2021, longpanda <admin@ventoy.net>
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.
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.
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/>.
20 #ifndef __VENTOY_JSON_H__
21 #define __VENTOY_JSON_H__
23 #define JSON_NEW_ITEM(pstJson, ret) \
25 (pstJson) = (VTOY_JSON *)zalloc(sizeof(VTOY_JSON)); \
26 if (NULL == (pstJson)) \
28 vdebug("Failed to alloc memory for json."); \
33 #define ssprintf(curpos, buf, len, fmt, args...) \
34 curpos += snprintf(buf + curpos, len - curpos, fmt, ##args)
36 #define VTOY_JSON_IS_SKIPABLE(c) (((c) <= 32) ? 1 : 0)
38 #define VTOY_JSON_PRINT_PREFIX(uiDepth, args...) \
40 uint32_t _uiLoop = 0; \
41 for (_uiLoop = 0; _uiLoop < (uiDepth); _uiLoop++) \
43 ssprintf(uiCurPos, pcBuf, uiBufLen, " "); \
45 ssprintf(uiCurPos, pcBuf, uiBufLen, ##args); \
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\" }"
65 typedef enum tagJSON_TYPE
76 typedef struct tagVTOY_JSON
78 struct tagVTOY_JSON
*pstPrev
;
79 struct tagVTOY_JSON
*pstNext
;
80 struct tagVTOY_JSON
*pstChild
;
93 #define VTOY_JSON_FMT_BEGIN(uiCurPos, pcBuf, uiBufLen) \
95 uint32_t __uiCurPos = (uiCurPos);\
96 uint32_t __uiBufLen = (uiBufLen);\
97 char *__pcBuf = (pcBuf);
99 #define VTOY_JSON_FMT_END(uiCurPos) \
100 (uiCurPos) = __uiCurPos;\
103 #define VTOY_JSON_FMT_OBJ_BEGIN() ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "{")
105 #define VTOY_JSON_FMT_OBJ_END() \
107 if (',' == *(__pcBuf + (__uiCurPos - 1)))\
111 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "}");\
114 #define VTOY_JSON_FMT_OBJ_ENDEX() \
116 if (',' == *(__pcBuf + (__uiCurPos - 1)))\
120 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "},");\
123 #define VTOY_JSON_FMT_KEY(Key) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":", (Key))
125 #define VTOY_JSON_FMT_ITEM(Item) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\",", (Item))
127 #define VTOY_JSON_FMT_COMA() ssprintf(__uiCurPos, __pcBuf, __uiBufLen, ",");
129 #define VTOY_JSON_FMT_APPEND_BEGIN() \
131 if ('}' == *(__pcBuf + (__uiCurPos - 1)))\
135 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, ",");\
138 #define VTOY_JSON_FMT_APPEND_END() \
140 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "}");\
143 #define VTOY_JSON_FMT_ARY_BEGIN() ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "[")
145 #define VTOY_JSON_FMT_ARY_END() \
147 if (',' == *(__pcBuf + (__uiCurPos - 1)))\
151 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "]");\
154 #define VTOY_JSON_FMT_ARY_ENDEX() \
156 if (',' == *(__pcBuf + (__uiCurPos - 1)))\
160 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "],");\
163 #define VTOY_JSON_FMT_UINT64(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":%llu,", Key, (_ull)Val)
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)
168 #define VTOY_JSON_FMT_UINT(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":%u,", Key, Val)
170 #define VTOY_JSON_FMT_STRINT(Key, Val) \
171 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":\"%u\",", Key, Val)
173 #define VTOY_JSON_FMT_STRINT64(Key, Val) \
174 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":\"%llu\",", Key, Val)
176 #define VTOY_JSON_FMT_SINT(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":%d,", Key, Val)
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)
181 #define VTOY_JSON_FMT_STRN(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":\"%s\",", Key, Val)
183 #define VTOY_JSON_FMT_NULL(Key) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":null,", Key)
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))
188 #define VTOY_JSON_FMT_BOOL(Key, Val) \
192 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":false,", (Key));\
196 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":true,", (Key));\
200 typedef struct tagVTOY_JSON_PARSE
207 #define JSON_SUCCESS 0
208 #define JSON_FAILED 1
209 #define JSON_NOT_FOUND 2
211 int vtoy_json_parse_value
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
225 JSON_TYPE enDataType
,
228 int vtoy_json_scan_parse
230 const VTOY_JSON
*pstJson
,
232 VTOY_JSON_PARSE_S
*pstJsonParse
234 int vtoy_json_get_int
240 int vtoy_json_get_uint
246 int vtoy_json_get_uint64
252 int vtoy_json_get_bool
258 int vtoy_json_get_string
265 const char * vtoy_json_get_string_ex(VTOY_JSON
*pstJson
, const char *szKey
);
267 #endif /* __VENTOY_JSON_H__ */