-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConfigFile.cpp
81 lines (60 loc) · 3.03 KB
/
ConfigFile.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
/*
Copyright 2018 Yatish Turakhia, Gill Bejerano and William Dally
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.
*/
#include "ConfigFile.h"
#include <fstream>
std::string trim(std::string const& source, char const* delims = " \t\r\n") {
std::string result(source);
std::string::size_type index = result.find_last_not_of(delims);
if(index != std::string::npos)
result.erase(++index);
index = result.find_first_not_of(delims);
if(index != std::string::npos)
result.erase(0, index);
else
result.erase();
return result;
}
ConfigFile::ConfigFile(std::string const& configFile) {
std::ifstream file(configFile.c_str());
std::string line;
std::string name;
std::string value;
std::string inSection;
int posEqual;
while (std::getline(file,line)) {
if (! line.length()) continue;
if (line[0] == '#') continue;
if (line[0] == ';') continue;
if (line[0] == '[') {
inSection=trim(line.substr(1,line.find(']')-1));
continue;
}
posEqual=line.find('=');
name = trim(line.substr(0,posEqual));
value = trim(line.substr(posEqual+1));
content_[inSection+'/'+name]=Chameleon(value);
}
}
Chameleon const& ConfigFile::Value(std::string const& section, std::string const& entry) const {
std::map<std::string,Chameleon>::const_iterator ci = content_.find(section + '/' + entry);
if (ci == content_.end()) throw "does not exist";
return ci->second;
}
Chameleon const& ConfigFile::Value(std::string const& section, std::string const& entry, double value) {
try {
return Value(section, entry);
} catch(const char *) {
return content_.insert(std::make_pair(section+'/'+entry, Chameleon(value))).first->second;
}
}
Chameleon const& ConfigFile::Value(std::string const& section, std::string const& entry, std::string const& value) {
try {
return Value(section, entry);
} catch(const char *) {
return content_.insert(std::make_pair(section+'/'+entry, Chameleon(value))).first->second;
}
}