/
conf.cpp
131 lines (113 loc) · 2.66 KB
/
conf.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
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
128
129
130
131
#include <sstream>
#include <fstream>
#include <iostream>
#include <vector>
#include <cstdlib>
#include "conf.hpp"
void ConfParser::parse_file(std::string filename)
{
std::ifstream infile (filename.c_str());
bool in_domains = false;
std::string line;
std::string key;
std::string value;
while( std::getline(infile, line) ) {
if (line.size() != 0) {
//Remove comments if necessary
int eol = line.find("#");
if (eol != -1) {
line = line.substr(eol,line.size()-eol);
}
std::istringstream is_line(line);
//Handle the DOMAINS array
if (in_domains) {
this->m_domains.push_back(this->handle_array_line(line));
if (line.find("]") != std::string::npos) {
in_domains = false;
}
}
//Else look for the key=value structure
else if( std::getline(is_line, key, '=') ) {
if( std::getline(is_line, value) ) {
if (key.compare("DOMAINS")==0) {
in_domains = true;
this->m_domains.push_back(this->handle_array_line(value));
}
else {
store_line(key, value);
}
}
}
else {
std::cerr << "Non-valid line in conf file: "<<line<<std::endl;
exit(EXIT_FAILURE);
}
}
}
}
std::string ConfParser::handle_array_line(std::string line)
{
int offset = line.find_first_not_of(" \t\f\v\n\r[");
std::string result = line.substr(offset,line.size()-offset);
result = result.substr(0,result.find_last_of(",]"));
return result;
}
void ConfParser::store_line(std::string key, std::string value)
{
std::istringstream temp(value);
if (key.compare("RANDOM_PREFIX_SIZE")==0) {
temp >> (this->m_randomPrefixSize);
}
else if (key.compare("MYSQL_USER")==0) {
this->m_mysqlUser = value;
}
else if (key.compare("MYSQL_PASSWORD")==0) {
this->m_mysqlPass = value;
}
else if (key.compare("MYSQL_DB")==0) {
this->m_mysqlDB = value;
}
else if (key.compare("MYSQL_ADDRESS")==0) {
this->m_mysqlAddr = value;
}
else if (key.compare("MYSQL_PORT")==0) {
temp >> this->m_mysqlPort;
}
else if (key.compare("DOMAINS")==0) {
}
else {
std::cerr << "Unknown field in conf file: " << key << std::endl;
exit(EXIT_FAILURE);
}
}
/*
* GETTERS BLOCK
*/
const std::vector<std::string>& ConfParser::get_domains() const
{
return m_domains;
}
const std::string& ConfParser::get_mysql_addr() const
{
return m_mysqlAddr;
}
const std::string& ConfParser::get_mysql_db() const
{
return m_mysqlDB;
}
const std::string& ConfParser::get_mysql_pass() const
{
return m_mysqlPass;
}
uint32_t ConfParser::get_mysql_port() const
{
return m_mysqlPort;
}
const std::string& ConfParser::get_mysql_user() const
{
return m_mysqlUser;
}
uint32_t ConfParser::get_random_prefix_size() const
{
return m_randomPrefixSize;
}