1 |
torben |
146 |
#include "ConfigFile.h" |
2 |
|
|
|
3 |
|
|
#include <fstream> |
4 |
|
|
|
5 |
|
|
const char SEPERATOR = '/'; |
6 |
|
|
|
7 |
|
|
std::string trim(std::string const& source, char const* delims = " \t\r\n") |
8 |
|
|
{ |
9 |
|
|
std::string result(source); |
10 |
|
|
std::string::size_type index = result.find_last_not_of(delims); |
11 |
|
|
if (index != std::string::npos) |
12 |
|
|
result.erase(++index); |
13 |
|
|
|
14 |
|
|
index = result.find_first_not_of(delims); |
15 |
|
|
if (index != std::string::npos) |
16 |
|
|
result.erase(0, index); |
17 |
|
|
else |
18 |
|
|
result.erase(); |
19 |
|
|
return result; |
20 |
|
|
} |
21 |
|
|
|
22 |
|
|
ConfigFile::ConfigFile() |
23 |
|
|
{ |
24 |
|
|
} |
25 |
|
|
|
26 |
|
|
ConfigFile::ConfigFile(std::string const& configFile) |
27 |
|
|
{ |
28 |
|
|
Open(configFile); |
29 |
|
|
} |
30 |
|
|
|
31 |
|
|
bool ConfigFile::Open(std::string const& configFile) |
32 |
|
|
{ |
33 |
|
|
std::ifstream file(configFile.c_str()); |
34 |
|
|
|
35 |
|
|
std::string line; |
36 |
|
|
std::string name; |
37 |
|
|
std::string value; |
38 |
|
|
std::string inSection; |
39 |
|
|
int posEqual; |
40 |
|
|
|
41 |
|
|
if (!file) |
42 |
|
|
{ |
43 |
|
|
return false; |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
while (std::getline(file,line)) |
47 |
|
|
{ |
48 |
|
|
|
49 |
|
|
if (! line.length()) continue; |
50 |
|
|
|
51 |
|
|
if (line[0] == '#') continue; |
52 |
|
|
if (line[0] == ';') continue; |
53 |
|
|
|
54 |
|
|
if (line[0] == '[') |
55 |
|
|
{ |
56 |
|
|
inSection=trim(line.substr(1,line.find(']')-1)); |
57 |
|
|
continue; |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
posEqual=line.find('='); |
61 |
|
|
name = trim(line.substr(0,posEqual)); |
62 |
|
|
value = trim(line.substr(posEqual+1)); |
63 |
|
|
|
64 |
|
|
|
65 |
|
|
content_[inSection+SEPERATOR+name]=Value(value); |
66 |
|
|
} |
67 |
|
|
return true; |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
Value const& ConfigFile::GetValue(std::string const& section, std::string const& entry) const |
71 |
|
|
{ |
72 |
|
|
|
73 |
|
|
std::map<std::string,Value>::const_iterator ci = content_.find(section + SEPERATOR + entry); |
74 |
|
|
|
75 |
|
|
if (ci == content_.end()) throw "does not exist"; |
76 |
|
|
|
77 |
|
|
return ci->second; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
Value const& ConfigFile::GetValue(std::string const& section, std::string const& entry, double value) |
81 |
|
|
{ |
82 |
|
|
try |
83 |
|
|
{ |
84 |
|
|
return GetValue(section, entry); |
85 |
|
|
} |
86 |
|
|
catch (const char *) |
87 |
|
|
{ |
88 |
|
|
return content_.insert(std::make_pair(section+SEPERATOR+entry, Value(value))).first->second; |
89 |
|
|
} |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
Value const& ConfigFile::GetValue(std::string const& section, std::string const& entry, std::string const& value) |
93 |
|
|
{ |
94 |
|
|
try |
95 |
|
|
{ |
96 |
|
|
return GetValue(section, entry); |
97 |
|
|
} |
98 |
|
|
catch (const char *) |
99 |
|
|
{ |
100 |
|
|
return content_.insert(std::make_pair(section+SEPERATOR+entry, Value(value))).first->second; |
101 |
|
|
} |
102 |
|
|
} |