Debian Codegraph @

src/cdogs/json_utils.h

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 #pragma once
30
31 #include <stdbool.h>
32
33 #include <json/json.h>
34
35 #include "pic.h"
36 #include "sounds.h"
37 #include "vector.h"
38
39 void AddIntPair(json_t *parent, const char *name, int number);
40 void AddBoolPair(json_t *parent, const char *name, int value);
41 void AddStringPair(json_t *parent, const char *name, const char *s);
42 void AddColorPair(json_t *parent, const char *name, const color_t c);
43 void AddVec2iPair(json_t *parent, const char *name, const struct vec2i v);
44 void AddRect2iPair(json_t *parent, const char *name, const Rect2i r);
45 void AddIntArray(json_t *parent, const char *name, const CArray *a);
46
47 void LoadBool(bool *value, json_t *node, const char *name);
48 void LoadInt(int *value, json_t *node, const char *name);
49 void LoadDouble(double *value, json_t *node, const char *name);
50 void LoadFloat(float *value, json_t *node, const char *name);
51 void LoadFullInt(float *value, json_t *node, const char *name);
52 void LoadVec2i(struct vec2i *value, json_t *node, const char *name);
53 void LoadVec2(struct vec2 *value, json_t *node, const char *name);
54 void LoadRect2i(Rect2i *value, json_t *node, const char *name);
55 void LoadIntArray(CArray *a, const json_t *node, const char *name);
56
57 // remember to free
58 void LoadStr(char **value, json_t *node, const char *name);
59 char *GetString(const json_t *node, const char *name);
60
61 void LoadSoundFromNode(Mix_Chunk **value, json_t *node, const char *name);
62 // Load a const Pic * based on a name
63 void LoadPic(const Pic **value, json_t *node, const char *name);
64 // Load an array of const WeaponClass *
65 void LoadBulletGuns(CArray *guns, json_t *node, const char *name);
66 void LoadColor(color_t *c, json_t *node, const char *name);
67
68 // Try to load a JSON node using a slash-delimited "path"
69 // If at any point the path fails, NULL is returned.
70 json_t *JSONFindNode(json_t *node, const char *path);
71
72 #define JSON_UTILS_ADD_ENUM_PAIR(parent, name, value, func) \
73 json_insert_pair_into_object( \
74 (parent), (name), json_new_string(func(value)));
75
76 bool TryLoadValue(json_t **node, const char *name);
77 #define JSON_UTILS_LOAD_ENUM(value, node, name, func) \
78 { \
79 json_t *_node = (node); \
80 if (TryLoadValue(&_node, (name))) \
81 { \
82 (value) = func(_node->text); \
83 } \
84 }
85
86 bool TrySaveJSONFile(json_t *node, const char *filename);