forked from gsiems/go-marc21
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf-extract.go
350 lines (288 loc) · 7.98 KB
/
cf-extract.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Here be some ugliness
//
// Parses a LoC MARC21 fields list page and extracts the control field
// definitions contained within
package codegen
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"strings"
)
// CfTags is a list of Control field definitions
type CfTags struct {
Tags []*CfTag
}
// CfTag is a Control field definition
type CfTag struct {
Tag string
Label string
Subtags []*CfSubtag
}
// CfSubtag is a Control field *subtag* definition (not that all control
// fields have subtags (in which case the subtag label is "DEFAULT"))
type CfSubtag struct {
Label string
Elements []*CfElement
}
// CfElement is the definition for one or more characters that make up
// an "element"
type CfElement struct {
Name string
CamelName string
Offset int
Width int
wrapIndent int
CodeWidth int
FnType string
LookupValues []*LookupValue
}
// String should return a string that matches, excepting some whitespace
// and cleaned-up linewraps, the input data. This is primarily intended
// for testing that the parsing/extraction is working correctly.
func (t CfTag) String() string {
var lines []string
lines = append(lines, fmt.Sprintf("%s - %s", t.Tag, t.Label))
if len(t.Subtags) == 0 {
lines = append(lines, "")
}
for _, v := range t.Subtags {
if v.Label != "DEFAULT" {
lines = append(lines, fmt.Sprintf(" %s--%s", t.Tag, v.Label))
}
if len(v.Elements) > 0 {
lines = append(lines, " Character Positions")
}
for _, v2 := range v.Elements {
offset := fmt.Sprintf("%02d", v2.Offset)
if v2.Width > 1 {
offset = fmt.Sprintf("%s-%02d", offset, v2.Offset+v2.Width-1)
}
lines = append(lines, fmt.Sprintf(" %s - %s", offset, v2.Name))
// codes/labels
for _, cv := range v2.LookupValues {
lines = append(lines, fmt.Sprintf(" %s - %s", cv.Code, cv.Label))
}
}
}
lines = append(lines, "")
return strings.Join(lines, "\n")
}
// ExtractCfStruct extracts the structure for control file entries as
// defined in the [saved-to-disc] fields list webpage
func ExtractCfStruct(filename string) (tags CfTags) {
f, err := os.Open(filename)
if err != nil {
log.Fatal(fmt.Printf("File open failed: %q", err))
}
defer func() {
if cerr := f.Close(); cerr != nil && err == nil {
err = cerr
}
}()
notInCfBlock := true
cfTg := new(CfTag)
cfSt := new(CfSubtag)
cfEl := new(CfElement)
cfLv := new(LookupValue)
reElem := regexp.MustCompile("^ [ ]{0,1}[0-9][0-9]")
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
// We are only interested in looking at control field definitions
notInCfBlock = isNotInCfBlock(line, notInCfBlock)
if notInCfBlock {
continue
}
if canIgnoreCfLine(line) {
continue
}
//// Check if this is a "NEW TAG" line
if pluckBytes(line, 0, 2) == "00" {
if cfTg.Tag != "" {
if cfSt.Label != "" {
if cfEl.Name != "" {
cfSt.Elements = append(cfSt.Elements, cfEl)
cfEl = new(CfElement)
}
cfTg.Subtags = append(cfTg.Subtags, cfSt)
cfSt = new(CfSubtag)
}
tags.Tags = append(tags.Tags, cfTg)
cfTg = new(CfTag)
}
tl := strings.SplitN(line, "-", 2)
cfTg.Tag = strings.TrimSpace(tl[0])
if len(tl) > 1 {
cfTg.Label = strings.TrimSpace(tl[1])
}
continue
}
// Just in case we are not in a tag block
if cfTg.Tag == "" {
continue
}
//// Check if this is a "NEW SUBTAG" line
if strings.Contains(line, cfTg.Tag+"--") {
if cfSt.Label != "" {
if cfEl.Name != "" {
cfSt.Elements = append(cfSt.Elements, cfEl)
cfEl = new(CfElement)
}
cfTg.Subtags = append(cfTg.Subtags, cfSt)
cfSt = new(CfSubtag)
}
x := strings.SplitN(line, "--", 2)
cfSt.Label = x[len(x)-1]
continue
}
//// Check if this is a "NEW ELEMENT" line
if reElem.MatchString(line) {
if cfSt.Label == "" {
cfSt.Label = "DEFAULT"
}
if cfEl.Name != "" {
if cfLv.Code != "" {
cfEl.LookupValues = append(cfEl.LookupValues, cfLv)
cfLv = new(LookupValue)
}
cfSt.Elements = append(cfSt.Elements, cfEl)
cfEl = new(CfElement)
}
line = strings.TrimSpace(line)
offset, err := toInt([]byte(pluckBytes(line, 0, 2)))
if err != nil {
cfEl.Name = "BAD_PARSE"
log.Printf("BAD PARSE: %s, %q, %q\n", cfTg.Label, cfSt.Label, line)
continue
}
width, err := toInt([]byte(pluckBytes(line, 3, 2)))
if err == nil {
width = width - offset + 1
line = strings.TrimSpace(line[5:])
} else {
width = 1
line = strings.TrimSpace(line[2:])
}
// There is usually a hyphen here, but not always...
if strings.Index(line, "-") == 0 {
line = strings.TrimSpace(line[1:])
}
cfEl = new(CfElement)
cfEl.Offset = offset
cfEl.Width = width
cfEl.Name = line
cfEl.CamelName = camelizeString(line)
cfEl.FnType = "read"
continue
}
//// At this point, we assert that this is a "LOOKUP VALUE" line
// Determine if this line is a continuation line from the previous
if isWrappedLine(cfEl.wrapIndent, line) {
il := len(cfEl.LookupValues) - 1
cfEl.LookupValues[il].Label += " " + strings.TrimSpace(line)
continue
}
lv := strings.SplitN(strings.TrimSpace(line), " ", 2)
if len(lv) == 1 {
log.Printf("BAD PARSE: %s, %q, %q, %q\n", cfTg.Label, cfSt.Label, cfEl.Name, line)
continue
}
// We need to check and account for lookup definitions that do
// not match the typical " ... xx - label" pattern such as:
// " 1-9 Number of units" // code is a range of values
// or
// " --- - Unknown" // the dashes ARE the code
// or
// " |- No attempt to code" // no space before the separating dash
code := lv[0]
label := lv[1]
if pluckBytes(code, 0, 1) != "-" {
// line is not like: " --- - Unknown"
code = strings.TrimRight(code, "- ")
}
label = strings.TrimLeft(label, "- ")
// On the first item in the list we want to determine how
// "wide" the codes are and how much indentation to check for
// when testing for line-wraps
if len(cfEl.LookupValues) == 0 {
// Determine how much indent qualifies as a line-wrap
cfEl.wrapIndent = strings.Index(line, "-") + 1
cfEl.CodeWidth = calcCodeWidth(code)
if isRangeCode(code) {
cfEl.FnType = "range"
} else {
cfEl.FnType = "lookup"
}
}
if isRangeCode(code) {
if cfEl.FnType == "lookup" || cfEl.FnType == "hybrid" {
cfEl.FnType = "hybrid"
} else if cfEl.CodeWidth < cfEl.Width && strings.Contains(label, "Date") {
cfEl.FnType = "hybrid-date"
} else {
cfEl.FnType = "range"
}
} else if cfEl.FnType != "hybrid-date" {
if cfEl.FnType == "range" {
cfEl.FnType = "hybrid"
} else if cfEl.CodeWidth < cfEl.Width {
cfEl.FnType = "multi"
} else if strings.Contains(code, "[") {
cfEl.FnType = "hybrid"
}
}
e := new(LookupValue)
e.Code = code
e.Label = label
cfEl.LookupValues = append(cfEl.LookupValues, e)
}
if cfSt.Label != "" {
if cfEl.Name != "" {
cfSt.Elements = append(cfSt.Elements, cfEl)
}
cfTg.Subtags = append(cfTg.Subtags, cfSt)
}
tags.Tags = append(tags.Tags, cfTg)
return tags
}
// isRangeCode checks the code to determine if is defining a range
func isRangeCode(code string) bool {
if pluckBytes(code, 0, 1) != "-" {
if strings.Index(code, "-") > 0 {
return true
}
}
return false
}
func canIgnoreCfLine(line string) bool {
// For the few lines that we really want to ignore
if pluckByte(line, 0) == "#" {
return true
}
// Toss the few extra comments also
if pluckBytes(line, 0, 2) == "--" {
return true
}
// Empty lines.
if len(strings.TrimSpace(line)) < 1 {
return true
}
if strings.TrimSpace(strings.ToUpper(line)) == "CHARACTER POSITIONS" {
return true
}
return false
}
func isNotInCfBlock(line string, notInCfBlock bool) bool {
// Start looking at control field definitions
if pluckBytes(line, 0, 18) == "--Control Fields (" {
return false
}
// Stop looking at control field definitions
if pluckBytes(line, 0, 8) == "--Number" {
return true
}
return notInCfBlock
}