forked from Matir/adifparser
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adifreader.go
260 lines (236 loc) · 5.43 KB
/
adifreader.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
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
package adifparser
import (
"bufio"
"errors"
"io"
"strconv"
)
// Interface for ADIFReader
type ADIFReader interface {
ReadRecord() (ADIFRecord, error)
RecordCount() int
}
// Real implementation of ADIFReader
type baseADIFReader struct {
// Underlying bufio Reader
rdr *bufio.Reader
// Whether or not the header is included
noHeader bool
// Whether or not the header has been read
headerRead bool
// Version string of the adif file
version string
// Record count
records int
}
type dedupeADIFReader struct {
baseADIFReader
// Store seen entities
seen map[string]bool
}
type elementData struct {
// ADIF field name (in ASCII, set to lowercase)
name string
// ADIF field (if nil, only the field name exists)
value string
// ADIF data type indicator (optional, set to uppercase)
typecode byte
// ADIF specifier always has a corresponding value
// If hasValue is false, string inside "<>" is
// a tag without a value
hasValue bool
// ADIF specifier can optionally have a type
hasType bool
// Length of value bytes/string
valueLength int
}
func (ardr *baseADIFReader) ReadRecord() (ADIFRecord, error) {
record := NewADIFRecord()
if !ardr.headerRead {
ardr.readHeader()
}
foundeor := false
for !foundeor {
element, err := ardr.readElement()
if err != nil {
if err != io.EOF {
adiflog.Printf("readElement: %v", err)
}
return nil, err
}
if element.name == "eor" && !element.hasValue {
foundeor = true
break
}
if element.hasValue {
// TODO: accomodate types
record.values[element.name] = element.value
}
}
// Successfully parsed the record
ardr.records++
return record, nil
}
// Errors
var ErrInvalidFieldLength = errors.New("invalid field length")
var ErrTypeCodeExceedOneByte = errors.New("ADIF typecode exceeds one byte")
var ErrUnknownColons = errors.New("unknown colons in the tag")
func (ardr *dedupeADIFReader) ReadRecord() (ADIFRecord, error) {
for {
record, err := ardr.baseADIFReader.ReadRecord()
if err != nil {
return nil, err
}
fp := record.Fingerprint()
if _, ok := ardr.seen[fp]; !ok {
ardr.seen[fp] = true
return record, nil
}
}
}
func NewADIFReader(r io.Reader) *baseADIFReader {
reader := &baseADIFReader{}
reader.init(r)
return reader
}
func NewDedupeADIFReader(r io.Reader) *dedupeADIFReader {
reader := &dedupeADIFReader{}
reader.init(r)
reader.seen = make(map[string]bool)
return reader
}
func (ardr *baseADIFReader) init(r io.Reader) {
ardr.rdr = bufio.NewReader(r)
// Assumption
ardr.version = "2.0"
ardr.records = 0
// check header
filestart, err := ardr.rdr.Peek(1)
if err != nil {
// TODO: Log the error somewhere
return
}
ardr.noHeader = filestart[0] == '<'
// if header does not exist, header can be skipped
// and treated as read
ardr.headerRead = ardr.noHeader
}
func (ardr *baseADIFReader) readHeader() {
foundeoh := false
for !foundeoh {
element, err := ardr.readElement()
if err != nil {
// TODO: Log the error somewhere
return
}
if element.name == "eoh" && !element.hasValue {
foundeoh = true
break
}
if element.name == "adif_ver" && element.hasValue {
ardr.version = element.value
}
}
ardr.headerRead = true
}
func (ardr *baseADIFReader) RecordCount() int {
return ardr.records
}
func (ardr *baseADIFReader) readElement() (*elementData, error) {
var c byte
var err error
fieldname := make([]byte, 0, 128)
fieldvalue := make([]byte, 0, 256)
var fieldtype byte
fieldlenstr := make([]byte, 0, 8)
var fieldlength int = 0
data := &elementData{}
// Look for "<" (open tag) first
foundopentag := false
for !foundopentag {
// Read a byte (aka character)
c, err = ardr.rdr.ReadByte()
if err != nil {
return nil, err
}
foundopentag = c == '<'
}
// Get field name
data.hasValue = false
data.hasType = false
// Look for ">" (close tag) next
foundclosetag := false
foundcolonnum := 0
foundtype := false
for !foundclosetag {
// Read a byte (aka character)
c, err = ardr.rdr.ReadByte()
if err != nil {
return nil, err
}
foundclosetag = c == '>'
if foundclosetag {
break
}
switch foundcolonnum {
case 0:
// no colon yet: append the byte to the field name
if c == ':' {
foundcolonnum++
data.hasValue = true
} else {
fieldname = append(fieldname, c)
}
case 1:
// 1 colon found:
// handle the byte as a digit in the length
if c == ':' {
foundcolonnum++
data.hasType = true
} else {
if c >= '0' && c <= '9' {
fieldlenstr = append(fieldlenstr, c)
} else {
return nil, ErrInvalidFieldLength
}
}
case 2:
// 2 colons found:
// pick up only one byte and use it as a field type
if !foundtype {
fieldtype = c
foundtype = true
} else {
return nil, ErrTypeCodeExceedOneByte
}
default:
// This code should not be reached...
return nil, ErrUnknownColons
}
}
// Make the field name lowercase
data.name = string(bStrictToLower(fieldname))
// Make the field type name uppercase
if foundtype {
data.typecode = charToUpper(fieldtype)
}
// Get field length
if data.hasValue {
fieldlength, err = strconv.Atoi(string(fieldlenstr))
if err != nil {
return nil, err
}
data.valueLength = fieldlength
// Get field value/content,
// with the byte length specified by the field length
for i := 0; i < fieldlength; i++ {
c, err = ardr.rdr.ReadByte()
if err != nil {
return nil, err
}
fieldvalue = append(fieldvalue, c)
}
data.value = string(fieldvalue)
}
return data, nil
}