From 91bb953d71c7530517aa83039199cc6866f3bb47 Mon Sep 17 00:00:00 2001 From: squeeze69 Date: Fri, 20 Jan 2017 11:07:00 +0100 Subject: [PATCH] Changed parameter in Read from uint16 to int The uint16 can accomodate 65536 records, but dbf has no practical limits beyond maximum file size. int is guaranteed to be, at least, 32bit. Added some comment to methods. Changed FieldName to read up to the first "\x00" , some headers aren't padded with "\x00", so it could return a wrong field name. This happens with some Clipper compiled programs. --- dbf.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/dbf.go b/dbf.go index 272bd52..a71009c 100644 --- a/dbf.go +++ b/dbf.go @@ -80,10 +80,18 @@ func (r *Reader) ModDate() (int, int, int) { return r.year, r.month, r.day } +//FieldName - returns field name, read up to the first "\x00" rune, to accomodate some malformed dbf func (r *Reader) FieldName(i int) (name string) { - return strings.TrimRight(string(r.fields[i].Name[:]), "\x00") + for _, val := range string(r.fields[i].Name[:]) { + if val == 0 { + return + } + name = name + string(val) + } + return } +//FieldNames - return the full list of Field Names func (r *Reader) FieldNames() (names []string) { for i := range r.fields { names = append(names, r.FieldName(i)) @@ -114,11 +122,12 @@ type Field struct { // http://play.golang.org/p/-CUbdWc6zz type Record map[string]interface{} -func (r *Reader) Read(i uint16) (rec Record, err error) { +//Read - read record i +func (r *Reader) Read(i int) (rec Record, err error) { r.Lock() defer r.Unlock() - offset := int64(r.headerlen + r.recordlen*i) + offset := int64(r.headerlen) + int64(r.recordlen)*int64(i) r.r.Seek(offset, 0) var deleted byte