Documentation ¶
Overview ¶
http://aa.usno.navy.mil/faq/docs/JD_Formula.php
Package dbf provides code for reading data from FoxPro DBF/FPT files
Index ¶
- Constants
- Variables
- func J2YMD(d int) (int, int, int)
- func Number(t time.Time) int
- func SetValidFileVersionFunc(f func(version byte) error)
- func ToBool(in interface{}) bool
- func ToDate(number int) string
- func ToFloat64(in interface{}) float64
- func ToInt64(in interface{}) int64
- func ToNumber(date string) (int, error)
- func ToString(in interface{}) string
- func ToTime(in interface{}) time.Time
- func ToTrimmedString(in interface{}) string
- func YMD2J(i, j, k int) int
- type DBF
- func (dbf *DBF) BOF() bool
- func (dbf *DBF) Close() error
- func (dbf *DBF) Deleted() (bool, error)
- func (dbf *DBF) DeletedAt(recordpos uint32) (bool, error)
- func (dbf *DBF) EOF() bool
- func (dbf *DBF) Field(fieldpos int) (interface{}, error)
- func (dbf *DBF) FieldNames() []string
- func (dbf *DBF) FieldPos(fieldname string) int
- func (dbf *DBF) Fields() []FieldHeader
- func (dbf *DBF) GoTo(recno uint32) error
- func (dbf *DBF) Header() *DBFHeader
- func (dbf *DBF) NumFields() uint16
- func (dbf *DBF) NumRecords() uint32
- func (dbf *DBF) Record() (*Record, error)
- func (dbf *DBF) RecordAt(nrec uint32) (*Record, error)
- func (dbf *DBF) RecordToJSON(nrec uint32, trimspaces bool) ([]byte, error)
- func (dbf *DBF) RecordToMap(nrec uint32) (map[string]interface{}, error)
- func (dbf *DBF) Skip(offset int64) error
- func (dbf *DBF) Stat() (os.FileInfo, error)
- func (dbf *DBF) StatFPT() (os.FileInfo, error)
- type DBFHeader
- type Decoder
- type FPTHeader
- type FieldHeader
- type ReaderAtSeeker
- type Record
- type UTF8Decoder
- type UTF8Validator
- type Win1250Decoder
Examples ¶
Constants ¶
const VERSION = "0.0.1"
Variables ¶
var ( // ErrEOF is returned when on end of DBF file (after the last record) ErrEOF = errors.New("EOF") // ErrBOF is returned when the record pointer is attempted to be moved before the first record ErrBOF = errors.New("BOF") // ErrIncomplete is returned when the read of a record or field did not complete ErrIncomplete = errors.New("incomplete read") // ErrInvalidField is returned when an invalid fieldpos is used (<1 or >NumFields) ErrInvalidField = errors.New("invalid field pos") // ErrNoFPTFile is returned when there should be an FPT file but it is not found on disc ErrNoFPTFile = errors.New("no FPT file") // ErrNoDBFFile is returned when a file operation is attempted on a DBF but a reader is open ErrNoDBFFile = errors.New("no DBF file") // ValidFileVersionFunc can be used to override file version checks to open untested files ValidFileVersionFunc = validFileVersion )
var ErrInvalidUTF8 = errors.New("invalid UTF-8 data")
Functions ¶
func SetValidFileVersionFunc ¶
SetValidFileVersionFunc sets variable ValidFileVersionFunc to a new function used to verify if the opened file has a valid version flag.
Example ¶
// create function which checks that only file flag 0x03 is valid validFileVersionFunc := func(version byte) error { if version == 0x03 { return nil } return errors.New("not 0x03") } // set the new function as verifier SetValidFileVersionFunc(validFileVersionFunc) // open DBF as usual dbf, err := OpenFile("/var/test.dbf", new(Win1250Decoder)) if err != nil { log.Fatal(err) } defer dbf.Close()
Output:
func ToTrimmedString ¶
func ToTrimmedString(in interface{}) string
ToTrimmedString always returns a string with spaces trimmed
Types ¶
type DBF ¶
type DBF struct {
// contains filtered or unexported fields
}
DBF is the main DBF struct which provides all methods for reading files and embeds the file readers and handlers.
func OpenFile ¶
OpenFile opens a DBF file (and FPT if needed) from disk. After a successful call to this method (no error is returned), the caller should call DBF.Close() to close the embedded file handle(s). The Decoder is used for charset translation to UTF8, see decoder.go
func OpenStream ¶
func OpenStream(dbffile, fptfile ReaderAtSeeker, dec Decoder) (*DBF, error)
OpenStream creates a new DBF struct from a bytes stream, for example a bytes.Reader The fptfile parameter is optional, but if the DBF header has the FPT flag set, the fptfile must be provided. The Decoder is used for charset translation to UTF8, see decoder.go
func (*DBF) Close ¶
Close closes the file handlers to the disk files. The caller is responsible for calling Close to close the file handle(s)!
func (*DBF) Field ¶
Field reads field number fieldpos at the record number the internal pointer is pointing to and returns its Go value
func (*DBF) FieldNames ¶
FieldNames returnes a slice of all the fieldnames
func (*DBF) FieldPos ¶
FieldPos returns the zero-based field position of a fieldname or -1 if not found.
func (*DBF) GoTo ¶
GoTo sets the internal record pointer to record recno (zero based). Returns ErrEOF if at EOF and positions the pointer at lastRec+1.
func (*DBF) NumRecords ¶
NumRecords returns the number of records
func (*DBF) RecordToJSON ¶
RecordToJSON returns a complete record as a JSON object. If nrec > 0 it returns the record at nrec, if nrec <= 0 it returns the record at dbf.recpointer. If trimspaces is true we trim spaces from string values (this is slower because of an extra reflect operation and all strings in the record map are re-assigned)
func (*DBF) RecordToMap ¶
RecordToMap returns a complete record as a map. If nrec > 0 it returns the record at nrec, if nrec <= 0 it returns the record at dbf.recpointer
func (*DBF) Skip ¶
Skip adds offset to the internal record pointer. Returns ErrEOF if at EOF and positions the pointer at lastRec+1. Returns ErrBOF is recpointer would be become negative and positions the pointer at 0. Does not skip deleted records.
type DBFHeader ¶
type DBFHeader struct { FileVersion byte // File type flag ModYear uint8 // Last update year (0-99) ModMonth uint8 // Last update month ModDay uint8 // Last update day NumRec uint32 // Number of records in file FirstRec uint16 // Position of first data record RecLen uint16 // Length of one data record, including delete flag Reserved [16]byte // Reserved TableFlags byte // Table flags CodePage byte // Code page mark }
DBFHeader is the struct containing all raw DBF header fields. Header info from https://docs.microsoft.com/en-us/previous-versions/visualstudio/foxpro/st4a0s68(v=vs.80)
func (*DBFHeader) Modified ¶
Modified parses the ModYear, ModMonth and ModDay to time.Time. Note: The year is stored in 2 digits, 15 is 2015, we assume here that all files were modified after the year 2000 and always add 2000.
func (*DBFHeader) NumFields ¶
NumFields returns the calculated number of fields from the header info alone (without the need to read the fieldinfo from the header). This is the fastest way to determine the number of records in the file. Note: when OpenFile is used the fields have already been parsed so it is better to call DBF.NumFields in that case.
type FPTHeader ¶
type FPTHeader struct { NextFree uint32 // Location of next free block Unused [2]byte // Unused BlockSize uint16 // Block size (bytes per block) }
FPTHeader is the raw header of the Memo file. Header info from https://docs.microsoft.com/en-us/previous-versions/visualstudio/foxpro/8599s21w(v=vs.80)
type FieldHeader ¶
type FieldHeader struct { Name [11]byte // Field name with a maximum of 10 characters. If less than 10, it is padded with null characters (0x00). Type byte // Field type Pos uint32 // Displacement of field in record Len uint8 // Length of field (in bytes) Decimals uint8 // Number of decimal places Flags byte // Field flags Next uint32 // Value of autoincrement Next value Step uint16 // Value of autoincrement Step value Reserved [8]byte // Reserved }
FieldHeader contains the raw field info structure from the DBF header. Header info from https://docs.microsoft.com/en-us/previous-versions/visualstudio/foxpro/st4a0s68(v=vs.80)
func (*FieldHeader) FieldName ¶
func (f *FieldHeader) FieldName() string
FieldName returns the name of the field as a trimmed string (max length 10)
func (*FieldHeader) FieldType ¶
func (f *FieldHeader) FieldType() string
FieldType returns the type of the field as string (length 1)
type ReaderAtSeeker ¶
type ReaderAtSeeker interface { io.ReadSeeker io.ReaderAt }
ReaderAtSeeker is used when opening files from memory
type Record ¶
type Record struct { Deleted bool // contains filtered or unexported fields }
Record contains the raw record data and a deleted flag
func (*Record) FieldSlice ¶
func (r *Record) FieldSlice() []interface{}
FieldSlice gets all fields as a slice
type UTF8Decoder ¶
type UTF8Decoder struct{}
UTF8Decoder assumes your DBF is in UTF8 so it does nothing
type Win1250Decoder ¶
type Win1250Decoder struct{}
Win1250Decoder translates a Windows-1250 DBF to UTF8