-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsg_parse.cpp
More file actions
171 lines (147 loc) · 3.03 KB
/
msg_parse.cpp
File metadata and controls
171 lines (147 loc) · 3.03 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
#include "msg_parse.hpp"
#include "User.hpp"
std::string erase_CR_LF(std::string buff)
{
size_t pos = std::string::npos;
if ((pos = buff.find("\r\n") )!= std::string::npos)
buff.erase(pos, 2);
return (buff);
}
msg_parse message_splitter(const char *buffer, int ret, msg_parse &parsed_command, User &user)
{
std::string buff = buffer;
size_t pos = std::string::npos;
if ((pos = buff.find("\r\n") ) == std::string::npos)
{
user.get_msgs().push_back(buff);
}
if (buff.find("\r\n") != std::string::npos)
{
user.get_msgs().push_back(buff);
buff.clear();
for (std::vector<std::string>::iterator it = user.get_msgs().begin(); it != user.get_msgs().end() ; it++)
{
buff = buff + *it;
}
buff = erase_CR_LF(buff);
parsed_command.set_msg(buff);
if (!parsed_command.parser())
ret = 0;
user.get_msgs().clear();
}
return(parsed_command);
}
std::string msg_parse::get_msg(void)
{
return (msg);
}
int msg_parse::get_pos(void) const
{
return (this->pos);
}
int msg_parse::get_has_additional_param(void) const
{
return (this->has_additional_param);
}
void msg_parse::set_msg(std::string buff)
{
msg = buff;
}
void msg_parse::set_pos(int pos)
{
this->pos = pos;
}
void msg_parse::set_has_additional_param(int additional_param)
{
this->has_additional_param = additional_param;
}
msg_parse::msg_parse( void) : msg(), cmd() , cmd_params() , space_par()
{
}
msg_parse::msg_parse(std::string buffer) : cmd() , cmd_params() , space_par() , has_additional_param(0)
{
msg = buffer;
}
int msg_parse::command_checker(int *idx)
{
int pos;
int start_cmd = 0;
while (msg[*idx] && (msg[*idx] == ' ' || msg[*idx] == '\t'))
{
start_cmd++;
(*idx)++;
}
for (; msg[*idx] && msg[*idx] != ' '; (*idx)++)
{
if (!isupper(msg[*idx]))
{
pos = msg.find(" ", start_cmd);
cmd = msg.substr(start_cmd, pos - start_cmd);
return (0);
}
}
if (*idx)
cmd = msg.substr(start_cmd, *idx - start_cmd);
return (1);
}
std::string msg_parse::get_additional_param( void)
{
return (space_par);
}
void msg_parse::params(int idx, char **tab)
{
char *tmp_msg = (char *)msg.c_str() + idx;
*tab = strtok(tmp_msg, " \t");
while (*tab && *tab[0] != ':')
{
cmd_params.push_back(*tab);
*tab = strtok(NULL, " \t");
}
}
void msg_parse::additional_param(char *tab)
{
has_additional_param = 0;
if (tab && tab[0] == ':')
{
while (tab)
{
space_par += tab;
tab = strtok(NULL, " ");
if (tab)
space_par += " ";
}
space_par.erase(0, 1);
has_additional_param = 1;
}
}
int msg_parse::parser( void)
{
int index = 0;
if (msg.empty())
std::cout << "Empty msg" << std::endl;
if (!command_checker(&index))
return (0);
char *tab;
params(index, &tab);
additional_param(tab);
return (1);
}
std::string msg_parse::get_cmd( void)
{
return (cmd);
}
std::vector<char *> msg_parse::get_cmd_params( void)
{
return (cmd_params);
}
msg_parse &msg_parse::operator=(const msg_parse & f)
{
this->msg = f.msg;
this->cmd = f.cmd;
this->cmd_params = f.cmd_params;
this->space_par = f.space_par;
return (*this);
}
msg_parse::~msg_parse()
{
}