-
Notifications
You must be signed in to change notification settings - Fork 2
/
ConfigParser.hpp
43 lines (35 loc) · 1.06 KB
/
ConfigParser.hpp
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
#ifndef _CONFIGPARSER_H_
#define _CONFIGPARSER_H_
#include <map>
#include <boost/tokenizer.hpp>
class Config
{
public:
Config(std::ifstream& in) {
if (!in.is_open()) throw std::runtime_error("File not found");
typedef boost::tokenizer< boost::char_separator<char> > Tokenizer;
boost::char_separator<char> sep("=");
std::vector< std::string > vec;
std::string line;
std::size_t commentId;
while (getline(in,line)){
commentId=line.find("##");
if(commentId == std::string::npos) {
Tokenizer tok(line,sep);
//unsafe - wrong syntax like 'newlines' will crash
Tokenizer::iterator it=tok.begin();
dbConfig.insert(std::pair<std::string, std::string>(*(it++), *(it)));
}
}
}
const std::string& operator[](const std::string& x) const {
CfgMap::const_iterator it = dbConfig.find(x);
if(it == dbConfig.end())
throw std::runtime_error("Unknown config value requested");
return it->second;
}
private:
typedef std::map<std::string, std::string> CfgMap;
CfgMap dbConfig;
};
#endif /* _CONFIGPARSER_H_ */