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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
#include "commands.h"
#include "error.h"
#include <cctype>
#include <iostream>
#include <algorithm>
#include <string>
#include <utility>
#include <vector>
#include <regex>
#include <cstring>
using std::cout, std::endl, std::string, std::vector, std::tolower;
CommandsModule::CommandsModule()
{
}
CommandsModule::~CommandsModule()
{
for(Command c : commandList)
{
if(c.argc > 0)
delete[] c.argTypes;
}
}
void CommandsModule::addCommand(Command c)
{
if(lookupCommand(c.name) != nullptr)
{
cout << "Duplicate command: " << c.name << endl;
}
commandList.push_back(c);
}
void CommandsModule::addCommand(std::string name, const void (*func)(const CommandArg *), const int argc, CommandArgType *argTypes)
{
Command c = {name, nullptr, func, argc, argTypes, nullptr};
addCommand(c);
}
void CommandsModule::addCommand(std::string name, const void(*func)(const CommandArg*), const int argc, std::vector<CommandArgType> argTypes)
{
CommandArgType* argTypesArr = new CommandArgType[argc];
for(int i = 0; i < argc; i++)
{
argTypesArr[i] = argTypes[i];
}
addCommand(name, func, argc, argTypesArr);
}
struct NameMatches
{
NameMatches(string s): s_{s} {}
bool operator()(Command c) { return (c.name == s_); }
string s_;
};
Command* CommandsModule::lookupCommand(string name)
{
auto elem = std::find_if(commandList.begin(), commandList.end(), NameMatches(name));
if (elem != commandList.end())
{
int i = elem - commandList.begin();
return &(commandList[i]);
}
else
{
return nullptr;
}
}
vector<string> CommandsModule::splitCommand(string command)
{
vector<string> v;
string regexString = "((?:\"[^\"\n]+\")|(?:'[^'\n]+')|(?:[^ \n]+))+";
std::regex regex(regexString, std::regex_constants::_S_icase);
auto begin = std::sregex_iterator(command.begin(), command.end(), regex);
auto end = std::sregex_iterator();
for (std::sregex_iterator i = begin; i != end; i++)
{
std::smatch match = *i;
std::string str = match.str();
if(str[0] == '\'' || str[0] == '"')
str = str.substr(1, str.size() - 2);
v.push_back(str);
}
return v;
}
template <typename T>
std::basic_string<T> lowercase(const std::basic_string<T>& s)
{
std::basic_string<T> s2 = s;
std::transform(s2.begin(), s2.end(), s2.begin(), [](unsigned char c){ return std::tolower(c); });
return s2;
}
CommandArg* CommandsModule::getCommandArgs(vector<string>& split, const CommandArgType* argTypes, const int argc)
{
CommandArg* args = new CommandArg[argc];
for(int i = 1; i < argc + 1; i++)
{
switch(argTypes[i-1])
{
case STR: args[i-1].str = (char*)split[i].c_str(); break;
case NUM: args[i-1].num = std::stoi(split[i]); break;
case MOVDIR: args[i-1].dir = LEFT; break;
case STR_REST:
{
string rest = "";
for(int j = i; j < split.size(); j++)
{
rest += split[j];
if(j != split.size() - 1)
rest += " ";
}
args[i-1].str = new char[rest.size()];
strcpy(args[i-1].str, rest.c_str());
return args;
}
case NUM_ARR_REST:
{
int* rest = new int[split.size() - i];
for(int j = 0; j < split.size() - i; j++)
{
rest[j] = std::stoi(split[j + i]);
}
args[i-1].numArr = {rest, (int) split.size() - i};
return args;
}
default: cout << "UH OH SOMETHING IS VERY WRONG" << endl;
}
}
return args;
}
void CommandsModule::runCommand(string command)
{
vector<string> split = splitCommand(command);
Command* cmd = lookupCommand(split[0]);
if(cmd == nullptr)
throw Err(CMD_ERR_NOT_FOUND, split[0] + " is not a valid command name");
if(cmd->argc > split.size() - 1)
throw Err(CMD_ERR_WRONG_ARGS, command + " is the wrong args");
CommandArg* args = getCommandArgs(split, cmd->argTypes, cmd->argc);
try
{
if(cmd->module == nullptr)
cmd->staticFunc(args);
else
cmd->func(*cmd->module, args);
}
catch (Err e)
{
for(int i = 0; i < cmd->argc; i++)
{
if(cmd->argTypes[i] == STR_REST)
delete[] args[i].str;
}
delete[] args;
throw e;
}
for(int i = 0; i < cmd->argc; i++)
{
if(cmd->argTypes[i] == STR_REST)
delete[] args[i].str;
}
delete[] args;
}
Err CommandsModule::checkCommand(string command)
{
vector<string> split = splitCommand(command);
Command* cmd = lookupCommand(split[0]);
if(cmd == nullptr)
return Err(CMD_ERR_NOT_FOUND, split[0] + " is not a valid command name");
if(cmd->argc > split.size())
return Err(CMD_ERR_WRONG_ARGS, command + " is the wrong args");
CommandArg* args = getCommandArgs(split, cmd->argTypes, cmd->argc);
for(int i = 0; i < cmd->argc; i++)
{
if(cmd->argTypes[i] == STR_REST || cmd->argTypes[i] == NUM_ARR_REST)
delete[] args[i].str;
}
delete[] args;
return Err(NOERR, "");
}
|