-
Notifications
You must be signed in to change notification settings - Fork 93
/
config.h
295 lines (244 loc) · 6.72 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
#ifndef _CONFIG_H
#define _CONFIG_H
#include <set>
#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <cassert>
//see the user's guide at the bottom of the file
class CONFIG
{
public:
CONFIG();
CONFIG(std::string fname);
~CONFIG();
bool Load(std::string fname);
// will fail to process include directives
bool Load(std::istream & f);
void Clear();
void DebugPrint(std::ostream & out, bool with_brackets = true) const;
bool Write(bool with_brackets = true) const;
bool Write(bool with_brackets, std::string save_as) const;
void SuppressError(bool newse) {SUPPRESS_ERROR = newse;}
const std::string & GetName() const {return filename;}
typedef std::map<std::string, std::string> SECTION;
typedef std::map<std::string, SECTION> SECTIONMAP;
typedef SECTIONMAP::const_iterator const_iterator;
typedef SECTIONMAP::iterator iterator;
const_iterator begin() const {return sections.begin();}
const_iterator end() const {return sections.end();}
size_t size() const {return sections.size();}
bool GetSection(const std::string & section, const_iterator & it) const
{
it = sections.find(section);
if (it != sections.end())
{
return true;
}
return false;
}
/// will create section if not available
void GetSection(const std::string & section, iterator & it)
{
it = sections.insert(std::pair<const std::string, SECTION>(section, SECTION())).first;
}
bool GetSection(const std::string & section, const_iterator & it, std::ostream & error_output) const
{
if (!GetSection(section, it))
{
error_output << "Couldn't get section \"" << section << "\" from \"" << filename << "\"" << std::endl;
return false;
}
return true;
}
template <typename T>
bool GetParam(const const_iterator & section, const std::string & param, T & output) const
{
assert(section != sections.end());
SECTION::const_iterator i = section->second.find(param);
if (i != section->second.end())
{
std::stringstream st(i->second);
st >> std::boolalpha >> output;
return true;
}
return false;
}
template <typename T>
bool GetParam(const const_iterator & section, const std::string & param, std::vector<T> & out) const
{
assert(section != sections.end());
SECTION::const_iterator i = section->second.find(param);
if (i != section->second.end())
{
std::stringstream st(i->second);
if (out.size() > 0)
{
// set vector
for (size_t i = 0; i < out.size() && !st.eof(); ++i)
{
std::string str;
std::getline(st, str, ',');
std::stringstream s(str);
s >> out[i];
}
}
else
{
// fill vector
while (!st.eof())
{
std::string str;
std::getline(st, str, ',');
std::stringstream s(str);
T value;
s >> value;
out.push_back(value);
}
}
return true;
}
return false;
}
template <typename T>
bool GetParam(const const_iterator & section, const std::string & param, T & output, std::ostream & error_output) const
{
assert(section != sections.end());
if (!GetParam(section, param, output))
{
error_output << "Couldn't get parameter \"" << section->first << "." << param << "\" from \"" << filename << "\"" << std::endl;
return false;
}
return true;
}
template <typename T>
bool GetParam(const std::string & section, const std::string & param, T & output) const
{
const_iterator it = end();
return GetSection(section, it) && GetParam(it, param, output);
}
template <typename T>
bool GetParam(const std::string & section, const std::string & param, T & output, std::ostream & error_output) const
{
const_iterator it = end();
return GetSection(section, it, error_output) && GetParam(it, param, output, error_output);
}
/// will create param if not available
template <typename T>
void SetParam(iterator section, const std::string & param, const T & invar)
{
if (section != sections.end())
{
std::stringstream st;
st << std::boolalpha << invar;
section->second[param] = st.str();
}
}
template <typename T>
void SetParam(iterator section, const std::string & param, const std::vector<T> & invar)
{
if (section != sections.end())
{
std::stringstream st;
typename std::vector<T>::const_iterator it = invar.begin();
while (it < invar.end() - 1)
{
st << *it++ << ",";
}
st << *it;
section->second[param] = st.str();
}
}
template <typename T>
void SetParam(const std::string & section, const std::string & param, const T & invar)
{
iterator it;
GetSection(section, it);
SetParam(it, param, invar);
}
private:
SECTIONMAP sections;
std::set<std::string> include;
std::string filename;
bool SUPPRESS_ERROR;
bool ProcessLine(CONFIG::iterator & section, std::string & linestr);
};
// specializations
template <>
inline bool CONFIG::GetParam(const const_iterator & section, const std::string & param, std::string & output) const
{
assert(section != sections.end());
SECTION::const_iterator i = section->second.find(param);
if (i != section->second.end())
{
output = i->second;
return true;
}
return false;
}
template <>
inline bool CONFIG::GetParam(const const_iterator & section, const std::string & param, bool & output) const
{
assert(section != sections.end());
SECTION::const_iterator i = section->second.find(param);
if (i != section->second.end())
{
output = false;
if (i->second == "1")
output = true;
else if (i->second == "true")
output = true;
else if (i->second == "on")
output = true;
return true;
}
return false;
}
template <>
inline void CONFIG::SetParam(iterator section, const std::string & param, const std::string & invar)
{
if (section != sections.end())
{
section->second[param] = invar;
}
}
#endif /* _CONFIG_H */
/* USER GUIDE
Paste the file included below somewhere, then run this code:
CONFIG testconfig("/home/joe/.vdrift/test.cfg");
testconfig.DebugPrint();
string tstr = "notfound";
cout << "!!! test vectors: " << endl;
testconfig.GetParam("variable outside of", tstr);
cout << tstr << endl;
tstr = "notfound";
testconfig.GetParam(".variable outside of", tstr);
cout << tstr << endl;
float vec[3];
testconfig.GetParam("what about.even vectors", vec);
cout << vec[0] << "," << vec[1] << "," << vec[2] << endl;
your output should be the debug print of all sections, then:
!!! test vectors:
a section
a section
2.1,0.9,0
*/
/* EXAMPLE (not a good example -- tons of errors -- to demonstrate robustness)
#comment on the FIRST LINE??
variable outside of=a section
test section numero UNO
look at me = 23.4
i'm so great= BANANA
#break!
[ section dos??]
why won't you = breeeak #trying to break it
what about ] # this malformed thing???
nope works = fine.
even vectors = 2.1,0.9,GAMMA
this is a duplicate = 0
this is a duplicate = 1
random = intermediary
this is a duplicate = 2
*/