forked from TylerGlaiel/GON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgon.cpp
417 lines (343 loc) · 11.1 KB
/
gon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include "gon.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
GonObject::ErrorCallback GonObject::error_callback = GonObject::DefaultErrorCallback;
bool IsWhitespace(char c){
return c==' '||c=='\n'||c=='\r'||c=='\t';
}
bool IsSymbol(char c){
return c=='='||c==','||c==':'||c=='{'||c=='}'||c=='['||c==']';
}
bool IsIgnoredSymbol(char c){
return c=='='||c==','||c==':';
}
std::vector<std::string> Tokenize(std::string data){
std::vector<std::string> tokens;
bool inString = false;
bool inComment = false;
bool escaped = false;
std::string current_token = "";
for(int i = 0; i<data.size(); i++){
if(!inString && !inComment){
if(IsSymbol(data[i])){
if(!current_token.empty()){
tokens.push_back(current_token);
current_token = "";
}
if(!IsIgnoredSymbol(data[i])){
current_token += data[i];
tokens.push_back(current_token);
current_token = "";
}
continue;
}
if(IsWhitespace(data[i])){
if(!current_token.empty()){
tokens.push_back(current_token);
current_token = "";
}
continue;
}
if(data[i] == '#'){
if(!current_token.empty()){
tokens.push_back(current_token);
current_token = "";
}
inComment = true;
continue;
}
if(data[i] == '"'){
if(!current_token.empty()){
tokens.push_back(current_token);
current_token = "";
}
inString = true;
continue;
}
current_token += data[i];
}
//TODO: escape sequences
if(inString){
if(escaped){
if(data[i] == 'n'){
current_token += '\n';
} else {
current_token += data[i];
}
escaped = false;
} else if(data[i] == '\\'){
escaped = true;
} else if(!escaped && data[i] == '"'){
if(!current_token.empty()){
tokens.push_back(current_token);
current_token = "";
}
inString = false;
continue;
} else {
current_token += data[i];
continue;
}
}
if(inComment){
if(data[i] == '\n'){
inComment = false;
continue;
}
}
}
return tokens;
}
struct TokenStream {
std::vector<std::string> Tokens;
size_t current;
bool error;
TokenStream():current(0),error(false){
}
std::string Read(){
if(current>=Tokens.size()){
error = true;
return "!";
}
return Tokens[current++];
}
std::string Peek(){
if(current>=Tokens.size()){
error = true;
return "!";
}
return Tokens[current];
}
};
GonObject::GonObject(){
name = "";
type = Type::Null;
int_data = 0;
float_data = 0;
bool_data = false;
string_data = "";
}
GonObject GonObject::LoadFromTokens(TokenStream& Tokens){
GonObject ret;
if(Tokens.Peek() == "{"){ //read object
ret.type = Type::Object;
Tokens.Read(); //consume '{'
while(Tokens.Peek() != "}"){
std::string name = Tokens.Read();
ret.children_array.push_back(LoadFromTokens(Tokens));
ret.children_map[name] = ret.children_array.size()-1;
ret.children_array[ret.children_array.size()-1].name = name;
if(Tokens.error) error_callback("GON ERROR: missing a '}' somewhere");
}
Tokens.Read(); //consume '}'
return ret;
} else if(Tokens.Peek() == "["){ //read array
ret.type = Type::Array;
Tokens.Read(); //consume '['
while(Tokens.Peek() != "]"){
ret.children_array.push_back(LoadFromTokens(Tokens));
if(Tokens.error) error_callback("GON ERROR: missing a ']' somewhere");
}
Tokens.Read(); //consume ']'
return ret;
} else { //read data value
ret.type = Type::String;
ret.string_data = Tokens.Read();
//if string data can be converted to a number, do so
char* endptr;
ret.int_data = strtoll(ret.string_data.c_str(), &endptr, 0);
if(*endptr == 0){
ret.type = Type::Number;
}
ret.float_data = strtod(ret.string_data.c_str(), &endptr);
if(*endptr == 0){
ret.type = Type::Number;
}
//if string data can be converted to a bool or null, convert
if(ret.string_data == "null") ret.type = Type::Null;
if(ret.string_data == "true") {
ret.type = Type::Bool;
ret.bool_data = true;
}
if(ret.string_data == "false") {
ret.type = Type::Bool;
ret.bool_data = false;
}
return ret;
}
return ret;
}
void GonObject::DefaultErrorCallback(const char* msg) {
throw msg;
}
GonObject GonObject::Load(const std::string& filename){
std::ifstream in(filename.c_str(), std::ios::binary);
if (!in.is_open()) {
error_callback("Could not open file");
return null_gon;
}
in.seekg (0, std::ios::end);
size_t length = in.tellg();
in.seekg (0, std::ios::beg);
std::string str(length + 2, '\0');
in.read(&str[1], length);
in.close();
str.front() = '{';
str.back() = '}';
std::vector<std::string> Tokens = Tokenize(str);
TokenStream ts;
ts.current = 0;
ts.Tokens = Tokens;
return LoadFromTokens(ts);
}
GonObject GonObject::LoadFromBuffer(std::string buffer){
std::string str = std::string("{")+buffer+"}";
std::vector<std::string> Tokens = Tokenize(str);
TokenStream ts;
ts.current = 0;
ts.Tokens = Tokens;
return LoadFromTokens(ts);
}
//options with error throwing
std::string GonObject::String() const {
if(type != Type::String && type != Type::Number && type != Type::Bool) error_callback("GSON ERROR: Field is not a string");
return string_data;
}
int GonObject::Int() const {
if(type != Type::Number) error_callback("GON ERROR: Field is not a number");
return static_cast<int>(int_data);
}
uint32_t GonObject::UInt() const {
if(type != Type::Number) error_callback("GON ERROR: Field is not a number");
return static_cast<uint32_t>(int_data);
}
double GonObject::Number() const {
if(type != Type::Number) error_callback("GON ERROR: Field is not a number");
return float_data;
}
bool GonObject::Bool() const {
if(type != Type::Bool) error_callback("GON ERROR: Field is not a bool");
return bool_data;
}
//options with a default value
std::string GonObject::String(const std::string& _default) const {
if(type != Type::String && type != Type::Number && type != Type::Bool) return _default;
return string_data;
}
int GonObject::Int(int _default) const {
if(type != Type::Number) return _default;
return static_cast<int>(int_data);
}
double GonObject::Number(double _default) const {
if(type != Type::Number) return _default;
return float_data;
}
bool GonObject::Bool(bool _default) const {
if(type != Type::Bool) return _default;
return bool_data;
}
bool GonObject::Contains(const std::string& child) const{
if(type != Type::Object) return false;
const auto iter = children_map.find(child);
return iter != children_map.end();
}
bool GonObject::Contains(int child) const{
if(type != Type::Array) return true;
if(child < 0) return false;
if(static_cast<size_t>(child) >= children_array.size()) return false;
return true;
}
bool GonObject::Exists() const{
return type != Type::Null;
}
const GonObject& GonObject::operator[](const std::string& child) const {
if(type == Type::Null) return null_gon;
if(type != Type::Object) error_callback("GON ERROR: Field is not an object");
const auto iter = children_map.find(child);
if(iter != children_map.end()){
return children_array[iter->second];
}
return null_gon;
//error_callback("GON ERROR: Field not found: "+child);
}
const GonObject& GonObject::operator[](int childindex) const {
if(type != Type::Array) error_callback("GSON ERROR: Field is not an array");
return children_array[childindex];
}
size_t GonObject::Length() const {
if(type != Type::Array) error_callback("GSON ERROR: Field is not an array");
return children_array.size();
}
void GonObject::DebugOut(){
switch (type) {
case Type::Null:
std::cout << name << " is null " << std::endl;
break;
case Type::String:
std::cout << name << " is string \"" << String() << "\"" << std::endl;
break;
case Type::Number:
std::cout << name << " is number " << Int() << std::endl;
break;
case Type::Object:
std::cout << name << " is object {" << std::endl;
for (auto& i : children_array) {
i.DebugOut();
}
std::cout << "}" << std::endl;
break;
case Type::Array:
std::cout << name << " is array [" << std::endl;
for (auto& i : children_array) {
i.DebugOut();
}
std::cout << "]" << std::endl;
break;
case Type::Bool:
std::cout << name << " is bool " << Bool() << std::endl;
break;
default: break;
}
}
void GonObject::Save(const std::string& filename){
std::ofstream outfile(filename);
for (auto& i : children_array) {
outfile << i.name+" "+ i.getOutStr()+"\n";
}
outfile.close();
}
std::string GonObject::getOutStr(){
std::string out;
switch (type) {
case Type::Null:
out += "null";
break;
// If we're a number or bool, we already have a string representation
// of our value, thanks to the way we parse the input file
case Type::String:
case Type::Number:
case Type::Bool:
out += String();
break;
case Type::Object:
out += "{\n";
for (auto& i : children_array) {
out += i.name+" "+ i.getOutStr()+"\n";
}
out += "}\n";
break;
case Type::Array:
out += "[";
for (auto& i : children_array) {
out += i.getOutStr()+" ";
}
out += "]\n";
break;
default:
break;
}
return out;
}
GonObject GonObject::null_gon;