-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigParser.cpp
More file actions
127 lines (100 loc) · 3.17 KB
/
ConfigParser.cpp
File metadata and controls
127 lines (100 loc) · 3.17 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "ConfigParser.hpp"
ConfigParser::ConfigParser(const std::vector<std::string> &v, const std::string &sep):
keys(v), separator(sep) {}
ConfigParser::ConfigParser(const std::vector<std::string> &v):
ConfigParser(v, "=") {}
void ConfigParser::viewConfig(){
std::cout << "separator:\t" << this->separator << std::endl;
std::cout << "keys:\t";
for(auto item : this->keys){
std::cout << item << ", ";
}
std::cout << "\b\b " << std::endl;
}
std::unique_ptr<struct parsed_t> ConfigParser::parseString(std::string &str){
std::string::size_type sep_pos;
std::string key_c, key, val;
sep_pos = str.find(this->separator);
if(sep_pos == std::string::npos){
std::cout << "ERROR: separator not found: '" << str << "'" << std::endl;
}
else{
// std::cout << "found at: " << str.substr(sep_pos) << " " << sep_pos << std::endl;
key = str.substr(0, sep_pos);
val = str.substr(sep_pos + 1);
if(this->trim(key)){
std::cerr << "ERROR: trim(): '"<< key << "'" << std::endl;
}
if(this->trim(val)){
std::cerr << "ERROR: trim(): '"<< val << "'" << std::endl;
}
// std::cout << "parsed: '" << key << "'='" << val << "'" << std::endl;
if(this->checkKeyValidity(key)){
std::cerr << "ERROR: parsed key is not in key set inserted: '" << key << "'" << std::endl;
return nullptr;
}
else{
std::unique_ptr<struct parsed_t> p (new struct parsed_t(key, val));
return p;
}
}
return nullptr;
}
bool ConfigParser::parseStream(std::fstream &stream){
std::map<std::string, std::string> m;
std::map<std::string, std::string>::iterator it;
std::string line;
while ( getline (stream, line) ){
// std::cout << "raw:" << line << '\n';
auto p = this->parseString(line);
if(p != nullptr){
// std::cout << "par: " << p->lvalue << "=" << p->rvalue << std::endl;
it = m.find(p->lvalue);
if (it == m.end()){
m.emplace(p->lvalue, p->rvalue);
}
else{
m[p->lvalue] = p->rvalue;
}
}
}
this->output_map = m;
if(this->checkMapValidity()){
return false;
}
else{
return true;
}
}
bool ConfigParser::trim(std::string &str){
if(str.length() <= 0){
return true;
}
std::locale loc;
while(std::isspace(str.back(),loc)){
str.pop_back();
}
while(std::isspace(str.front(),loc)){
str = str.substr(1);
}
return false;
}
bool ConfigParser::checkKeyValidity(const std::string &key){
for(auto a: this->keys){
// std::cout << "Cmp:" << a << ":" << key << std::endl;
if(!key.compare(a)){
return false;
}
else{
continue;
}
}
return 1;
}
bool ConfigParser::checkMapValidity(){
std::cerr << "ERROR: Override ConfigParser::checkMapValidity() virtual method!" << std::endl;
return false;
}
std::map<std::string, std::string> ConfigParser::getMap(){
return this->output_map;
}