' Cheetah 4 include file for PowerBASIC compilers.
                                            
                                            
' Type used for each database that is opened
Type CHEETAH4_TYPE 
   nAction      As Long   ' Cheetah action to take
   nData        As Long   ' usually address to a string
   nLen         As Long   ' usually length of a string  
   nData4byte   As Long
   nData8byte   As Double     
   errcode      As Long
   reserved     As Long   ' do not tamper with this value
End Type


' Type for use with %XDB_FIELDINFO     
Type CHEETAH4_FIELD_TYPE
   FieldName      As Asciiz * 11   ' name of the field in ASCII
   FieldType      As String * 1    ' type of field in ASCII
   FieldLength    As Long          ' Length
   FieldDecimals  As Long          ' number of decimals in numeric field
End Type
                                            

' Type for use with %XDB_STATS
Type CHEETAH4_STATS_TYPE
   RecordCount      As Long    ' number of records in the database
   RecordSize       As Long    ' includes 1 character for the deleted flag
   FieldCount       As Long    ' number of fields in the database  
End Type

                                            
' Action Codes 
%XDB_STATS               = 1
%XDB_SHUTDOWN            = 2
%XDB_CREATEDATABASE      = 3
%XDB_OPENDATABASE        = 4
%XDB_CLOSEDATABASE       = 5 
%XDB_SETFIELD            = 6
%XDB_GETFIELD            = 7
%XDB_FIELDNUMBER         = 8
%XDB_CLEARBUFFER         = 9
%XDB_GETRECORD           = 10
%XDB_PUTRECORD           = 11
%XDB_DELETERECORD        = 12
%XDB_RECALLRECORD        = 13
%XDB_MAKEDATETIME        = 14
%XDB_PACK                = 15
%XDB_ZAP                 = 16
%XDB_ISDELETED           = 17
%XDB_FIELDINFO           = 18
%XDB_REINDEX             = 19

' If the DLL was compiled to only include the indexing routines, then only
' the following equates are valid.
%XDB_CREATEINDEX         = 20
%XDB_OPENINDEX           = 21
%XDB_CLOSEINDEX          = 22
%XDB_CREATETAG           = 23
%XDB_GETTAG              = 24
%XDB_DELETETAG           = 25
%XDB_ADDKEY              = 26 
%XDB_DELETEKEY           = 27
%XDB_GETKEY              = 28
%XDB_MOVEFIRST           = 29
%XDB_MOVENEXT            = 30
%XDB_MOVELAST            = 31
%XDB_MOVEPREV            = 32
%XDB_MOVEPREVIOUS        = %XDB_MOVEPREV
%XDB_MOVENEXT_ATOMIC     = 33
%XDB_MOVEPREV_ATOMIC     = 34
%XDB_MOVEPREVIOUS_ATOMIC = %XDB_MOVEPREV_ATOMIC
%XDB_SEEK                = 35

%XDB_DUMPINDEX      = 500


'Error Codes
%ERR_ACTION           = 1000
%ERR_DATA             = 1001
%ERR_FILENAME         = 1002 
%ERR_FILETYPE         = 1003   
%ERR_ACCESSMODE       = 1004
%ERR_SHAREMODE        = 1005
%ERR_FILEACCESS       = 1006
%ERR_MAXFIELDS        = 1007
%ERR_FIELDNAME        = 1008
%ERR_FIELDTYPE        = 1009
%ERR_FIELDLENGTH      = 1010
%ERR_FIELDDECIMALS    = 1011
%ERR_MEMORYALLOC      = 1012
%ERR_FIELDNUMBER      = 1013
%ERR_RECORDNUMBER     = 1014
%ERR_BLOCKSIZE        = 1015
%ERR_FIELDMISMATCH    = 1016  ' field is invalid for file type (dbase, foxpro)
%ERR_DATETIME         = 1017   
%ERR_MISSINGMEMO      = 1018
%ERR_TAGNAME          = 1019
%ERR_TAGEXPRESSION    = 1020
%ERR_INDEXTAGFULL     = 1021  
%ERR_KEYSIZE          = 1022  
%ERR_MOVEERROR        = 1023
%ERR_SEEKNOMATCH      = 1024
%ERR_SEEKEXACTMATCH   = 1025
%ERR_SEEKPARTIALMATCH = 1026

' The Cheetah source code using the Types and Equates defined above but it does
' not need the wrappers. Therefore, the Cheetah source will define the %CHEETAH_BYPASS_WRAPPERS
' in order to exclude the following unneeded wrappers.
#If Not %Def (%CHEETAH_BYPASS_WRAPPERS)

#If %Def (%CHEETAH_CREATE_SLL)
Declare Function xdbcheetah ( ByVal xdb As CHEETAH4_TYPE Ptr ) Common As Long
#Else
Declare Function xdbcheetah Lib "CHEETAH4.DLL" Alias "XDBCHEETAH" ( ByVal xdb As CHEETAH4_TYPE Ptr ) As Long
#endif

#If Not %Def (%TRUE)
   %TRUE = 1
#endif
#If Not %Def (%FALSE)
   %FALSE = 0
#endif
   

'// Wrappers for Cheetah4 and PowerBASIC //

Function xdbShutdown() As Long
   Local dbf As CHEETAH4_TYPE 
   dbf.nAction = %XDB_SHUTDOWN
   Function    = xdbCheetah( VarPtr(dbf) ) 
End Function


Function xdbCreateDatabase( ByVal sData As String ) As Long
   Local dbf As CHEETAH4_TYPE 
   dbf.nAction = %XDB_CREATEDATABASE
   dbf.nData   = StrPtr( sData )
   dbf.nLen    = Len( sData )
   Function    = xdbCheetah( VarPtr(dbf) ) 
End Function
         
         
Function xdbOpenDatabase( ByVal sData As String, _
                          ByRef dbfHandle As Long _
                          ) As Long
   Local dbf As CHEETAH4_TYPE 
   dbf.nAction = %XDB_OPENDATABASE
   dbf.nData   = StrPtr( sData )
   dbf.nLen    = Len( sData ) 
   Function    = xdbCheetah( VarPtr(dbf) )
   ' Return the database handle 
   dbfHandle   = dbf.reserved
End Function
               

Function xdbCloseDatabase( ByRef dbfHandle As Long ) As Long
   Local dbf As CHEETAH4_TYPE 
   dbf.reserved  = dbfHandle
   dbf.nAction   = %XDB_CLOSEDATABASE
   Function      = xdbCheetah( VarPtr(dbf) ) 
   dbfHandle     = 0
End Function


Function xdbClearBuffer( ByVal dbfHandle As Long ) As Long
   Local dbf As CHEETAH4_TYPE 
   dbf.nAction  = %XDB_CLEARBUFFER
   dbf.reserved = dbfHandle
   Function = xdbCheetah( VarPtr(dbf) ) 
End Function

               
Function xdbPutRecord( ByVal dbfHandle As Long, _
                       ByVal nRecordNumber As Long, _
                       Optional ByVal nDataAddress As Dword _
                       ) As Long
   Local dbf As CHEETAH4_TYPE 
   ' If the incoming Record Number is 0 or negative then the record is Appended.
   dbf.nAction     = %XDB_PUTRECORD
   dbf.nData       = nDataAddress                  
   dbf.nData4byte  = nRecordNumber   
   dbf.reserved    = dbfHandle
   Function = xdbCheetah( VarPtr(dbf) ) 
End Function         


Function xdbRecordCount( ByVal dbfHandle As Long, _
                         ByRef nRecordCount As Long _
                         ) As Long
   Local dbf   As CHEETAH4_TYPE 
   Local stats As CHEETAH4_STATS_TYPE
   dbf.nAction  = %XDB_STATS
   dbf.nData    = VarPtr(stats)
   dbf.reserved = dbfHandle
   Function = xdbCheetah( VarPtr(dbf) )
   nRecordCount = stats.RecordCount
End Function         


Function xdbRecordSize( ByVal dbfHandle As Long, _
                        ByRef nRecordSize As Long _
                        ) As Long
   Local dbf   As CHEETAH4_TYPE 
   Local stats As CHEETAH4_STATS_TYPE
   dbf.nAction  = %XDB_STATS
   dbf.nData    = VarPtr(stats)
   dbf.reserved = dbfHandle
   Function = xdbCheetah( VarPtr(dbf) )
   nRecordSize  = stats.RecordSize
End Function         


Function xdbFieldCount( ByVal dbfHandle As Long, _
                        ByRef nFieldCount As Long _
                        ) As Long
   Local dbf   As CHEETAH4_TYPE 
   Local stats As CHEETAH4_STATS_TYPE
   dbf.nAction  = %XDB_STATS
   dbf.nData    = VarPtr(stats)
   dbf.reserved = dbfHandle
   Function = xdbCheetah( VarPtr(dbf) )
   nFieldCount  = stats.FieldCount
End Function         


Function xdbGetRecord( ByVal dbfHandle As Long, _
                       ByVal nRecordNumber As Long, _
                       Optional ByVal nDataAddress As Dword _
                       ) As Long
   Local dbf As CHEETAH4_TYPE 
   ' If nDataAddress is specified then the record buffer is copied to
   ' that data location. This is useful for retrieving a record directly
   ' into a TYPE variable.
   dbf.nAction     = %XDB_GETRECORD
   dbf.nData       = nDataAddress                  
   dbf.nData4byte  = nRecordNumber
   dbf.reserved    = dbfHandle
   Function = xdbCheetah( VarPtr(dbf) ) 
End Function

               
Function xdbFieldNumber( ByVal dbfHandle As Long, _
                         ByVal sFieldName As String, _
                         ByRef nFieldNumber As Long _
                         ) As Long 
   Local dbf As CHEETAH4_TYPE 
   ' Convert the Field Name to Field Number
   dbf.nAction  = %XDB_FIELDNUMBER
   dbf.nData    = StrPtr(sFieldName)
   dbf.nLen     = Min(10, Len(sFieldName))
   dbf.reserved = dbfHandle
   Function = xdbCheetah( VarPtr(dbf) ) 
   nFieldNumber = dbf.nData4byte
End Function


Function xdbSetField( ByVal dbfHandle As Long, _
                      ByVal sFieldName As String, _
                      ByVal nFieldNumber As Long, _
                      ByVal sData As String _
                      ) As Long
   Local dbf     As CHEETAH4_TYPE 
   Local errcode As Long
   ' If the field number was not specified then retrieve 
   ' the number via the field name.
   If nFieldNumber = 0 Then      
      errcode = xdbFieldNumber( dbfHandle, sFieldName, nFieldNumber )
      If errcode Then Function = errcode: Exit Function
   End If                 

   dbf.nAction     = %XDB_SETFIELD
   dbf.nData       = StrPtr(sData)
   dbf.nLen        = Len(sData)
   dbf.nData4byte  = nFieldNumber
   dbf.reserved    = dbfHandle
   Function        = xdbCheetah( VarPtr(dbf) ) 
End Function
                  

Function xdbGetField( ByVal dbfHandle As Long, _
                      ByVal sFieldName As String, _
                      ByVal nFieldNumber As Long, _ 
                      ByVal nTrimSpaces As Long, _
                      ByRef sFieldValue As String _
                      ) As Long
   Local dbf     As CHEETAH4_TYPE 
   Local errcode As Long
   ' If the field number was not specified then retrieve 
   ' the number via the field name.
   If nFieldNumber = 0 Then      
      errcode = xdbFieldNumber( dbfHandle, sFieldName, nFieldNumber )
      If errcode Then Function = errcode: Exit Function
   End If                 
   
   dbf.nAction     = %XDB_GETFIELD
   dbf.nData4byte  = nFieldNumber
   dbf.reserved    = dbfHandle
   Function = xdbCheetah( VarPtr(dbf) )
   
   If dbf.nData Then sFieldValue = Peek$( dbf.nData, dbf.nLen )
   If IsTrue( nTrimSpaces ) Then sFieldValue = RTrim$( sFieldValue )
   
End Function


Function xdbFieldInfo( ByVal dbfHandle As Long, _
                       FieldInfo As CHEETAH4_FIELD_TYPE, _
                       ByVal nFieldNumber As Long _
                       ) As Long      
   Local dbf As CHEETAH4_TYPE 
   dbf.nAction     = %XDB_FIELDINFO
   dbf.nData       = VarPtr( FieldInfo )
   dbf.nData4byte  = nFieldNumber
   dbf.reserved    = dbfHandle
   Function = xdbCheetah( VarPtr(dbf) ) 
End Function         



Function xdbGetFieldType( ByVal dbfHandle As Long, _
                          ByVal sFieldName As String, _
                          ByVal nFieldNumber As Long _ 
                          ) As String
   Local dbf       As CHEETAH4_TYPE 
   Local FieldInfo As CHEETAH4_FIELD_TYPE
   Local errcode   As Long                              
   
   ' ** NOTE: This wrapper does not return an error like
   ' all of the other wrappers. It simply returns the
   ' field type of the field. This is for convenience.
   
   ' If the field number was not specified then retrieve 
   ' the number via the field name.
   If nFieldNumber = 0 Then      
      errcode = xdbFieldNumber( dbfHandle, sFieldName, nFieldNumber )
      If errcode Then Function = "": Exit Function
   End If                 
   
   dbf.nAction     = %XDB_FIELDINFO
   dbf.nData       = VarPtr( FieldInfo )
   dbf.nData4byte  = nFieldNumber
   dbf.reserved    = dbfHandle
   xdbCheetah VarPtr(dbf) 
   
   Function = FieldInfo.FieldType

End Function


Function xdbGetFieldLength( ByVal dbfHandle As Long, _
                            ByVal sFieldName As String, _
                            ByVal nFieldNumber As Long _ 
                            ) As Long
   Local dbf       As CHEETAH4_TYPE 
   Local FieldInfo As CHEETAH4_FIELD_TYPE
   Local errcode   As Long                              
   
   ' ** NOTE: This wrapper does not return an error like
   ' all of the other wrappers. It simply returns the
   ' field length of the field. This is for convenience.
   
   ' If the field number was not specified then retrieve 
   ' the number via the field name.
   If nFieldNumber = 0 Then      
      errcode = xdbFieldNumber( dbfHandle, sFieldName, nFieldNumber )
      If errcode Then Function = 0: Exit Function
   End If                 
   
   dbf.nAction     = %XDB_FIELDINFO
   dbf.nData       = VarPtr( FieldInfo )
   dbf.nData4byte  = nFieldNumber
   dbf.reserved    = dbfHandle
   xdbCheetah VarPtr(dbf) 
   
   Function = FieldInfo.FieldLength

End Function



Function xdbDeleteRecord( ByVal dbfHandle As Long ) As Long
   Local dbf As CHEETAH4_TYPE 
   ' Deletes the currently loaded database record. The database
   ' record must already have been loaded via xdbGetRecord
   dbf.nAction     = %XDB_DELETERECORD
   dbf.reserved    = dbfHandle
   Function = xdbCheetah( VarPtr(dbf) ) 
End Function         


Function xdbIsDeleted( ByVal dbfHandle As Long ) As Long
   Local dbf As CHEETAH4_TYPE 
   ' Determines if the currently loaded database record is marked
   ' for deletion. The database record must already have been loaded 
   ' via xdbGetRecord. 
   
   ' ** NOTE: This wrapper does not return an error like
   ' all of the other wrappers. It simply returns TRUE/FALSE.
   ' This is for convenience.

   dbf.nAction  = %XDB_ISDELETED
   dbf.reserved = dbfHandle
   xdbCheetah VarPtr(dbf) 
   Function     = dbf.nLen
End Function         


Function xdbRecallRecord( ByVal dbfHandle As Long ) As Long
   Local dbf As CHEETAH4_TYPE 
   ' Undeletes the currently loaded database record if it was marked
   ' for deletion. The database record must already have been loaded 
   ' via xdbGetRecord
   dbf.nAction     = %XDB_RECALLRECORD
   dbf.reserved    = dbfHandle
   Function = xdbCheetah( VarPtr(dbf) ) 
End Function         


Function xdbPack( ByVal dbfHandle As Long ) As Long
   Local dbf As CHEETAH4_TYPE 
   dbf.nAction  = %XDB_PACK
   dbf.reserved = dbfHandle
   Function = xdbCheetah( VarPtr(dbf) ) 
End Function         


Function xdbZap( ByVal dbfHandle As Long ) As Long
   Local dbf As CHEETAH4_TYPE 
   dbf.nAction  = %XDB_ZAP
   dbf.reserved = dbfHandle
   Function = xdbCheetah( VarPtr(dbf) ) 
End Function         


Function xdbReindex( ByVal dbfHandle As Long ) As Long
   Local dbf As CHEETAH4_TYPE 
   ' Reindexes all open indexes/Tags associated with the dbfHandle. You
   ' can not reindex individual indexes.
   dbf.nAction    = %XDB_REINDEX
   dbf.reserved   = dbfHandle
   Function = xdbCheetah( VarPtr(dbf) ) 
End Function         


Function xdbSetFieldBinary8( ByVal dbfHandle As Long, _
                             ByVal sFieldName As String, _
                             ByVal nFieldNumber As Long, _
                             ByVal nData As Double _
                             ) As Long
   Local dbf     As CHEETAH4_TYPE 
   Local errcode As Long
   ' If the field number was not specified then retrieve 
   ' the number via the field name.
   If nFieldNumber = 0 Then      
      errcode = xdbFieldNumber( dbfHandle, sFieldName, nFieldNumber )
      If errcode Then Function = errcode: Exit Function
   End If                 

   dbf.nAction     = %XDB_SETFIELD
   dbf.nData       = VarPtr(nData)
   dbf.nLen        = 8
   dbf.nData4byte  = nFieldNumber
   dbf.reserved    = dbfHandle
   Function        = xdbCheetah( VarPtr(dbf) ) 
End Function



Function xdbSetFieldBinary4( ByVal dbfHandle As Long, _
                             ByVal sFieldName As String, _
                             ByVal nFieldNumber As Long, _
                             ByVal nData As Long _
                             ) As Long
   Local dbf     As CHEETAH4_TYPE 
   Local errcode As Long
   ' If the field number was not specified then retrieve 
   ' the number via the field name.
   If nFieldNumber = 0 Then      
      errcode = xdbFieldNumber( dbfHandle, sFieldName, nFieldNumber )
      If errcode Then Function = errcode: Exit Function
   End If                 

   dbf.nAction     = %XDB_SETFIELD
   dbf.nData       = VarPtr(nData)
   dbf.nLen        = 4
   dbf.nData4byte  = nFieldNumber
   dbf.reserved    = dbfHandle
   Function        = xdbCheetah( VarPtr(dbf) ) 
End Function


Function xdbSetFieldCurrency( ByVal dbfHandle As Long, _
                              ByVal sFieldName As String, _
                              ByVal nFieldNumber As Long, _
                              ByVal nData As Currency _
                              ) As Long
   Local dbf     As CHEETAH4_TYPE 
   Local errcode As Long
   ' If the field number was not specified then retrieve 
   ' the number via the field name.
   If nFieldNumber = 0 Then      
      errcode = xdbFieldNumber( dbfHandle, sFieldName, nFieldNumber )
      If errcode Then Function = errcode: Exit Function
   End If                 

   dbf.nAction     = %XDB_SETFIELD
   dbf.nData       = VarPtr(nData)
   dbf.nLen        = 8
   dbf.nData4byte  = nFieldNumber
   dbf.reserved    = dbfHandle
   Function        = xdbCheetah( VarPtr(dbf) ) 
End Function



Function xdbGetFieldBinary8( ByVal dbfHandle As Long, _
                             ByVal sFieldName As String, _
                             ByVal nFieldNumber As Long, _
                             ByRef nFieldValue As Double _
                             ) As Long
   Local dbf     As CHEETAH4_TYPE 
   Local errcode As Long
   ' If the field number was not specified then retrieve 
   ' the number via the field name.
   If nFieldNumber = 0 Then      
      errcode = xdbFieldNumber( dbfHandle, sFieldName, nFieldNumber )
      If errcode Then Function = errcode: Exit Function
   End If                 

   dbf.nAction    = %XDB_GETFIELD
   dbf.nData4byte = nFieldNumber
   dbf.reserved   = dbfHandle
   Function = xdbCheetah( VarPtr(dbf) )
   
   nFieldValue = dbf.nData8byte

End Function



Function xdbGetFieldBinary4( ByVal dbfHandle As Long, _
                             ByVal sFieldName As String, _
                             ByVal nFieldNumber As Long, _
                             ByRef nFieldValue As Long _
                             ) As Long
   Local dbf     As CHEETAH4_TYPE 
   Local errcode As Long
   ' If the field number was not specified then retrieve 
   ' the number via the field name.
   If nFieldNumber = 0 Then      
      errcode = xdbFieldNumber( dbfHandle, sFieldName, nFieldNumber )
      If errcode Then Function = errcode: Exit Function
   End If                 

   dbf.nAction    = %XDB_GETFIELD
   dbf.nData4byte = nFieldNumber
   dbf.reserved   = dbfHandle
   Function = xdbCheetah( VarPtr(dbf) )
   
   nFieldValue = dbf.nData4byte     

End Function



Function xdbGetFieldCurrency( ByVal dbfHandle As Long, _
                              ByVal sFieldName As String, _
                              ByVal nFieldNumber As Long, _
                              ByVal nFieldValue As Double _
                              ) As Currency
   Local dbf     As CHEETAH4_TYPE 
   Local errcode As Long
   ' If the field number was not specified then retrieve 
   ' the number via the field name.
   If nFieldNumber = 0 Then      
      errcode = xdbFieldNumber( dbfHandle, sFieldName, nFieldNumber )
      If errcode Then Function = errcode: Exit Function
   End If                 
   
   dbf.nAction    = %XDB_GETFIELD
   dbf.nData4byte = nFieldNumber
   dbf.reserved   = dbfHandle
   Function = xdbCheetah( VarPtr(dbf) )
   
   Function = dbf.nData8byte

End Function



Function xdbCreateIndex( ByVal sData As String ) As Long
   Local dbf As CHEETAH4_TYPE 
   dbf.nAction = %XDB_CREATEINDEX
   dbf.nData   = StrPtr( sData )
   dbf.nLen    = Len( sData )
   Function    = xdbCheetah( VarPtr(dbf) ) 
End Function


Function xdbOpenIndex( ByVal nData As String, _
                       ByVal dbfHandle As Long, _
                       ByRef idxHandle As Long _
                       ) As Long
   Local dbf As CHEETAH4_TYPE 
   dbf.nAction    = %XDB_OPENINDEX
   dbf.nData      = StrPtr(nData)
   dbf.nLen       = Len(nData) 
   dbf.nData4byte = dbfHandle
   Function = xdbCheetah( VarPtr(dbf) )
   ' Return the index handle 
   idxHandle   = dbf.reserved
End Function


Function xdbCreateTag( ByVal sData As String, _
                       ByVal idxHandle As Long _
                       ) As Long
   Local dbf As CHEETAH4_TYPE 
   dbf.nAction    = %XDB_CREATETAG
   dbf.nData      = StrPtr( sData )
   dbf.nLen       = Len( sData )
   dbf.nData4byte = idxHandle
   Function = xdbCheetah( VarPtr(dbf) ) 
End Function


Function xdbGetTag( ByVal sData As String, _
                    ByVal idxHandle As Long, _
                    ByRef tagHandle As Long _
                    ) As Long
   Local dbf As CHEETAH4_TYPE 
   dbf.nAction    = %XDB_GETTAG
   dbf.nData      = StrPtr( sData )
   dbf.nLen       = Len( sData )
   dbf.nData4byte = idxHandle
   Function = xdbCheetah( VarPtr(dbf) ) 
   tagHandle      = dbf.reserved
End Function


Function xdbDeleteTag( ByVal sData As String, _
                       ByVal idxHandle As Long _
                       ) As Long
   Local dbf As CHEETAH4_TYPE 
   dbf.nAction    = %XDB_DELETETAG
   dbf.nData      = StrPtr( sData )
   dbf.nLen       = Len( sData )
   dbf.nData4byte = idxHandle
   Function = xdbCheetah( VarPtr(dbf) ) 
End Function


               
Function xdbCloseIndex( ByRef idxHandle As Long ) As Long
   Local dbf As CHEETAH4_TYPE 
   dbf.reserved  = idxHandle
   dbf.nAction   = %XDB_CLOSEINDEX
   Function      = xdbCheetah( VarPtr(dbf) ) 
   idxHandle     = 0
End Function


Function xdbAddKey( ByVal tagHandle As Long, _
                    ByVal sKey As String, _
                    ByVal nRecordNumber As Long _
                    ) As Long
   Local dbf As CHEETAH4_TYPE 
   ' This function is only useful for standalone indexes. For indexes
   ' associated with a an open database, the keys are added automatically
   ' whenever a database record is added or updated.
   
   ' NOTE: Remember to pass the Tag Handle rather than the Index Handle.
   
   dbf.reserved   = tagHandle
   dbf.nData      = StrPtr( sKey )
   dbf.nLen       = Len( sKey )
   dbf.nAction    = %XDB_ADDKEY
   dbf.nData4byte = nRecordNumber   
   Function       = xdbCheetah( VarPtr(dbf) ) 
End Function


Function xdbDeleteKey( ByVal tagHandle As Long, _
                       ByVal sKey As String, _
                       ByVal nRecordNumber As Long _
                       ) As Long
   Local dbf As CHEETAH4_TYPE 
   ' This function is only useful for standalone indexes. For indexes
   ' associated with a an open database, the keys are deleted automatically
   ' whenever a database record is Packed or Zapped.

   ' NOTE: Remember to pass the Tag Handle rather than the Index Handle.

   dbf.reserved   = tagHandle
   dbf.nData      = StrPtr( sKey )
   dbf.nLen       = Len( sKey )
   dbf.nAction    = %XDB_DELETEKEY
   dbf.nData4byte = nRecordNumber   
   Function       = xdbCheetah( VarPtr(dbf) ) 
End Function


Function xdbGetKey( ByVal tagHandle As Long, _
                    ByRef sKeyValue As String, _
                    ByRef nRecordNumber As Long _
                    ) As Long
   Local dbf As CHEETAH4_TYPE 
   dbf.nAction  = %XDB_GETKEY
   dbf.reserved = tagHandle
   Function = xdbCheetah( VarPtr(dbf) )
   
   If dbf.nData Then sKeyValue = Peek$( dbf.nData, dbf.nLen )
   nRecordNumber = dbf.nData4byte
End Function
   

Function xdbMove( ByVal dbfHandle  As Long, _
                  ByVal nTagHandle As Long, _
                  ByVal nOperation As Long, _
                  ByRef nRecordNumber As Long _
                  ) As Long
   Local dbf As CHEETAH4_TYPE 
   dbf.nAction    = nOperation
   dbf.nData4byte = dbfHandle
   dbf.reserved   = nTagHandle
   Function = xdbCheetah( VarPtr(dbf) )
   ' Return the record number as retrieved from the index tag
   nRecordNumber  = dbf.nData4byte
End Function


Function xdbSeek( ByVal nTagHandle As Long, _
                  ByVal sKey As String, _
                  ByRef nRecordNumber As Long _
                  ) As Long
   Local dbf As CHEETAH4_TYPE 
   dbf.nAction    = %XDB_SEEK
   dbf.reserved   = nTagHandle
   dbf.nData      = StrPtr( sKey )
   dbf.nLen       = Len( sKey )
   Function       = xdbCheetah( VarPtr(dbf) ) 
   ' Return the record number as retrieved from the index tag
   nRecordNumber  = dbf.nData4byte
End Function


Function xdbDumpIndex( ByVal sFilename As String, _
                       ByVal idxHandle As Long, _
                       ByVal nMaxKeySize As Long _
                       ) As Long
   Local dbf As CHEETAH4_TYPE 
   ' This function is used for developer purposes in order to dump
   ' information about an index to disk ("_dump.txt"). For regular
   ' programs, this function is meaningless.
   dbf.nData      = StrPtr( sFilename )
   dbf.nLen       = Len( sFilename )
   dbf.reserved   = idxHandle
   dbf.nAction    = %XDB_DUMPINDEX              
   ' nMaxKeySize is the amount of the Key that will be printed to the
   ' debug text file. This is useful for when trying to display a report
   ' that has keys greater than the screen width. By specifying a smaller
   ' value for nMaxkeySize then only the Left(Key, nMaxKeySize) characters
   ' will be displayed.
   dbf.nData4Byte = nMaxKeySize
   Function = xdbCheetah( VarPtr(dbf) ) 
End Function


Function xdbMakeDateTime( ByVal sDateTime As String, _
                          ByRef nDateTime As Double _
                          ) As Long
   Local dbf     As CHEETAH4_TYPE 
   dbf.nAction = %XDB_MAKEDATETIME
   dbf.nData   = StrPtr( sDateTime )
   dbf.nLen    = Len( sDateTime )
   Function = xdbCheetah( VarPtr(dbf) )

   nDateTime = dbf.nData8byte
End Function

#EndIf
