-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageLab.cpp
More file actions
57 lines (46 loc) · 1.1 KB
/
Copy pathImageLab.cpp
File metadata and controls
57 lines (46 loc) · 1.1 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 <iostream>
#include <sstream>
#include "MyString.h"
#include "MyVector.h"
#include "Commands.h"
// Splits the input string into separate words
MyVector<MyString> splitCommand(const MyString& input) {
MyVector<MyString> tokens;
std::istringstream stream(input.c_str());
MyString token;
while (stream >> token) {
tokens.push_back(token);
}
return tokens;
}
// Parses and executes a command
void executeCommand(MyString& command) {
MyVector<MyString> args = splitCommand(command);
if (args.size() == 0) return;
MyString cmdName = args[0];
// Remove the first element (command name) - only pure arguments remain
MyVector<MyString> cmdArgs;
for (size_t i = 1; i < args.size(); i++)
cmdArgs.push_back(args[i]);
Command* cmd = Command::create(cmdName, cmdArgs);
if (!cmd) {
std::cout << "Invalid command!\n";
return;
}
cmd->execute();
delete cmd;
}
// Main program loop
// "quit" exits the program.
int main()
{
MyString input;
while (true)
{
std::cout << ">> ";
getline(std::cin, input);
if (input == "quit") break;
if (input.empty()) continue;
executeCommand(input);
}
}