/
field.go
85 lines (71 loc) · 1.7 KB
/
field.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
package dbf
import (
"bytes"
"encoding/binary"
"io"
"golang.org/x/text/encoding"
)
const fieldDescriptorTerminator = 0x0D
type FieldFlag byte
const (
FieldFlagNone = 0x00
FieldFlagSystem = 0x01
FieldFlagNull = 0x02
FieldFlagBinary = 0x04
FieldFlagBinaryAndNull = 0x06
FieldFlagAutoInc = 0x0C
)
// Field provides methods to access a DBF
type Field struct {
Name string
Type rune
Displacement uint32
Length byte
DecimalCount byte
Flags FieldFlag
NextAutoIncrement uint32
AutoIncrementStep byte
Index int
VarLengthSizeIndex int
NullFieldIndex int
}
func readFields(r io.ReadSeeker, decoder *encoding.Decoder) ([]Field, error) {
var fields []Field
if _, err := r.Seek(32, io.SeekStart); err != nil {
return nil, err
}
buf := make([]byte, 32, 32)
index := 0
nullFieldIndex := -1
for {
if _, err := readAll(r, buf); err != nil {
return nil, err
}
if buf[0] == fieldDescriptorTerminator {
break
}
f := Field{}
f.Name, _ = decoder.String(string(buf[:bytes.IndexByte(buf, 0x00)]))
f.Type = rune(buf[11])
f.Displacement = binary.LittleEndian.Uint32(buf[12:])
f.Length = buf[16]
f.DecimalCount = buf[17]
f.Flags = FieldFlag(buf[18])
f.NextAutoIncrement = binary.LittleEndian.Uint32(buf[19:])
f.AutoIncrementStep = buf[23]
f.Index = index
f.VarLengthSizeIndex = -1
f.NullFieldIndex = -1
if f.Type == 'V' || f.Type == 'Q' {
nullFieldIndex++
f.VarLengthSizeIndex = nullFieldIndex
}
if (f.Flags & FieldFlagNull) != 0 {
nullFieldIndex++
f.NullFieldIndex = nullFieldIndex
}
index++
fields = append(fields, f)
}
return fields, nil
}