-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandProcessor.cpp
More file actions
57 lines (49 loc) · 1.16 KB
/
CommandProcessor.cpp
File metadata and controls
57 lines (49 loc) · 1.16 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
#include "CommandProcessor.h"
vector<COMMAND> commands;
vector<string> Split(string str)
{
vector<string> tokens;
char* buffer;
buffer = strtok((char*)str.c_str()," ");
while(buffer != NULL)
{
tokens.push_back(buffer);
buffer = strtok(NULL," ");
}
return tokens;
}
void AddCommand(char* name,CommandFunc function)
{
COMMAND c;
c.command = name;
c.func = function;
if(commands.size() == 0)
commands.push_back(c);
else
for(int i=0;i<commands.size();i++)
{
if(strcmp(commands[i].command,name)==0)
{
cout <<"Command: " << name << " has already been defined" << endl;
break;
} else {
commands.push_back(c);
break;
}
}
}
void ProcessCommand(char* cmd)
{
vector<string> arguments = Split(cmd);
string c = arguments.at(0); //save command before we remove it from argument list
arguments.erase(arguments.begin()); //remove first token so we only pass arguments of the command to the function
for(int i=0;i<commands.size();i++)
{
if(strcmp(c.c_str(),commands[i].command)==0)
{
commands[i].func(arguments);
return;
}
}
cout <<"Command not recognized"<<endl;
}