-
Notifications
You must be signed in to change notification settings - Fork 46
/
io.go
58 lines (49 loc) · 1.36 KB
/
io.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
// Package godbf offers functionality for loading and saving "dBASE Version 5" dbf formatted files.
// (https://en.wikipedia.org/wiki/.dbf#File_format_of_Level_5_DOS_dBASE) file structure.
// For the definitive source, see http://www.dbase.com/manuals/57LanguageReference.zip
package godbf
import (
"fmt"
"os"
)
// NewFromFile creates a DbfTable, reading it from a file with the given file name, expecting the supplied encoding.
func NewFromFile(fileName string, fileEncoding string) (table *DbfTable, newErr error) {
defer func() {
if e := recover(); e != nil {
newErr = fmt.Errorf("%v", e)
}
}()
data, readErr := readFile(fileName)
if readErr != nil {
return nil, readErr
}
return NewFromByteArray(data, fileEncoding)
}
// SaveToFile saves the supplied DbfTable to a file of the specified filename
func SaveToFile(dt *DbfTable, filename string) (saveErr error) {
defer func() {
if e := recover(); e != nil {
saveErr = fmt.Errorf("%v", e)
}
}()
f, createErr := fsWrapper.Create(filename)
if createErr != nil {
return createErr
}
defer func() {
if closeErr := f.Close(); closeErr != nil {
saveErr = closeErr
}
}()
writeErr := writeContent(dt, f)
if writeErr != nil {
return writeErr
}
return saveErr
}
func writeContent(dt *DbfTable, f *os.File) error {
if _, dsErr := f.Write(dt.dataStore); dsErr != nil {
return dsErr
}
return nil
}