diff --git a/dbase/database.go b/dbase/database.go index 8bee067..bc86455 100644 --- a/dbase/database.go +++ b/dbase/database.go @@ -1,6 +1,7 @@ package dbase import ( + "errors" "fmt" "path" "path/filepath" @@ -16,10 +17,10 @@ type Database struct { // The database file must be a DBC file and the tables must be DBF files and in the same directory as the database func OpenDatabase(config *Config) (*Database, error) { if config == nil { - return nil, newError("dbase-io-opendatabase-1", fmt.Errorf("missing config")) + return nil, newError("dbase-io-opendatabase-1", errors.New("missing config")) } if len(strings.TrimSpace(config.Filename)) == 0 { - return nil, newError("dbase-io-opendatabase-2", fmt.Errorf("missing filename")) + return nil, newError("dbase-io-opendatabase-2", errors.New("missing filename")) } if strings.ToUpper(filepath.Ext(config.Filename)) != string(DBC) { return nil, newError("dbase-io-opendatabase-3", fmt.Errorf("invalid file name: %v", config.Filename)) @@ -47,7 +48,7 @@ func OpenDatabase(config *Config) (*Database, error) { } tableName, ok := objectName.(string) if !ok { - return nil, newError("dbase-io-opendatabase-8", fmt.Errorf("table name is not a string")) + return nil, newError("dbase-io-opendatabase-8", errors.New("table name is not a string")) } tableName = strings.Trim(tableName, " ") if tableName == "" { diff --git a/dbase/file.go b/dbase/file.go index 25c1888..0cc811c 100644 --- a/dbase/file.go +++ b/dbase/file.go @@ -2,6 +2,7 @@ package dbase import ( "encoding/json" + "errors" "fmt" "reflect" "strings" @@ -238,7 +239,7 @@ func (file *File) BytesToRow(data []byte) (*Row, error) { // a row should start with te delete flag, a space ACTIVE(0x20) or DELETED(0x2A) rec.Deleted = Marker(data[0]) == Deleted if !rec.Deleted && Marker(data[0]) != Active { - return nil, newError("dbase-table-bytestorow-2", fmt.Errorf("invalid row data, no delete flag found at beginning of row")) + return nil, newError("dbase-table-bytestorow-2", errors.New("invalid row data, no delete flag found at beginning of row")) } // deleted flag already read offset := uint16(1) diff --git a/dbase/interpreter.go b/dbase/interpreter.go index d1910df..f5d222f 100644 --- a/dbase/interpreter.go +++ b/dbase/interpreter.go @@ -4,6 +4,7 @@ import ( "encoding/binary" "fmt" "math" + "strconv" "time" ) @@ -268,8 +269,8 @@ func (file *File) getFloatRepresentation(field *Field, skipSpacing bool) ([]byte } var bin []byte if b == float64(int64(b)) { - // if the value is an integer, store as integer - bin = []byte(fmt.Sprintf("%d", int64(b))) + // if the value has no decimals, store as integer + bin = []byte(strconv.FormatInt(int64(b), 10)) } else { // if the value is a float, store as float expression := fmt.Sprintf("%%.%df", field.column.Decimals) @@ -431,8 +432,8 @@ func (file *File) getNumericRepresentation(field *Field, skipSpacing bool) ([]by f, fok := field.value.(float64) if fok { if f == float64(int64(f)) { - // if the value is an integer, store as integer - bin = []byte(fmt.Sprintf("%d", int64(f))) + // if the value has no decimals, store as integer + bin = []byte(strconv.FormatInt(int64(f), 10)) } else { // if the value is a float, store as float expression := fmt.Sprintf("%%.%df", field.column.Decimals) diff --git a/dbase/io_generic.go b/dbase/io_generic.go index 0157fbc..5d0f6bc 100644 --- a/dbase/io_generic.go +++ b/dbase/io_generic.go @@ -3,6 +3,7 @@ package dbase import ( "bytes" "encoding/binary" + "errors" "fmt" "io" "path/filepath" @@ -21,7 +22,7 @@ type GenericIO struct { func (g GenericIO) OpenTable(config *Config) (*File, error) { if config == nil { - return nil, newError("dbase-io-generic-opentable-1", fmt.Errorf("missing configuration")) + return nil, newError("dbase-io-generic-opentable-1", errors.New("missing configuration")) } debugf("Opening table from custom io interface - Untested: %v - Trim spaces: %v - ValidateCodepage: %v - InterpretCodepage: %v", config.Untested, config.TrimSpaces, config.ValidateCodePage, config.InterpretCodePage) fileName := filepath.Clean(config.Filename) @@ -71,7 +72,7 @@ func (g GenericIO) OpenTable(config *Config) (*File, error) { // If the FPT file does not exist an error is returned. if MemoFlag.Defined(file.header.TableFlags) { if file.relatedHandle == nil { - return nil, newError("dbase-io-generic-opentable-6", fmt.Errorf("no related handle defined")) + return nil, newError("dbase-io-generic-opentable-6", errors.New("no related handle defined")) } err = file.ReadMemoHeader() if err != nil { @@ -431,7 +432,7 @@ func (g GenericIO) ReadNullFlag(file *File, position uint64, column *Column) (bo return false, false, newError("dbase-io-generic-readnullflag-1", err) } if file.nullFlagColumn == nil || (column.DataType != byte(Varchar) && column.DataType != byte(Varbinary)) { - return false, false, newError("dbase-io-generic-readnullflag-2", fmt.Errorf("null flag column missing or not a varchar/varbinary field")) + return false, false, newError("dbase-io-generic-readnullflag-2", errors.New("null flag column missing or not a varchar/varbinary field")) } nullFlagPosition := file.table.nullFlagPosition(column) position = uint64(file.header.FirstRow) + position*uint64(file.header.RowLength) + uint64(file.nullFlagColumn.Position) @@ -519,7 +520,7 @@ func (g GenericIO) WriteRow(file *File, row *Row) error { func (g GenericIO) Search(file *File, field *Field, exactMatch bool) ([]*Row, error) { if field.column.DataType == 'M' { - return nil, newError("dbase-io-generic-search-1", fmt.Errorf("searching memo fields is not supported")) + return nil, newError("dbase-io-generic-search-1", errors.New("searching memo fields is not supported")) } handle, err := g.getHandle(file) if err != nil { diff --git a/dbase/io_windows.go b/dbase/io_windows.go index a721b96..32e7bba 100644 --- a/dbase/io_windows.go +++ b/dbase/io_windows.go @@ -6,6 +6,7 @@ package dbase import ( "bytes" "encoding/binary" + "errors" "fmt" "os" "path" @@ -25,7 +26,7 @@ type WindowsIO struct{} func (w WindowsIO) OpenTable(config *Config) (*File, error) { if config == nil || len(strings.TrimSpace(config.Filename)) == 0 { - return nil, newError("dbase-io-windows-opentable-1", fmt.Errorf("missing configuration or filename")) + return nil, newError("dbase-io-windows-opentable-1", errors.New("missing configuration or filename")) } debugf("Opening table: %s - Read-only: %v - Exclusive: %v - Untested: %v - Trim spaces: %v - Write lock: %v - ValidateCodepage: %v - InterpretCodepage: %v", config.Filename, config.ReadOnly, config.Exclusive, config.Untested, config.TrimSpaces, config.WriteLock, config.ValidateCodePage, config.InterpretCodePage) var err error @@ -161,7 +162,7 @@ func (w WindowsIO) Create(file *File) error { file.config.Filename = strings.ToUpper(strings.TrimSpace(file.config.Filename)) // Check for valid file name if len(file.config.Filename) == 0 { - return newError("dbase-io-windows-create-1", fmt.Errorf("missing filename")) + return newError("dbase-io-windows-create-1", errors.New("missing filename")) } dbfname, err := windows.UTF16FromString(file.config.Filename) if err != nil { @@ -170,7 +171,7 @@ func (w WindowsIO) Create(file *File) error { // Check if file exists already _, err = windows.GetFileAttributes(&dbfname[0]) if err == nil { - return newError("dbase-io-windows-create-3", fmt.Errorf("file already exists")) + return newError("dbase-io-windows-create-3", errors.New("file already exists")) } // Create the file debugf("Creating file: %s", file.config.Filename) @@ -388,7 +389,7 @@ func (w WindowsIO) ReadNullFlag(file *File, position uint64, column *Column) (bo return false, false, newError("dbase-io-windows-readnullflag-1", err) } if file.nullFlagColumn == nil || (column.DataType != byte(Varchar) && column.DataType != byte(Varbinary)) { - return false, false, newError("dbase-io-windows-readnullflag-2", fmt.Errorf("null flag column is nil or column is not varchar or varbinary")) + return false, false, newError("dbase-io-windows-readnullflag-2", errors.New("null flag column is nil or column is not varchar or varbinary")) } nullFlagPosition := file.table.nullFlagPosition(column) pos := uint64(file.header.FirstRow) + position*uint64(file.header.RowLength) + uint64(file.nullFlagColumn.Position) @@ -688,7 +689,7 @@ func (w WindowsIO) WriteRow(file *File, row *Row) (err error) { func (w WindowsIO) Search(file *File, field *Field, exactMatch bool) ([]*Row, error) { if field.column.DataType == 'M' { - return nil, newError("dbase-io-windows-search-1", fmt.Errorf("searching memo fields is not supported")) + return nil, newError("dbase-io-windows-search-1", errors.New("searching memo fields is not supported")) } handle, err := w.getHandle(file) if err != nil { diff --git a/dbase/table.go b/dbase/table.go index b772c70..62292fc 100644 --- a/dbase/table.go +++ b/dbase/table.go @@ -114,7 +114,7 @@ func (row *Row) StringValueByName(name string) (string, error) { if ok { return string(sanitizeString(bslice)), nil } - return "", newError("dbase-table-stringvaluebyname-2", fmt.Errorf("value is not a string")) + return "", newError("dbase-table-stringvaluebyname-2", errors.New("value is not a string")) } return "", nil } @@ -142,7 +142,7 @@ func (row *Row) IntValueByName(name string) (int64, error) { if ok { return intVal, nil } - return 0, newError("dbase-table-intvaluebyname-2", fmt.Errorf("value is not an int")) + return 0, newError("dbase-table-intvaluebyname-2", errors.New("value is not an int")) } return 0, nil } @@ -170,7 +170,7 @@ func (row *Row) FloatValueByName(name string) (float64, error) { if ok { return floatVal, nil } - return 0, newError("dbase-table-floatvaluebyname-2", fmt.Errorf("value is not a float")) + return 0, newError("dbase-table-floatvaluebyname-2", errors.New("value is not a float")) } return 0, nil } @@ -197,7 +197,7 @@ func (row *Row) BoolValueByName(name string) (bool, error) { if ok { return boolVal, nil } - return false, newError("dbase-table-boolvaluebyname-2", fmt.Errorf("value is not a bool")) + return false, newError("dbase-table-boolvaluebyname-2", errors.New("value is not a bool")) } return false, nil @@ -226,7 +226,7 @@ func (row *Row) TimeValueByName(name string) (time.Time, error) { if ok { return timeVal, nil } - return time.Time{}, newError("dbase-table-timevaluebyname-2", fmt.Errorf("value is not a time")) + return time.Time{}, newError("dbase-table-timevaluebyname-2", errors.New("value is not a time")) } return time.Time{}, nil } @@ -262,7 +262,7 @@ func (row *Row) BytesValueByName(name string) ([]byte, error) { if ok { return []byte(strVal), nil } - return nil, newError("dbase-table-bytesvaluebyname-2", fmt.Errorf("value is not a byte slice")) + return nil, newError("dbase-table-bytesvaluebyname-2", errors.New("value is not a byte slice")) } return nil, nil } @@ -462,7 +462,7 @@ func (c *Column) Reflect() (reflect.Type, error) { // SetValue allows to change the field value func (field *Field) SetValue(value interface{}) error { if field == nil { - return newError("dbase-table-setvalue-1", fmt.Errorf("field is not defined by table")) + return newError("dbase-table-setvalue-1", errors.New("field is not defined by table")) } field.value = value return nil