1 |
#include "ConfigFile.h" |
#include "ConfigFile.h" |
2 |
|
#include "Util.h" |
3 |
|
|
4 |
#include <fstream> |
#include <fstream> |
5 |
|
#include <sstream> |
6 |
|
#include <stdexcept> |
7 |
|
|
8 |
|
#include <stdlib.h> |
9 |
|
|
10 |
const char SEPERATOR = '/'; |
const char SEPERATOR = '/'; |
11 |
|
|
29 |
} |
} |
30 |
|
|
31 |
ConfigFile::ConfigFile(std::string const& configFile) |
ConfigFile::ConfigFile(std::string const& configFile) |
32 |
|
: filename_(configFile) |
33 |
{ |
{ |
34 |
Open(configFile); |
Open(); |
35 |
|
} |
36 |
|
|
37 |
|
|
38 |
|
bool ConfigFile::Reload() |
39 |
|
{ |
40 |
|
content_.clear(); |
41 |
|
return Open(); |
42 |
} |
} |
43 |
|
|
44 |
bool ConfigFile::Open(std::string const& configFile) |
bool ConfigFile::Open(std::string const& configFile) |
45 |
{ |
{ |
46 |
std::ifstream file(configFile.c_str()); |
content_.clear(); |
47 |
|
filename_ = configFile; |
48 |
|
return Open(); |
49 |
|
} |
50 |
|
|
51 |
|
bool ConfigFile::Open() |
52 |
|
{ |
53 |
|
std::ifstream file( filename_.c_str() ); |
54 |
|
|
55 |
std::string line; |
std::string line; |
56 |
std::string name; |
std::string name; |
133 |
} |
} |
134 |
} |
} |
135 |
|
|
136 |
|
std::string ConfigFile::DumpConfig() const |
137 |
|
{ |
138 |
|
std::ostringstream ss; |
139 |
|
for (content_iterator it=content_.begin(); it!=content_.end(); ++it) |
140 |
|
{ |
141 |
|
ss << it->first << "=" << it->second.StringValue() << std::endl; |
142 |
|
} |
143 |
|
return ss.str(); |
144 |
|
} |
145 |
|
|
146 |
|
|
147 |
|
namespace ConfigHelper |
148 |
|
{ |
149 |
|
std::map<std::string, std::string> ParseArguments(const std::string& args) |
150 |
|
{ |
151 |
|
std::map<std::string, std::string> config; |
152 |
|
|
153 |
|
std::vector<std::string> sections = Util::str_split(args, ";"); |
154 |
|
|
155 |
|
for (unsigned i=0; i<sections.size(); i++) |
156 |
|
{ |
157 |
|
std::string current = Util::str_trim(sections[i]); |
158 |
|
std::vector<std::string> params = Util::str_split( current, "="); |
159 |
|
if (params.size() != 2) |
160 |
|
throw std::runtime_error(std::string("syntax error in config line: ") + args); |
161 |
|
|
162 |
|
std::string key = Util::str_tolower(Util::str_trim(params[0])); |
163 |
|
std::string val = Util::str_trim(params[1]); |
164 |
|
config[key] = val; |
165 |
|
} |
166 |
|
|
167 |
|
return config; |
168 |
|
} |
169 |
|
|
170 |
|
int StringToInt(const std::string& input) |
171 |
|
{ |
172 |
|
char* endptr = (char*) input.c_str(); |
173 |
|
int retval = strtol(input.c_str(), &endptr, 10); |
174 |
|
|
175 |
|
if ( *endptr != 0) |
176 |
|
throw std::runtime_error( std::string("Error: can not convert this to a number: ") + input); |
177 |
|
|
178 |
|
return retval; |
179 |
|
} |
180 |
|
} |