-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsole.cpp
More file actions
109 lines (96 loc) · 2.63 KB
/
Console.cpp
File metadata and controls
109 lines (96 loc) · 2.63 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
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
#include "Console.h"
Console::Console()
{
}
void Console::addBasicCommand(Console* console)
{
ListCommand* listCommand = new ListCommand();
listCommand->commands = console->getCommands();
console->addCommand(listCommand);
HelpCommand* helpCommand = new HelpCommand();
helpCommand->commands = console->getCommands();
console->addCommand(helpCommand);
}
std::string Console::commandeExecute(std::string buffer)
{
std::string ret;
bool use = false;
size_t first_delimitor = buffer.find_first_of(' ');
std::string command_name = buffer;
std::string args = "";
if (first_delimitor != std::string::npos)
{
command_name = buffer.substr(0, first_delimitor);
args = buffer.substr(first_delimitor + 1);
}
for (size_t i = 0; i < this->commands.size(); i++)
{
if( this->commands.at(i)->getName() == command_name)
{
try
{
ret = this->commands.at(i)->execute(args);
}catch(CommandNotFoundException e)
{
throw CommandRuntimeException(this->commands.at(i)->getName(), e.what());
}catch(CommandRuntimeException e)
{
throw CommandRuntimeException(this->commands.at(i)->getName(), e.what());
}catch(std::exception e)
{
throw CommandRuntimeException(this->commands.at(i)->getName(), e.what());
}
catch (...)
{
throw CommandRuntimeException(this->commands.at(i)->getName(), "unknown");
}
use = true;
break;
}
}
if (!use)
{
throw CommandNotFoundException(buffer);
}
return ret;
}
std::string Console::autoCompleteCommand(std::string buffer)
{
std::string ret;
for (size_t i = 0; i < this->commands.size(); i++)
{
if (this->commands.at(i)->getName().find_first_of(buffer) == 0u)
{
ret = this->commands.at(i)->getName();
break;
}
}
return ret;
}
void Console::addCommand(Command* command)
{
this->commands.push_back(command);
}
void Console::removeCommand(size_t id)
{
delete this->commands.at(id);
this->commands.erase(this->commands.begin() + id);
}
void Console::removeCommand(std::string name)
{
size_t id = SIZE_MAX;
for (size_t i = 0; i < this->commands.size(); i++)
{
if (this->commands.at(i)->getName() == name)
{
id = i;
break;
}
}
if (id != SIZE_MAX)
this->removeCommand(id);
}
std::vector<Command*>* Console::getCommands()
{
return &this->commands;
}