forked from LindsayBradford/go-dbf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
column.go
83 lines (65 loc) · 1.4 KB
/
column.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
package dbf
import (
"bytes"
"golang.org/x/text/encoding"
)
// Column represents a dBase column
type Column struct {
Name string
Type ColumnType
Length int
DecimalPlaces int
index int
}
// Columns is a slice of Columns
type Columns []*Column
func newColumn(rawData []byte, enc encoding.Encoding) (*Column, error) {
if len(rawData) != 32 {
return nil, ErrInvalidColumnData
}
nameData := rawData[:10]
if enc != encoding.Nop {
var err error
dec := enc.NewDecoder()
nameData, err = dec.Bytes(rawData[:10])
if err != nil {
return nil, err
}
}
name := string(bytes.Trim(nameData, "\x00"))
ct, err := getColumnType(rawData[11])
if err != nil {
return nil, err
}
length := int(rawData[16])
decimalPlaces := int(rawData[17])
return &Column{
Name: name,
Type: ct,
Length: length,
DecimalPlaces: decimalPlaces,
}, nil
}
// RowLength returns the length of a row
func (c Columns) RowLength() int {
var length int
for _, column := range c {
length += column.Length
}
return length
}
func parseColumns(
rawData []byte,
columnLength int,
enc encoding.Encoding,
) (Columns, error) {
var columns []*Column
for i := 0; i < len(rawData); i += columnLength {
column, err := newColumn(rawData[i:i+columnLength], enc)
if err != nil {
return nil, err
}
columns = append(columns, column)
}
return columns, nil
}