-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConfigManager.cpp
61 lines (55 loc) · 1.15 KB
/
ConfigManager.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
#include <sstream>
#include <iostream>
#include "ConfigManager.h"
#include "exceptions.h"
ConfigManager::ConfigManager()
{
}
void ConfigManager::clear()
{
values_.clear();
}
void ConfigManager::read(const std::string& input)
{
std::istringstream data(input);
while (!data.eof()) {
std::string line;
getline(data,line);
if (line.find_first_of('#') == 0) {
continue;
}
else if (line.empty()) {
continue;
}
else {
int i = line.find_first_of('=');
if (i > 0) {
if ((unsigned int) i == line.size() - 1) {
throw ConfigParseError(line);
}
values_[line.substr(0,i)] = line.substr(i+1,line.size()-i);
}
else {
throw ConfigParseError(line);
}
}
}
}
std::string ConfigManager::getValue(const std::string& key) const
{
std::map<std::string,std::string>::const_iterator it = values_.find(key);
if (it != values_.end()) {
return (*it).second;
}
else {
return "";
}
}
std::vector<std::string> ConfigManager::keys() const
{
std::vector<std::string> result;
for (std::map<std::string,std::string>::const_iterator it = values_.begin(); it != values_.end(); ++it) {
result.push_back(it->first);
}
return result;
}