-
Notifications
You must be signed in to change notification settings - Fork 3
/
controlfile.go
202 lines (175 loc) · 3.75 KB
/
controlfile.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
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
package dpkg
import (
"bufio"
"bytes"
"fmt"
"io"
"strings"
)
type ControlFile struct {
cache *map[string]string
Raw string
}
func LoadPackages(fPath string) ([]ControlFile, error) {
f, err := ReadFile(fPath)
if err != nil {
return nil, err
}
defer f.Close()
return NewControlFiles(f, ScanBufferSize)
}
func NewControlFiles(r io.Reader, bufSize int) ([]ControlFile, error) {
s := bufio.NewScanner(r)
s.Buffer(nil, ScanBufferSize)
s.Split(splitControlFile)
var ts []ControlFile
for s.Scan() {
raw := string(s.Bytes())
cf, err := ParseControlFile(string(s.Bytes()))
if err != nil {
return nil, err
}
ts = append(ts, ControlFile{
cache: &cf,
Raw: raw,
})
}
return ts, nil
}
func NewControlFile(raw string) (ControlFile, error) {
cache, err := ParseControlFile(raw)
if err != nil {
return ControlFile{}, err
}
return ControlFile{
Raw: raw,
cache: &cache,
}, nil
}
func ParseControlFile(raw string) (map[string]string, error) {
s := bufio.NewScanner(bytes.NewBufferString(raw))
s.Buffer(nil, len(raw))
s.Split(splitControlFileLine)
f := make(map[string]string)
for s.Scan() {
line := s.Text()
if line == "" {
continue
}
kv := strings.SplitN(line, ":", 2)
if len(kv) != 2 {
if Strict {
return nil, FormatError{"ConrolfileField", line, nil}
}
continue
}
f[strings.ToLower(kv[0])] = strings.TrimRight(kv[1], " \n")
}
if len(f) == 0 {
return nil, NotFoundError{"Empty result"}
}
return f, nil
}
func (d ControlFile) String() string {
return d.Raw
}
func (d ControlFile) Get(key string) string {
if d.cache == nil {
cache, err := ParseControlFile(d.Raw)
if err != nil {
DebugPrintln("ControlFile.GetString E:", err)
return ""
}
d.cache = &cache
}
cache := *d.cache
return strings.TrimSpace(cache[strings.ToLower(key)])
}
func (d ControlFile) GetArray(key string, sep string) []string {
s := d.Get(key)
return getArrayString(s, sep)
}
func (d ControlFile) GetMultiline(key string) []string {
return getMultiline(d.Get(key))
}
func last(s []string) string {
switch n := len(s); n {
case 0:
return ""
default:
return s[n-1]
}
}
func getArrayString(s string, sep string) []string {
var r []string
for _, c := range strings.Split(s, sep) {
f := strings.TrimSpace(c)
if f != "" {
r = append(r, f)
}
}
return r
}
func getMultiline(s string) []string {
if s == "" {
return nil
}
var ret []string
for _, f := range strings.Split(s, "\n") {
ret = append(ret, strings.TrimSpace(f))
}
return ret
}
func splitControlFileLine(chunk []byte, noMoreChunk bool) (int, []byte, error) {
chunkSize := len(chunk)
maybeWrap := 0
EndOfChunk := func(i int) bool { return i+1 == chunkSize }
NextIsNotSpace := func(i int) bool { c := chunk[i+1]; return c != ' ' && c != '\t' }
for i, c := range chunk {
if EndOfChunk(i) {
continue
}
switch c {
case '\n':
if maybeWrap != 0 {
continue
} else if NextIsNotSpace(i) {
return i + 1, chunk[:i], nil
}
case '\\':
//TODO: strip the wrap characters before returning
maybeWrap = i
default:
if NextIsNotSpace(i) {
maybeWrap = 0
}
}
}
if !noMoreChunk {
return 0, nil, nil
} else if chunkSize > 0 {
return chunkSize, chunk, nil
}
return 0, nil, fmt.Errorf("End of file")
}
func splitControlFile(chunk []byte, noMoreChunk bool) (int, []byte, error) {
chunkSize := len(chunk)
EndOfChunk := func(i int) bool { return i+1 == chunkSize }
for i, c := range chunk {
if c != '\n' {
continue
}
if !EndOfChunk(i) && chunk[i+1] == '\n' {
return i + 2, chunk[:i], nil
}
if EndOfChunk(i) && noMoreChunk {
return i + 1, chunk[:i], nil
}
}
if !noMoreChunk {
return 0, nil, nil
} else if chunkSize > 0 {
return chunkSize, chunk, nil
}
return 0, nil, fmt.Errorf("end of file")
}