' Cheetah 4 include file for FreeBASIC compilers.
                                            
#include "crt/string.bi"
                                            
' Type used for each database that is opened
Type CHEETAH4_TYPE Field = 1
   nAction      As Integer   ' Cheetah action to take
   nData        As Integer   ' usually address to a string
   nLen         As Integer   ' usually length of a string  
   nData4byte   As Integer
   nData8byte   As Double     
   errcode      As Integer
   reserved     As Integer   ' do not tamper with this value
End Type


' Type for use with XDB_FIELDINFO     
Type CHEETAH4_FIELD_TYPE Field = 1
   FieldName      As zString * 11  ' name of the field in ASCII
   FieldType      As Byte          ' type of field in ASCII
   FieldLength    As Integer       ' Length
   FieldDecimals  As Integer       ' number of decimals in numeric field
End Type
                                            

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

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

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

Const XDB_DUMPINDEX           = 500


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


extern "Windows-MS" lib "cheetah4"
declare function xdbcheetah alias "XDBCHEETAH" ( byval xdb as CHEETAH4_TYPE ptr ) as uinteger
end extern

Const TRUE  = 1
Const FALSE = 0
                    
                    
' // Wrappers For Cheetah4 And FreeBASIC //

Function xdbShutdown() As uInteger
   Dim dbf As CHEETAH4_TYPE 
   dbf.nAction = XDB_SHUTDOWN
   Function    = xdbCheetah( Varptr(dbf) ) 
End Function


Function xdbCreateDatabase( ByVal sData As String ) As uInteger
   Dim dbf As CHEETAH4_TYPE 
   dbf.nAction = XDB_CREATEDATABASE
   dbf.nData   = cast(integer, strptr( sData ))
   dbf.nLen    = Len( sData )
   Function    = xdbCheetah( Varptr(dbf) ) 
End Function
         
         
Function xdbOpenDatabase( ByVal sData As String, _
                          ByRef dbfHandle As uInteger _
                          ) As uInteger
   Dim dbf As CHEETAH4_TYPE 
   dbf.nAction = XDB_OPENDATABASE
   dbf.nData   = cast(integer, strptr( sData ))
   dbf.nLen    = Len( sData ) 
   Function    = xdbCheetah( Varptr(dbf) )
   ' Return the database handle 
   dbfHandle   = dbf.reserved   
End Function
               

Function xdbCloseDatabase( ByRef dbfHandle As uInteger ) As uInteger
   Dim dbf As CHEETAH4_TYPE 
   dbf.reserved  = dbfHandle
   dbf.nAction   = XDB_CLOSEDATABASE
   Function      = xdbCheetah( Varptr(dbf) ) 
   dbfHandle     = 0
End Function


Function xdbClearBuffer( ByVal dbfHandle As uInteger ) As uInteger
   Dim dbf As CHEETAH4_TYPE 
   dbf.nAction  = XDB_CLEARBUFFER
   dbf.reserved = dbfHandle
   Function = xdbCheetah( Varptr(dbf) ) 
End Function

               
Function xdbPutRecord( ByVal dbfHandle As uInteger, _
                       ByVal nRecordNumber As uInteger, _
                       ByVal nDataAddress As uInteger = 0 _
                       ) As uInteger
   Dim 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 uInteger, _
                         ByRef nRecordCount As uInteger _
                         ) As uInteger
   Dim dbf   As CHEETAH4_TYPE 
   Dim stats As CHEETAH4_STATS_TYPE
   dbf.nAction  = XDB_STATS
   dbf.nData    = cast(integer, varptr(stats)) 
   dbf.reserved = dbfHandle
   Function = xdbCheetah( Varptr(dbf) )
   nRecordCount = stats.RecordCount
End Function         


Function xdbRecordSize( ByVal dbfHandle As uInteger, _
                        ByRef nRecordSize As uInteger _
                        ) As uInteger
   Dim dbf   As CHEETAH4_TYPE 
   Dim stats As CHEETAH4_STATS_TYPE
   dbf.nAction  = XDB_STATS
   dbf.nData    = cast(integer, varptr(stats))
   dbf.reserved = dbfHandle
   Function = xdbCheetah( Varptr(dbf) )
   nRecordSize  = stats.RecordSize
End Function         


Function xdbFieldCount( ByVal dbfHandle As uInteger, _
                        ByRef nFieldCount As uInteger _
                        ) As uInteger
   Dim dbf   As CHEETAH4_TYPE 
   Dim stats As CHEETAH4_STATS_TYPE
   dbf.nAction  = XDB_STATS
   dbf.nData    = cast(integer, varptr(stats)) 
   dbf.reserved = dbfHandle
   Function = xdbCheetah( Varptr(dbf) )
   nFieldCount  = stats.FieldCount
End Function         


Function xdbGetRecord( ByVal dbfHandle As uInteger, _
                       ByVal nRecordNumber As uInteger, _
                       ByVal nDataAddress As uInteger = 0 _
                       ) As uInteger
   Dim 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 uInteger, _
                         ByVal sFieldName As String, _
                         ByRef nFieldNumber As uInteger _
                         ) As uInteger 
   Dim dbf As CHEETAH4_TYPE 
   ' Convert the Field Name to Field Number
   dbf.nAction  = XDB_FIELDNUMBER
   dbf.nData    = cast(integer, strptr( sFieldName ))
   dbf.nLen     = IIf( Len(sFieldName) < 10, Len(sFieldName), 10 )
   dbf.reserved = dbfHandle
   Function = xdbCheetah( Varptr(dbf) ) 
   nFieldNumber = dbf.nData4byte
End Function


Function xdbSetField( ByVal dbfHandle As uInteger, _
                      ByVal sFieldName As String, _
                      ByVal nFieldNumber As uInteger, _
                      ByVal sData As String _
                      ) As uInteger
   Dim dbf     As CHEETAH4_TYPE 
   Dim errcode As uInteger
   ' 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       = cast(integer, strptr( sData ))
   dbf.nLen        = Len(sData)
   dbf.nData4byte  = nFieldNumber
   dbf.reserved    = dbfHandle
   Function        = xdbCheetah( Varptr(dbf) ) 
End Function
                  

Function xdbGetField( ByVal dbfHandle As uInteger, _
                      ByVal sFieldName As String, _
                      ByVal nFieldNumber As uInteger, _ 
                      ByVal nTrimSpaces As uInteger, _
                      ByRef sFieldValue As String _
                      ) As uInteger
   Dim dbf     As CHEETAH4_TYPE 
   Dim errcode As uInteger
   ' 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) )
   
   sFieldValue = Space$( dbf.nLen )
   If dbf.nData Then 
      memcpy byval strptr(sFieldValue), byval dbf.nData, byval dbf.nLen
   End If   
   if nTrimSpaces <> 0 then sFieldValue = rtrim( sFieldValue )
   
End Function


Function xdbFieldInfo( ByVal dbfHandle As uInteger, _
                       FieldInfo As CHEETAH4_FIELD_TYPE, _
                       ByVal nFieldNumber As uInteger _
                       ) As uInteger      
   Dim dbf As CHEETAH4_TYPE 
   dbf.nAction     = XDB_FIELDINFO
   dbf.nData       = cast(integer, varptr( FieldInfo )) 
   dbf.nData4byte  = nFieldNumber
   dbf.reserved    = dbfHandle
   Function = xdbCheetah( Varptr(dbf) ) 
End Function         



Function xdbGetFieldType( ByVal dbfHandle As uInteger, _
                          ByVal sFieldName As String, _
                          ByVal nFieldNumber As uInteger _ 
                          ) As String
   Dim dbf       As CHEETAH4_TYPE 
   Dim FieldInfo As CHEETAH4_FIELD_TYPE
   Dim errcode   As uInteger                              
   
   ' ** 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       = cast(integer, varptr( FieldInfo )) 
   dbf.nData4byte  = nFieldNumber
   dbf.reserved    = dbfHandle
   xdbCheetah Varptr(dbf) 
   
   Function = Chr(FieldInfo.FieldType)

End Function


Function xdbGetFieldLength( ByVal dbfHandle As uInteger, _
                            ByVal sFieldName As String, _
                            ByVal nFieldNumber As uInteger _ 
                            ) As Integer
   Dim dbf       As CHEETAH4_TYPE 
   Dim FieldInfo As CHEETAH4_FIELD_TYPE
   Dim errcode   As uInteger                              
   
   ' ** 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       = cast(integer, varptr( FieldInfo ))
   dbf.nData4byte  = nFieldNumber
   dbf.reserved    = dbfHandle
   xdbCheetah Varptr(dbf) 
   
   Function = FieldInfo.FieldLength

End Function



Function xdbDeleteRecord( ByVal dbfHandle As uInteger ) As uInteger
   Dim 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 uInteger ) As uInteger
   Dim 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 uInteger ) As uInteger
   Dim 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 uInteger ) As uInteger
   Dim dbf As CHEETAH4_TYPE 
   dbf.nAction  = XDB_PACK
   dbf.reserved = dbfHandle
   Function = xdbCheetah( Varptr(dbf) ) 
End Function         


Function xdbZap( ByVal dbfHandle As uInteger ) As uInteger
   Dim dbf As CHEETAH4_TYPE 
   dbf.nAction  = XDB_ZAP
   dbf.reserved = dbfHandle
   Function = xdbCheetah( Varptr(dbf) ) 
End Function         


Function xdbReindex( ByVal dbfHandle As uInteger ) As uInteger
   Dim 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 uInteger, _
                             ByVal sFieldName As String, _
                             ByVal nFieldNumber As uInteger, _
                             ByVal nData As Single _
                             ) As uInteger
   Dim dbf     As CHEETAH4_TYPE 
   Dim errcode As uInteger
   ' 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       = cast(integer, varptr(nData)) 
   dbf.nLen        = 8
   dbf.nData4byte  = nFieldNumber
   dbf.reserved    = dbfHandle
   Function        = xdbCheetah( Varptr(dbf) ) 
End Function



Function xdbSetFieldBinary4( ByVal dbfHandle As uInteger, _
                             ByVal sFieldName As String, _
                             ByVal nFieldNumber As uInteger, _
                             ByVal nData As uInteger _
                             ) As uInteger
   Dim dbf     As CHEETAH4_TYPE 
   Dim errcode As uInteger
   ' 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       = cast(integer, varptr(nData)) 
   dbf.nLen        = 4
   dbf.nData4byte  = nFieldNumber
   dbf.reserved    = dbfHandle
   Function        = xdbCheetah( Varptr(dbf) ) 
End Function


Function xdbSetFieldCurrency( ByVal dbfHandle As uInteger, _
                              ByVal sFieldName As String, _
                              ByVal nFieldNumber As uInteger, _
                              ByVal nData As Single _
                              ) As uInteger
   Dim dbf     As CHEETAH4_TYPE 
   Dim errcode As uInteger
   ' 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       = cast(integer, varptr(nData)) 
   dbf.nLen        = 8
   dbf.nData4byte  = nFieldNumber
   dbf.reserved    = dbfHandle
   Function        = xdbCheetah( Varptr(dbf) ) 
End Function



Function xdbGetFieldBinary8( ByVal dbfHandle As uInteger, _
                             ByVal sFieldName As String, _
                             ByVal nFieldNumber As uInteger, _
                             ByRef nFieldValue As Single _
                             ) As uInteger
   Dim dbf     As CHEETAH4_TYPE 
   Dim errcode As uInteger
   ' 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 uInteger, _
                             ByVal sFieldName As String, _
                             ByVal nFieldNumber As uInteger, _
                             ByRef nFieldValue As uInteger _
                             ) As uInteger
   Dim dbf     As CHEETAH4_TYPE 
   Dim errcode As uInteger
   ' 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 uInteger, _
                              ByVal sFieldName As String, _
                              ByVal nFieldNumber As uInteger, _
                              ByVal nFieldValue As Single _
                              ) As Single
   Dim dbf     As CHEETAH4_TYPE 
   Dim errcode As uInteger
   ' 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 uInteger
   Dim dbf As CHEETAH4_TYPE 
   dbf.nAction = XDB_CREATEINDEX
   dbf.nData   = cast(integer, strptr( sData ))
   dbf.nLen    = Len( sData )
   Function    = xdbCheetah( Varptr(dbf) ) 
End Function


Function xdbOpenIndex( ByVal nData As String, _
                       ByVal dbfHandle As uInteger, _
                       ByRef idxHandle As uInteger _
                       ) As uInteger
   Dim dbf As CHEETAH4_TYPE 
   dbf.nAction    = XDB_OPENINDEX
   dbf.nData      = cast(integer, 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 uInteger _
                       ) As uInteger
   Dim dbf As CHEETAH4_TYPE 
   dbf.nAction    = XDB_CREATETAG
   dbf.nData      = cast(integer, strptr( sData )) 
   dbf.nLen       = Len( sData )
   dbf.nData4byte = idxHandle
   Function = xdbCheetah( Varptr(dbf) ) 
End Function


Function xdbGetTag( ByVal sData As String, _
                    ByVal idxHandle As uInteger, _
                    ByRef tagHandle As uInteger _
                    ) As uInteger
   Dim dbf As CHEETAH4_TYPE 
   dbf.nAction    = XDB_GETTAG
   dbf.nData      = cast(integer, 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 uInteger _
                       ) As uInteger
   Dim dbf As CHEETAH4_TYPE 
   dbf.nAction    = XDB_DELETETAG
   dbf.nData      = cast(integer, strptr( sData )) 
   dbf.nLen       = len( sData )
   dbf.nData4byte = idxHandle
   Function = xdbCheetah( Varptr(dbf) ) 
End Function


               
Function xdbCloseIndex( ByRef idxHandle As uInteger ) As uInteger
   Dim dbf As CHEETAH4_TYPE 
   dbf.reserved  = idxHandle
   dbf.nAction   = XDB_CLOSEINDEX
   Function      = xdbCheetah( Varptr(dbf) ) 
   idxHandle     = 0
End Function


Function xdbAddKey( ByVal tagHandle As uInteger, _
                    ByVal sKey As String, _
                    ByVal nRecordNumber As uInteger _
                    ) As uInteger
   Dim 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      = cast(integer, strptr( sKey )) 
   dbf.nLen       = Len( sKey )
   dbf.nAction    = XDB_ADDKEY
   dbf.nData4byte = nRecordNumber   
   Function       = xdbCheetah( Varptr(dbf) ) 
End Function


Function xdbDeleteKey( ByVal tagHandle As uInteger, _
                       ByVal sKey As String, _
                       ByVal nRecordNumber As uInteger _
                       ) As uInteger
   Dim 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      = cast(integer, strptr( sKey )) 
   dbf.nLen       = len( sKey )
   dbf.nAction    = XDB_DELETEKEY
   dbf.nData4byte = nRecordNumber   
   Function       = xdbCheetah( Varptr(dbf) ) 
End Function


Function xdbGetKey( ByVal tagHandle As uInteger, _
                    ByRef sKeyValue As String, _
                    ByRef nRecordNumber As uInteger _
                    ) As uInteger
   Dim dbf As CHEETAH4_TYPE 
   dbf.nAction  = XDB_GETKEY
   dbf.reserved = tagHandle
   Function = xdbCheetah( Varptr(dbf) )
   
   sKeyValue = Space$( dbf.nLen )
   If dbf.nData Then 
      memcpy byval strptr(sKeyValue), byval dbf.nData, byval dbf.nLen
   End If   
   nRecordNumber = dbf.nData4byte
End Function
   

Function xdbMove( ByVal dbfHandle  As uInteger, _
                  ByVal nTagHandle As uInteger, _
                  ByVal nOperation As uInteger, _
                  ByRef nRecordNumber As uInteger _
                  ) As uInteger
   Dim 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 uInteger, _
                  ByVal sKey As String, _
                  ByRef nRecordNumber As uInteger _
                  ) As uInteger
   Dim dbf As CHEETAH4_TYPE 
   dbf.nAction    = XDB_SEEK
   dbf.reserved   = nTagHandle
   dbf.nData      = cast(integer, 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 sData As String, _
                       ByVal idxHandle As uInteger, _
                       ByVal nMaxKeySize As uInteger _
                       ) As uInteger
   Dim 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      = cast(integer, strptr( sData )) 
   dbf.nLen       = Len( sData )
   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 Single _
                          ) As uInteger
   Dim dbf     As CHEETAH4_TYPE 
   dbf.nAction = XDB_MAKEDATETIME
   dbf.nData   = cast(integer, strptr( sDateTime )) 
   dbf.nLen    = Len( sDateTime )
   Function = xdbCheetah( Varptr(dbf) )

   nDateTime = dbf.nData8byte
End Function

