-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.cpp
More file actions
214 lines (177 loc) · 4.68 KB
/
Server.cpp
File metadata and controls
214 lines (177 loc) · 4.68 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "Library.hpp"
Server::Server(int port, const std::string &password) {
_port = port;
_argvPass = password;
_configTokens.push_back(SERV_NAME);
_configTokens.push_back(ADMIN_NAME);
_configTokens.push_back(OPERATORS);
std::string line;
std::ifstream motdFile(PATH_TO_MOTD);
if (motdFile.is_open())
{
while (getline(motdFile, line))
_motd.push_back(line);
motdFile.close();
}
};
std::string Server::getServName() {
return _serverName;
}
std::vector<std::string> Server::getMotd() {
return _motd;
}
// *** удаление пробелов, кавычек и ; ***
void Server::newDeleteSpaces(std::string *str) {
size_t i = 0;
while (isspace((*str)[i])) {
++i;
}
if ((*str)[i] == '\"')
++i;
*str = (*str).substr(i, (*str).size());
i = (*str).size() - 1;
while (isspace((*str)[i]) || (*str)[i] == ';' || (*str)[i] == '\"') {
--i;
}
if (i <= (*str).size())
*str = (*str).substr(0, i + 1);
}
void Server::parseConfig() {
std::vector<std::string> config = splitLines(PATH_TO_CONFIG);
std::vector<std::string>::iterator end;
std::vector<std::string>::iterator start;
end = config.begin();
while (end < config.end()) {
size_t index = (*end).find(":");
if (index != std::string::npos) {
std::string sub = (*end).substr(1, index - 2);
if (sub == SERV_NAME) {
_serverName = (*end).substr(index + 1);
newDeleteSpaces(&_serverName);
}
else if (sub == ADMIN_NAME) {
_admin = (*end).substr(index + 1);
newDeleteSpaces(&_admin);
}
else if (sub == OPERATORS)
while (*(++end) != "}") {
size_t indexMap = (*end).find(":");
if (indexMap != std::string::npos) {
std::string tmp = (*end).substr(indexMap + 1);
newDeleteSpaces(&tmp);
if (indexMap != std::string::npos) {
_operators.insert(std::pair<std::string,std::string>((*end).substr(1, indexMap - 2), tmp));
}
}
}
}
end++;
}
config.clear();
}
std::vector<std::string> Server::splitLines(std::string path) {
std::ifstream file(path.c_str());
if (!(file.is_open()))
throw std::runtime_error("Cannot open config file");
std::string buf;
std::vector<std::string> split;
std::vector<std::string> res;
while (!file.eof()) {
getline(file, buf);
if (emptyOrComments(buf))
continue;
split = checkBraces(buf);
if (!split.empty()) {
for (size_t i = 0; i < split.size(); ++i) {
res.push_back(split[i]);
split[i].clear();
}
}
else {
deleteSpaces(&buf);
res.push_back(buf);
}
buf.clear();
}
res.push_back("");
file.close();
return res;
}
// *** вырезание по фигурным скобкам ***
std::vector<std::string> Server::checkBraces(std::string buf) {
std::vector<std::string> vector;
std::string lineBeforeBraces;
std::string lineAfterBraces;
std::string braces;
std::string tmp;
size_t i = 0;
while (buf[i]) {
if (buf[i] == '}' || buf[i] == '{') {
lineBeforeBraces = buf.substr(0, i);
deleteSpaces(&lineBeforeBraces);
if (!lineBeforeBraces.empty())
vector.push_back(lineBeforeBraces);
lineBeforeBraces.clear();
tmp = buf.substr(i, buf.size());
braces = tmp.substr(0, 1);
vector.push_back(braces);
braces.clear();
lineAfterBraces = tmp.substr(1, tmp.size());
deleteSpaces(&lineAfterBraces);
if (!lineAfterBraces.empty())
vector.push_back(lineAfterBraces);
lineAfterBraces.clear();
tmp.clear();
}
++i;
}
return vector;
}
// *** проверка на пустоту - только пробелы ***
int Server::onlySpaces(std::string buf) {
size_t i = 0;
while (buf[i]) {
if (!isspace(buf[i++]))
return 1;
}
return 0;
}
// *** проверка на пустоту - только пробелы и комменты ***
int Server::emptyOrComments(std::string buf) {
if (buf[0] == '#' || buf.empty()) {
return 1;
}
if (buf.find('#')) {
buf = buf.substr(0, buf.find('#'));
if (!onlySpaces(buf)) {
return 1;
}
}
return 0;
}
// *** удаление пробелов и ; ***
void Server::deleteSpaces(std::string *str) {
size_t i = 0;
while (isspace((*str)[i])) {
++i;
}
*str = (*str).substr(i, (*str).size());
i = (*str).size() - 1;
while (isspace((*str)[i]) || (*str)[i] == ';') {
--i;
}
if (i <= (*str).size())
*str = (*str).substr(0, i + 1);
}
std::string Server::getArgPass() {
return _argvPass;
}
std::string Server::getConfigPass() {
return _configPass;
}
std::string Server::getAdminLogin() {
return _admin;
}
const std::map<std::string, std::string>& Server::getOperatorsMap() {
return _operators;
}