Dude

_FileReadDBF (dbf_reader.au3) should skip deleted Records

5 posts in this topic

Hi. This Function exports an DBF File to an array. But it also exports deleted records. How can i prevent

this? The function should only export not as deleted marked records. How can i do this?

Share this post


Link to post
Share on other sites



Dude,

And where do we find this function so we can take a look?

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
#include-once
;===============================================================================
;
; Description:      Directly read DBF database file to array
; Parameter(s):     $sFileName - name of DBF file
;                   $nFlags 
;                       1 - convert from OEM to ANSI
;                       2 - strip leading and trailing witespaces
; Requirement(s):   Autoit 3.2.9.0 +
; Return Value(s):  On Success - 2D array, first row - names of fields
;                   On Failure - no decent way to check errors
; Author(s):        Dmitry Yudin (Lazycat)
; Version:          0.3
; Date:             14.12.2009
; Note(s):          Many DBF functions and flags are not read - just because 
;                   I wasn't need it
;
;===============================================================================

Func _FileReadDBF($sFileName, $nFlags = 0)
    Local $hFile = FileOpen($sFileName, 16)
    Local $pReadBuffer = DllStructCreate("byte[32]") ; Header size
    DllStructSetData($pReadBuffer, 1, FileRead($hFile, 32))
    $pDBFHeader = DllStructCreate("byte;byte;byte;byte;int;short;short;byte[20]" , DllStructGetPtr($pReadBuffer)) ; Header struct
    $nRecords = DllStructGetData($pDBFHeader, 5) ; Get number of records
    $nDataPos = DllStructGetData($pDBFHeader, 6) ; Get data start position
    $nRecordSize = DllStructGetData($pDBFHeader, 7) ; Get record size (included deleted flag)
    $nFields = Floor($nDataPos / 32) - 1
    $nDataGap = $nDataPos - ($nFields + 1) * 32 

    Local $aData[$nRecords+1][$nFields]
    Local $sRecordStruct = "byte;" ; Struct string, based on fields size and type, first byte - deleted mark

    For $i=0 To $nFields - 1
        DllStructSetData($pReadBuffer, 1, FileRead($hFile, 32))
        $pField = DllStructCreate("char[11];byte;int;byte;byte[15]" , DllStructGetPtr($pReadBuffer)) ; Field structure 
        $aData[0][$i] = DllStructGetData($pField, 1) ; Name of field
        ; Create struct based on field size and type (now unfinished, treat all types as string)
        Switch DllStructGetData($pField, 2)
            Case Asc("C") or Asc("D") ;or Else
                $sRecordStruct &= "char[" & DllStructGetData($pField, 4) & "];"
        EndSwitch
    Next

    $sRecordStruct = StringTrimRight($sRecordStruct, 1) ; Trim last ";"

    FileRead($hFile, $nDataGap) ; Skip ending marker, it's size may vary

    $pReadBuffer = DllStructCreate("byte["&$nRecordSize&"]") ; New buffer, now based on record size

    For $i = 1 To $nRecords
        DllStructSetData($pReadBuffer, 1, FileRead($hFile, DllStructGetSize($pReadBuffer)))
        $pRecord = DllStructCreate($sRecordStruct , DllStructGetPtr($pReadBuffer))
        For $j = 0 To $nFields - 1
            $aData[$i][$j] = DllStructGetData($pRecord, $j+2)
            If BitAND($nFlags, 1) Then $aData[$i][$j] = _Ascii2Ansi($aData[$i][$j])
            If BitAND($nFlags, 2) Then $aData[$i][$j] = StringStripWS($aData[$i][$j], 3)
        Next
    Next
    FileClose($hFile)
    Return $aData
EndFunc

; Helper function, convert OEM text to ANSI
Func _Ascii2Ansi($sText)
  Local $src = DllStructCreate("char[" & StringLen($sText) + 1 & "]")
  Local $dst = DllStructCreate("char[" & StringLen($sText) + 1 & "]")
  DllStructSetData($src, 1, $sText)
  DllCall("user32.dll", "int", "OemToCharA", "ptr", DllStructGetPtr($src), "ptr", DllStructGetPtr($dst))
  Return DllStructGetData($dst, 1)
EndFunc

 

Share this post


Link to post
Share on other sites

Here's a revised version that should give you what you are looking for:

Func _FileReadDBF($sFileName, $nFlags = 0, $lDeleted = True)
    Local $hFile = FileOpen($sFileName, 16)
    Local $pReadBuffer = DllStructCreate("byte[32]") ; Header size
    DllStructSetData($pReadBuffer, 1, FileRead($hFile, 32))
    $pDBFHeader = DllStructCreate("byte;byte;byte;byte;int;short;short;byte[20]" , DllStructGetPtr($pReadBuffer)) ; Header struct
    $nRecords = DllStructGetData($pDBFHeader, 5) ; Get number of records
    $nDataPos = DllStructGetData($pDBFHeader, 6) ; Get data start position
    $nRecordSize = DllStructGetData($pDBFHeader, 7) ; Get record size (included deleted flag)
    $nFields = Floor($nDataPos / 32) - 1
    $nDataGap = $nDataPos - ($nFields + 1) * 32

    Local $aData[$nRecords+1][$nFields]
    Local $sRecordStruct = "byte;" ; Struct string, based on fields size and type, first byte - deleted mark

    For $i=0 To $nFields - 1
        DllStructSetData($pReadBuffer, 1, FileRead($hFile, 32))
        $pField = DllStructCreate("char[11];byte;int;byte;byte[15]" , DllStructGetPtr($pReadBuffer)) ; Field structure
        $aData[0][$i] = DllStructGetData($pField, 1) ; Name of field
        ; Create struct based on field size and type (now unfinished, treat all types as string)
        Switch DllStructGetData($pField, 2)
            Case Asc("C") or Asc("D") ;or Else
                $sRecordStruct &= "char[" & DllStructGetData($pField, 4) & "];"
        EndSwitch
    Next

    $sRecordStruct = StringTrimRight($sRecordStruct, 1) ; Trim last ";"

    FileRead($hFile, $nDataGap) ; Skip ending marker, it's size may vary

    $pReadBuffer = DllStructCreate("byte["&$nRecordSize&"]") ; New buffer, now based on record size

    Local $nRow = 1, $cDeleted

    For $i = 1 To $nRecords
        DllStructSetData($pReadBuffer, 1, FileRead($hFile, DllStructGetSize($pReadBuffer)))
        $pRecord = DllStructCreate($sRecordStruct , DllStructGetPtr($pReadBuffer))

        $cDeleted = Chr(DllStructGetData($pRecord, 1))

        If $lDeleted Or $cDeleted <> '*' Then
            For $j = 0 To $nFields - 1
                $aData[$nRow][$j] = DllStructGetData($pRecord, $j+2)
                If BitAND($nFlags, 1) Then $aData[$nRow][$j] = _Ascii2Ansi($aData[$nRow][$j])
                If BitAND($nFlags, 2) Then $aData[$nRow][$j] = StringStripWS($aData[$nRow][$j], 3)
            Next

            $nRow += 1
        EndIf
    Next
    FileClose($hFile)
    Return $aData
EndFunc

Note: I didn't reduce the size of the array, so there will be one blank record at the bottom for each deleted record.

Share this post


Link to post
Share on other sites

Hi. Thanks a lot! seems to work ;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now