-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandList.cpp
217 lines (178 loc) · 6.83 KB
/
CommandList.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "CommandList.hpp"
#include "JSimple.hpp"
#include <fstream>
#include <cstdlib>
#include <chrono>
#include <ctime>
#pragma comment(lib, "user32.lib")
#include <Windows.h>
#pragma warning(disable : 4996)
CommandsList::CommandsList() {
Command DisplayCommandsCommand("Help", "Sends a list of commands and their info", "", 0, [this] (string Input)
{
this->DisplayCommands();
});
AddCommand(DisplayCommandsCommand);
Command StopCommand("Stop", "Stops the console", "", 0, [this](string Input)
{
exit(0);
});
AddCommand(StopCommand);
Command SayCommand("Say", "Repeats your message to the console", "Say Hello World!", 1, [this](string Input)
{
Print(Input);
});
AddCommand(SayCommand);
Command ClearCommand("Clear", "Clears the console", "", 0, [this](string Input)
{
system("cls");
Print("Ji9sw's Command Line Interface\n");
});
AddCommand(ClearCommand);
Command ExecuteCommand("Execute", "Executes a command just like in command prompt", "Execute notepad", 1, [this](string Input)
{
system(Input.c_str());
Print("\n");
});
AddCommand(ExecuteCommand);
Command CommandCountCommand("CommandCount", "Says how many commands are present", "", 0, [this](string Input)
{
Print(to_string(CommandList.size()));
});
AddCommand(CommandCountCommand);
Command CalculateCommand("Calculate", "Add, Multiply, Subtract, and Divide", "Calculate 1 x 1", 3, [this](string Input) {
vector<string> Parameters = StringToListWords(Input);
int Num1, Num2;
if (!(istringstream(Parameters[0]) >> Num1) || !(istringstream(Parameters[2]) >> Num2)) {
Print("Error: Invalid numeric values for calculation.");
return;
}
if (Parameters[1] == "x" || Parameters[1] == "X" || Parameters[1] == "*" || ToLowerCase(Parameters[1]) == "times") {
int result = Num1 * Num2;
Print(to_string(result));
}
else if (Parameters[1] == "+" || ToLowerCase(Parameters[1]) == "plus") {
int result = Num1 + Num2;
Print(to_string(result));
}
else if (Parameters[1] == "-" || ToLowerCase(Parameters[1]) == "minus") {
int result = Num1 - Num2;
Print(to_string(result));
}
else if (Parameters[1] == "/") {
if (Num2 == 0) {
Print("Error: Division by zero is not allowed.");
return;
}
float result = static_cast<float>(Num1) / static_cast<float>(Num2);
Print(to_string(result));
}
else {
Print("Error: Invalid operator. Supported operators are +, -, *, /");
}
});
AddCommand(CalculateCommand);
Command TimeCommand("Time", "Says the current system date and time", "", 0, [this](string Input)
{
time_t CurrentTime = chrono::system_clock::to_time_t(chrono::system_clock::now());
string TimeString = ctime(&CurrentTime);
Print("Current time: " + TimeString);
});
AddCommand(TimeCommand);
Command MessageBoxCommand("MessageBox", "Shows a message box with your title and text", "MessageBox MyTitle MyDescription", 2, [this](string Input)
{
vector<string> Parameters = StringToListWords(Input);
std::wstring Title = std::wstring(Parameters[0].begin(), Parameters[0].end());
LPCWSTR WideTitle = Title.c_str();
std::wstring Description = std::wstring(Parameters[1].begin(), Parameters[1].end());
LPCWSTR WideDescription = Description.c_str();
MessageBox(NULL, WideDescription, WideTitle, MB_OK | MB_ICONINFORMATION);
});
AddCommand(MessageBoxCommand);
Command CheckPrimeCommand("CheckPrime", "Tells you if a number is a prime number", "CheckPrime 2", 1, [this](string Input)
{
vector<string> Parameters = StringToListWords(Input);
try {
if (!Parameters.empty()) {
int number = std::stoi(Parameters[0]);
// The conversion was successful; you can use 'number' here.
}
else {
Print("Error: Missing parameter");
return;
}
}
catch (const std::invalid_argument& e) {
Print("Error: Invalid number");
return;
}
if (IsPrime(stoi(Parameters[0])))
{
Print("Number is a prime number");
}
else
{
Print("Number is not a prime number");
}
});
AddCommand(CheckPrimeCommand);
}
void CommandsList::AddCommand(const Command& command) {
CommandList.push_back(command);
}
bool CommandsList::RemoveCommandByName(const string& Name) {
auto it = std::find_if(CommandList.begin(), CommandList.end(), [Name](const Command& cmd)
{
return cmd.Name == Name;
});
if (it != CommandList.end()) {
CommandList.erase(it);
}
return true;
}
bool CommandsList::RemoveCommandByNumber(const int& Value) {
return RemoveCommandByName(GetCommandByNumber(Value)->Name);
}
void CommandsList::DisplayCommands() const {
int position = 0; // Initialize the position counter
for (const Command& cmd : CommandList) {
Print("Command Name: " + cmd.Name + " (" + to_string(position) + ")\n");
if (cmd.Description != "") {
Print("Description: " + cmd.Description + "\n");
}
if (cmd.Usage != "") {
Print("Usage: " + cmd.Usage + "\n");
}
Print("\n");
position++; // Increment the position counter
}
}
Command* CommandsList::GetCommandByName(const string& Name) {
for (Command& cmd : CommandList) {
if (cmd.Name == Name) {
return &cmd;
}
}
return nullptr; // Command not found
}
Command* CommandsList::GetCommandByNumber(const int& Value) {
if (CommandList[Value].Name != "")
{
return &CommandList[Value];
}
return nullptr; // Command not found
}
int CommandsList::GetCommandNumberByName(const string& Name) {
auto it = std::find_if(CommandList.begin(), CommandList.end(), [Name](const Command& cmd)
{
return cmd.Name == Name;
});
if (it != CommandList.end())
{
return std::distance(CommandList.begin(), it);
}
return -1; // Command not found
}
int CommandsList::GetCommandCount() {
return CommandList.size();
}