forked from GothenburgBitFactory/libshared
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfiguration.cpp
366 lines (314 loc) · 10.3 KB
/
Configuration.cpp
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2006 - 2021, Paul Beckingham, Federico Hernandez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// http://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////
#include <cmake.h>
#include <Configuration.h>
#include <inttypes.h>
#include <stdlib.h>
#include <FS.h>
#include <JSON.h>
#include <shared.h>
#include <format.h>
////////////////////////////////////////////////////////////////////////////////
bool setVariableInFile (
const std::string& file,
const std::string& name,
const std::string& value)
{
// Read the file contents.
std::vector <std::string> contents;
File::read (file, contents);
bool found = false;
bool change = false;
for (auto& line : contents)
{
// If there is a comment on the line, it must follow the pattern.
auto comment = line.find ('#');
auto pos = line.find (name + '=');
if (pos != std::string::npos &&
(comment == std::string::npos ||
comment > pos))
{
found = true;
if (comment != std::string::npos)
line = name + '=' + value + ' ' + line.substr (comment);
else
line = name + '=' + value;
change = true;
}
}
// Not found, so append instead.
if (! found)
{
contents.push_back (name + '=' + value);
change = true;
}
if (change)
File::write (file, contents);
return change;
}
////////////////////////////////////////////////////////////////////////////////
bool unsetVariableInFile (
const std::string& file,
const std::string& name)
{
// Read configuration file.
std::vector <std::string> contents;
File::read (file, contents);
bool change = false;
for (auto line = contents.begin (); line != contents.end (); )
{
bool lineDeleted = false;
// If there is a comment on the line, it must follow the pattern.
auto comment = line->find ('#');
auto pos = line->find (name + '=');
if (pos != std::string::npos &&
(comment == std::string::npos ||
comment > pos))
{
// vector::erase method returns a valid iterator to the next object
line = contents.erase (line);
lineDeleted = true;
change = true;
}
if (! lineDeleted)
line++;
}
if (change)
File::write (file, contents);
return change;
}
////////////////////////////////////////////////////////////////////////////////
// Read the Configuration file and populate the *this map. The file format is
// simply lines with name=value pairs. Whitespace between name, = and value is
// not tolerated, but blank lines and comments starting with # are allowed.
//
// Nested files are now supported, with the following construct:
// include /absolute/path/to/file
//
void Configuration::load (
const std::string& file,
int nest /* = 1 */,
const std::vector <std::string>& search_paths /* = {} */)
{
if (nest > 10)
throw std::string ("Configuration files may only be nested to 10 levels.");
// Read the file, then parse the contents.
File config (file);
if (nest == 1)
_original_file = config;
if (config.exists () &&
config.readable ())
{
std::string contents;
if (File::read (file, contents) && contents.length ())
parse (contents, nest, search_paths, file);
}
}
////////////////////////////////////////////////////////////////////////////////
// Write the Configuration file.
void Configuration::save ()
{
std::string contents;
for (const auto& i : *this)
contents += i.first + "=" + i.second + '\n';
File::write (_original_file, contents);
_dirty = false;
}
////////////////////////////////////////////////////////////////////////////////
void Configuration::parse (
const std::string& input,
int nest /* = 1 */,
const std::vector <std::string>& search_paths /* = {} */,
const std::string& file_path /* = {} */)
{
// Shortcut case for default constructor.
if (input.length () == 0)
return;
// Parse each line.
for (auto& line : split (input, '\n'))
{
// Remove comments.
auto pound = line.find ('#');
if (pound != std::string::npos)
line = line.substr (0, pound);
// Skip empty lines.
line = trim (line);
if (line.length () > 0)
{
auto equal = line.find ('=');
if (equal != std::string::npos)
{
std::string key = trim (line.substr (0, equal));
std::string value = trim (line.substr (equal+1, line.length () - equal));
(*this)[key] = json::decode (value);
}
else
{
auto include = line.find ("include");
if (include != std::string::npos)
{
Path included (trim (line.substr (include + 7)));
do
{
// 0. Absolute path is not searched.
if (included.is_absolute ())
break;
// 1. Try relative to CWD first, break if exists. This is the legacy behavior.
if (included.exists ())
break;
// 2. Try path relative to the config file itself.
if (!file_path.empty ())
{
std::string file_dir = Path(file_path).realpath();
auto slash = file_dir.rfind('/');
file_dir.resize (slash != std::string::npos ? slash + 1 : 0); // `/` is kept.
Path file_relative (file_dir + included._data);
if (file_relative.exists ())
{
included = file_relative;
break;
}
}
// 3. Try search paths.
for (auto &search: search_paths)
{
Path concated (search + "/" + included._data);
if (concated.exists ()) {
included = concated;
break;
}
}
if (!included.exists ())
throw format (
"Could not find file in CWD, directory of config file or search paths '{1}'.",
included._data);
}
while (0);
if (!included.readable ())
throw format ("Could not read include file '{1}'.", included._data);
load (included, nest + 1, search_paths);
}
else
throw format ("Malformed entry '{1}' in config file.", line);
}
}
}
_dirty = true;
}
////////////////////////////////////////////////////////////////////////////////
bool Configuration::has (const std::string& key) const
{
return (*this).find (key) != (*this).end ();
}
////////////////////////////////////////////////////////////////////////////////
// Return the configuration value given the specified key.
std::string Configuration::get (const std::string& key) const
{
auto found = find (key);
if (found != end ())
return found->second;
return "";
}
////////////////////////////////////////////////////////////////////////////////
int Configuration::getInteger (const std::string& key) const
{
auto found = find (key);
if (found != end ())
return strtoimax (found->second.c_str (), nullptr, 10);
return 0;
}
////////////////////////////////////////////////////////////////////////////////
double Configuration::getReal (const std::string& key) const
{
auto found = find (key);
if (found != end ())
return strtod (found->second.c_str (), nullptr);
return 0.0;
}
////////////////////////////////////////////////////////////////////////////////
bool Configuration::getBoolean (const std::string& key) const
{
auto found = find (key);
if (found != end ())
{
auto value = lowerCase (found->second);
if (value == "true" ||
value == "1" ||
value == "y" ||
value == "yes" ||
value == "on")
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
void Configuration::set (const std::string& key, const int value)
{
(*this)[key] = format (value);
_dirty = true;
}
////////////////////////////////////////////////////////////////////////////////
void Configuration::set (const std::string& key, const double value)
{
(*this)[key] = format (value, 1, 8);
_dirty = true;
}
////////////////////////////////////////////////////////////////////////////////
void Configuration::set (const std::string& key, const std::string& value)
{
(*this)[key] = value;
_dirty = true;
}
////////////////////////////////////////////////////////////////////////////////
// Autovivification is ok here.
void Configuration::setIfBlank (const std::string& key, const std::string& value)
{
if ((*this)[key] == "")
{
(*this)[key] = value;
_dirty = true;
}
}
////////////////////////////////////////////////////////////////////////////////
// Provide a vector of all configuration keys.
std::vector <std::string> Configuration::all () const
{
std::vector <std::string> items;
for (const auto& it : *this)
items.push_back (it.first);
return items;
}
////////////////////////////////////////////////////////////////////////////////
std::string Configuration::file () const
{
return _original_file._data;
}
////////////////////////////////////////////////////////////////////////////////
bool Configuration::dirty ()
{
return _dirty;
}
////////////////////////////////////////////////////////////////////////////////