-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommand.h
77 lines (57 loc) · 1.72 KB
/
Command.h
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
#pragma once
#include <string>
#include <vector>
#include "utils.h"
#include "AliasHandler.h"
using namespace std;
class Command {
private:
vector<char**>* commands = new vector<char**>;
vector<string>* strCommands = new vector<string>;
public:
Command(const string& command) {
AliasHandler* aliasHandler = AliasHandler::getInstance();
auto pipelineCommands = split(command, "|");
for(auto cmd : *pipelineCommands) {
cmd = aliasHandler->applyAlias(trim(cmd));
strCommands->push_back(cmd);
char** standardFormatCommand = convertToStandardFormat(cmd);
commands->push_back(standardFormatCommand);
}
}
char** getCommand(int index) const {
return commands->at(index);
}
string getStringCommand(int index) const {
return strCommands->at(index);
}
int commandCount() const {
return commands->size();
}
bool isChangeDirectoryCommand() const {
return isCommand("cd");
}
bool isFileExecutionCommand() const {
return isCommand("fshell");
}
bool isExitCommand() const {
return isCommand("exit");
}
bool isAliasCommand() const {
return isCommand("alias");
}
bool isUnaliasCommand() const {
return isCommand("unalias");
}
private:
static char** convertToStandardFormat(const string& command) {
auto pieces = split(command, " ");
char** args = toCharArrayArray(*pieces);
args = appendNull(args, pieces->size());
return args;
}
bool isCommand(const string&& command) const {
char* cmd = getCommand(0)[0];
return commandCount() == 1 && strcmp(cmd, command.c_str()) == 0;
}
};