Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
json.qh File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

bool _json_parse_array ()
bool _json_parse_false ()
bool _json_parse_float ()
bool _json_parse_int ()
bool _json_parse_members ()
bool _json_parse_null ()
bool _json_parse_number ()
bool _json_parse_object ()
 Parse a json object.
bool _json_parse_pair ()
bool _json_parse_string (bool add)
bool _json_parse_true ()
bool _json_parse_value ()

Function Documentation

◆ _json_parse_array()

bool _json_parse_array ( )

Definition at line 71 of file json.qc.

72{
73 JSON_BEGIN();
74 if (STRING_ITERATOR_GET(_json) != '[')
75 JSON_FAIL("expected '['");
76 int len = bufstr_add(_json_buffer, "0", 0);
77 if (len)
78 bufstr_set(_json_buffer, len - 1, strcat(bufstr_get(_json_buffer, len - 1), ".length"));
79 bool required = false;
80 for (int n = 0; ; ++n)
81 {
82 string key = ftos(n);
83 if (_json_ns)
84 key = strcat(_json_ns, ".", key);
85 int it = bufstr_add(_json_buffer, key, 0);
86 bool ret = false;
87 WITH(string, _json_ns, key, ret = _json_parse_value());
88 if (!ret)
89 {
90 bufstr_free(_json_buffer, it);
91 if (required)
92 JSON_FAIL("expected value");
93 else
94 break;
95 }
96 bufstr_set(_json_buffer, len, ftos(n + 1));
97 if (STRING_ITERATOR_PEEK(_json) == ',')
98 {
100 required = true;
101 continue;
102 }
103 break;
104 }
105 if (STRING_ITERATOR_GET(_json) != ']')
106 JSON_FAIL("expected ']'");
107 JSON_END();
108}
#define STRING_ITERATOR_NEXT(this)
Definition iter.qh:56
#define STRING_ITERATOR_GET(this)
Definition iter.qh:54
#define STRING_ITERATOR_PEEK(this)
Definition iter.qh:55
#define JSON_FAIL(reason)
Definition json.qc:13
#define JSON_BEGIN()
Definition json.qc:12
int _json_buffer
Store interleaved keys/values in a string buffer.
Definition json.qc:7
#define JSON_END()
Definition json.qc:14
string _json_ns
Current namespace.
Definition json.qc:9
ERASEABLE bool _json_parse_value()
Definition json.qc:111
string ftos(float f)
#define WITH(type, name, value, block)
Definition misc.qh:37
strcat(_("^F4Countdown stopped!"), "\n^BG", _("Teams are too unbalanced."))

References _json_buffer, _json_ns, _json_parse_value(), ftos(), JSON_BEGIN, JSON_END, JSON_FAIL, strcat(), STRING_ITERATOR_GET, STRING_ITERATOR_NEXT, STRING_ITERATOR_PEEK, and WITH.

Referenced by _json_parse_value(), and PlayerStats_PlayerBasic_Handler().

◆ _json_parse_false()

bool _json_parse_false ( )

Definition at line 139 of file json.qc.

140 {
141 JSON_BEGIN();
142 if (!(STRING_ITERATOR_GET(_json) == 'f'
143 && STRING_ITERATOR_GET(_json) == 'a'
144 && STRING_ITERATOR_GET(_json) == 'l'
145 && STRING_ITERATOR_GET(_json) == 's'
146 && STRING_ITERATOR_GET(_json) == 'e'))
147 JSON_FAIL("expected 'false'");
148 bufstr_add(_json_buffer, "0", 0);
149 JSON_END();
150 }

References _json_buffer, JSON_BEGIN, JSON_END, JSON_FAIL, and STRING_ITERATOR_GET.

Referenced by _json_parse_value().

◆ _json_parse_float()

bool _json_parse_float ( )

Definition at line 216 of file json.qc.

217 {
218 JSON_BEGIN();
219 string s = "";
220 bool needdot = true;
221 for (int c; (c = STRING_ITERATOR_GET(_json)); )
222 {
223 if (c < '0' || c > '9')
224 {
225 if (c == '.' && needdot) // fine
226 needdot = false;
227 else
228 {
230 break;
231 }
232 }
233 s = strcat(s, chr2str(c));
234 }
235 if (s == "")
236 JSON_FAIL("expected float");
237 bufstr_add(_json_buffer, s, 0);
238 JSON_END();
239 }
#define chr2str
#define STRING_ITERATOR_UNGET(this)
Definition iter.qh:57

References _json_buffer, chr2str, JSON_BEGIN, JSON_END, JSON_FAIL, strcat(), STRING_ITERATOR_GET, and STRING_ITERATOR_UNGET.

Referenced by _json_parse_number().

◆ _json_parse_int()

bool _json_parse_int ( )

Definition at line 242 of file json.qc.

243 {
244 JSON_BEGIN();
245 string s = "";
246 for (int c; (c = STRING_ITERATOR_GET(_json)); )
247 {
248 if (c < '0' || c > '9')
249 {
251 break;
252 }
253 if (s == "" && c == '0')
254 JSON_FAIL("expected [1-9]");
255 s = strcat(s, chr2str(c));
256 }
257 if (s == "")
258 JSON_FAIL("expected int");
259 if (ftos(stof(s)) != s)
260 JSON_FAIL("expected int");
261 bufstr_add(_json_buffer, s, 0);
262 JSON_END();
263 }
float stof(string val,...)

References _json_buffer, chr2str, ftos(), JSON_BEGIN, JSON_END, JSON_FAIL, stof(), strcat(), STRING_ITERATOR_GET, and STRING_ITERATOR_UNGET.

Referenced by _json_parse_number().

◆ _json_parse_members()

bool _json_parse_members ( )

Definition at line 33 of file json.qc.

34 {
35 JSON_BEGIN();
36 for (;;)
37 {
38 if (!_json_parse_pair())
39 JSON_FAIL("expected pair");
40 if (STRING_ITERATOR_PEEK(_json) == ',')
41 {
43 continue;
44 }
45 break;
46 }
47 JSON_END();
48 }
ERASEABLE bool _json_parse_pair()
Definition json.qc:51

References _json_parse_pair(), JSON_BEGIN, JSON_END, JSON_FAIL, STRING_ITERATOR_NEXT, and STRING_ITERATOR_PEEK.

Referenced by _json_parse_object().

◆ _json_parse_null()

bool _json_parse_null ( )

Definition at line 153 of file json.qc.

154 {
155 JSON_BEGIN();
156 if (!(STRING_ITERATOR_GET(_json) == 'n'
157 && STRING_ITERATOR_GET(_json) == 'u'
158 && STRING_ITERATOR_GET(_json) == 'l'
159 && STRING_ITERATOR_GET(_json) == 'l'))
160 JSON_FAIL("expected 'null'");
161 bufstr_add(_json_buffer, "", 0);
162 JSON_END();
163 }

References _json_buffer, JSON_BEGIN, JSON_END, JSON_FAIL, and STRING_ITERATOR_GET.

Referenced by _json_parse_value().

◆ _json_parse_number()

bool _json_parse_number ( )

Definition at line 207 of file json.qc.

208{
209 JSON_BEGIN();
211 JSON_FAIL("expected number");
212 JSON_END();
213}
ERASEABLE bool _json_parse_float()
Definition json.qc:216
ERASEABLE bool _json_parse_int()
Definition json.qc:242

References _json_parse_float(), _json_parse_int(), JSON_BEGIN, JSON_END, and JSON_FAIL.

Referenced by _json_parse_value().

◆ _json_parse_object()

bool _json_parse_object ( )

Parse a json object.

Definition at line 21 of file json.qc.

22{
23 JSON_BEGIN();
24 if (STRING_ITERATOR_GET(_json) != '{')
25 JSON_FAIL("expected '{'");
26 WITH(int, _json_keys, bufstr_add(_json_buffer, "", 0), _json_parse_members());
27 if (STRING_ITERATOR_GET(_json) != '}')
28 JSON_FAIL("expected '}'");
29 JSON_END();
30}
int _json_keys
Current keys.
Definition json.qc:10
ERASEABLE bool _json_parse_members()
Definition json.qc:33

References _json_buffer, _json_keys, _json_parse_members(), JSON_BEGIN, JSON_END, JSON_FAIL, STRING_ITERATOR_GET, and WITH.

Referenced by _json_parse_value(), MX_Messages_(), MX_Sync_(), and TEST().

◆ _json_parse_pair()

bool _json_parse_pair ( )

Definition at line 51 of file json.qc.

52 {
53 JSON_BEGIN();
54 if (!_json_parse_string(false))
55 JSON_FAIL("expected string");
56 string key = _json_temp;
57 bufstr_set(_json_buffer, _json_keys, cons(bufstr_get(_json_buffer, _json_keys), key));
58 if (_json_ns)
59 key = strcat(_json_ns, ".", key);
60 bufstr_add(_json_buffer, key, 0);
61 if (STRING_ITERATOR_GET(_json) != ':')
62 JSON_FAIL("expected ':'");
63 bool ret = false;
64 WITH(string, _json_ns, key, ret = _json_parse_value());
65 if (!ret)
66 JSON_FAIL("expected value");
67 JSON_END();
68 }
string _json_temp
Last read string.
Definition json.qc:8
ERASEABLE bool _json_parse_string(bool add)
Definition json.qc:166
ERASEABLE string cons(string a, string b)
Definition string.qh:277

References _json_buffer, _json_keys, _json_ns, _json_parse_string(), _json_parse_value(), _json_temp, cons(), JSON_BEGIN, JSON_END, JSON_FAIL, strcat(), STRING_ITERATOR_GET, and WITH.

Referenced by _json_parse_members().

◆ _json_parse_string()

bool _json_parse_string ( bool add)

Definition at line 166 of file json.qc.

167{
168 JSON_BEGIN();
169 if (STRING_ITERATOR_GET(_json) != '"')
170 JSON_FAIL("expected opening '\"'");
171 string s = "";
172 for (int c; (c = STRING_ITERATOR_GET(_json)); )
173 {
174 if (c == '"')
175 {
177 break;
178 }
179 else if (c == '\\')
180 {
181 string esc;
182 switch (STRING_ITERATOR_GET(_json))
183 {
184 default:
185 JSON_FAIL("expected ( '\"' | '\\' | 'n' | 't' )");
186 case '"': esc = "\""; break;
187 case '\\': esc = "\\"; break;
188 case 'n': esc = "\n"; break;
189 case 't': esc = "\t"; break;
190 case 'u': esc = "\\u"; break; // TODO
191 case '/': esc = "/"; break;
192 }
193 s = strcat(s, esc);
194 }
195 else
196 s = strcat(s, chr2str(c));
197 }
198 if (STRING_ITERATOR_GET(_json) != '"')
199 JSON_FAIL("expected closing '\"'");
200 if (add)
201 bufstr_add(_json_buffer, s, 0);
202 _json_temp = s;
203 JSON_END();
204}

References _json_buffer, _json_temp, chr2str, JSON_BEGIN, JSON_END, JSON_FAIL, strcat(), STRING_ITERATOR_GET, and STRING_ITERATOR_UNGET.

Referenced by _json_parse_pair(), and _json_parse_value().

◆ _json_parse_true()

bool _json_parse_true ( )

Definition at line 126 of file json.qc.

127 {
128 JSON_BEGIN();
129 if (!(STRING_ITERATOR_GET(_json) == 't'
130 && STRING_ITERATOR_GET(_json) == 'r'
131 && STRING_ITERATOR_GET(_json) == 'u'
132 && STRING_ITERATOR_GET(_json) == 'e'))
133 JSON_FAIL("expected 'true'");
134 bufstr_add(_json_buffer, "1", 0);
135 JSON_END();
136 }

References _json_buffer, JSON_BEGIN, JSON_END, JSON_FAIL, and STRING_ITERATOR_GET.

Referenced by _json_parse_value().

◆ _json_parse_value()

bool _json_parse_value ( )

Definition at line 111 of file json.qc.

112{
113 JSON_BEGIN();
114 if (!(_json_parse_string(true)
120 || _json_parse_null()))
121 JSON_FAIL("expected value");
122 JSON_END();
123}
ERASEABLE bool _json_parse_number()
Definition json.qc:207
ERASEABLE bool _json_parse_true()
Definition json.qc:126
ERASEABLE bool _json_parse_array()
Definition json.qc:71
ERASEABLE bool _json_parse_false()
Definition json.qc:139
ERASEABLE bool _json_parse_null()
Definition json.qc:153
ERASEABLE bool _json_parse_object()
Parse a json object.
Definition json.qc:21

References _json_parse_array(), _json_parse_false(), _json_parse_null(), _json_parse_number(), _json_parse_object(), _json_parse_string(), _json_parse_true(), JSON_BEGIN, JSON_END, and JSON_FAIL.

Referenced by _json_parse_array(), and _json_parse_pair().