gwenhywfar 5.14.1
json.c
Go to the documentation of this file.
1/***************************************************************************
2 copyright : (C) 2023 by Martin Preuss
3 email : martin@libchipcard.de
4
5 ***************************************************************************
6 * *
7 * This library is free software; you can redistribute it and/or *
8 * modify it under the terms of the GNU Lesser General Public *
9 * License as published by the Free Software Foundation; either *
10 * version 2.1 of the License, or (at your option) any later version. *
11 * *
12 * This library is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15 * Lesser General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with this library; if not, write to the Free Software *
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
20 * MA 02111-1307 USA *
21 * *
22 ***************************************************************************/
23
24#ifdef HAVE_CONFIG_H
25# include <config.h>
26#endif
27
28#define DISABLE_DEBUGLOG
29
30
31#include "./json_p.h"
32#include <gwenhywfar/misc.h>
33#include <gwenhywfar/debug.h>
34#include <gwenhywfar/text.h>
35#include <gwenhywfar/path.h>
36
37#include <ctype.h>
38
39
40
42
43
44
45static GWEN_JSON_ELEM *_findByTypeAndData(GWEN_JSON_ELEM *je, int t, const char *s);
46static void *_handlePath(const char *entry, void *data, int idx, uint32_t flags);
47
48
49
50GWEN_JSON_ELEM *GWEN_JsonElement_new(int t, const char *sData)
51{
53
55 GWEN_TREE2_INIT(GWEN_JSON_ELEM, je, GWEN_JsonElement);
57 GWEN_JsonElement_SetData(je, sData);
58 return je;
59}
60
61
62
64{
65 if (je) {
66 GWEN_TREE2_FINI(GWEN_JSON_ELEM, je, GWEN_JsonElement);
67 if (je->elementData)
68 free(je->elementData);
70 }
71}
72
73
74
76{
77 if (je)
78 return je->elementType;
80}
81
82
83
85{
86 if (je)
87 je->elementType=t;
88}
89
90
91
93{
94 if (je)
95 return je->elementData;
96 return NULL;
97}
98
99
100
102{
103 if (je) {
104 free(je->elementData);
105 je->elementData=(s&&*s)?strdup(s):NULL;
106 }
107}
108
109
110
111
113{
114 GWEN_JSON_ELEM *jeChild;
115
116 jeChild=GWEN_JsonElement_Tree2_GetFirstChild(je);
117 return _findByTypeAndData(jeChild, t, s);
118}
119
120
121
123{
124 GWEN_JSON_ELEM *jeNext;
125
126 jeNext=GWEN_JsonElement_Tree2_GetNext(je);
127 return _findByTypeAndData(jeNext, t, s);
128}
129
130
131
132GWEN_JSON_ELEM *GWEN_JsonElement_FindByIdxTypeAndData(const GWEN_JSON_ELEM *jeSearch, int t, const char *s, int idx)
133{
134 GWEN_JSON_ELEM *je;
135
137 while(je && idx) {
139 idx--;
140 }
141 return (je && idx==0)?je:NULL;
142}
143
144
145
146
148{
149 return (GWEN_JSON_ELEM *)GWEN_Path_HandleWithIdx(path, je, flags, _handlePath);
150}
151
152
153
155{
156 /*DBG_ERROR(NULL, "Searching for \"%s\" [%d]", s, t);*/
157 while(je) {
158 int currentType;
159
160 currentType=GWEN_JsonElement_GetType(je);
161 if (t==0 || (currentType==t)) {
162 if (s && *s) {
163 const char *s2;
164
166 /*DBG_ERROR(NULL, " Comparing \"%s\" [%d] against \"%s\" [%d]", s2?s2:"<empty>", currentType, s?s:"<empty>", t);*/
167 if (s2 && strcasecmp(s, s2)==0)
168 return je;
169 }
170 else
171 return je;
172 }
173 else {
174 if (t==GWEN_JSON_ELEMTYPE_KEY && (currentType==GWEN_JSON_ELEMTYPE_ARRAY || currentType==GWEN_JSON_ELEMTYPE_OBJECT)) {
175 GWEN_JSON_ELEM *je2;
176
177 /*DBG_ERROR(NULL, "Searching below [%d]", currentType);*/
178 je2=GWEN_JsonElement_Tree2_GetFirstChild(je);
179 if (je2) {
180 je2=_findByTypeAndData(je2, t, s);
181 if (je2)
182 return je2;
183 }
184 }
185 }
186 je=GWEN_JsonElement_Tree2_GetNext(je);
187 }
188 return NULL;
189}
190
191
192
193void *_handlePath(const char *entry, void *data, int idx, GWEN_UNUSED uint32_t flags)
194{
195 GWEN_JSON_ELEM *jeParent;
196 GWEN_JSON_ELEM *je;
197
198 jeParent=(GWEN_JSON_ELEM*) data;
199
201 if (je==NULL) {
202 /* does not exist */
203 return NULL;
204 }
205 je=GWEN_JsonElement_Tree2_GetFirstChild(je);
206 return je;
207}
208
209
210
211
212
#define NULL
Definition binreloc.c:300
#define GWEN_UNUSED
void GWEN_JsonElement_SetType(GWEN_JSON_ELEM *je, int t)
Definition json.c:84
GWEN_JSON_ELEM * GWEN_JsonElement_FindNextByTypeAndData(const GWEN_JSON_ELEM *je, int t, const char *s)
Definition json.c:122
const char * GWEN_JsonElement_GetData(const GWEN_JSON_ELEM *je)
Definition json.c:92
GWEN_JSON_ELEM * GWEN_JsonElement_FindByIdxTypeAndData(const GWEN_JSON_ELEM *jeSearch, int t, const char *s, int idx)
Definition json.c:132
static void * _handlePath(const char *entry, void *data, int idx, uint32_t flags)
void GWEN_JsonElement_SetData(GWEN_JSON_ELEM *je, const char *s)
Definition json.c:101
GWEN_JSON_ELEM * GWEN_JsonElement_FindFirstByTypeAndData(const GWEN_JSON_ELEM *je, int t, const char *s)
Definition json.c:112
GWEN_JSON_ELEM * GWEN_JsonElement_GetElementByPath(GWEN_JSON_ELEM *je, const char *path, uint32_t flags)
Definition json.c:147
GWEN_JSON_ELEM * GWEN_JsonElement_new(int t, const char *sData)
Definition json.c:50
static GWEN_JSON_ELEM * _findByTypeAndData(GWEN_JSON_ELEM *je, int t, const char *s)
Definition json.c:154
int GWEN_JsonElement_GetType(const GWEN_JSON_ELEM *je)
Definition json.c:75
void GWEN_JsonElement_free(GWEN_JSON_ELEM *je)
Definition json.c:63
@ GWEN_JSON_ELEMTYPE_OBJECT
Definition json.h:45
@ GWEN_JSON_ELEMTYPE_KEY
Definition json.h:39
@ GWEN_JSON_ELEMTYPE_ARRAY
Definition json.h:44
@ GWEN_JSON_ELEMTYPE_UNKNOWN
Definition json.h:38
struct GWEN_JSON_ELEM GWEN_JSON_ELEM
Definition json.h:33
#define GWEN_FREE_OBJECT(varname)
Definition memory.h:61
#define GWEN_NEW_OBJECT(typ, varname)
Definition memory.h:55
void * GWEN_Path_HandleWithIdx(const char *path, void *data, uint32_t flags, GWEN_PATHIDXHANDLERPTR elementFunction)
Definition path.c:210
#define GWEN_TREE2_FUNCTIONS(t, pr)
Definition tree2.h:354
#define GWEN_TREE2_FINI(t, element, pr)
Definition tree2.h:457
#define GWEN_TREE2_INIT(t, element, pr)
Definition tree2.h:445