-
Notifications
You must be signed in to change notification settings - Fork 12
/
Config.h
350 lines (301 loc) · 8.41 KB
/
Config.h
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
//
// Config.h
//
// DISCLAIMER:
// This software was developed by U.S. Government employees as part of
// their official duties and is not subject to copyright. No warranty implied
// or intended.
#ifndef __INICONFIG_H__
#define __INICONFIG_H__
#include <string>
#include <sstream>
#include <map>
#include <fstream>
#include <vector>
#include "StdStringFcn.h"
#pragma comment( user, "Compiled on " __DATE__ " at " __TIME__ )
namespace crp
{
/**
* The StringVariant class is a simple string-based variant implementation that allows
* the user to easily convert between simple numeric/string types.
*/
class StringVariant
{
std::string data;
public:
StringVariant() : data() {}
operator LPCSTR() { return this->c_str(); }
operator double() { return this->toNumber<double>(); }
operator int() { return this->toNumber<int>(); }
template<typename ValueType>
StringVariant(ValueType val)
{
std::ostringstream stream;
stream << val;
data.assign(stream.str());
}
template<typename ValueType>
StringVariant& operator=(const ValueType val)
{
std::ostringstream stream;
stream << val;
data.assign(stream.str());
return *this;
}
template<typename NumberType>
NumberType toNumber() const
{
NumberType result = 0;
std::istringstream stream(data);
if(stream >> result)
return result;
else if(data == "yes" || data == "true")
return 1;
return 0;
}
std::string str() const
{
return data;
}
const char * c_str() const
{
return data.c_str();
}
};
/**
* The Config class can be used to load simple key/value pairs from a file.
*
* @note An example of syntax:
* // An example of a comment
* username= Bob
* gender= male
* hair-color= black // inline comments are also allowed
* level= 42
*
* @note An example of usage:
* Config config;
* config.load("myFile.txt");
*
* std::string username = config["username"].str();
* int level = config["level"].toNumber<int>();
*
* Config config;
* config.load(inifile);
* OutputDebugString(config.GetSymbolValue("FANUC.INCHES", L"INCHES").c_str());
* OutputDebugString(StrFormat("%f\n", config.GetSymbolValue("MAIN.MAXRPM", "9000").toNumber<double>()));
*/
class Config
{
typedef std::map<std::string, std::vector<std::string> > SectionMap;
typedef std::map<std::string, StringVariant>::iterator ConfigIt;
SectionMap sections;
public:
std::map<std::string, StringVariant> inimap;
Config() {}
void Clear() { sections.clear(); inimap.clear(); }
std::vector<std::string> GetSections()
{
std::vector<std::string> s;
for(std::map<std::string, std::vector<std::string> >::iterator it=sections.begin(); it!= sections.end(); it++)
{
s.push_back((*it).first);
}
return s;
}
// The actual map used to store key/value pairs
// Utility function to trim whitespace off the ends of a string
std::string trim(std::string source, std::string delims=" \t\r\n")
{
std::string result = source.erase(source.find_last_not_of(delims) + 1);
return result.erase(0, result.find_first_not_of(delims));
}
// Loads key/value pairs from a file
// Returns whether or not this operation was successful
std::vector<std::string> getkeys(std::string section)
{
std::vector<std::string> dummy;
SectionMap::iterator it = sections.find(section.c_str());
if(it!=sections.end())
return sections[section.c_str()]; // qu'est-ce que c'est
return dummy;
}
std::map<std::string, std::string> getmap(std::string section)
{
std::map<std::string, std::string> mapping;
if(sections.find(section) == sections.end())
return mapping;
std::vector<std::string> keys = sections[section];
for(int i=0; i< keys.size(); i++)
{
mapping[keys[i]] = GetSymbolValue(section + "." + keys[i]);
}
return mapping;
}
std::vector<std::string> getsection(std::string section)
{
std::vector<std::string> list;
if(sections.find(section) == sections.end())
return list;
std::vector<std::string> keys = sections[section];
for(int i=0; i< keys.size(); i++)
{
std::string value = GetSymbolValue(section + "." + keys[i]);
list.push_back(keys[i] + ":=" + value + "\n");
}
return list;
}
bool Config::load(const std::string filename)
{
std::string section;
std::string line;
std::string comment = "#";
std::string delimiter = "=";
std::ifstream file(filename.c_str());
if(!file.is_open())
return false;
while(!file.eof())
{
getline(file, line);
// Remove any comments
size_t commIdx = line.find(comment);
if(commIdx != std::string::npos)
line = line.substr(0, commIdx);
// This should only match [section], not a=b[3]
trim(line);
if(line.size() < 1)
continue;
size_t delimIdx = line.find("[");
if( (line.find("[") == 0) ) // && (line.rfind("]")==(line.size()-1)) ) // << BUG here
{
line = trim(line);
line = line.erase(line.find("]"));
line = line.erase(0, line.find("[")+1);
section=trim(line);
continue;
}
delimIdx = line.find(delimiter);
if(delimIdx == std::string::npos)
continue;
std::string key = trim(line.substr(0, delimIdx));
std::string value = trim(line.substr(delimIdx + 1));
sections[section].push_back(key);
if(!key.empty())
{
if(!section.empty())
key = section + "." + key;
inimap[key] = value ;
}
}
file.close();
return true;
}
// Use the [] operator to get/set values just like a map container
const StringVariant& Config::operator[](const std::string& keyName) const
{
std::map<std::string, StringVariant>::const_iterator iter = inimap.find(keyName);
if(iter != inimap.end())
return iter->second;
static StringVariant empty;
return empty;
}
private:
inline std::string &LeftTrim(std::string &str)
{
size_t startpos = str.find_first_not_of(" \t\r\n");
if( std::string::npos != startpos )
str = str.substr( startpos );
return str;
}
// trim from end
inline std::string &RightTrim(std::string &str, std::string trim=" \t\r\n")
{
size_t endpos = str.find_last_not_of(trim);
if(std::string::npos != endpos )
str = str.substr( 0, endpos+1 );
return str;
}
// trim from both ends
inline std::string &Trim(std::string &s)
{
return LeftTrim(RightTrim(s));
}
inline std::vector<std::string> Tokenize(const std::string& str,const std::string& delimiters)
{
std::vector<std::string> tokens;
std::string::size_type delimPos = 0, tokenPos = 0, pos = 0;
if(str.length()<1) return tokens;
while(1){
delimPos = str.find_first_of(delimiters, pos);
tokenPos = str.find_first_not_of(delimiters, pos);
if(std::string::npos != delimPos){
if(std::string::npos != tokenPos){
if(tokenPos<delimPos){
tokens.push_back(str.substr(pos,delimPos-pos));
}else{
tokens.push_back("");
}
}else{
tokens.push_back("");
}
pos = delimPos+1;
} else {
if(std::string::npos != tokenPos){
tokens.push_back(str.substr(pos));
} else {
tokens.push_back("");
}
break;
}
}
return tokens;
}
std::vector<std::string>TrimmedTokenize(std::string value, std::string delimiter)
{
std::vector<std::string>tokens = Tokenize(value, delimiter);
for(int i=0; i< tokens.size(); i++)
{
tokens[i] = Trim(tokens[i]);
}
return tokens;
}
public:
std::vector<std::string> GetTokens(std::string keyName, std::string delimiter)
{
std::string value = GetSymbolValue(keyName,"");
std::vector<std::string> tokens = TrimmedTokenize(value, delimiter);
for(int i=0; i< tokens.size(); i++)
{
if(tokens[i].empty())
{
tokens.erase(tokens.begin() + i);
i--;
}
Trim(tokens[i]);
}
return tokens;
}
StringVariant GetSymbolValue(std::string keyName, StringVariant szDefault=StringVariant())
{
std::map<std::string, StringVariant>::const_iterator iter = inimap.find(keyName);
if(iter != inimap.end())
return iter->second;
return szDefault;
}
bool IsSymbol(std::string keyName)
{
std::map<std::string, StringVariant>::const_iterator iter = inimap.find(keyName);
if(iter != inimap.end())
return true;
return false;
}
/* std::string Dump()
{
for(std::map<std::string, StringVariant>::iterator it = config.inimap.begin(); it != config.inimap.end(); it++)
{
GLogger << DBUG << (*it).first << " =" << (*it).second.c_str() << std::endl;
}
}*/
};
};
#endif