-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperConfigFile.cpp
More file actions
266 lines (217 loc) · 5.07 KB
/
SuperConfigFile.cpp
File metadata and controls
266 lines (217 loc) · 5.07 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
/*
* SuperConfigFile.cpp
*
* Created on: Sep 14, 2009
* Author: awcheng
*/
#include "SuperConfigFile.h"
set<string> SuperConfigFile::keys() const
{
set<string> keyset;
for(ConfigFile::mapci i=this->myContents.begin();i!=this->myContents.end();i++)
keyset.insert(i->first);
return keyset;
}
SuperConfigFile::SuperConfigFile(string filename,string delimiter,string comment,string sentry):ConfigFile(filename,delimiter,comment,sentry), varDelim("$"), varMarker('@')
{
loadExterns();
}
void SuperConfigFile::justLoad(string filename)
{
std::ifstream fin(filename.c_str());
if(!fin.good())
throw ConfigFile::file_not_found(filename);
this->justLoad(fin);
fin.close();
}
SuperConfigFile::SuperConfigFile():varDelim("$"), varMarker('@')
{
}
SuperConfigFile::SuperConfigFile(int argc,const char**argv,string arr_delim): varDelim("$"), varMarker('@')
{
bool acceptValue=false;
string key="";
vector<string> general_argvs;
for(int i=0;i<argc;i++)
{
if(!acceptValue)
{
if(argv[i][0]=='-')
{
acceptValue=true;
const char*curarg=argv[i];
key=string(curarg+1);
}
else
{
general_argvs.push_back(argv[i]);
}
}
else
{
this->add(key,argv[i]);
acceptValue=false;
}
}
this->add("@argv",StringUtil::formArrayString(general_argvs,arr_delim));
this->loadExterns();
}
istream& SuperConfigFile::justLoad(istream& is)
{
ConfigFile& baseObj=*this;
is>>baseObj;
return is;
}
istream& operator>>(istream& is,SuperConfigFile& scf)
{
scf.justLoad(is);
scf.loadExterns();
return is;
}
ostream& operator<<(ostream& os,const SuperConfigFile& scf)
{
const ConfigFile& baseObj=scf;
os<<baseObj;
return os;
}
ostream& SuperConfigFile::listAllItemsAsSuperString(ostream& os,bool includeExterns) const
{
if(includeExterns)
{
os<<"externs:"<<endl;
for(vector<string>::const_iterator i=this->externRecord.begin();i!=this->externRecord.end();i++)
{
os<<"\t"<<(*i)<<endl;
}
}
os<<"items:"<<endl;
for(ConfigFile::mapci i=this->myContents.begin();i!=this->myContents.end();i++)
{
os<<"\t"<<i->first<<"="<<this->readSuperString(i->first)<<endl;
}
return os;
}
void SuperConfigFile::loadExterns()
{
if(this->keyExists("@externs"))
{
//whether to initiate the tree
//load external stuff
vector<string> curExtern=this->readArray("@externs");
this->remove("@externs");
for(vector<string>::iterator i=curExtern.begin();i!=curExtern.end();i++)
this->externQueue.push_back(*i);
while(this->externQueue.size()>0)
{
string u=this->externQueue.front();
this->externRecord.push_back(u);
this->externQueue.pop_front();
this->justLoad(u);
if(this->keyExists("@externs"))
{
curExtern=this->readArray("@externs");
for(vector<string>::iterator i=curExtern.begin();i!=curExtern.end();i++)
this->externQueue.push_back(*i);
this->remove("@externs");
}
}
}
}
void SuperConfigFile::readArray(string name,vector<string>& resultStrings,bool clear,string _delim) const
{
string preString=this->read<string>(name);
vector<string> splits;
StringUtil::split(preString,_delim,splits);
if(clear)
{
resultStrings.clear();
}
for(vector<string>::iterator i=splits.begin();i!=splits.end();i++)
{
resultStrings.push_back(this->parseSuperString(*i));
}
}
bool SuperConfigFile::readBool(string _name) const
{
string preString=StringUtil::toUpper(this->readSuperString(_name));
if(preString=="1" || preString=="TRUE" || preString=="YES" || preString=="Y" || preString=="T")
{
return true;
}
else
{
return false;
}
}
vector<string> SuperConfigFile::readArray(string _name, string delim) const
{
vector<string> vstring;
this->readArray(_name,vstring,true,delim);
return vstring;
}
void SuperConfigFile::setVarDelim(string _varDelim)
{
this->varDelim=_varDelim;
}
string SuperConfigFile::getVarDelim() const
{
return this->varDelim;
}
void SuperConfigFile::setVarMarker(char _varMarker)
{
this->varMarker=_varMarker;
}
char SuperConfigFile::getVarMarker() const
{
return this->varMarker;
}
string SuperConfigFile::escape(const string& str) const
{
return StringUtil::escape(str,"ntz@","\n\t#@",'\\');
}
string SuperConfigFile::readSuperString(string _name,string _default) const
{
if(!this->keyExists(_name))
{
return _default;
}
else
{
return this->readSuperString(_name);
}
}
string SuperConfigFile::parseSuperString(string preString) const
{
vector<string> splits;
StringUtil::split(preString,this->varDelim,splits);
int nSplit=splits.size();
string resultString=this->escape(splits[0]);
for(int i=1;i<nSplit-1;i++)
{
//cerr<<"split #"<<(i+1)<<splits[i]<<endl;
string &curSplit=splits[i];
if(curSplit.length()==0)
{
resultString+=this->varDelim;
}
else if(curSplit.length()>1 && curSplit[0]==this->varMarker)
{
string ref=curSplit.substr(1);
resultString+=this->readSuperString(ref);
}
else
{
resultString+=this->escape(curSplit);
}
}
if(nSplit>1)
{
resultString+=splits[nSplit-1];
}
return resultString;
}
string SuperConfigFile::readSuperString(string _name) const
{
string preString=this->read<string>(_name);
return this->parseSuperString(preString);
}