Debian Codegraph @

src/cdogs/yajl_utils.c

raw symbols SCIP
1 /*
2 Copyright (c) 2013-2017, 2025 Cong Xu
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10 Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 POSSIBILITY OF SUCH DAMAGE.
25 */
26 #include "yajl_utils.h"
27
28 #include "log.h"
29
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 static char *ReadFile(const char *filename);
34 yajl_val YAJLReadFile(const char *filename)
35 {
36 yajl_val node = NULL;
37 char errbuf[1024];
38 char *buf = ReadFile(filename);
39 if (buf == NULL)
40 {
41 fprintf(stderr, "Error reading JSON file '%s'\n", filename);
42 goto bail;
43 }
44 node = yajl_tree_parse(buf, errbuf, sizeof errbuf);
45 if (node == NULL)
46 {
47 fprintf(
48 stderr, "Error parsing font JSON '%s': %s\n", filename, errbuf);
49 goto bail;
50 }
51
52 bail:
53 free(buf);
54 return node;
55 }
56 // Read a file into a dynamic buffer
57 static char *ReadFile(const char *filename)
58 {
59 FILE *f = fopen(filename, "r");
60 char *buf = NULL;
61 if (f == NULL)
62 goto bail;
63 if (fseek(f, 0L, SEEK_END) != 0)
64 goto bail;
65 const long bufsize = ftell(f);
66 if (bufsize == -1)
67 goto bail;
68 if (fseek(f, 0L, SEEK_SET) != 0)
69 goto bail;
70 buf = malloc(bufsize + 1);
71 if (buf == NULL)
72 goto bail;
73 const size_t readlen = fread(buf, 1, bufsize, f);
74 buf[readlen] = '\0';
75
76 bail:
77 if (f)
78 {
79 fclose(f);
80 }
81 return buf;
82 }
83
84 #define YAJL_CHECK(func) \
85 { \
86 yajl_gen_status _status = func; \
87 if (_status != yajl_gen_status_ok) \
88 { \
89 return _status; \
90 } \
91 }
92
93 yajl_gen_status YAJLAddIntPair(yajl_gen g, const char *name, const int number)
94 {
95 YAJL_CHECK(yajl_gen_string(g, (const unsigned char *)name, strlen(name)));
96 YAJL_CHECK(yajl_gen_integer(g, number));
97 return yajl_gen_status_ok;
98 }
99 yajl_gen_status YAJLAddBoolPair(yajl_gen g, const char *name, const bool value)
100 {
101 YAJL_CHECK(yajl_gen_string(g, (const unsigned char *)name, strlen(name)));
102 YAJL_CHECK(yajl_gen_bool(g, value));
103 return yajl_gen_status_ok;
104 }
105 yajl_gen_status YAJLAddStringPair(yajl_gen g, const char *name, const char *s)
106 {
107 YAJL_CHECK(yajl_gen_string(g, (const unsigned char *)name, strlen(name)));
108 YAJL_CHECK(yajl_gen_string(
109 g, (const unsigned char *)(s ? s : ""), s ? strlen(s) : 0));
110 return yajl_gen_status_ok;
111 }
112 yajl_gen_status YAJLAddColorPair(yajl_gen g, const char *name, const color_t c)
113 {
114 char buf[COLOR_STR_BUF];
115 ColorStr(buf, c);
116 return YAJLAddStringPair(g, name, buf);
117 }
118
119 bool YAJLTryLoadValue(yajl_val *node, const char *name)
120 {
121 if (*node == NULL || !YAJL_IS_OBJECT(*node))
122 {
123 return false;
124 }
125 const char *path[] = {name, NULL};
126 *node = yajl_tree_get(*node, path, yajl_t_any);
127 return *node != NULL;
128 }
129 void YAJLBool(bool *value, yajl_val node, const char *name)
130 {
131 if (!YAJLTryLoadValue(&node, name))
132 {
133 return;
134 }
135 *value = YAJL_IS_TRUE(node);
136 }
137 void YAJLInt(int *value, yajl_val node, const char *name)
138 {
139 if (!YAJLTryLoadValue(&node, name) || !YAJL_IS_INTEGER(node))
140 {
141 return;
142 }
143 *value = (int)YAJL_GET_INTEGER(node);
144 }
145 void YAJLDouble(double *value, yajl_val node, const char *name)
146 {
147 if (!YAJLTryLoadValue(&node, name) || !YAJL_IS_DOUBLE(node))
148 {
149 return;
150 }
151 *value = YAJL_GET_DOUBLE(node);
152 }
153 void YAJLVec2i(struct vec2i *value, yajl_val node, const char *name)
154 {
155 if (!YAJLTryLoadValue(&node, name) || !YAJL_IS_ARRAY(node))
156 {
157 return;
158 }
159 *value = YAJL_GET_VEC2I(node);
160 }
161 void YAJLStr(char **value, yajl_val node, const char *name)
162 {
163 if (!YAJLTryLoadValue(&node, name) || !YAJL_IS_STRING(node))
164 {
165 return;
166 }
167 CSTRDUP(*value, node->u.string);
168 }
169 char *YAJLGetStr(yajl_val node, const char *name)
170 {
171 char *in = YAJL_GET_STRING(YAJLFindNode(node, name));
172 CASSERT(in != NULL, "cannot get JSON string");
173 char *out;
174 CSTRDUP(out, in);
175 return out;
176 }
177 /*
178 void LoadSoundFromNode(Mix_Chunk **value, json_t *node, const char *name)
179 {
180 if (json_find_first_label(node, name) == NULL)
181 {
182 return;
183 }
184 if (!TryLoadValue(&node, name))
185 {
186 return;
187 }
188 *value = StrSound(node->text);
189 }
190 void LoadPic(const Pic **value, json_t *node, const char *name)
191 {
192 if (json_find_first_label(node, name))
193 {
194 char *tmp = GetString(node, name);
195 *value = PicManagerGetPic(&gPicManager, tmp);
196 CFREE(tmp);
197 }
198 }
199 void LoadBulletGuns(CArray *guns, json_t *node, const char *name)
200 {
201 node = json_find_first_label(node, name);
202 if (node == NULL || node->child == NULL)
203 {
204 return;
205 }
206 CArrayInit(guns, sizeof(const WeaponClass *));
207 for (json_t *gun = node->child->child; gun; gun = gun->next)
208 {
209 const WeaponClass *wc = StrWeaponClass(gun->text);
210 CArrayPushBack(guns, &g);
211 }
212 }
213 */
214 void YAJLLoadColor(color_t *c, yajl_val node, const char *name)
215 {
216 if (!YAJLTryLoadValue(&node, name) || !YAJL_IS_STRING(node))
217 {
218 return;
219 }
220 *c = StrColor(node->u.string);
221 }
222 yajl_val YAJLFindNode(yajl_val node, const char *path)
223 {
224 // max 256 levels
225 const char *pathSplit[256];
226 for (int i = 0; i < 256; i++)
227 pathSplit[i] = NULL;
228 char *pathCopy;
229 CSTRDUP(pathCopy, path);
230 char *pch = strtok(pathCopy, "/");
231 int i = 0;
232 yajl_val out = NULL;
233 while (pch != NULL)
234 {
235 if (i == 256)
236 {
237 fprintf(stderr, "JSON path too long: '%s'\n", path);
238 goto bail;
239 }
240 pathSplit[i] = pch;
241 i++;
242 pch = strtok(NULL, "/");
243 }
244 out = yajl_tree_get(node, pathSplit, yajl_t_any);
245
246 bail:
247 CFREE(pathCopy);
248 return out;
249 }
250
251 bool YAJLTrySaveJSONFile(yajl_gen g, const char *filename)
252 {
253 const char *buf;
254 size_t len;
255 bool res = true;
256 yajl_gen_get_buf(g, (const unsigned char **)&buf, &len);
257 FILE *f = fopen(filename, "w");
258 if (f == NULL)
259 {
260 LOG(LM_MAIN, LL_ERROR, "Unable to save %s\n", filename);
261 res = false;
262 goto bail;
263 }
264 fwrite(buf, 1, len, f);
265
266 #ifdef __EMSCRIPTEN__
267 EM_ASM(
268 // persist changes
269 FS.syncfs(false, function(err) { assert(!err); }););
270 #endif
271
272 bail:
273 if (f != NULL)
274 {
275 fclose(f);
276 }
277 return res;
278 }