-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathmain.cpp
97 lines (89 loc) · 1.93 KB
/
main.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
#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
// 该文件为原始文件 具体实现在各个文件中
#define ERR_EXIF(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0);
char* trim(char* cmdline)
{
char* s = cmdline;
while (*s == ' ')
{
s++;
}
char* e = s;
int len = 0;
while ((strcmp(e, "") != 0) && *e != ' ')
{
len++;
e++;
}
char* res = new char[len];
memcpy(res, s, len);
return res;
}
vector<char*> getcmd(char* cmdline) {
vector<char*> res;
char *s = trim(cmdline);
if (strcmp(s, "") == 0) {
return res;
}
int len = 0;
char *temp;
char *tmp ;
int count = 3;
while (count--) {
s = trim(cmdline);
if (strcmp(s, "") == 0) {
return res;
}
len = 0;
temp = s;
tmp = new char[100];
while ((strcmp(temp, "") != 0) && *temp != ' ') {
len++;
temp++;
}
memcpy(tmp, s, len);
res.push_back(tmp);
cmdline = cmdline + len;
while ((strcmp(cmdline, "") != 0) && *cmdline == ' ')
{
cmdline++;
}
}
return res;
}
int main() {
char* p;
char* cmdline = new char[100];
while (cin.getline(cmdline, 100))
{
vector<char*> res = getcmd(cmdline);
if (res.size() == 0 || res.size() == 2)
{
cout << "err" << endl;
}
else if (strcmp(res[0], "exit") == 0)
{
cout << "exit" << endl;
}
else if (strcmp(res[0], "send") == 0 && res.size() == 3)
{
cout << "send" << endl;
cout << "username:\t" << res[1] << endl;
cout << "msg:\t" << res[2] << endl;
}
else
{
cout << "err" << endl;
}
}
return 0;
}