-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
151 lines (124 loc) · 2.53 KB
/
main.go
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
package main
import (
"fmt"
"sort"
"strings"
)
func main() {
fmt.Println("Hello world")
}
type (
FileSystem struct {
fs map[string]*Dir
}
File struct {
name string
body string
}
Dir struct {
fsd map[string]*Dir
files []*File
}
)
func Constructor() FileSystem {
mp := make(map[string]*Dir)
mp["/"] = &Dir{map[string]*Dir{}, []*File{}}
return FileSystem{
fs: mp,
}
}
func (this *FileSystem) Ls(path string) []string {
//fmt.Println("LS", path)
dir := this.fs["/"]
var result []string
pa := strings.Split(path, "/")
for i := 0; i < len(pa); i++ {
if pa[i] == "" {
continue
}
newDir, ok := dir.fsd[pa[i]]
if !ok {
break
}
dir = newDir
}
for _, v := range dir.files {
if v.name == pa[len(pa)-1] {
return []string{v.name}
}
result = append(result, v.name)
}
for v := range dir.fsd {
result = append(result, v)
}
sort.Strings(result)
return result
}
func (this *FileSystem) Mkdir(path string) {
//fmt.Println("Mkdir", path)
dir := this.fs["/"]
pa := strings.Split(path, "/")
for i := 0; i < len(pa); i++ {
if pa[i] == "" {
continue
}
newDir, ok := dir.fsd[pa[i]]
if !ok {
dir.fsd[pa[i]] = &Dir{map[string]*Dir{}, []*File{}}
newDir = dir.fsd[pa[i]]
}
dir = newDir
}
}
func (this *FileSystem) AddContentToFile(filePath string, content string) {
//fmt.Println("AddContentToFile", filePath, content)
dir := this.fs["/"]
path := strings.Split(filePath, "/")
for i := 0; i < len(path)-1; i++ {
if path[i] == "" {
continue
}
newDir, ok := dir.fsd[path[i]]
if !ok {
//fmt.Println(path, "not exists")
return
}
dir = newDir
}
for i := 0; i < len(dir.files); i++ {
if dir.files[i].name == path[len(path)-1] {
dir.files[i].body += content
return
}
}
dir.files = append(dir.files, &File{path[len(path)-1], content})
}
func (this *FileSystem) ReadContentFromFile(filePath string) string {
//fmt.Println("ReadContentFromFile", filePath)
dir := this.fs["/"]
path := strings.Split(filePath, "/")
for i := 0; i < len(path)-1; i++ {
if path[i] == "" {
continue
}
newDir, ok := dir.fsd[path[i]]
if !ok {
//fmt.Println(path, path[i], "not exists")
}
dir = newDir
}
for i := 0; i < len(dir.files); i++ {
if dir.files[i].name == path[len(path)-1] {
return dir.files[i].body
}
}
return ""
}
/**
* Your FileSystem object will be instantiated and called as such:
* obj := Constructor();
* param_1 := obj.Ls(path);
* obj.Mkdir(path);
* obj.AddContentToFile(filePath,content);
* param_4 := obj.ReadContentFromFile(filePath);
*/