-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreaderIni.go
More file actions
299 lines (281 loc) · 6.42 KB
/
readerIni.go
File metadata and controls
299 lines (281 loc) · 6.42 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package configuration
import (
"bufio"
"bytes"
"errors"
"io"
"strings"
)
func (cr *configReader) parseIniData(r *bufio.Reader, it intermediateTree, si []structInfo, sourceId int) error {
prefix := ""
for {
str, isName, err := cr.readIniNameOrSection(r)
if err != nil {
if err == io.EOF {
break
}
return err
}
if !isName {
if strings.ToUpper(str) == "ROOT" {
prefix = ""
continue
}
if str == "" {
return errors.New("wrong format: specify section or subsection name " + cr.currentPointInfo())
}
if strings.HasSuffix(str, ".") {
return errors.New("wrong format: section or subsection must not end with '.' " + cr.currentPointInfo())
}
if strings.HasPrefix(str, ".") {
if prefix == "" {
return errors.New("wrong format: subsection for root must not start with '.' " + cr.currentPointInfo())
}
prefix = prefix + str[1:] + "."
} else {
prefix = str + "."
}
continue
}
name := prefix + str
isSlice := strings.HasSuffix(name, "[]")
if isSlice {
name = name[:len(name)-2]
}
openIndex := strings.Index(name, "[")
isMap := len(name) >= 4 && !isSlice && openIndex > 0 && openIndex < len(name)-2 && strings.HasSuffix(name, "]")
key := ""
if isMap {
key = name[openIndex+1 : len(name)-1]
name = name[:openIndex]
}
found, foundInfo, continue_, err := cr.findFieldByName(r, si, name, false)
if err != nil {
if err == io.EOF {
break
}
return err
} else if !found && continue_ {
continue
} else if !found && !continue_ {
break
}
value, err := cr.readIniValue(r)
if err != nil && err != io.EOF {
return err
}
addValue(foundInfo, it, name, value, key, isSlice, sourceId)
if err == io.EOF {
break
}
}
return nil
}
func (cr *configReader) readIniNameOrSection(r *bufio.Reader) (string, bool, error) {
var buffer bytes.Buffer
started := false
finished := false
readToEol := false
rn := ' '
isName := true
var err error = nil
for {
if rn, _, err = r.ReadRune(); err == nil {
cr.data.currentPos++
if readToEol {
if !containsRune([]rune{'\r', '\n', ' ', '\t', ';', '#'}, rn) {
return "", false, cr.invalidCharacterError()
} else {
cr.checkNewLine(rn)
if rn == '\n' {
break
} else if rn == '\r' {
continue
} else if rn == ';' || rn == '#' {
err = cr.readComment(r, true)
if err != nil && err != io.EOF {
return "", false, err
}
break
}
}
} else if (rn == '#' || rn == ';') && !started {
err = cr.readComment(r, true)
if err != nil && err != io.EOF {
return "", false, err
}
continue
} else if rn == '[' {
if !isName {
return "", false, cr.invalidCharacterError()
} else if !started {
isName = false
continue
}
} else if rn == ']' && started && !isName {
readToEol = true
continue
} else if rn == '\r' || rn == '\n' {
if !started {
cr.checkNewLine(rn)
continue
} else if started {
return "", false, cr.invalidCharacterError()
}
} else if (rn == ' ' || rn == '\t') && !started {
continue
} else if rn != ' ' && rn != '\t' && rn != '=' && finished {
return "", false, cr.invalidCharacterError()
} else if rn == '=' {
if isName {
break
} else {
return "", false, cr.invalidCharacterError()
}
}
buffer.WriteRune(rn)
started = true
} else if err != io.EOF {
return "", false, err
} else {
if started && !finished {
return "", false, errors.New("unexpected end of file")
}
break
}
}
str := buffer.String()
if str == "" && started {
return "", false, errors.New("wrong format: can't read name")
}
str = strings.Trim(str, " \t")
str = strings.ToLower(str)
return str, isName, err
}
func (cr *configReader) readIniValue(r *bufio.Reader) (string, error) {
var buffer bytes.Buffer
started := false
finished := false
inQuote := false
quote := ' '
backslash := false
readToBol := false
addNewLine := false
newLineCntr := 0
escape := false
rn := ' '
var err error = nil
for {
if rn, _, err = r.ReadRune(); err == nil {
cr.data.currentPos++
if rn == '\n' {
newLineCntr += 1
if newLineCntr > 1 {
break
}
} else if rn != '\r' {
newLineCntr = 0
}
if readToBol {
if rn == '\r' || rn == '\n' {
cr.checkNewLine(rn)
if rn == '\n' && addNewLine {
buffer.WriteRune('\n')
addNewLine = false
}
continue
} else {
readToBol = false
}
}
if (rn == ' ' || rn == '\t') && (!started || finished) {
continue
} else if rn == '\\' {
if started && !inQuote {
backslash = true
continue
} else if started && inQuote && !finished && !escape {
escape = true
continue
}
} else if backslash {
if rn == '\r' || rn == '\n' {
cr.checkNewLine(rn)
if rn == '\n' {
buffer.WriteRune('\n')
} else {
addNewLine = true
}
backslash = false
readToBol = true
continue
} else if rn == ';' || rn == '#' {
err = cr.readComment(r, true)
if err != nil && err != io.EOF {
return "", err
}
buffer.WriteRune('\n')
backslash = false
continue
} else {
return "", cr.invalidCharacterError()
}
} else if rn == '#' || rn == ';' {
if !inQuote || finished {
finished = true
err = cr.readComment(r, true)
if err != nil && err != io.EOF {
return "", err
}
break
}
} else if rn == '\r' || rn == '\n' {
if inQuote {
return "", cr.invalidCharacterError()
} else {
cr.checkNewLine(rn)
if rn == '\n' {
break
} else {
continue
}
}
} else if rn == '"' || rn == '\'' {
if !started && !inQuote {
inQuote = true
quote = rn
started = true
continue
} else if started && inQuote && quote == rn {
if escape {
buffer.WriteRune(rn)
escape = false
continue
} else {
inQuote = false
finished = true
continue
}
} else if started && !inQuote {
return "", cr.invalidCharacterError()
}
} else if !containsRune([]rune{' ', '\t', '#', ';', '\r', '\n'}, rn) && finished {
return "", cr.invalidCharacterError()
}
if escape {
buffer.WriteRune('\\')
escape = false
}
buffer.WriteRune(rn)
started = true
} else if err != io.EOF {
return "", err
} else {
if inQuote {
return "", cr.invalidCharacterError()
}
break
}
}
return buffer.String(), err
}