-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
63 lines (55 loc) · 1.91 KB
/
cache.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
package main
import (
"encoding/xml"
"fmt"
"strconv"
"strings"
)
type processCache struct {
depth int
currentDirectory []string
directoryCounter map[string]int
fileCounter map[string]int
totalFiles int
innerText string
line string
file bool
ioActions []ioAction
}
func (p *processCache) newDirectory(name string) {
p.currentDirectory = append(p.currentDirectory, name)
dirKey := strings.Join(p.currentDirectory, "/")
if _, ok := p.directoryCounter[dirKey]; ok {
p.directoryCounter[dirKey]++
p.currentDirectory = append(p.currentDirectory, strconv.Itoa(p.directoryCounter[dirKey]))
} else {
p.directoryCounter[dirKey] = 0
p.currentDirectory = append(p.currentDirectory, "0")
}
p.ioActions = append(p.ioActions, ioAction{actionType: newDirectory, path: fmt.Sprintf("%s/%d", dirKey, p.directoryCounter[dirKey]), ready: true})
}
func (p *processCache) exitDirectory() {
p.currentDirectory = p.currentDirectory[:len(p.currentDirectory)-2]
}
func (p *processCache) openFile(prefix string) {
filekey := strings.Join(p.currentDirectory, "/") + "/" + prefix
if _, ok := p.fileCounter[filekey]; ok {
p.fileCounter[filekey]++
} else {
p.fileCounter[filekey] = 0
}
p.ioActions = append(p.ioActions, ioAction{actionType: writeFile, path: fmt.Sprintf("%s.%d.xml", filekey, p.fileCounter[filekey]), lines: []string{xml.Header}})
p.file = true
p.totalFiles++
}
func (p *processCache) closeFile() {
p.ioActions[len(p.ioActions)-1].ready = true
p.file = false
}
func (p *processCache) appendLine(line string) {
p.ioActions[len(p.ioActions)-1].lines = append(p.ioActions[len(p.ioActions)-1].lines, line)
}
func (p *processCache) appendFile(name, text string) {
p.ioActions = append(p.ioActions, ioAction{actionType: writeFile, path: strings.Join(append(p.currentDirectory, name), "/") + ".xml", ready: true, lines: []string{xml.Header + text}})
p.totalFiles++
}