-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cc
349 lines (282 loc) · 10.1 KB
/
Config.cc
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
#include <cstdlib>
#include <fstream>
#include <iostream>
#include "Rtypes.h"
#include "Config.h"
// Constructor. Parses the configfile and stores the definitions.
// They can be obtained using the 'operator()(key)' method.
//
// The config syntax is as follows:
// - empty line: an empty line is ignored
// - comment: a line starting with '#' is ignored
// - definition: a line of the form '<key> :: <attributes>',
// where the attributes part is a semi-colon (';') separated
// list of <name> : <value> pairs. Keys can occour several
// times.
// ----------------------------------------------------------------------------
Config::Config(const TString &fileName)
: fileName_(fileName), keyAndAttributesDelimiter_("::"), attributeNameAndValueDelimiter_(":") {
// Open file for reading
std::ifstream file(fileName_.Data());
if( !file.is_open() ) {
std::cerr << "\n\nERROR error opening config-file '" << fileName_ << "'\n";
exit(-1);
}
// Loop over lines and parse
unsigned int lineNum = 0;
std::string line = "";
while( !file.eof() ) {
++lineNum;
std::getline(file,line);
if( !isComment(line) && line.size() ) {
// separate line into key and attribute list
std::string key = "";
std::string attrList;
if( split(line,keyAndAttributesDelimiter_,key,attrList) ) {
TString tKey = key;
std::map< TString,std::vector<Attributes> >::iterator it = keyAttributesMap_.find(tKey);
if( it == keyAttributesMap_.end() ) {
std::vector<Config::Attributes> attr;
attr.push_back(getAttributes(attrList,lineNum));
keyAttributesMap_[tKey] = attr;
} else {
it->second.push_back(getAttributes(attrList,lineNum));
}
}
}
}
file.close();
}
// Returns a vector of attribute (name-value pair) lists for
// a given key. Each vector entry corresponds to one line
// in the config file.
// ----------------------------------------------------------------------------
std::vector<Config::Attributes> Config::operator()(const TString &key) const {
std::map< TString,std::vector<Attributes> >::const_iterator it = keyAttributesMap_.find(key);
if( it != keyAttributesMap_.end() ) {
return it->second;
} else {
std::vector<Config::Attributes> list;
return list;
}
}
// ----------------------------------------------------------------------------
Config::Attributes Config::getAttributes(const std::string &line, unsigned int lineNum) const {
Config::Attributes result(lineNum);
std::string str = line;
while( str.find(";") != std::string::npos ) {
// Comma-separated list of attributes
size_t end = str.find(";");
std::string nameAndValue = str.substr(0,end);
str = str.substr(end+1,str.size());
// Each attribute has a name and value, separated by attributeNameAndValueDelimiter_
std::string name = "";
std::string value = "";
split(nameAndValue,attributeNameAndValueDelimiter_,name,value);
result.add(name,value);
}
// Last attribute
trim(str);
std::string name = "";
std::string value = "";
split(str,attributeNameAndValueDelimiter_,name,value);
result.add(name,value);
return result;
}
// ----------------------------------------------------------------------------
bool Config::split(const TString &str, const TString &delim, std::vector<TString> &parts) {
TString rest = str;
TString part1 = "";
TString part2 = "";
while( split(rest,delim,part1,part2) ) {
parts.push_back(part1);
rest = part2;
}
parts.push_back(rest);
return split(str,delim,part1,part2);
}
// ----------------------------------------------------------------------------
bool Config::split(const TString &str, const TString &delim, TString &first, TString &second) {
std::string strt(str.Data());
std::string delimt(delim.Data());
std::string firstt(first.Data());
std::string secondt(second.Data());
bool result = split(strt,delimt,firstt,secondt);
first = firstt.c_str();
second = secondt.c_str();
return result;
}
// ----------------------------------------------------------------------------
bool Config::split(const std::string &str, const std::string &delim, std::string &first, std::string &second) {
bool hasDelim = false;
first = str;
second = "";
if( str.find(delim) != std::string::npos ) {
hasDelim = true;
size_t end = str.find(delim);
first = str.substr(0,end);
second = str.substr(end+delim.size(),str.size());
}
trim(first);
trim(second);
return hasDelim;
}
// ----------------------------------------------------------------------------
TString Config::after(const TString &str, const TString &delim) {
TString first = "";
TString second = "";
Config::split(str,delim,first,second);
return second;
}
// ----------------------------------------------------------------------------
bool Config::enclosed(const TString &str, const TString &delimStart, const TString &delimEnd, TString &encl) {
bool hasDelims = false;
std::string strt(str.Data());
std::string enclt;
size_t start = strt.find(delimStart.Data());
size_t end = strt.find(delimEnd.Data());
if( start != std::string::npos && end != std::string::npos && end > start ) {
hasDelims = true;
enclt = strt.substr(start+delimStart.Length(),end-start-1);
}
trim(enclt);
encl = enclt;
return hasDelims;
}
// ----------------------------------------------------------------------------
bool Config::isComment(const std::string &line) {
bool result = false;
if( line.length() && line.at(0) == '#' ) result = true;
return result;
}
// ----------------------------------------------------------------------------
void Config::trim(std::string &str) {
while( str.size() && str[0] == ' ' ) str.erase(0,1);
while( str.size() && str[str.size()-1] == ' ' ) str.erase(str.size()-1,str.size());
}
// Expects one of the following configuration strings:
// - an integer number
// - a TColor mnemonic, e.g. kRed
// - a TColor mnemonic modified by an integer number, e.g. kRed+3, kBlue-2
// ----------------------------------------------------------------------------
int Config::color(const TString &cfg) {
int color = 1;
TString cfgLow = cfg;
cfgLow.ToLower();
if( cfgLow.Contains("k") ) { // Expect mnemonic
std::string kSth;
int modifier = 0;
std::string str(cfgLow.Data());
std::string modifierStr;
if( split(str,"+",kSth,modifierStr) ) {
TString tmp(modifierStr);
modifier = tmp.Atoi();
} else if( split(str,"-",kSth,modifierStr) ) {
TString tmp(modifierStr);
modifier = -1*(tmp.Atoi());
}
if( kSth == "kyellow" ) color = kYellow+modifier;
else if( kSth == "korange" ) color = kOrange+modifier;
else if( kSth == "kred" ) color = kRed+modifier;
else if( kSth == "kpink" ) color = kPink+modifier;
else if( kSth == "kmagenta" ) color = kMagenta+modifier;
else if( kSth == "kviolet" ) color = kViolet+modifier;
else if( kSth == "kblue" ) color = kBlue+modifier;
else if( kSth == "kazure" ) color = kAzure+modifier;
else if( kSth == "kcyan" ) color = kCyan+modifier;
else if( kSth == "kteal" ) color = kTeal+modifier;
else if( kSth == "kgreen" ) color = kGreen+modifier;
else if( kSth == "kspring" ) color = kSpring+modifier;
} else { // Expect integer
color = cfg.Atoi();
}
return color;
}
TString Config::Attributes::value(const TString &name) const {
TString val = "";
std::map<TString,Value>::const_iterator it = values_.find(name);
if( it != values_.end() ) val = it->second.value();
return val;
}
bool Config::Attributes::isBoolean(const TString &name) const {
bool res = false;
std::map<TString,Value>::const_iterator it = values_.find(name);
if( it != values_.end() ) res = it->second.isBoolean();
return res;
}
bool Config::Attributes::valueBoolean(const TString &name) const {
bool val = "";
std::map<TString,Value>::const_iterator it = values_.find(name);
if( it != values_.end() ) val = it->second.valueBoolean();
return val;
}
bool Config::Attributes::isInteger(const TString &name) const {
bool res = false;
std::map<TString,Value>::const_iterator it = values_.find(name);
if( it != values_.end() ) res = it->second.isInteger();
return res;
}
int Config::Attributes::valueInteger(const TString &name) const {
int val = 999999;
std::map<TString,Value>::const_iterator it = values_.find(name);
if( it != values_.end() ) val = it->second.valueInteger();
return val;
}
bool Config::Attributes::isDouble(const TString &name) const {
bool res = false;
std::map<TString,Value>::const_iterator it = values_.find(name);
if( it != values_.end() ) res = it->second.isDouble();
return res;
}
double Config::Attributes::valueDouble(const TString &name) const {
double val = 999999.;
std::map<TString,Value>::const_iterator it = values_.find(name);
if( it != values_.end() ) val = it->second.valueDouble();
return val;
}
std::vector<TString> Config::Attributes::listOfNames() const {
std::vector<TString> list(values_.size());
unsigned int i = 0;
for(std::map<TString,Value>::const_iterator it = values_.begin();
it != values_.end(); ++it,++i) {
list.at(i) = it->first;
}
return list;
}
std::vector<TString> Config::Attributes::listOfNames(const TString &containedStr) const {
std::vector<TString> list;
for(std::map<TString,Value>::const_iterator it = values_.begin();
it != values_.end(); ++it) {
if( (it->first).Contains(containedStr) ) {
list.push_back(it->first);
}
}
return list;
}
Config::Attributes::Value::Value()
: value_(""), isBoolean_(false), valueBoolean_(false), isInteger_(false), isDouble_(false) {};
Config::Attributes::Value::Value(const TString &value)
: value_(value) {
isBoolean_ = false;
isInteger_ = false;
isDouble_ = false;
valueBoolean_ = false;
if( value_.IsAlpha() ) {
TString low = value_;
low.ToLower();
if( low == "true" || low == "ktrue" ) {
isBoolean_ = true;
valueBoolean_ = true;
} else if( low == "false" || low == "kfalse" ) {
isBoolean_ = true;
valueBoolean_ = false;
}
}
if( !isBoolean_ ) {
if( value_.IsDigit() ) {
isInteger_ = true;
} else if( value_.IsFloat() ) {
isDouble_ = true;
}
}
}