Documentation
¶
Overview ¶
Package dbf provides functionality for reading DBF (dBase, FoxPro, Visual FoxPro) files.
The package supports various DBF file formats and encodings, with automatic encoding detection based on Language Driver ID when available.
Basic usage:
reader, err := dbf.NewFromFile("data.dbf", dbf.WithCP866())
if err != nil {
log.Fatal(err)
}
defer reader.Close()
records, err := reader.ReadAll()
if err != nil {
log.Fatal(err)
}
For large files, use streaming to avoid loading everything into memory:
for reader.Next() {
record, err := reader.Read()
if err != nil {
log.Fatal(err)
}
// process record
}
Index ¶
- Variables
- type Field
- type FileType
- type Option
- type Reader
- func (r *Reader) Close() error
- func (r *Reader) Err() error
- func (r *Reader) Fields() []Field
- func (r *Reader) FieldsCount() int
- func (r *Reader) FileType() FileType
- func (r *Reader) LastUpdate() time.Time
- func (r *Reader) Next() bool
- func (r *Reader) Read() (*Record, error)
- func (r *Reader) ReadAll() ([]*Record, error)
- func (r *Reader) RecordsCount() uint32
- func (r *Reader) String() string
- type Record
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidFileType = errors.New("invalid file type") ErrInvalidHeaderSize = errors.New("invalid header size") ErrInvalidRecordSize = errors.New("invalid record size") ErrUnknownEncoding = errors.New("unable to determine encoding") ErrInvalidTerminator = errors.New("invalid field descriptor terminator") ErrFieldOutOfBounds = errors.New("field exceeds record bounds") ErrReadBeforeNext = errors.New("Read called before Next") ErrRecordSizeMismatch = errors.New("record size mismatch") )
Sentinel errors returned by the library.
Functions ¶
This section is empty.
Types ¶
type Field ¶
type Field struct {
Name string // field name (max 11 characters)
Type byte // field type (C=Character, N=Numeric, D=Date, L=Logical, M=Memo, F=Float)
MemoryAddress uint32 // memory address (reserved, not used in file-based DBF)
Length uint16 // field length in bytes; uint16 to support VFP character fields > 255 bytes
DecimalCount byte // number of decimal places (for numeric fields)
}
Field represents a single field definition in a DBF table.
func (Field) TypeString ¶
TypeString returns a human-readable description of the field type.
type FileType ¶
type FileType byte
FileType represents the type of DBF file format.
const ( FoxBASE FileType = 0x02 FoxBASEPlusNoMemo FileType = 0x03 VisualObjects FileType = 0x07 VisualFoxPro FileType = 0x30 VisualFoxProAI FileType = 0x31 VisualFoxProVarchar FileType = 0x32 FoxBASEPlusMemo FileType = 0x83 VisualObjectsMemo FileType = 0x87 HiPerSix FileType = 0xE5 FoxPro2 FileType = 0xF5 FoxBASE2 FileType = 0xFB )
Supported DBF file type constants.
type Option ¶
type Option func(*Reader)
Option is a functional option for configuring a Reader.
func WithCP866 ¶
func WithCP866() Option
WithCP866 sets the encoding to Code Page 866 (Russian MS-DOS). This is commonly used for Russian DBF files created in DOS.
func WithCP1251 ¶
func WithCP1251() Option
WithCP1251 sets the encoding to Windows-1251 (Russian Windows). This is commonly used for Russian DBF files created in Windows.
func WithCP1252 ¶
func WithCP1252() Option
WithCP1252 sets the encoding to Windows-1252 (Western European). This is the default Windows encoding for Western European languages.
func WithDecoder ¶
WithDecoder sets a custom text encoding decoder for reading character fields. This is the most flexible option, allowing any encoding.Decoder to be used. A nil decoder is ignored; pass WithEncoding or WithCP* to set encoding explicitly.
func WithEncoding ¶
WithEncoding sets the text encoding using a charmap.Charmap. This is a convenience wrapper around WithDecoder.
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader provides methods for reading DBF files. It supports both streaming (Next/Read) and batch (ReadAll) reading modes.
Reader is not safe for concurrent use.
func New ¶
New creates a new DBF Reader from an io.Reader.
If no encoding is specified via options, the reader will attempt to auto-detect the encoding from the Language Driver ID byte in the DBF header. If auto-detection fails, an error is returned.
Example:
file, _ := os.Open("data.dbf")
reader, err := dbf.New(file, dbf.WithCP866())
func NewFromFile ¶
NewFromFile creates a new DBF Reader from a file path. This is a convenience wrapper around New() for file-based reading.
Example:
reader, err := dbf.NewFromFile("data.dbf", dbf.WithCP866())
func (*Reader) Err ¶
Err returns any error that occurred during iteration. It should be called after Next() returns false to check for errors.
func (*Reader) FieldsCount ¶
FieldsCount returns the number of fields in the DBF table.
func (*Reader) LastUpdate ¶
LastUpdate returns the date when the DBF file was last modified.
func (*Reader) Next ¶
Next advances to the next record in the DBF file. It returns false when there are no more records or an error occurred. Use Err() to check for errors after the iteration completes.
Example:
for reader.Next() {
record, err := reader.Read()
if err != nil {
log.Fatal(err)
}
// process record
}
if err := reader.Err(); err != nil {
log.Fatal(err)
}
func (*Reader) Read ¶
Read reads the current record. Must be called after a successful Next() call. Returns an error if reading fails or if called without a prior Next() call.
func (*Reader) RecordsCount ¶
RecordsCount returns the total number of records in the DBF file, including deleted records.