-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.cpp
141 lines (123 loc) · 3.73 KB
/
Parser.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
132
133
134
135
136
137
138
139
140
141
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
#include <iostream>
#include <functional>
#include <cctype>
#include <utility>
#include <algorithm>
#include <locale>
#include "Parser.h"
#include "ServerElement.h"
#include "UserElement.h"
#include "CredentialElement.h"
#include "AccessElement.h"
Spatch::Parsing::Parser::Parser(Spatch::Configuration::Config &conf)
: _conf(conf)
{
}
Spatch::Parsing::Parser::~Parser()
{
}
inline std::string &Spatch::Parsing::Parser::trim(std::string& s)
{
return ltrim(rtrim(s));
}
inline std::string &Spatch::Parsing::Parser::rtrim(std::string& s)
{
s.erase(std::find_if(s.rbegin(), s.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
inline std::string &Spatch::Parsing::Parser::ltrim(std::string& s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
void Spatch::Parsing::Parser::parse(std::istream &is)
{
std::string buff;
size_t pos;
Spatch::Parsing::IElement *tmp = nullptr;
while (std::getline (is, buff))
{
if ((pos = buff.find("##")) != std::string::npos)
buff.erase(pos);
if (!std::all_of(buff.begin(),buff.end(),isspace))
{
if ((pos = buff.find("#")) != std::string::npos)
{
if (tmp != nullptr)
{
tmp->finish(_conf);
delete tmp;
tmp = nullptr;
}
if ((tmp = this->getElement(buff)) == nullptr)
std::cout << "Parsing error (unknown element) !\n\tIgnoring : \"" << buff << "\"" << std::endl;
}
else if (tmp != nullptr)
this->setKeyValueToElement(tmp, buff);
else
std::cout << "Parsing error (no element found)!\n\tIgnoring : \"" << buff << "\"" << std::endl;
}
}
if (tmp != nullptr)
{
tmp->finish(_conf);
delete tmp;
}
}
Spatch::Parsing::IElement *Spatch::Parsing::Parser::getElement(std::string &line)
{
Spatch::Parsing::IElement *ret = nullptr;
size_t pos = line.find("#");
std::string value = line.substr(pos + 1);
pos = line.find(" ");
value = value.substr(0 , pos);
std::string id = line.substr(pos + 1);
value = trim(value);
id = trim(id);
if (value.length() == 0 || id.length() == 0)
return ret;
return createElement(value, id);
}
Spatch::Parsing::IElement *Spatch::Parsing::Parser::createElement(std::string &type, std::string &id)
{
Spatch::Parsing::IElement *ret = nullptr;
if (type == "user")
{
ret = new Spatch::Parsing::UserElement();
ret->setId(id);
}
else if (type == "server")
{
ret = new Spatch::Parsing::ServerElement();
ret->setId(id);
}
else if (type == "credential")
{
ret = new Spatch::Parsing::CredentialElement();
ret->setId(id);
}
else if (type == "access")
{
ret = new Spatch::Parsing::AccessElement();
ret->setId(id);
}
return ret;
}
bool Spatch::Parsing::Parser::setKeyValueToElement(Spatch::Parsing::IElement *elem, std::string &line)
{
size_t pos = line.find("=");
std::string key = line.substr(0, pos);
std::string value = line.substr(pos + 1);
value = trim(value);
key = trim(key);
if (value.length() == 0 || key.length() == 0)
return false;
return (elem->addProperty(key, value));
}