-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectory.js
73 lines (65 loc) · 1.61 KB
/
Directory.js
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
class Directory {
constructor(aName) {
this.name = aName;
this.files = {};
this.directories = {};
this.fileList = [];
this.directoryList = [];
this.list = "";
}
getName() {
return this.name;
}
addFile(aFile, aName) {
this.files[aName.toLowerCase()] = aFile;
this.fileList.push(aName);
}
getFile(aName) {
return this.files[aName.toLowerCase()];
}
addDirectory(aDirectory) {
aDirectory.setParent(this);
this.directories[aDirectory.getName().toLowerCase()] = aDirectory;
this.directoryList.push(aDirectory.getName());
}
getDirectory(aPath) {
while (aPath.lastIndexOf("/") === aPath.length - 1) {
aPath = aPath.substring(0, aPath.length - 1);
}
let index = aPath.indexOf("/");
while (index === 0) {
aPath = aPath.substring(1, aPath.length);
index = aPath.indexOf("/");
}
if (index > 0) {
let directory = aPath.substring(0, index).toLowerCase();
if (directory in this.directories) {
aPath = aPath.substring(index + 1, aPath.length);
return this.directories[directory].getDirectory(aPath);
}
} else {
let directory = aPath.toLowerCase();
if (directory in this.directories) {
return this.directories[directory];
}
}
return null;
}
setParent(aParent) {
this.directories[".."] = aParent;
}
makeList() {
this.fileList.sort();
this.directoryList.sort();
this.list = "";
for (let index = 0; index < this.directoryList.length; index++) {
this.list += this.directoryList[index] + "/\n";
}
for (let index = 0; index < this.fileList.length; index++) {
this.list += this.fileList[index] + "\n";
}
}
getList() {
return this.list;
}
}