-
Notifications
You must be signed in to change notification settings - Fork 26
/
config_reader.hpp
59 lines (50 loc) · 1.42 KB
/
config_reader.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* @file config_reader.hpp
* @brief Reads configuration file (like standard Linux .conf files)
* @author philave (philave7@gmail.com)
*/
#ifndef CONFIG_READER_HPP
#define CONFIG_READER_HPP
#include <map>
#include <string>
#include <fstream>
#include <sstream>
#include <regex>
class ConfigReader
{
public:
ConfigReader(){}
ConfigReader(const ConfigReader& config_reader) : settings(config_reader.settings) { }
void parse(std::string file_name)
{
std::ifstream configFile(file_name);
std::string line;
while (std::getline(configFile, line))
{
size_t posComment = line.find('#');
if (posComment != std::string::npos) line = line.substr(0, posComment);
line = std::regex_replace(line, std::regex("^[ \t]*"), "");
if (line.size() == 0) continue;
line = std::regex_replace(line, std::regex("[ \t]+"), " ");
std::istringstream string_reader(line);
std::string key, value;
string_reader >> key >> value;
settings[key] = value;
}
// For test
//for (const auto& item : settings) std::cout << item.first << "=" << item.second << std::endl;
}
bool check_key(const std::string& key)
{
auto it = settings.find(key);
if (it == settings.end()) return false;
return true;
}
const char* get_key_value(const std::string& key)
{
return settings[key].c_str();
}
private:
std::map<std::string, std::string> settings;
};
#endif // CONFIG_READER_HPP