Debian Codegraph @

src/cdogs/json_utils.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4
5 Copyright (c) 2013-2021 Cong Xu
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11 Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
13 Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
28 */
29 #include "json_utils.h"
30
31 #include <errno.h>
32 #include <stdlib.h>
33
34 #include "config.h"
35 #include "log.h"
36 #include "pic_manager.h"
37 #include "sys_config.h"
38 #include "weapon.h"
39
40 void AddIntPair(json_t *parent, const char *name, int number)
41 {
42 char buf[32];
43 sprintf(buf, "%d", number);
44 json_insert_pair_into_object(parent, name, json_new_number(buf));
45 }
46 void AddBoolPair(json_t *parent, const char *name, int value)
47 {
48 json_insert_pair_into_object(parent, name, json_new_bool(value));
49 }
50 void AddStringPair(json_t *parent, const char *name, const char *s)
51 {
52 if (!s)
53 {
54 json_insert_pair_into_object(parent, name, json_new_string(""));
55 }
56 else
57 {
58 json_insert_pair_into_object(
59 parent, name, json_new_string(json_escape(s)));
60 }
61 }
62 void AddColorPair(json_t *parent, const char *name, const color_t c)
63 {
64 char buf[COLOR_STR_BUF];
65 ColorStr(buf, c);
66 AddStringPair(parent, name, buf);
67 }
68 void AddVec2iPair(json_t *parent, const char *name, const struct vec2i v)
69 {
70 json_t *node = json_new_array();
71 char buf[32];
72 sprintf(buf, "%d", v.x);
73 json_insert_child(node, json_new_number(buf));
74 sprintf(buf, "%d", v.y);
75 json_insert_child(node, json_new_number(buf));
76 json_insert_pair_into_object(parent, name, node);
77 }
78 void AddRect2iPair(json_t *parent, const char *name, const Rect2i r)
79 {
80 json_t *node = json_new_array();
81 char buf[32];
82 sprintf(buf, "%d", r.Pos.x);
83 json_insert_child(node, json_new_number(buf));
84 sprintf(buf, "%d", r.Pos.y);
85 json_insert_child(node, json_new_number(buf));
86 sprintf(buf, "%d", r.Size.x);
87 json_insert_child(node, json_new_number(buf));
88 sprintf(buf, "%d", r.Size.y);
89 json_insert_child(node, json_new_number(buf));
90 json_insert_pair_into_object(parent, name, node);
91 }
92 void AddIntArray(json_t *parent, const char *name, const CArray *a)
93 {
94 json_t *node = json_new_array();
95 CA_FOREACH(int, i, *a)
96 char buf[32];
97 sprintf(buf, "%d", *i);
98 json_insert_child(node, json_new_number(buf));
99 CA_FOREACH_END()
100 json_insert_pair_into_object(parent, name, node);
101 }
102
103 bool TryLoadValue(json_t **node, const char *name)
104 {
105 if (*node == NULL || (*node)->type != JSON_OBJECT)
106 {
107 return 0;
108 }
109 *node = json_find_first_label(*node, name);
110 if (*node == NULL)
111 {
112 return 0;
113 }
114 *node = (*node)->child;
115 if (*node == NULL)
116 {
117 return 0;
118 }
119 return 1;
120 }
121 void LoadBool(bool *value, json_t *node, const char *name)
122 {
123 if (!TryLoadValue(&node, name))
124 {
125 return;
126 }
127 *value = node->type == JSON_TRUE;
128 }
129 void LoadInt(int *value, json_t *node, const char *name)
130 {
131 if (!TryLoadValue(&node, name))
132 {
133 return;
134 }
135 *value = atoi(node->text);
136 }
137 void LoadDouble(double *value, json_t *node, const char *name)
138 {
139 if (!TryLoadValue(&node, name))
140 {
141 return;
142 }
143 *value = atof(node->text);
144 }
145 void LoadFloat(float *value, json_t *node, const char *name)
146 {
147 if (!TryLoadValue(&node, name))
148 {
149 return;
150 }
151 *value = strtof(node->text, NULL);
152 }
153 void LoadFullInt(float *value, json_t *node, const char *name)
154 {
155 if (!TryLoadValue(&node, name))
156 {
157 return;
158 }
159 const int fullValue = atoi(node->text);
160 *value = fullValue / 256.0f;
161 }
162 void LoadVec2i(struct vec2i *value, json_t *node, const char *name)
163 {
164 if (!TryLoadValue(&node, name))
165 {
166 return;
167 }
168 node = node->child;
169 value->x = atoi(node->text);
170 node = node->next;
171 value->y = atoi(node->text);
172 }
173 void LoadVec2(struct vec2 *value, json_t *node, const char *name)
174 {
175 if (!TryLoadValue(&node, name))
176 {
177 return;
178 }
179 node = node->child;
180 value->x = strtof(node->text, NULL);
181 node = node->next;
182 value->y = strtof(node->text, NULL);
183 }
184 void LoadRect2i(Rect2i *value, json_t *node, const char *name)
185 {
186 if (!TryLoadValue(&node, name))
187 {
188 return;
189 }
190 node = node->child;
191 value->Pos.x = atoi(node->text);
192 node = node->next;
193 value->Pos.y = atoi(node->text);
194 node = node->next;
195 value->Size.x = atoi(node->text);
196 node = node->next;
197 value->Size.y = atoi(node->text);
198 }
199 void LoadIntArray(CArray *a, const json_t *node, const char *name)
200 {
201 const json_t *child = json_find_first_label(node, name);
202 if (!child || !child->child)
203 {
204 return;
205 }
206 child = child->child;
207 for (child = child->child; child; child = child->next)
208 {
209 const int n = atoi(child->text);
210 CArrayPushBack(a, &n);
211 }
212 }
213
214 void LoadStr(char **value, json_t *node, const char *name)
215 {
216 if (!TryLoadValue(&node, name))
217 {
218 return;
219 }
220 *value = json_unescape(node->text);
221 }
222 char *GetString(const json_t *node, const char *name)
223 {
224 return json_unescape(json_find_first_label(node, name)->child->text);
225 }
226 void LoadSoundFromNode(Mix_Chunk **value, json_t *node, const char *name)
227 {
228 if (json_find_first_label(node, name) == NULL)
229 {
230 return;
231 }
232 if (!TryLoadValue(&node, name))
233 {
234 return;
235 }
236 *value = StrSound(node->text);
237 }
238 void LoadPic(const Pic **value, json_t *node, const char *name)
239 {
240 if (json_find_first_label(node, name))
241 {
242 char *tmp = GetString(node, name);
243 *value = PicManagerGetPic(&gPicManager, tmp);
244 CFREE(tmp);
245 }
246 }
247 void LoadBulletGuns(CArray *guns, json_t *node, const char *name)
248 {
249 node = json_find_first_label(node, name);
250 if (node == NULL || node->child == NULL)
251 {
252 return;
253 }
254 CArrayInit(guns, sizeof(const WeaponClass *));
255 for (json_t *gun = node->child->child; gun; gun = gun->next)
256 {
257 const WeaponClass *wc = StrWeaponClass(gun->text);
258 if (wc == NULL)
259 {
260 CASSERT(false, "cannot find weapon class!");
261 }
262 else
263 {
264 CArrayPushBack(guns, &wc);
265 }
266 }
267 }
268 void LoadColor(color_t *c, json_t *node, const char *name)
269 {
270 if (json_find_first_label(node, name) == NULL)
271 {
272 return;
273 }
274 if (!TryLoadValue(&node, name))
275 {
276 return;
277 }
278 *c = StrColor(node->text);
279 }
280
281 json_t *JSONFindNode(json_t *node, const char *path)
282 {
283 char *pathCopy;
284 CSTRDUP(pathCopy, path);
285 char *pch = strtok(pathCopy, "/");
286 while (pch != NULL)
287 {
288 node = json_find_first_label(node, pch);
289 if (node == NULL)
290 {
291 goto bail;
292 }
293 node = node->child;
294 if (node == NULL)
295 {
296 goto bail;
297 }
298 pch = strtok(NULL, "/");
299 }
300 bail:
301 CFREE(pathCopy);
302 return node;
303 }
304
305 bool TrySaveJSONFile(json_t *node, const char *filename)
306 {
307 bool res = true;
308 char *text;
309 json_tree_to_string(node, &text);
310 char *ftext = json_format_string(text);
311 FILE *f = fopen(filename, "w");
312 if (f == NULL)
313 {
314 LOG(LM_MAIN, LL_ERROR, "failed to open file(%s) for saving: %s",
315 filename, strerror(errno));
316 res = false;
317 goto bail;
318 }
319 size_t writeLen = strlen(ftext);
320 const size_t rc = fwrite(ftext, 1, writeLen, f);
321 if (rc != writeLen)
322 {
323 LOG(LM_MAIN, LL_ERROR, "Wrote (%d) of (%d) bytes: %s", (int)rc,
324 (int)writeLen, strerror(errno));
325 res = false;
326 goto bail;
327 }
328
329 bail:
330 CFREE(text);
331 CFREE(ftext);
332 if (f != NULL)
333 fclose(f);
334 return res;
335 }