Repository navigation
Expand file tree
/
Copy pathcommandParser.cpp
More file actions
69 lines (63 loc) · 1.67 KB
/
Copy pathcommandParser.cpp
File metadata and controls
69 lines (63 loc) · 1.67 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
/*
* CommandParser.cpp
*
* Created on: May 4, 2012
* Author: randy
*/
#include "commandParser.h"
#include "command.h"
#include "boost/algorithm/string.hpp"
#include <stdio.h>
#include <vector>
using namespace std;
#define TELNET_PORT "21"
Command CommandParser::parseCommand(string com) {
//using boost to split the string
Command cmd;
cmd.setType(CommandType_Error);
vector<string> splitVec;
boost::split(splitVec, com, boost::algorithm::is_space(),
boost::algorithm::token_compress_on);
if (splitVec[0] == "get") { //get command
if (splitVec.size() < 3) {
printf("arguments for get command is too low\n");
} else {
cmd.setType(CommandType_Get);
cmd.addParam(splitVec[1]);
cmd.addParam(splitVec[2]);
}
} else if (splitVec[0] == "open") { //open command
if (splitVec.size() < 2) {
printf("arguments for open command is too low\n");
} else {
cmd.setType(CommandType_Open);
cmd.addParam(splitVec[1]);
if (splitVec.size() >= 3) {
cmd.addParam(splitVec[2]);
} else {
//add default port
cmd.addParam(TELNET_PORT);
}
}
} else if (splitVec[0] == "dir") {
cmd.setType(CommandType_Dir);
if (splitVec.size() > 1) {
cmd.addParam(splitVec[1]);
}
} else if (splitVec[0] == "close" || splitVec[0] == "quit") {
cmd.setType(CommandType_Exit);
} else if (splitVec[0] == "cd") {
if (splitVec.size() < 2) {
printf("arguments for open command is too low\n");
} else {
cmd.setType(CommandType_Cd);
cmd.addParam(splitVec[1]);
}
} else if (splitVec[0] == "put") {
cmd.setType(CommandType_Put);
cmd.addParam(splitVec[1]);
} else {
printf("command %s is not supported yet\n", splitVec[0].c_str());
}
return cmd;
}