-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.cpp
74 lines (66 loc) · 1.99 KB
/
config.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
#include "config.h"
using std::string;
Config::Config(): _cf(ConfigFile()){}
/**
* @brief Load configuration from a comma separated string.
* @param conf a string containing key=value pairs separated by commas.
*/
void Config::loadFromString(string conf){
// lazy solution... paying this copy and replace cost once per parameter combo...
// TODO: test if this is affordable
std::replace( conf.begin(), conf.end(), ',', '\n'); // replace all commas with newlines... now this should be a format ConfigFile likes
std::stringstream tmp;
tmp << conf;
_cf = ConfigFile();
tmp >> _cf;
}
/**
* @brief Load configuration from INI-style file
* @details Relying on ConfigFile for all the heavy lifting.
* @param filename name of config file
* @param delimiter delimiter between keys and values (default =)
* @param comment comment character (default #)
* @param sentry end of config marker (default "EndOfFile")
*/
void Config::loadFromFile( string filename, string delimiter, string comment, string sentry){
_cf = ConfigFile(filename, delimiter, comment, sentry);
}
/**
* @brief Save configuration to an INI file.
*/
void Config::save(std::string filename){
std::ofstream f;
std::stringstream tmp;
tmp << _cf;
f.open(filename);
f << tmp.str();
f.close();
}
/**
* @brief Delete a key in config.
*/
void Config::unset(const string & key){
#ifndef DISABLE_ERROR_CHECKS
if (!_cf.keyExists(key)) throw fatal_error() << "Trying to delete a key that doesn't exist! ("<<key << ")";
#endif
_cf.remove(key);
}
/**
* @brief Dump a CSV representation of the config.
* @return "key=value,key=value"
*/
std::string Config::stringRepr(){
std::stringstream tmp;
tmp << _cf;
std::string out = tmp.str();
std::replace(out.begin(), out.end(), '\n', ',');
return out;
}
/**
* @brief Check if this key is set.
* @details Useful for throwing informative errors (rather than
* relying on Config throwing).
*/
bool Config::keyExists(const string & key) const{
return _cf.keyExists(key);
}