]>
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_INVALID_RSV_RET "{ \"result\" : \"reserve_invalid\" }"
62 #define VTOY_JSON_FILE_NOT_FOUND_RET "{ \"result\" : \"file_not_found\" }"
64 typedef enum tagJSON_TYPE
75 typedef struct tagVTOY_JSON
77 struct tagVTOY_JSON
*pstPrev
;
78 struct tagVTOY_JSON
*pstNext
;
79 struct tagVTOY_JSON
*pstChild
;
92 #define VTOY_JSON_FMT_BEGIN(uiCurPos, pcBuf, uiBufLen) \
94 uint32_t __uiCurPos = (uiCurPos);\
95 uint32_t __uiBufLen = (uiBufLen);\
96 char *__pcBuf = (pcBuf);
98 #define VTOY_JSON_FMT_END(uiCurPos) \
99 (uiCurPos) = __uiCurPos;\
102 #define VTOY_JSON_FMT_OBJ_BEGIN() ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "{")
104 #define VTOY_JSON_FMT_OBJ_END() \
106 if (',' == *(__pcBuf + (__uiCurPos - 1)))\
110 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "}");\
113 #define VTOY_JSON_FMT_OBJ_ENDEX() \
115 if (',' == *(__pcBuf + (__uiCurPos - 1)))\
119 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "},");\
122 #define VTOY_JSON_FMT_KEY(Key) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":", (Key))
124 #define VTOY_JSON_FMT_ITEM(Item) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\",", (Item))
126 #define VTOY_JSON_FMT_COMA() ssprintf(__uiCurPos, __pcBuf, __uiBufLen, ",");
128 #define VTOY_JSON_FMT_APPEND_BEGIN() \
130 if ('}' == *(__pcBuf + (__uiCurPos - 1)))\
134 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, ",");\
137 #define VTOY_JSON_FMT_APPEND_END() \
139 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "}");\
142 #define VTOY_JSON_FMT_ARY_BEGIN() ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "[")
144 #define VTOY_JSON_FMT_ARY_END() \
146 if (',' == *(__pcBuf + (__uiCurPos - 1)))\
150 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "]");\
153 #define VTOY_JSON_FMT_ARY_ENDEX() \
155 if (',' == *(__pcBuf + (__uiCurPos - 1)))\
159 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "],");\
162 #define VTOY_JSON_FMT_UINT64(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":%llu,", Key, (_ull)Val)
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)
167 #define VTOY_JSON_FMT_UINT(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":%u,", Key, Val)
169 #define VTOY_JSON_FMT_STRINT(Key, Val) \
170 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":\"%u\",", Key, Val)
172 #define VTOY_JSON_FMT_STRINT64(Key, Val) \
173 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":\"%llu\",", Key, Val)
175 #define VTOY_JSON_FMT_SINT(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":%d,", Key, Val)
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)
180 #define VTOY_JSON_FMT_STRN(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":\"%s\",", Key, Val)
182 #define VTOY_JSON_FMT_NULL(Key) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":null,", Key)
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))
187 #define VTOY_JSON_FMT_BOOL(Key, Val) \
191 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":false,", (Key));\
195 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":true,", (Key));\
199 typedef struct tagVTOY_JSON_PARSE
206 #define JSON_SUCCESS 0
207 #define JSON_FAILED 1
208 #define JSON_NOT_FOUND 2
210 int vtoy_json_parse_value
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
224 JSON_TYPE enDataType
,
227 int vtoy_json_scan_parse
229 const VTOY_JSON
*pstJson
,
231 VTOY_JSON_PARSE_S
*pstJsonParse
233 int vtoy_json_get_int
239 int vtoy_json_get_uint
245 int vtoy_json_get_uint64
251 int vtoy_json_get_bool
257 int vtoy_json_get_string
264 const char * vtoy_json_get_string_ex(VTOY_JSON
*pstJson
, const char *szKey
);
266 #endif /* __VENTOY_JSON_H__ */