-
Notifications
You must be signed in to change notification settings - Fork 0
/
Command.cpp
150 lines (125 loc) · 3.3 KB
/
Command.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
142
143
144
145
146
147
148
149
150
#include "headers\Headers.h"
using namespace std;
void Command::sendCommand(SOCKET sock, const char *command)
{
int length = strlen(command);
char *buffer = new char[length + 3];
strcpy_s(buffer, length + 1, command);
strcat_s(buffer, length + 3, "\r\n");
try {
int backed_code = send(sock, buffer, strlen(buffer), 0);
if (backed_code <= 0) {
throw backed_code;
}
}
catch (int code)
{
switch (code) {
case 0:
cout << "Cannot send command. Connection closed..." << endl;
break;
case -1:
cout << "Cannot send command. Connection failed..." << endl;
break;
default:
cout << "Something error: " << WSAGetLastError() << endl;
break;
}
}
catch (...)
{
cout << "Something error: " << WSAGetLastError() << endl;
}
}
Commands Command::getCommandByStroke(char *comm, bool haveParams)
{
char *command = new char[strlen(comm) + 1];
strcpy_s(command, strlen(comm) + 1, comm);
if (haveParams) {
char * pch = strtok(command, " ");
if (pch != NULL) {
command = pch;
}
}
if (!strcmp(command, "create"))
return CONNECT;
else if (!strcmp(command, "close"))
return DISCONNECT;
else if (!strcmp(command, "mkdir"))
return MAKE_DIR;
else if (!strcmp(command, "pwd"))
return CURRENT_DIR;
else if (!strcmp(command, "up"))
return MOVE_UP;
else if (!strcmp(command, "rmdir"))
return DELETE_DIR;
else if (!strcmp(command, "rm"))
return DELETE_FILE;
else if (!strcmp(command, "rn"))
return RENAME_FILE;
else if (!strcmp(command, "hosts"))
return HOSTS;
else if (!strcmp(command, "addhost"))
return ADD_HOST;
else if (!strcmp(command, "rmhost"))
return DELETE_HOST;
else if (!strcmp(command, "download"))
return DOWNLOAD_FILE;
else if (!strcmp(command, "upload"))
return UPLOAD_FILE;
else if (!strcmp(command, "ls"))
return LIST;
else if (!strcmp(command, "cd"))
return CHANGE_DIR;
else if (!strcmp(command, "help"))
return HELP;
else if (!strcmp(command, "clear"))
return CLEAR_CONSOLE;
else if (!strcmp(command, "exit"))
return EXIT;
else {
return COMMAND_ERROR;
}
}
char * Command::getCommandLineArguments(char *cmd)
{
char *arguments = new char[2];
arguments[0] = '\0';
char *commandLine = new char[strlen(cmd) + 1];
strcpy_s(commandLine, strlen(cmd) + 1, cmd);
char *pch = strtok(commandLine, " ");
pch = strtok(NULL, " ");
while (pch != NULL) {
int length = strlen(arguments) + strlen(pch) + 1;
arguments = (char *)realloc(arguments, length + 1);
strcat_s(arguments, length, pch);
pch = strtok(NULL, " ");
if (pch != NULL) {
arguments = (char *)realloc(arguments, strlen(arguments) + 2);
strcat_s(arguments, strlen(arguments) + 2, ",");
}
}
return arguments;
}
bool Command::haveAnyArgs(char *cmd)
{
int countArguments;
char *commandLine = new char[strlen(cmd) + 1];
strcpy_s(commandLine, strlen(cmd) + 1, cmd);
char *pch = strtok(commandLine, " -");
for (countArguments = -1; pch != NULL; countArguments++) {
pch = strtok(NULL, " -");
}
return (countArguments > 0) ? true : false;
}
bool Command::checkCountParams(char *params, CountParams count)
{
int countArguments = 0;
char *arg = new char[strlen(params) + 1];
strcpy(arg, params);
char *pch = strtok(arg, ",");
for (countArguments = 0; pch != NULL; countArguments++) {
pch = strtok(NULL, ",");
}
return (countArguments == count) ? true : false;
}