-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
300 lines (277 loc) · 9.54 KB
/
main.cpp
File metadata and controls
300 lines (277 loc) · 9.54 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include <iostream>
#include <string>
#include <utility>
#include <Windows.h>
#include <functional>
#include <iomanip>
#include <algorithm>
#define MaxDirCount 20
#define MaxFileCount 20
struct File {
std::string name;
};
struct Directory {
std::string name;
int Dcount{};
int Fcount{};
struct Directory *LastDir{};
struct Directory *SubDir[MaxDirCount]{};
struct File *SubFile[MaxFileCount]{};
};
Directory *RootDir(std::string Username) {
auto *root = new Directory;
root->name = std::move(Username);
root->Dcount = 0;
root->Fcount = 0;
root->LastDir = nullptr;
return root;
}
int search(Directory *root, const std::string &target) {
Directory *temp = root;
for (int i = 0; i < temp->Dcount; i++) {
if (temp->SubDir[i]->name == target)
return i;
}
return -1;
}
void macro(Directory *Root, const std::string &username) {
system("color 02");
std::cout << username;
system("color");
if (Root->name == username) {
std::cout << ":~$ >> ";
} else {
std::cout << ":" << Root->name << " $ >> ";
}
}
Directory *ChangeDir(const std::string &CL, Directory *root) {
try {
Directory *temp = root;
if (CL == "..") {
if (!(temp->LastDir)) {
std::cout << "처음 디렉토리 입니다" << std::endl;
return temp;
} else {
temp = temp->LastDir;
return temp;
}
}
int index = search(temp, CL);
if (index > -1) {
temp = temp->SubDir[index];
return temp;
} else {
std::cout << "없는 디렉토리 입니다" << std::endl;
return temp;
}
} catch (std::exception e) {
std::cout << "에러가 발생했습니다 명령어를 점검해주십시오" << std::endl;
}
}
void MakeDir(Directory *root, const std::string &ND) {
Directory *temp = root;
int i = 0;
try {
if(ND.rfind(' ',0) == 0 || ND.empty()){
std::cout << "폴더명을 점검해 주십시오"<< std::endl;
return;
}
while (i < temp->Dcount) {
if (temp->SubDir[i]->name == ND)
throw ND;
i++;
}
Directory *NewDir = RootDir(ND);
NewDir->LastDir = temp;
temp->SubDir[i] = NewDir;
temp->Dcount++;
} catch (std::string e) {
std::cout << e << "는 이미 있는 폴더명 입니다" << std::endl;
}
}
void MakeFile(Directory *root, const std::string &NF) {
Directory *temp = root;
int i = 0;
try {
while (i < temp->Dcount) {
if (temp->SubFile[i]->name == NF) throw NF;
i++;
}
File *NewFile = new File;
NewFile->name = NF;
temp->SubFile[i] = NewFile;
temp->Fcount++;
} catch (std::string e) {
std::cout << e << "는 이미 있는 파일명 입니다" << std::endl;
}
}
void List(Directory *root) {
Directory *temp = root;
for (int i = 0; i < temp->Dcount; i++) {
std::cout << temp->SubDir[i]->name << "/ ";
}
for (int j = 0; j < temp->Fcount; j++) {
std::cout << temp->SubFile[j]->name << " ";
}
std::cout << std::endl;
}
void PWD(Directory *root) {
Directory *temp = root;
if (temp->LastDir) PWD(temp->LastDir);
std::cout << temp->name << "/";
}
void findDir(Directory *root, const std::string &target) {
Directory *temp = root;
std::cout << target << std::endl;
int i = 0;
if (temp->name == target) return PWD(temp);
while (i < temp->Dcount) {
if (temp->SubDir[i]->name == target) return PWD(temp);
i++;
}
std::cout << "찾는 폴더가 없습니다" << std::endl;
}
void findFile(Directory *root, const std::string &target) {
Directory *temp = root;
std::cout << target << std::endl;
int i = 0;
while (i < temp->Fcount) {
if (temp->SubFile[i]->name == target) {
PWD(temp);
std::cout << "/target" << std::endl;
}
i++;
}
std::cout << "찾는 폴더가 없습니다" << std::endl;
}
void Del2(Directory *root, Directory *deleted) {
Directory *temp = root;
int i = 0;
while (i < temp->Dcount) {
if (temp->SubDir[i] == deleted) {
temp->SubDir[i] = nullptr;
break;
}
i++;
}
while (i < temp->Dcount) {
if (temp->SubDir[i + 1]) temp->SubDir[i] = temp->SubDir[i + 1];
i++;
}
temp->Dcount--;
}
Directory *Delete(Directory *root) {
Directory *temp = root;
Directory *deleted = root->LastDir;
int i = 0, j = 0;
while (i < temp->Dcount) {
if (temp->SubDir[i]) Delete(temp->SubDir[i]);
i++;
}
while (j < temp->Fcount) {
if (temp->SubFile[i]) delete (temp->SubFile[i]);
j++;
}
Del2(temp->LastDir, temp);
delete (temp);
std::cout << "삭제 되었습니다" << std::endl;
return deleted;
}
bool customSort (Directory* i,Directory* j) {
char a = i->name[0];
char b = j->name[0];
return (a<b);
}
bool customSort2 (File* i,File* j) {
char a = i->name[0];
char b = j->name[0];
return (a<b);
}
int main() {
std::cout << "유저 이름을 입력해주세요 : ";
std::string Username;
std::cin >> Username;
system("cls");
Directory *root = RootDir(Username);
Directory *NL = root;
std::string cmd;
std::cin.ignore();
while (cmd != "exit") {
try {
macro(NL, Username);
getline(std::cin, cmd, '\n');
//Command(cmd,NL);
if (cmd.rfind("cd ", 0) == 0) {
std::string temp = cmd.substr(3);
NL = ChangeDir(temp, NL);
} else if (cmd.rfind("mkdir ", 0) == 0) {
std::string temp = cmd.substr(6);
MakeDir(NL, temp);
} else if (cmd.rfind("touch ", 0) == 0) {
std::string temp = cmd.substr(6);
MakeFile(NL, temp);
} else if (cmd.rfind("ls", 0) == 0) {
List(NL);
} else if (cmd.rfind("pwd", 0) == 0) {
PWD(NL);
std::cout << std::endl;
} else if (cmd.rfind("finddir ", 0) == 0) {
std::string temp = cmd.substr(8);
findDir(root, temp);
std::cout << std::endl;
} else if (cmd.rfind("findfile ", 0) == 0) {
std::string temp = cmd.substr(9);
findFile(NL, temp);
std::cout << std::endl;
} else if (cmd.rfind("delete ", 0) == 0) {
std::string temp = cmd.substr(7);
int index = search(NL,temp);
if(index != -1)
Delete(NL->SubDir[index]);
}else if (cmd.rfind("delete", 0) == 0) {
if (!(NL->name == Username)) Delete(NL);
else {
std::cout << "사용자 폴더는 지울 수 없습니다" << std::endl;
}
} else if (cmd.rfind("sort", 0) == 0) {
if (NL->Dcount == 0){
std::cout << "폴더가 있어야 합니다" << std::endl;
if (NL->Fcount == 0){
std::cout << "파일가 있어야 합니다" << std::endl;
continue;
}else{
std::sort(NL->SubFile,NL->SubFile+NL->Fcount, customSort2);
}
continue;
}else{
std::sort(NL->SubDir,NL->SubDir+NL->Dcount, customSort);
if (NL->Fcount == 0){
continue;
} else {
std::sort(NL->SubFile, NL->SubFile + NL->Fcount, customSort2);
}
}
} else if (cmd.rfind("real ", 0) == 0) {
const char *temp = cmd.substr(5).data();
system(temp);
} else if (cmd.rfind("help", 0) == 0) {
std::cout << "가상 파일 탐색기 입니다\n명령어 : " << std::endl;
std::cout << "cd %s :: %s폴더로 이동합니다" << std::endl;
std::cout << "mkdir %s :: 현재 위치에 %s폴더를 만듭니다" << std::endl;
std::cout << "touch %s :: 현재 위치에 %s파일을 만듭니다" << std::endl;
std::cout << "finddir %s :: 현재 위치에 있는 %s 폴더를 찾습니다" << std::endl;
std::cout << "findfile %s :: 현재 위치에 있는 %s 파일을 찾습니다" << std::endl;
std::cout << "delete (%s) :: 현재 위치의 폴더를 삭제 하고 전 폴더로 이동합니다 (%s)가 존재할경우 그폴더를 지웁니다" << std::endl;
std::cout << "sort :: 현재 위치 폴더와 파일 목록을 정렬합니다 정렬은 가/1/a 순입니다" << std::endl;
std::cout << "ls :: 현재 위치 폴더와 파일 목록을 제공합니다" << std::endl;
std::cout << "pwd :: root폴더부터 현재까지의 경로를 보여줍니다" << std::endl;
} else {
std::cout << cmd << " 명령어는 존재 하지 않습니다" << std::endl;
std::cout << "기능이 궁금하시다면 help를 사용해주십시오" << std::endl;
}
} catch (std::exception e) {
std::cout << "에러가 발생했습니다 명령어를 점검해주십시오" << std::endl;
}
}
return 0;
}