-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfiguration.h
290 lines (256 loc) · 9.7 KB
/
Configuration.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
#ifndef _libconfig_configuration_included_
#define _libconfig_configuration_included_
#include "Types.h"
#include "Parse.h"
#include "Printing.h"
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/regex.hpp>
#include <boost/format.hpp>
namespace libconfig {
// ==========================================================================
// Configuraiton class is the main interface to loading and parsing libconfig
// files.
class Configuration
{
private:
struct FormatValue : boost::static_visitor<std::string>
{
public:
// :: ---------------------------------------------------------------
// :: Construction
FormatValue(std::string formatString = "%1%")
: m_formatString(formatString)
{}
// :: ---------------------------------------------------------------
// :: Public Interface
template<typename T>
std::string operator()(T const& t) const
{
return boost::str( boost::format(m_formatString) % t );
}
template<typename T>
std::string operator()(std::vector<T> const& t) const
{
throw std::runtime_error("String reference pointing to a list.");
}
std::string operator()(ConfigType const& t) const
{
throw std::runtime_error("String reference pointing to a section.");
}
private:
// :: ---------------------------------------------------------------
// :: Members
std::string m_formatString;
};
public:
// :: -------------------------------------------------------------------
// :: Construction
Configuration(const ConfigType& configurationMap)
: m_configurationMap(configurationMap)
{}
Configuration(const std::string& configFilename)
: m_configurationMap(parse::parseConfigFile(configFilename))
{}
public:
// :: -------------------------------------------------------------------
// :: Public Interface
// Lookup a configuration item given the address and the value where the
// item will be stored. Returns 'true' or 'false' depending on if the
// item is found in the configuration.
template<typename T>
bool lookupValue(const std::string& address, T& value)
{
std::vector<std::string> keys;
boost::split(keys, address, boost::is_any_of("."));
return prv_lookupValue(m_configurationMap, value, keys);
}
void load(std::string configFilename)
{
m_configurationMap = parse::parseConfigFile(configFilename);
}
// Print the configuration to std::cout
void print()
{
printing::ConfigPrinter()(m_configurationMap);
}
private:
// :: ------------------------------------------------------------------
// :: Private Member Functions
// Retrieve a value from a map, this is used for the last key in the
// configuration address.
template<typename T>
bool prv_getValue(const ConfigType& subConfig,
const std::string& key, T& value)
{
if(subConfig.find(key) == subConfig.end())
return false;
try {
value = boost::get<T>(subConfig.find(key)->second);
}
catch(boost::bad_get e) {
throw std::runtime_error("Type requested does not match"
"the configuration item's type.");
}
return true;
}
// Specialization for lookupValue when the value type is a list, this is
// to handle empty lists.
template<typename T>
bool prv_getValue(const ConfigType& subConfig,
const std::string& key, std::vector<T>& value)
{
if(subConfig.find(key) == subConfig.end())
return false;
try {
value = boost::get<std::vector<T> >(
subConfig.find(key)->second);
}
catch(boost::bad_get) {
try {
boost::get<std::vector<boost::none_t> >(
subConfig.find(key)->second);
value = std::vector<T>();
}
catch(boost::bad_get) {
throw std::runtime_error("Type requested does not match"
"the configuration item's type.");
}
}
return true;
}
// Specialization for std::string values, this will look up any references
// in the string values.
bool prv_getValue(const ConfigType& subConfig,
const std::string& key, std::string& value)
{
if(subConfig.find(key) == subConfig.end())
return false;
try {
value = prv_resolveReferences(
boost::get<std::string>(subConfig.find(key)->second));
}
catch(boost::bad_get e) {
throw std::runtime_error("Type requested does not match"
"the configuration item's type.");
}
return true;
}
bool prv_getValueAsString(const ConfigType& subConfig,
const std::string& key, std::string& value)
{
if(subConfig.find(key) == subConfig.end())
return false;
value = boost::apply_visitor(FormatValue(), subConfig.find(key)->second);
return true;
}
// Recursive lookupValue function to traverse the configuration tree
// searching for the configuration item specified in the address.
template<typename T>
bool prv_lookupValue(const ConfigType& subConfig,
T& value, const std::vector<std::string>& keys,
size_t keysIdx = 0, bool convertToString = false)
{
// If the index is the last key in the configuration address, then get
// the value of the config item.
if(keys.size() == keysIdx + 1)
return convertToString
? prv_getValueAsString(subConfig, keys[keysIdx], value)
: prv_getValue(subConfig, keys[keysIdx], value);
// RECURSION: Lookup the key and attempt to access the next section.
if(subConfig.find(keys[keysIdx]) != subConfig.end())
{
try {
if(prv_lookupValue(
boost::get<ConfigType>(subConfig.find(keys[keysIdx])->second),
value, keys, keysIdx + 1))
{
return true;
}
}
catch(boost::bad_get e) {
throw std::runtime_error("The specified key is not a section");
}
}
// Check for the address in an #include_section.
boost::optional<std::vector<std::string> > includeSectionKeys =
prv_findIncludeSection(subConfig, keys[keysIdx]);
if(includeSectionKeys)
{
return prv_lookupValue(m_configurationMap, value,
prv_combineKeys(keys, keysIdx + 1, *includeSectionKeys));
}
else
{
return false;
}
}
// Look for the give key in the #include_section.
boost::optional<std::vector<std::string> >
prv_findIncludeSection(const ConfigType& subConfig,
const std::string& key)
{
if(subConfig.find("$references") != subConfig.end())
{
std::string includeSection;
std::vector<std::string> includeSectionKeys;
if(prv_lookupValue(
boost::get<ConfigType>(subConfig.find("$references")->second),
includeSection, std::vector<std::string>(1, key)))
{
return boost::optional<std::vector<std::string> >(
boost::split(includeSectionKeys,
includeSection,
boost::is_any_of(".")));
}
else
{
return boost::none;
}
}
return boost::none;
}
// Combine the keys from the two addresses.
std::vector<std::string>
prv_combineKeys(std::vector<std::string> keys1,
size_t keys1Idx, std::vector<std::string> keys2)
{
keys2.insert(keys2.end(), keys1.begin() + keys1Idx, keys1.end());
return keys2;
}
// resolve any of the references found the string value.
std::string prv_resolveReferences(std::string value)
{
boost::regex referencePattern("\\$\\{([\\w\\.]*)\\}");
boost::sregex_iterator it(value.begin(), value.end(), referencePattern);
boost::sregex_iterator end;
for(/**/; it != end; ++it)
{
std::string address((*it)[1].first, (*it)[1].second);
std::string resolvedValue;
std::vector<std::string> keys;
boost::split(keys, address, boost::is_any_of("."));
if(not prv_lookupValue(
m_configurationMap, resolvedValue, keys, 0, true))
{
char* env;
env = std::getenv(address.c_str());
if(env == NULL) {
throw std::runtime_error(boost::str(boost::format(
"Unable to resolve reference '%1%' in string value.")
% address));
}
resolvedValue = env;
}
value.replace(it->position(), it->length(), resolvedValue);
}
return value;
}
private:
// :: ------------------------------------------------------------------
// :: Members
ConfigType m_configurationMap;
};
} // namespace libconfig
#endif // _libconfig_configuration_included_