-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTerminal.cpp
168 lines (148 loc) · 3.72 KB
/
Terminal.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
#include "Terminal.h"
#include <sstream>
Terminal::Terminal()
{
this->tree = new Tree();
getcwd(this->cwd, sizeof(cwd));
this->commandManager = new Commands();
this->tree->loadTree();
}
Terminal::~Terminal()
{
delete this->tree;
delete this->commandManager;
}
void Terminal::Initiate()
{
std::system("clear");
std::cout << "Welcome to the Distribute File System" << std::endl;
std::cout << "Actual remote directory is: " << std::endl;
this->commandManager->pwd(this->tree);
std::cout << std::endl;
std::cout << "Actual local directory is: " << std::endl;
this->commandManager->lpwd();
std::cout << std::endl;
std::cout << "If you want to leave, please write <exit>." << std::endl;
}
std::string Terminal::ReadInput()
{
std::string input = "";
this->commandManager->pwd(this->tree);
std::cout << ">";
std::getline(std::cin, input);
return input;
}
void Terminal::Tokenize(std::vector<std::string> &tokens, std::string option){
std::stringstream ss(option);
std::string token = "";
if (option != "")
{
while(std::getline(ss, token, ' '))
{
tokens.push_back(token);
}
}
}
std::string Terminal::filterCommand(std::vector<std::string> tokens)
{
std::string firstToken = "";
firstToken = tokens.at(0);
return firstToken;
}
std::string Terminal::filterFirstParam(std::vector<std::string> tokens)
{
std::string firstParam = "";
firstParam = tokens.at(1);
return firstParam;
}
std::string Terminal::filterSecondParam(std::vector<std::string> tokens)
{
std::string secondParam = "";
secondParam = tokens.at(2);
return secondParam;
}
void Terminal::executeCommand(std::string command, std::string firstParam, std::string secondParam)
{
bool executed = false;
if (command == "ls")
{
this->commandManager->ls(this->tree);
executed = true;
}
if (command == "pwd")
{
this->commandManager->pwd(this->tree);
executed = true;
}
if (command == "cd")
{
this->commandManager->cd(this->tree, firstParam);
executed = true;
}
if (command == "mv")
{
this->commandManager->mv(this->tree, firstParam, secondParam);
executed = true;
}
if (command == "cp")
{
this->commandManager->cp(this->tree, firstParam, secondParam);
executed = true;
}
if (command == "mkdir")
{
this->commandManager->mkdir(this->tree, firstParam);
executed = true;
}
if (command == "rmdir")
{
this->commandManager->rmdir(this->tree, firstParam);
executed = true;
}
if (command == "rm")
{
this->commandManager->rm(this->tree, firstParam);
executed = true;
}
if (command == "lls")
{
this->commandManager->lls();
executed = true;
}
if (command == "lcd")
{
this->commandManager->lcd(firstParam);
executed = true;
}
if (command == "lpwd")
{
this->commandManager->lpwd();
executed = true;
}
if (command == "upload")
{
this->commandManager->upload(this->tree, firstParam);
executed = true;
}
if (command == "format")
{
if (this->commandManager->format(firstParam))
{
delete this->tree;
this->tree = new Tree();
this->tree->loadTree();
}
executed = true;
}
if (command == "download")
{
this->commandManager->download(this->tree, firstParam);
executed = true;
}
if(!executed) std::cout << "Command "<< command << " not found." << std::endl;
else std::cout << std::endl;
}
Tree* Terminal::getTree()
{
return this->tree;
}