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