-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandPrompt.cpp
101 lines (92 loc) · 2.48 KB
/
CommandPrompt.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
/*
Author: Jon Shidal
Purpose: CSE 332 lab 5
*/
#include "CommandPrompt.h"
#include<iostream>
#include <sstream>
using namespace std;
CommandPrompt::CommandPrompt() : CWD("root"), fileSystem(nullptr), fileFactory(nullptr) {}
int CommandPrompt::run() {
while (1) {
string input = prompt();
if (input == "q") {
return userquit;
}
else if (input == "help") {
listCommands();
}
else if (input.find_first_of(' ') == string::npos) { // single word command
auto it = commands.find(input);
if (it == commands.end()) {
cout << "invalid command" << endl;
}
else {
int result = it->second->execute(CWD, ""); // no additional options
if (result != AbstractCommand::success) {
cout << "command failed" << endl;
}
}
}
else{ // multiple words, parse
istringstream iss(input);
string command;
iss >> command;
if (command == "help") {
string commandToDisplay;
iss >> commandToDisplay;
auto it = commands.find(commandToDisplay);
if (it != commands.end()) { // if valid command, display its information
it->second->displayInfo();
}
else {
cout << "command does not exist" << endl;
}
}
else {
auto it = commands.find(command);
if (it != commands.end()) {
// substring options and call execute
string options = input.substr(input.find_first_of(' ') + 1, string::npos);
int result = it->second->execute(CWD, options);
if (result != AbstractCommand::success) {
cout << "command failed" << endl;
}
}
else {
cout << "invalid command" << endl;
}
}
}
}
}
string CommandPrompt::prompt() {
cout << "To quit: q, ";
cout << "For a list of commands: help, ";
cout << "For information about a specific command: help <command name>" << endl;
cout << CWD << " $ ";
string input;
getline(cin, input);
return input;
}
void CommandPrompt::listCommands() {
cout << "Commands:" << endl;
for (auto it = commands.begin(); it != commands.end(); ++it) {
cout << it->first << endl;
}
}
int CommandPrompt::addCommand(string name, AbstractCommand* cmd) {
auto result = commands.insert({ name,cmd });
if (result.second) {
return success;
}
else {
return failedtoaddcommand;
}
}
void CommandPrompt::setFileSystem(AbstractFileSystem* filesystem) {
fileSystem = filesystem;
}
void CommandPrompt::setFileFactory(AbstractFileFactory* filefactory) {
fileFactory = filefactory;
}