1 ////////////////////////////////////////////////////////////////////////////////
3 // Copyright 2006 - 2017, Paul Beckingham, Federico Hernandez.
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included
13 // in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // http://www.opensource.org/licenses/mit-license.php
25 ////////////////////////////////////////////////////////////////////////////////
28 #include <Configuration.h>
36 ////////////////////////////////////////////////////////////////////////////////
37 bool setVariableInFile (
38 const std::string& file,
39 const std::string& name,
40 const std::string& value)
42 // Read the file contents.
43 std::vector <std::string> contents;
44 File::read (file, contents);
49 for (auto& line : contents)
51 // If there is a comment on the line, it must follow the pattern.
52 auto comment = line.find ('#');
53 auto pos = line.find (name + '=');
55 if (pos != std::string::npos &&
56 (comment == std::string::npos ||
60 if (comment != std::string::npos)
61 line = name + '=' + value + ' ' + line.substr (comment);
63 line = name + '=' + value;
69 // Not found, so append instead.
72 contents.push_back (name + '=' + value);
77 File::write (file, contents);
82 ////////////////////////////////////////////////////////////////////////////////
83 bool unsetVariableInFile (
84 const std::string& file,
85 const std::string& name)
87 // Read configuration file.
88 std::vector <std::string> contents;
89 File::read (file, contents);
93 for (auto line = contents.begin (); line != contents.end (); )
95 bool lineDeleted = false;
97 // If there is a comment on the line, it must follow the pattern.
98 auto comment = line->find ('#');
99 auto pos = line->find (name + '=');
101 if (pos != std::string::npos &&
102 (comment == std::string::npos ||
105 // vector::erase method returns a valid iterator to the next object
106 line = contents.erase (line);
116 File::write (file, contents);
121 ////////////////////////////////////////////////////////////////////////////////
122 // Read the Configuration file and populate the *this map. The file format is
123 // simply lines with name=value pairs. Whitespace between name, = and value is
124 // not tolerated, but blank lines and comments starting with # are allowed.
126 // Nested files are now supported, with the following construct:
127 // include /absolute/path/to/file
129 void Configuration::load (const std::string& file, int nest /* = 1 */)
132 throw std::string ("Configuration files may only be nested to 10 levels.");
134 // Read the file, then parse the contents.
138 _original_file = config;
140 if (config.exists () &&
143 std::string contents;
144 if (File::read (file, contents) && contents.length ())
145 parse (contents, nest);
149 ////////////////////////////////////////////////////////////////////////////////
150 // Write the Configuration file.
151 void Configuration::save ()
153 std::string contents;
154 for (const auto& i : *this)
155 contents += i.first + "=" + i.second + '\n';
157 File::write (_original_file, contents);
161 ////////////////////////////////////////////////////////////////////////////////
162 void Configuration::parse (const std::string& input, int nest /* = 1 */)
164 // Shortcut case for default constructor.
165 if (input.length () == 0)
169 for (auto& line : split (input, '\n'))
172 auto pound = line.find ('#');
173 if (pound != std::string::npos)
174 line = line.substr (0, pound);
178 if (line.length () > 0)
180 auto equal = line.find ('=');
181 if (equal != std::string::npos)
183 std::string key = trim (line.substr (0, equal));
184 std::string value = trim (line.substr (equal+1, line.length () - equal));
186 (*this)[key] = json::decode (value);
190 auto include = line.find ("include");
191 if (include != std::string::npos)
193 Path included (trim (line.substr (include + 7)));
194 if (included.is_absolute ())
196 if (included.readable ())
197 load (included, nest + 1);
199 throw format ("Could not read include file '{1}'.", included._data);
202 throw format ("Can only include files with absolute paths, not '{1}'", included._data);
205 throw format ("Malformed entry '{1}' in config file.", line);
213 ////////////////////////////////////////////////////////////////////////////////
214 bool Configuration::has (const std::string& key) const
216 return (*this).find (key) != (*this).end ();
219 ////////////////////////////////////////////////////////////////////////////////
220 // Return the configuration value given the specified key.
221 std::string Configuration::get (const std::string& key) const
223 auto found = find (key);
225 return found->second;
230 ////////////////////////////////////////////////////////////////////////////////
231 int Configuration::getInteger (const std::string& key) const
233 auto found = find (key);
235 return strtoimax (found->second.c_str (), nullptr, 10);
240 ////////////////////////////////////////////////////////////////////////////////
241 double Configuration::getReal (const std::string& key) const
243 auto found = find (key);
245 return strtod (found->second.c_str (), nullptr);
250 ////////////////////////////////////////////////////////////////////////////////
251 bool Configuration::getBoolean (const std::string& key) const
253 auto found = find (key);
256 auto value = lowerCase (found->second);
257 if (value == "true" ||
268 ////////////////////////////////////////////////////////////////////////////////
269 void Configuration::set (const std::string& key, const int value)
271 (*this)[key] = format (value);
275 ////////////////////////////////////////////////////////////////////////////////
276 void Configuration::set (const std::string& key, const double value)
278 (*this)[key] = format (value, 1, 8);
282 ////////////////////////////////////////////////////////////////////////////////
283 void Configuration::set (const std::string& key, const std::string& value)
285 (*this)[key] = value;
289 ////////////////////////////////////////////////////////////////////////////////
290 // Autovivification is ok here.
291 void Configuration::setIfBlank (const std::string& key, const std::string& value)
293 if ((*this)[key] == "")
295 (*this)[key] = value;
300 ////////////////////////////////////////////////////////////////////////////////
301 // Provide a vector of all configuration keys.
302 std::vector <std::string> Configuration::all () const
304 std::vector <std::string> items;
305 for (const auto& it : *this)
306 items.push_back (it.first);
311 ////////////////////////////////////////////////////////////////////////////////
312 std::string Configuration::file () const
314 return _original_file._data;
317 ////////////////////////////////////////////////////////////////////////////////
318 bool Configuration::dirty ()
323 ////////////////////////////////////////////////////////////////////////////////