-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigLoader.cc
More file actions
423 lines (363 loc) · 7.79 KB
/
ConfigLoader.cc
File metadata and controls
423 lines (363 loc) · 7.79 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
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
418
419
420
421
422
423
#include "ConfigLoader.hh"
#include <vector>
#include <map>
#include <exception>
#include <fstream>
#include <boost/filesystem.hpp>
using namespace gzweb;
void ConfigLoader::loadAllFiles(ConfigLoader* c, const std::string& path)
{
try
{
for ( boost::filesystem::recursive_directory_iterator end, dir(path); dir != end; ++dir )
{
boost::filesystem::path p(*dir);
if(p.extension() == c->m_fileEnding)
{
std::ifstream in((*dir).path().string().c_str(), std::ios::binary);
c->parseScript(in);
}
}
}
catch (boost::filesystem::filesystem_error &e)
{
std::cerr << e.what() << std::endl;
}
}
ConfigLoader::ConfigLoader(const std::string& fileEnding)
{
//Register as a ScriptLoader
m_fileEnding = fileEnding;
}
ConfigLoader::~ConfigLoader()
{
clearScriptList();
}
void ConfigLoader::clearScriptList()
{
std::map <std::string, ConfigNode *>::iterator i;
for (i = m_scriptList.begin(); i != m_scriptList.end(); i++)
{
delete i->second;
}
m_scriptList.clear();
}
ConfigNode *ConfigLoader::getConfigScript(const std::string &name)
{
std::map <std::string, ConfigNode*>::iterator i;
std::string key = name;
i = m_scriptList.find(key);
//If found..
if (i != m_scriptList.end())
{
return i->second;
}
else
{
return NULL;
}
}
std::map <std::string, ConfigNode*> ConfigLoader::getAllConfigScripts ()
{
return m_scriptList;
}
void ConfigLoader::parseScript(std::ifstream &stream)
{
//Get first token
_nextToken(stream);
if (tok == TOKEN_EOF)
{
stream.close();
return;
}
//Parse the script
_parseNodes(stream, 0);
stream.close();
}
void ConfigLoader::_nextToken(std::ifstream &stream)
{
lastTok = tok;
lastTokVal = tokVal;
//EOF token
if (stream.eof())
{
tok = TOKEN_EOF;
return;
}
//(Get next character)
int ch = stream.get();
if (ch == -1)
{
tok = TOKEN_EOF;
return;
}
while ((ch == ' ' || ch == 9) && !stream.eof())
{ //Skip leading spaces / tabs
ch = stream.get();
}
if (stream.eof())
{
tok = TOKEN_EOF;
return;
}
//Newline token
if (ch == '\r' || ch == '\n')
{
do
{
ch = stream.get();
} while ((ch == '\r' || ch == '\n') && !stream.eof());
stream.unget();
tok = TOKEN_NewLine;
return;
}
//Open brace token
else if (ch == '{')
{
tok = TOKEN_OpenBrace;
return;
}
//Close brace token
else if (ch == '}')
{
tok = TOKEN_CloseBrace;
return;
}
//Text token
if (ch < 32 || ch > 122) //Verify valid char
{
throw std::runtime_error("Parse Error: Invalid character, ConfigLoader::load()");
}
tokVal = "";
tok = TOKEN_Text;
do
{
//Skip comments
if (ch == '/')
{
int ch2 = stream.peek();
//C++ style comment (//)
if (ch2 == '/')
{
stream.get();
do
{
ch = stream.get();
} while (ch != '\r' && ch != '\n' && !stream.eof());
tok = TOKEN_NewLine;
return;
}
else if (ch2 == '*')
{
stream.get();
do
{
ch = stream.get();
ch2 = stream.peek();
} while (!(ch == '*' && ch2 == '/') && !stream.eof());
stream.get();
do
{
ch = stream.get();
} while (ch != '\r' && ch != '\n' && !stream.eof());
continue;
}
}
//Add valid char to tokVal
tokVal += (char)ch;
//Next char
ch = stream.get();
} while (ch > 32 && ch <= 122 && !stream.eof());
stream.unget();
return;
}
void ConfigLoader::_skipNewLines(std::ifstream &stream)
{
while (tok == TOKEN_NewLine)
{
_nextToken(stream);
}
}
void ConfigLoader::_parseNodes(std::ifstream &stream, ConfigNode *parent)
{
typedef std::pair<std::string, ConfigNode*> ScriptItem;
while (true)
{
switch (tok)
{
//Node
case TOKEN_Text:
//Add the new node
ConfigNode *newNode;
if (parent)
{
newNode = parent->addChild(tokVal);
}
else
{
newNode = new ConfigNode(0, tokVal);
}
//Get values
_nextToken(stream);
while (tok == TOKEN_Text)
{
newNode->addValue(tokVal);
_nextToken(stream);
}
//Add root nodes to scriptList
if (!parent){
std::string key;
if (newNode->getValues().empty())
{
key = newNode->getName() + ' ';
}
else
{
key = newNode->getName() + ' ' + newNode->getValues().front();
}
m_scriptList.insert(ScriptItem(key, newNode));
}
_skipNewLines(stream);
//Add any sub-nodes
if (tok == TOKEN_OpenBrace)
{
//Parse nodes
_nextToken(stream);
_parseNodes(stream, newNode);
//Check for matching closing brace
if (tok != TOKEN_CloseBrace)
{
throw std::runtime_error("Parse Error: Expecting closing brace");
}
_nextToken(stream);
_skipNewLines(stream);
}
break;
//Out of place brace
case TOKEN_OpenBrace:
throw std::runtime_error("Parse Error: Opening brace out of plane");
break;
//Return if end of nodes have been reached
case TOKEN_CloseBrace:
return;
//Return if reached end of file
case TOKEN_EOF:
return;
case TOKEN_NewLine:
_nextToken(stream);
break;
}
};
}
ConfigNode::ConfigNode(ConfigNode *parent, const std::string &name)
{
m_name = name;
m_parent = parent;
_removeSelf = true; //For proper destruction
m_lastChildFound = -1;
//Add self to parent's child list (unless this is the root node being created)
if (parent != NULL)
{
m_parent->m_children.push_back(this);
_iter = --(m_parent->m_children.end());
}
}
ConfigNode::~ConfigNode()
{
//Delete all children
std::vector<ConfigNode*>::iterator i;
for (i = m_children.begin(); i != m_children.end(); i++)
{
ConfigNode *node = *i;
node->_removeSelf = false;
delete node;
}
m_children.clear();
//Remove self from parent's child list
if (_removeSelf && m_parent != NULL)
{
m_parent->m_children.erase(_iter);
}
}
ConfigNode *ConfigNode::addChild(const std::string &name, bool replaceExisting)
{
if (replaceExisting)
{
ConfigNode *node = findChild(name, false);
if (node)
{
return node;
}
}
return new ConfigNode(this, name);
}
ConfigNode *ConfigNode::findChild(const std::string &name, bool recursive)
{
int indx, prevC, nextC;
int childCount = (int)m_children.size();
if (m_lastChildFound != -1)
{
//If possible, try checking the nodes neighboring the last successful search
//(often nodes searched for in sequence, so this will boost search speeds).
prevC = m_lastChildFound-1; if (prevC < 0) prevC = 0; else if (prevC >= childCount) prevC = childCount-1;
nextC = m_lastChildFound+1; if (nextC < 0) nextC = 0; else if (nextC >= childCount) nextC = childCount-1;
for (indx = prevC; indx <= nextC; ++indx)
{
ConfigNode *node = m_children[indx];
if (node->m_name == name)
{
m_lastChildFound = indx;
return node;
}
}
//If not found that way, search for the node from start to finish, avoiding the
//already searched area above.
for (indx = nextC + 1; indx < childCount; ++indx)
{
ConfigNode *node = m_children[indx];
if (node->m_name == name) {
m_lastChildFound = indx;
return node;
}
}
for (indx = 0; indx < prevC; ++indx)
{
ConfigNode *node = m_children[indx];
if (node->m_name == name) {
m_lastChildFound = indx;
return node;
}
}
}
else
{
//Search for the node from start to finish
for (indx = 0; indx < childCount; ++indx){
ConfigNode *node = m_children[indx];
if (node->m_name == name) {
m_lastChildFound = indx;
return node;
}
}
}
//If not found, search child nodes (if recursive == true)
if (recursive)
{
for (indx = 0; indx < childCount; ++indx)
{
m_children[indx]->findChild(name, recursive);
}
}
//Not found anywhere
return NULL;
}
void ConfigNode::setParent(ConfigNode *newParent)
{
//Remove self from current parent
m_parent->m_children.erase(_iter);
//Set new parent
m_parent = newParent;
//Add self to new parent
m_parent->m_children.push_back(this);
_iter = --(m_parent->m_children.end());
}