1 |
#include "ConfigFile.h" |
2 |
#include "Util.h" |
3 |
|
4 |
#include <fstream> |
5 |
#include <sstream> |
6 |
#include <stdexcept> |
7 |
|
8 |
#include <stdlib.h> |
9 |
|
10 |
const char SEPERATOR = '/'; |
11 |
|
12 |
std::string trim(std::string const& source, char const* delims = " \t\r\n") |
13 |
{ |
14 |
std::string result(source); |
15 |
std::string::size_type index = result.find_last_not_of(delims); |
16 |
if (index != std::string::npos) |
17 |
result.erase(++index); |
18 |
|
19 |
index = result.find_first_not_of(delims); |
20 |
if (index != std::string::npos) |
21 |
result.erase(0, index); |
22 |
else |
23 |
result.erase(); |
24 |
return result; |
25 |
} |
26 |
|
27 |
ConfigFile::ConfigFile() |
28 |
{ |
29 |
} |
30 |
|
31 |
ConfigFile::ConfigFile(std::string const& configFile) |
32 |
{ |
33 |
Open(configFile); |
34 |
} |
35 |
|
36 |
bool ConfigFile::Open(std::string const& configFile) |
37 |
{ |
38 |
std::ifstream file(configFile.c_str()); |
39 |
|
40 |
std::string line; |
41 |
std::string name; |
42 |
std::string value; |
43 |
std::string inSection; |
44 |
int posEqual; |
45 |
|
46 |
if (!file) |
47 |
{ |
48 |
return false; |
49 |
} |
50 |
|
51 |
while (std::getline(file,line)) |
52 |
{ |
53 |
|
54 |
if (! line.length()) continue; |
55 |
|
56 |
if (line[0] == '#') continue; |
57 |
if (line[0] == ';') continue; |
58 |
|
59 |
if (line[0] == '[') |
60 |
{ |
61 |
inSection=trim(line.substr(1,line.find(']')-1)); |
62 |
continue; |
63 |
} |
64 |
|
65 |
posEqual=line.find('='); |
66 |
name = trim(line.substr(0,posEqual)); |
67 |
value = trim(line.substr(posEqual+1)); |
68 |
|
69 |
content_.insert(std::make_pair(inSection+SEPERATOR+name,Value(value))); |
70 |
} |
71 |
return true; |
72 |
} |
73 |
|
74 |
Value const& ConfigFile::GetValue(std::string const& section, std::string const& entry) const |
75 |
{ |
76 |
|
77 |
content_iterator ci = content_.find(section + SEPERATOR + entry); |
78 |
|
79 |
if (ci == content_.end()) throw "does not exist"; |
80 |
|
81 |
return ci->second; |
82 |
} |
83 |
|
84 |
std::vector<Value> ConfigFile::GetValues(std::string const& section, std::string const& entry) const |
85 |
{ |
86 |
std::vector<Value> values; |
87 |
std::string search = section + SEPERATOR + entry; |
88 |
std::pair<content_iterator, content_iterator> range = content_.equal_range(search); |
89 |
|
90 |
for (content_iterator it=range.first; it != range.second; ++it) |
91 |
{ |
92 |
values.push_back( it->second ); |
93 |
} |
94 |
return values; |
95 |
} |
96 |
|
97 |
Value const& ConfigFile::GetValue(std::string const& section, std::string const& entry, double value) |
98 |
{ |
99 |
try |
100 |
{ |
101 |
return GetValue(section, entry); |
102 |
} |
103 |
catch (const char *) |
104 |
{ |
105 |
return content_.insert(std::make_pair(section+SEPERATOR+entry, Value(value)))->second; |
106 |
} |
107 |
} |
108 |
|
109 |
Value const& ConfigFile::GetValue(std::string const& section, std::string const& entry, std::string const& value) |
110 |
{ |
111 |
try |
112 |
{ |
113 |
return GetValue(section, entry); |
114 |
} |
115 |
catch (const char *) |
116 |
{ |
117 |
return content_.insert(std::make_pair(section+SEPERATOR+entry, Value(value)))->second; |
118 |
} |
119 |
} |
120 |
|
121 |
std::string ConfigFile::DumpConfig() const |
122 |
{ |
123 |
std::ostringstream ss; |
124 |
for (content_iterator it=content_.begin(); it!=content_.end(); ++it) |
125 |
{ |
126 |
ss << it->first << "=" << it->second.StringValue() << std::endl; |
127 |
} |
128 |
return ss.str(); |
129 |
} |
130 |
|
131 |
|
132 |
namespace ConfigHelper |
133 |
{ |
134 |
std::map<std::string, std::string> ParseArguments(const std::string& args) |
135 |
{ |
136 |
std::map<std::string, std::string> config; |
137 |
|
138 |
std::vector<std::string> sections = Util::str_split(args, ";"); |
139 |
|
140 |
for (unsigned i=0; i<sections.size(); i++) |
141 |
{ |
142 |
std::string current = Util::str_trim(sections[i]); |
143 |
std::vector<std::string> params = Util::str_split( current, "="); |
144 |
if (params.size() != 2) |
145 |
throw std::runtime_error(std::string("syntax error in config line: ") + args); |
146 |
|
147 |
std::string key = Util::str_tolower(Util::str_trim(params[0])); |
148 |
std::string val = Util::str_trim(params[1]); |
149 |
config[key] = val; |
150 |
} |
151 |
|
152 |
return config; |
153 |
} |
154 |
|
155 |
int StringToInt(const std::string& input) |
156 |
{ |
157 |
char* endptr = (char*) input.c_str(); |
158 |
int retval = strtol(input.c_str(), &endptr, 10); |
159 |
|
160 |
if ( *endptr != 0) |
161 |
throw std::runtime_error( std::string("Error: can not convert this to a number: ") + input); |
162 |
|
163 |
return retval; |
164 |
} |
165 |
} |