-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reader.go
229 lines (193 loc) · 4.93 KB
/
Reader.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
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
package mxt
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"strings"
)
//********************************************************************** Reader
// A Reader allows to read chunks from a mxt file.
type Reader struct {
expPatt string
*bufio.Reader
}
// NewReader returns a new Reader that reads from r.
func NewReader(r io.Reader) *Reader {
return &Reader{
expPatt: "",
Reader: bufio.NewReader(r),
}
}
//***************************************************************** Reader.func
func readString(r *bufio.Reader, delim []byte) (string, error) {
delimByte := delim[0]
delimTail := delim[1:]
var builder strings.Builder
search := true
for search {
tmpStr, err := r.ReadString(delimByte)
builder.WriteString(tmpStr)
if err != nil {
return builder.String(), err
}
buf, err := r.Peek(len(delimTail))
if err != nil {
return builder.String(), err
}
if bytes.Equal(buf, delimTail) {
search = false
_, err = r.Read(buf)
if err != nil {
return builder.String(), err
}
_, err = builder.Write(buf)
if err != nil {
return builder.String(), err
}
}
}
return builder.String(), nil
}
func parseComment(rawComment string) string {
lines := strings.Split(rawComment, "//")
for i := 0; i < len(lines); i++ {
lines[i] = strings.TrimSpace(lines[i])
}
var builder strings.Builder
space := false
for _, line := range lines {
s := strings.TrimSpace(line)
if len(s) > 0 {
if space {
builder.WriteByte(' ')
}
builder.WriteString(s)
space = true
} else {
builder.WriteByte('\n')
space = false
}
}
return strings.TrimSpace(builder.String())
}
func (my *Reader) readHeader() (Header, error) {
header := Header{"", ""}
str, err := readString(my.Reader, []byte("-->"))
if err != nil {
return header, err
}
nameBeg := strings.Index(str, " ")
if nameBeg == -1 {
return header, fmt.Errorf("no name beg")
}
str = strings.TrimSpace(str[nameBeg+1:])
nameEnd := strings.IndexAny(str, " \n")
if nameEnd == -1 {
return header, fmt.Errorf("invalid header")
}
header.Name = str[:nameEnd]
commentEnd := strings.LastIndexAny(str, " \n")
if nameEnd == -1 {
return header, fmt.Errorf("invalid header")
}
header.Comment = parseComment(str[nameEnd:commentEnd])
my.expPatt, err = my.ReadString('\n')
if err != nil && err != io.EOF {
return header, err
}
my.expPatt = strings.TrimSpace(my.expPatt)
return header, nil
}
func trimLastNewLineSuffix(str string) string {
if strings.HasSuffix(str, "\r\n") {
return strings.TrimSuffix(str, "\r\n")
}
return strings.TrimSuffix(str, "\n")
}
func (my *Reader) readContent() (string, error) {
delim := "//"
if my.expPatt != "" {
delim = "//" + my.expPatt
}
cnt, err := readString(my.Reader, []byte(delim))
if err != nil && err != io.EOF {
return "", err
}
return trimLastNewLineSuffix(strings.TrimSuffix(cnt, delim)), nil
}
// ReadChunk reads one Chunk from r.
// If there is no data left to be read, ReadChunk returns a empty Chunk, io.EOF.
func (my *Reader) ReadChunk() (ch Chunk, err error) {
ch.Header, err = my.readHeader()
if err == nil {
ch.Content, err = my.readContent()
}
return ch, err
}
//************************************************************************ Read
// ReadChunks creates a Reader and reads all chunks from r and stores them in
// the map.
// The name will be used as key, the content will be stored as value.
// Possible comments in the mxt will be ignored.
//
// A successful call returns err == nil, not err == io.EOF.
// Because ReadChunks is defined to read until EOF, it does not treat end of
// file as an error to be reported.
func ReadChunks(r io.Reader) ([]Chunk, error) {
res := []Chunk{}
var err error
my := NewReader(r)
for err == nil {
var c Chunk
c, err = my.ReadChunk()
if err == nil || err != io.EOF {
res = append(res, c)
}
}
if err == io.EOF {
err = nil
}
return res, err
}
// ReadStringChunks
func ReadStringChunks(str string) ([]Chunk, error) {
return ReadChunks(strings.NewReader(str))
}
// ReadFileChunks reads the mxt file behind path with Read.
func ReadFileChunks(path string) ([]Chunk, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
return ReadChunks(file)
}
// Read creates a Reader and reads all with ReadChunks and stores them in the
// map.
// The name will be used as key, the content will be stored as value.
// Possible comments in the mxt will be ignored.
func Read(r io.Reader) (map[string]string, error) {
res := make(map[string]string)
chunks, err := ReadChunks(r)
if err == nil {
for _, c := range chunks {
res[c.Header.Name] = c.Content
}
}
return res, err
}
// ReadString reads str with Read.
func ReadString(str string) (map[string]string, error) {
return Read(strings.NewReader(str))
}
// ReadFile reads the mxt file behind path with Read.
func ReadFile(path string) (map[string]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
return Read(file)
}