ventoy_json.h 7.66 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/******************************************************************************
 * ventoy_json.h
 *
 * Copyright (c) 2021, longpanda <admin@ventoy.net>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */
#ifndef __VENTOY_JSON_H__
#define __VENTOY_JSON_H__

#define JSON_NEW_ITEM(pstJson, ret) \
{ \
    (pstJson) = (VTOY_JSON *)zalloc(sizeof(VTOY_JSON)); \
    if (NULL == (pstJson)) \
    { \
        vdebug("Failed to alloc memory for json."); \
        return (ret); \
    } \
}

#define ssprintf(curpos, buf, len, fmt, args...) \
    curpos += snprintf(buf + curpos, len - curpos, fmt, ##args)

#define VTOY_JSON_IS_SKIPABLE(c) (((c) <= 32) ? 1 : 0)

#define VTOY_JSON_PRINT_PREFIX(uiDepth, args...) \
{ \
    uint32_t _uiLoop = 0; \
    for (_uiLoop = 0; _uiLoop < (uiDepth); _uiLoop++) \
    { \
        ssprintf(uiCurPos, pcBuf, uiBufLen, "    "); \
    } \
    ssprintf(uiCurPos, pcBuf, uiBufLen, ##args); \
}

#define VTOY_JSON_SUCCESS_RET      "{ \"result\" : \"success\" }"
#define VTOY_JSON_FAILED_RET       "{ \"result\" : \"failed\" }"
#define VTOY_JSON_INVALID_RET      "{ \"result\" : \"invalidfmt\" }"
#define VTOY_JSON_TOKEN_ERR_RET    "{ \"result\" : \"tokenerror\" }"
#define VTOY_JSON_EXIST_RET        "{ \"result\" : \"exist\" }"
#define VTOY_JSON_TIMEOUT_RET      "{ \"result\" : \"timeout\" }"
#define VTOY_JSON_BUSY_RET         "{ \"result\" : \"busy\" }"
#define VTOY_JSON_INUSE_RET        "{ \"result\" : \"inuse\" }"
#define VTOY_JSON_NOTFOUND_RET     "{ \"result\" : \"notfound\" }"
#define VTOY_JSON_NOTRUNNING_RET   "{ \"result\" : \"notrunning\" }"
#define VTOY_JSON_NOT_READY_RET    "{ \"result\" : \"notready\" }"
#define VTOY_JSON_NOT_SUPPORT_RET  "{ \"result\" : \"notsupport\" }"
#define VTOY_JSON_MBR_2TB_RET      "{ \"result\" : \"mbr2tb\" }"
61
#define VTOY_JSON_4KN_RET          "{ \"result\" : \"4kn\" }"
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#define VTOY_JSON_INVALID_RSV_RET      "{ \"result\" : \"reserve_invalid\" }"
#define VTOY_JSON_FILE_NOT_FOUND_RET     "{ \"result\" : \"file_not_found\" }"

typedef enum tagJSON_TYPE
{
    JSON_TYPE_NUMBER = 0,
    JSON_TYPE_STRING,
    JSON_TYPE_BOOL,
    JSON_TYPE_ARRAY,
    JSON_TYPE_OBJECT,
    JSON_TYPE_NULL,
    JSON_TYPE_BUTT
}JSON_TYPE;

typedef struct tagVTOY_JSON
{
    struct tagVTOY_JSON *pstPrev;
    struct tagVTOY_JSON *pstNext;
    struct tagVTOY_JSON *pstChild;

    JSON_TYPE enDataType;
    union 
    {
        char  *pcStrVal;
        int    iNumVal;
        uint64_t lValue;
    }unData;

    char *pcName;
}VTOY_JSON;

#define VTOY_JSON_FMT_BEGIN(uiCurPos, pcBuf, uiBufLen) \
{\
    uint32_t __uiCurPos = (uiCurPos);\
    uint32_t __uiBufLen = (uiBufLen);\
    char *__pcBuf = (pcBuf);
    
#define VTOY_JSON_FMT_END(uiCurPos) \
    (uiCurPos) = __uiCurPos;\
}
    
#define VTOY_JSON_FMT_OBJ_BEGIN()  ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "{")
    
#define VTOY_JSON_FMT_OBJ_END()  \
{\
    if (',' == *(__pcBuf + (__uiCurPos - 1)))\
    {\
        __uiCurPos -= 1;\
    }\
    ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "}");\
}

#define VTOY_JSON_FMT_OBJ_ENDEX()  \
{\
    if (',' == *(__pcBuf + (__uiCurPos - 1)))\
    {\
        __uiCurPos -= 1;\
    }\
    ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "},");\
}
    
#define VTOY_JSON_FMT_KEY(Key)  ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":", (Key))

#define VTOY_JSON_FMT_ITEM(Item)  ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\",", (Item))

#define VTOY_JSON_FMT_COMA()  ssprintf(__uiCurPos, __pcBuf, __uiBufLen, ",");

#define VTOY_JSON_FMT_APPEND_BEGIN() \
{ \
    if ('}' == *(__pcBuf + (__uiCurPos - 1)))\
    {\
        __uiCurPos -= 1;\
    }\
    ssprintf(__uiCurPos, __pcBuf, __uiBufLen, ",");\
}

#define VTOY_JSON_FMT_APPEND_END() \
{ \
    ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "}");\
}

#define VTOY_JSON_FMT_ARY_BEGIN()  ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "[")
    
#define VTOY_JSON_FMT_ARY_END()    \
{\
    if (',' == *(__pcBuf + (__uiCurPos - 1)))\
    {\
        __uiCurPos -= 1;\
    }\
    ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "]");\
}

#define VTOY_JSON_FMT_ARY_ENDEX()    \
{\
    if (',' == *(__pcBuf + (__uiCurPos - 1)))\
    {\
        __uiCurPos -= 1;\
    }\
    ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "],");\
}
    
#define VTOY_JSON_FMT_UINT64(Key, Val)  ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":%llu,", Key, (_ull)Val)

#define VTOY_JSON_FMT_ULONG(Key, Val)  ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":%lu,", Key, Val)
#define VTOY_JSON_FMT_LONG(Key, Val)  ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":%ld,", Key, Val)

#define VTOY_JSON_FMT_UINT(Key, Val)  ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":%u,", Key, Val)

#define VTOY_JSON_FMT_STRINT(Key, Val)  \
    ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":\"%u\",", Key, Val)

#define VTOY_JSON_FMT_STRINT64(Key, Val)  \
    ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":\"%llu\",", Key, Val)

#define VTOY_JSON_FMT_SINT(Key, Val)  ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":%d,", Key, Val)
    
#define VTOY_JSON_FMT_DUBL(Key, Val)  ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":%.1lf,", Key, Val)
#define VTOY_JSON_FMT_DUBL2(Key, Val)  ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":%10.02lf,", Key, Val)

#define VTOY_JSON_FMT_STRN(Key, Val)  ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":\"%s\",", Key, Val) 
    
#define VTOY_JSON_FMT_NULL(Key)       ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":null,", Key)

#define VTOY_JSON_FMT_TRUE(Key) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":true,", (Key))
#define VTOY_JSON_FMT_FALSE(Key) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":false,", (Key))

#define VTOY_JSON_FMT_BOOL(Key, Val)  \
{\
    if (0 == (Val))\
    {\
        ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":false,", (Key));\
    }\
    else \
    {\
        ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":true,", (Key));\
    }\
}

typedef struct tagVTOY_JSON_PARSE
{
    char *pcKey;
    void *pDataBuf;
    uint32_t  uiBufSize;
}VTOY_JSON_PARSE_S;

#define JSON_SUCCESS    0
#define JSON_FAILED     1
#define JSON_NOT_FOUND  2

int vtoy_json_parse_value
(
    char *pcNewStart,
    char *pcRawStart,
    VTOY_JSON *pstJson, 
    const char *pcData,
    const char **ppcEnd
);
VTOY_JSON * vtoy_json_create(void);
int vtoy_json_parse(VTOY_JSON *pstJson, const char *szJsonData);
int vtoy_json_destroy(VTOY_JSON *pstJson);
VTOY_JSON *vtoy_json_find_item
(
    VTOY_JSON *pstJson,
    JSON_TYPE  enDataType,
    const char *szKey
);
int vtoy_json_scan_parse
(
    const VTOY_JSON    *pstJson,
    uint32_t            uiParseNum,
    VTOY_JSON_PARSE_S  *pstJsonParse
);
int vtoy_json_get_int
(
    VTOY_JSON *pstJson, 
    const char *szKey, 
    int *piValue
);
int vtoy_json_get_uint
(
    VTOY_JSON *pstJson, 
    const char *szKey, 
    uint32_t *puiValue
);
int vtoy_json_get_uint64
(
    VTOY_JSON *pstJson, 
    const char *szKey, 
    uint64_t *pui64Value
);
int vtoy_json_get_bool
(
    VTOY_JSON *pstJson,
    const char *szKey, 
    uint8_t *pbValue
);
int vtoy_json_get_string
(
     VTOY_JSON *pstJson, 
     const char *szKey, 
     uint32_t  uiBufLen,
     char *pcBuf
);
const char * vtoy_json_get_string_ex(VTOY_JSON *pstJson,  const char *szKey);

#endif /* __VENTOY_JSON_H__ */