forked from svolodeev/xbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheader.go
More file actions
80 lines (65 loc) · 1.38 KB
/
header.go
File metadata and controls
80 lines (65 loc) · 1.38 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
package xbase
import (
"encoding/binary"
"fmt"
"io"
"time"
)
type header struct {
DbfId byte
ModYear byte
ModMonth byte
ModDay byte
RecCount uint32
DataOffset uint16
RecSize uint16
Filler1 [17]byte
CP byte
Filler2 [2]byte
}
func newHeader() *header {
h := &header{}
h.DbfId = dbfId
h.setModDate(time.Now())
return h
}
// Read/write
func (h *header) read(reader io.Reader) {
if err := binary.Read(reader, binary.LittleEndian, h); err != nil {
panic(err)
}
if h.DbfId != dbfId {
panic(fmt.Errorf("not DBF file"))
}
}
func (h *header) write(writer io.Writer) {
if err := binary.Write(writer, binary.LittleEndian, h); err != nil {
panic(err)
}
}
// Field count
func (h *header) fieldCount() int {
return (int(h.DataOffset) - headerSize - 1) / fieldSize
}
func (h *header) setFieldCount(count int) {
h.DataOffset = uint16(count*fieldSize + headerSize + 1)
}
// Modified date
func (h *header) modDate() time.Time {
year := int(h.ModYear) + 1900
month := time.Month(h.ModMonth)
day := int(h.ModDay)
return time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
}
func (h *header) setModDate(d time.Time) {
h.ModYear = byte(d.Year() - 1900)
h.ModMonth = byte(d.Month())
h.ModDay = byte(d.Day())
}
// Code page
func (h *header) codePage() int {
return pageByCode(h.CP)
}
func (h *header) setCodePage(cp int) {
h.CP = codeByPage(cp)
}