Newer
Older
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
//============================================================================//
// A class based on the support from ConfigParser and ConfigValue //
// It provides a simple way to read text file as configuration file, and read //
// or modify a parameter in the inherited class //
// The Configure() function should be overloaded according to specialized //
// requirements, and be called after the parameters being configured //
// //
// Chao Peng //
// 10/31/2016 //
//============================================================================//
#include <fstream>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include "ConfigObject.h"
//============================================================================//
// Constructor, Destructor //
//============================================================================//
// constructor
ConfigObject::ConfigObject(const std::string &splitter, const std::string &ignore, bool case_ins)
: split_chars(splitter), ignore_chars(ignore), case_insensitive(case_ins), __empty_value("")
{
// set default replace bracket
replace_pair = std::make_pair("{", "}");
}
// destructor
ConfigObject::~ConfigObject()
{
// place holder
}
//============================================================================//
// Public Member Function //
//============================================================================//
// configure the cluster method
void ConfigObject::Configure(const std::string &path)
{
// save the path
config_path = path;
// clear the map
config_map.clear();
// read configuration file in
ReadConfigFile(path);
}
// clear all the loaded configuration values
void ConfigObject::ClearConfig()
{
config_path = "";
config_map.clear();
}
// read configuration file and build the configuration map
bool ConfigObject::ReadConfigFile(const std::string &path)
{
ConfigParser c_parser;
c_parser.SetSplitters(split_chars); // self-defined splitters
if (c_parser.ReadFile(path)) {
// current directory
parserProcess(c_parser, path);
return true;
} else {
std::cerr << "Cannot open configuration file "
<< "\"" << path << "\""
<< std::endl;
return false;
}
}
// read the configuration string directly
void ConfigObject::ReadConfigString(const std::string &content)
{
ConfigParser c_parser;
c_parser.SetSplitters(split_chars);
c_parser.ReadBuffer(content.c_str());
parserProcess(c_parser, "buffer_string");
}
// continue parse the terms
void ConfigObject::parserProcess(ConfigParser &c_parser, const std::string &source)
{
std::string cur_dir = ConfigParser::decompose_path(source).dir;
while (c_parser.ParseLine()) {
// possible control words
if (c_parser.NbofElements() == 1) {
std::string control = c_parser.TakeFirst();
size_t pos = control.find("{THIS_DIR}");
if(pos != std::string::npos)
control.replace(pos, 10, cur_dir);
parseControl(control);
// var_name and var_value
} else if (c_parser.NbofElements() == 2) {
std::string var_name, key, var_value;
c_parser >> var_name >> var_value;
size_t pos = var_value.find("{THIS_DIR}");
if(pos != std::string::npos)
var_value.replace(pos, 10, cur_dir);
parseTerm(std::move(var_name), std::move(var_value));
// unsupported format
} else {
std::cout << "Warning: Unsupported format at line "
<< c_parser.LineNumber() << " from " << source << "\n"
<< "\"" << c_parser.CurrentLine() << "\""
<< std::endl;
}
}
}
// check if a certain term is configured
bool ConfigObject::HasKey(const std::string &var_name)
const
{
auto key = formKey(var_name);
if (config_map.find(key) != config_map.end())
return true;
return false;
}
// list all the existing configuration keys
void ConfigObject::ListKeys()
const
{
for (auto &it : config_map) {
std::cout << it.first << std::endl;
}
}
// get all the existing configuration keys
std::vector<std::string> ConfigObject::GetKeyList()
const
{
std::vector<std::string> res;
for (auto &it : config_map) {
res.push_back(it.first);
}
return res;
}
// save current configuration into a file
void ConfigObject::SaveConfig(const std::string &path)
const
{
std::string save_path;
if (path.empty())
save_path = config_path;
if(save_path.empty())
save_path = "latest.conf";
std::ofstream save(save_path);
for (auto &it : config_map) {
save << it.first
<< " " << split_chars.front() << " "
<< it.second
<< std::endl;
}
}
// get configuration value by its name/key
ConfigValue ConfigObject::GetConfigValue(const std::string &var_name)
const
{
// convert to lower case and remove uninterested characters
auto key = formKey(var_name);
auto it = config_map.find(key);
if (it == config_map.end()) {
return __empty_value;
} else {
ConfigValue result(it->second);
reform(result._value, replace_pair.first, replace_pair.second);
return result;
}
}
// set configuration value by its name/key
void ConfigObject::SetConfigValue(const std::string &var_name, const ConfigValue &c_value)
{
// convert to lower case and remove uninterested characters
auto key = formKey(var_name);
config_map[key] = c_value;
}
// get configuration value from the map
// if no such config value exists, it will fill the default value in
ConfigValue ConfigObject::GetConfigValue(const std::string &var_name, const ConfigValue &def_value, bool verbose)
{
auto key = formKey(var_name);
auto it = config_map.find(key);
if (it == config_map.end()) {
if (def_value.IsEmpty())
return __empty_value;
if (verbose) {
std::cout << var_name << " (key: " << key << ")"
<< " not defined in configuration file, set to default value "
<< def_value
<< std::endl;
}
config_map[key] = def_value;
return def_value;
}
ConfigValue result(it->second);
reform(result._value, replace_pair.first, replace_pair.second);
return result;
}
//============================================================================//
// Protected Member Function //
//============================================================================//
// build the key
std::string ConfigObject::formKey(const std::string &rawKey)
const
{
std::string key = ConfigParser::str_remove(rawKey, ignore_chars);
if (case_insensitive) {
key = ConfigParser::str_lower(key);
}
return key;
}
// replace the contents inside replace_pair with the configuration value
void ConfigObject::reform(std::string &input, const std::string &op, const std::string &cl)
const
{
// loop until no pair found
while (true) {
auto rpair = ConfigParser::find_pair(input, op, cl);
if (rpair.first != std::string::npos && rpair.second != std::string::npos) {
// get content inside the bracket
std::string var = input.substr(rpair.first + op.size(),
rpair.second - op.size() - rpair.first);
// deal with nested structure
reform(var, op, cl);
// replace content
std::string val;
// environment variable
if (rpair.first > 0 && input.at(rpair.first - 1) == '$') {
val = std::getenv(var.c_str());
// replace $ mark also
rpair.first--;
// ConfigObject variable
} else {
val = GetConfigValue(var)._value;
}
// replace variable with configuration value
input.replace(rpair.first, rpair.second - rpair.first + cl.size(), val);
} else {
// no pair found any more
return;
}
}
}
//============================================================================//
// Private Member Function //
//============================================================================//
// parse the control word and respond
void ConfigObject::parseControl(const std::string &word)
{
if (ConfigParser::str_upper(word.substr(0, 7)) == "INCLUDE") {
// need the most outer pair
auto p = ConfigParser::find_pair(word, "(", ")");
// not find pair
if (p.first == std::string::npos || p.second == std::string::npos) {
std::cout << "Unsupported control word format: " << word << "."
<< "Expected: INCLUDE(path)"
<< std::endl;
return;
}
int begin = p.first + 1;
int length = p.second - begin;
std::string new_path = word.substr(begin, length);
reform(new_path, replace_pair.first, replace_pair.second);
ReadConfigFile(new_path);
}
else {
std::cout << "Unsupported control word: " << word << std::endl;
}
}
// parse the configuration term
void ConfigObject::parseTerm(std::string &&var_name, std::string &&var_value)
{
// convert to lower case and remove uninterested characters
auto key = formKey(var_name);
if (key.back() == '+') {
key.pop_back();
auto it = config_map.find(key);
if (it != config_map.end())
it->second += var_value;
else
config_map[key] = var_value;
} else {
config_map[key] = var_value;
}
}