-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveState.cpp
More file actions
370 lines (336 loc) · 7.91 KB
/
Copy pathSaveState.cpp
File metadata and controls
370 lines (336 loc) · 7.91 KB
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
#include "Misc.h"
#include "SaveState.h"
#include <assert.h>
#define assert_exp(x) do { if (f.get() != x) throw (std::runtime_error("Unexpected character"));} while (false)
static void skip_whitespace(std::istream& f)
{
while (true)
{
int c = f.peek();
if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
f.get();
else if (c == 0xEF)
{
f.get();f.get();f.get();
}
else
break;
}
}
std::string SaveObject::to_string()
{
std::ostringstream stream;
save(stream);
return stream.str();
}
SaveObject* SaveObject::load(std::string& input)
{
std::istringstream stream(input);
return SaveObject::load(stream);
}
SaveObject* SaveObject::load(std::istream& f)
{
skip_whitespace(f);
char c = f.peek();
if (c == '{')
return new SaveObjectMap(f);
if (c == 'n')
return new SaveObjectNull(f);
if (c == 'f')
{
assert_exp('f');
assert_exp('a');
assert_exp('l');
assert_exp('s');
assert_exp('e');
return new SaveObjectNumber(0);
}
if (c == 't')
{
assert_exp('t');
assert_exp('r');
assert_exp('u');
assert_exp('e');
return new SaveObjectNumber(1);
}
if (c == '[')
return new SaveObjectList(f);
if (c == '"')
return new SaveObjectString(f);
if ((c >= '0' && c <= '9') || c == '-')
return new SaveObjectNumber(f);
printf("%d\n", (int)(unsigned char)c);
throw(std::runtime_error("Parse Error"));
}
static std::string parse_string(std::istream& f)
{
char c;
std::string str;
assert_exp('\"');
while ((c = f.get()) != '\"')
{
if (c == '\\')
{
c = f.get();
if (c == 'n')
c = '\n';
}
str.push_back(c);
}
return str;
}
SaveObjectString::SaveObjectString(std::istream& f)
{
str = parse_string(f);
}
std::string SaveObjectString::get_string()
{
return str;
}
void SaveObjectString::save(std::ostream& f)
{
f << '"';
for(std::string::iterator it = str.begin(); it != str.end(); ++it)
{
char c = *it;
if (c == '\n')
f << "\\n";
else if (c == '"')
f << "\\\"";
else if (c == '\\')
f << "\\\\";
else
f << c;
}
f << '"';
}
SaveObjectMap::SaveObjectMap(std::istream& f)
{
assert_exp('{');
while (true)
{
skip_whitespace(f);
if (f.peek() == '}')
break;
std::string key = parse_string(f);
skip_whitespace(f);
assert_exp(':');
SaveObject* obj = SaveObject::load(f);
add_item(key, obj);
skip_whitespace(f);
if (f.peek() == '}')
break;
assert_exp(',');
}
assert_exp('}');
}
SaveObjectMap::~SaveObjectMap()
{
for(std::map<std::string, SaveObject*>::iterator it = omap.begin();it != omap.end();++it)
delete it->second;
}
void SaveObjectMap::add_item(std::string key, SaveObject* value)
{
assert(!omap[key]);
omap[key]=value;
}
SaveObject* SaveObjectMap::get_item(std::string key)
{
if (omap.find(key) == omap.end())
{
std::cout << key << "\n";
throw(std::runtime_error("Bad map key"));
}
return omap[key];
}
int64_t SaveObjectMap::get_num(std::string key)
{
if (omap.find(key) != omap.end())
return omap[key]->get_num();
std::cout << "failed indexing for an int with key:" << key << "\n";
return 0;
}
void SaveObjectMap::get_num(std::string key, int& value)
{
if (omap.find(key) == omap.end())
throw(std::runtime_error("Bad map key"));
value = omap[key]->get_num();
}
void SaveObjectMap::add_num(std::string key, int64_t value)
{
add_item(key, new SaveObjectNumber(value));
}
void SaveObjectMap::add_string(std::string key, std::string value)
{
add_item(key, new SaveObjectString(value));
}
void SaveObjectMap::get_string(std::string key, std::string& value)
{
if (omap.find(key) == omap.end())
throw(std::runtime_error("Bad map key"));
value = omap[key]->get_string();
}
std::string SaveObjectMap::get_string(std::string key)
{
if (omap.find(key) == omap.end())
throw(std::runtime_error("Bad map key"));
return omap[key]->get_string();
}
bool SaveObjectMap::has_key(std::string key)
{
return omap.find(key) != omap.end();
}
void SaveObjectMap::save(std::ostream& f)
{
f.put('{');
bool first = true;
for (std::map<std::string, SaveObject*>::iterator it=omap.begin(); it!=omap.end(); ++it)
{
if (!first)
f << ',';
first = false;
f << '"';
f << it->first;
f << '"';
f << ':';
it->second->save(f);
}
f.put('}');
};
void SaveObjectMap::pretty_print(std::ostream& f, int indent)
{
f.put('\n');
f << std::string(indent, ' ');
f.put('{');
bool first = true;
for (std::map<std::string, SaveObject*>::iterator it=omap.begin(); it!=omap.end(); ++it)
{
if (!first)
f << ',';
f.put('\n');
f << std::string(indent + 2, ' ');
first = false;
f << '"';
f << it->first;
f << '"';
f << ':';
it->second->pretty_print(f, indent + 4);
}
f.put('\n');
f << std::string(indent, ' ');
f.put('}');
}
SaveObject* SaveObjectMap::dup()
{
SaveObjectMap* rep = new SaveObjectMap;
for (std::map<std::string, SaveObject*>::iterator it=omap.begin(); it!=omap.end(); ++it)
{
rep->add_item(it->first, it->second->dup());
}
return rep;
};
SaveObjectList::SaveObjectList(std::istream& f)
{
assert_exp('[');
while (true)
{
skip_whitespace(f);
if (f.peek() == ']')
break;
SaveObject* obj = SaveObject::load(f);
add_item(obj);
skip_whitespace(f);
if (f.peek() == ']')
break;
assert_exp(',');
}
assert_exp(']');
}
SaveObjectList::~SaveObjectList()
{
for(std::vector<SaveObject*>::iterator it = olist.begin(); it != olist.end(); it++)
delete *it;
}
void SaveObjectList::add_item(SaveObject* value)
{
olist.push_back(value);
}
SaveObject* SaveObjectList::get_item(unsigned index)
{
if (index >= get_count())
throw(std::runtime_error("Bad list index"));
return olist[index];
}
unsigned SaveObjectList::get_count()
{
return olist.size();
}
void SaveObjectList::add_num(int64_t value)
{
add_item(new SaveObjectNumber(value));
}
int64_t SaveObjectList::get_num(int index)
{
return get_item(index)->get_num();
}
void SaveObjectList::save(std::ostream& f)
{
f.put('[');
bool first = true;
for (std::vector<SaveObject*>::iterator it=olist.begin(); it!=olist.end(); ++it)
{
if (!first)
f << ',';
first = false;
(*it)->save(f);
}
f.put(']');
}
void SaveObjectList::pretty_print(std::ostream& f, int indent)
{
f.put('[');
bool first = true;
for (std::vector<SaveObject*>::iterator it=olist.begin(); it!=olist.end(); ++it)
{
if (!first)
f << ',';
first = false;
(*it)->pretty_print(f, indent);
}
f.put(']');
}
void SaveObjectList::add_string(std::string value)
{
add_item(new SaveObjectString(value));
}
std::string SaveObjectList::get_string(int index)
{
if (index >= (int)olist.size())
return "";
return get_item(index)->get_string();
}
SaveObject* SaveObjectList::dup()
{
SaveObjectList* rep = new SaveObjectList;
for (std::vector<SaveObject*>::iterator it=olist.begin(); it!=olist.end(); ++it)
{
rep->add_item((*it)->dup());
}
return rep;
};
void SaveObjectList::pop_back()
{
delete olist.back();
olist.pop_back();
}
SaveObjectNull::SaveObjectNull(std::istream& f)
{
assert_exp('n');
assert_exp('u');
assert_exp('l');
assert_exp('l');
}
void SaveObjectNull::save(std::ostream& f)
{
f << "null";
}