'//
'//
'//  CHEETAH DATABASE 
'//  This is the main source file to compile.
'//  Code is compatible with PowerBASIC for Windows Versions 10+
'//
'//
   

#Dim All

'JPRO_COMPILER=PB/WIN

' Define the following equate because it is used in the cheetah4_powerbasic.inc
' file in order to exclude the compilation of the wrapper functions into the
' DLL itself. 
%CHEETAH_BYPASS_WRAPPERS = 1

' If the following equate is defined then additional developer debug information
' is added to the DLL (eg. dump index info to a file, dump memo garbage chain to file)
'%CHEETAH_DEVELOPER_DEBUG = 1

' If the following equate is defined then functions that support the handling
' of databases will be ommitted. This is needed if you want to create a DLL
' that will only be used to create standalone indexes and nothing else.
'%CHEETAH_INDEX_ONLY = 1             

' If the following equate is defined then a PowerBASIC compatible SLL (static
' Linked Library) is created.
'%CHEETAH_CREATE_SLL = 1             


#If %Def(%CHEETAH_INDEX_ONLY)
   #If %Def(%CHEETAH_CREATE_SLL)
      #Compile Sll "..\bin\IndexOnly\cheetah4.sll"
   #Else
      #Compile Dll "..\bin\IndexOnly\cheetah4.dll"
   #endif
#Else
   #If %Def(%CHEETAH_CREATE_SLL)
      #Compile Sll "..\bin\cheetah4.sll"
   #Else
      #Compile Dll "..\bin\cheetah4.dll"
   #endif   
#endif


#Include "..\include\Cheetah4_PowerBasic.inc"
#Include "ctx_Cheetah4.inc"

#Include "win32api.inc"


Global gCS       As CRITICAL_SECTION
Global workspace As WORKSPACE_TYPE 

' Internally used equates for the SeekEngine that specify the
' type of seek to perform. Some seeks require that the exact
' Key and RecNum be matched (xdbMoveNext/xdbMovePrev) while other
' seeks only need the Key matched (xdbSeek)
%XDB_SEEK_INSERT         = 100
%XDB_SEEK_KEYONLY        = 101
%XDB_SEEK_KEY_AND_RECNUM = 102

#Include "sys_diskio.inc"
#Include "sys_memory.inc"

' If the DLL is being built for indexing only (i.e. no routines to
' handle database operations) then we do not need to include the
' code for databases. This will make the DLL much smaller. If the
' index only equate has not been defined then include the database
' routines as well.
#If Not %Def(%CHEETAH_INDEX_ONLY)
   #Include "sys_misc.inc"
   #Include "dbf_createopen.inc"
   #Include "dbf_records.inc"
   #Include "dbf_fields.inc"
   #Include "dbf_maintenance.inc"
   #Include "dbf_memo_dbase.inc"
   #Include "dbf_memo_foxpro.inc"
   #Include "btx_maintenance.inc"
#EndIf

#Include "btx_createopen.inc"
#Include "btx_insert.inc"             
#Include "btx_delete.inc"             
#Include "btx_move.inc"
#Include "btx_seek.inc"

#If %Def(%CHEETAH_DEVELOPER_DEBUG)
   #Include "sys_debug.inc"
#EndIf
             

#If Not %Def(%CHEETAH_CREATE_SLL)

Function LibMain( ByVal hInstance   As Long, _
                  ByVal fwdReason   As Long, _
                  ByVal lpvReserved As Long _
                  ) Export As Long

   Select Case fwdReason
      Case 0  '  %DLL_PROCESS_DETACH
              DeleteCriticalSection gCS
      Case 1  '  %DLL_PROCESS_ATTACH
              InitializeCriticalSection gCS
      Case 2  '  %DLL_THREAD_ATTACH
      Case 3  '  %DLL_THREAD_DETACH
   End Select

   Function = 1

End Function

#endif



#If %Def(%CHEETAH_CREATE_SLL)
   Function xdbCheetah( ByVal xdb As CHEETAH4_TYPE Ptr ) Common As Long 
#Else
   Function xdbCheetah( ByVal xdb As CHEETAH4_TYPE Ptr ) Export As Long 
  
   EnterCriticalSection gCS
#endif


   Select Case As Long @xdb.nAction

' Include all of the database routines if the DLL is not being
' compiled to only have indexing capabilities.    
#If Not %Def(%CHEETAH_INDEX_ONLY)
      Case %XDB_STATS:          Function = ctx_Stats_Internal (xdb)
      Case %XDB_SHUTDOWN:       Function = ctx_Shutdown_Internal (xdb)
      Case %XDB_CREATEDATABASE: Function = ctx_CreateDatabase_Internal (xdb)
      Case %XDB_OPENDATABASE:   Function = ctx_OpenDatabase_Internal (xdb)
      Case %XDB_CLOSEDATABASE:  Function = ctx_CloseDatabase_Internal (xdb)
      Case %XDB_SETFIELD:       Function = ctx_SetField_Internal (xdb)
      Case %XDB_GETFIELD:       Function = ctx_GetField_Internal (xdb)
      Case %XDB_GETRECORD:      Function = ctx_GetRecord_Internal (xdb)
      Case %XDB_PUTRECORD:      Function = ctx_PutRecord_Internal (xdb)
      Case %XDB_FIELDNUMBER:    Function = ctx_FieldNumber_Internal (xdb)
      Case %XDB_CLEARBUFFER:    Function = ctx_ClearBuffer_Internal (xdb)
      Case %XDB_DELETERECORD:   Function = ctx_DeleteRecord_Internal (xdb)
      Case %XDB_ISDELETED:      Function = ctx_IsDeleted_Internal (xdb)
      Case %XDB_RECALLRECORD:   Function = ctx_RecallRecord_Internal (xdb)
      Case %XDB_FIELDINFO:      Function = ctx_FieldInfo_Internal (xdb)
      Case %XDB_PACK:           Function = ctx_PackDatabase_Internal (xdb)
      Case %XDB_ZAP:            Function = ctx_ZapDatabase_Internal (xdb)
      Case %XDB_REINDEX:        Function = ctx_Reindex_Internal (xdb)
      Case %XDB_MAKEDATETIME:   Function = ctx_MakeDateTime_Internal (xdb)
#EndIf

      Case %XDB_CREATEINDEX:    Function = ctx_CreateIndex_Internal (xdb)
      Case %XDB_CREATETAG:      Function = ctx_CreateTag_Internal (xdb)
      Case %XDB_DELETETAG:      Function = ctx_DeleteTag_Internal (xdb)
      Case %XDB_GETTAG:         Function = ctx_GetTag_Internal (xdb)
      Case %XDB_OPENINDEX:      Function = ctx_OpenIndex_Internal (xdb)
      Case %XDB_ADDKEY:         Function = ctx_InsertKey_Internal( xdb )
      Case %XDB_GETKEY:         Function = ctx_GetKey_Internal( xdb )
      Case %XDB_DELETEKEY:      Function = ctx_DeleteKey_Internal( xdb )
      Case %XDB_SEEK:           Function = ctx_Seek_Internal( xdb )

      Case %XDB_MOVEFIRST, %XDB_MOVENEXT, %XDB_MOVELAST, %XDB_MOVEPREV, _
           %XDB_MOVENEXT_ATOMIC, %XDB_MOVEPREV_ATOMIC       
         Function = ctx_Move_Internal (xdb)
         
         
#If %Def(%CHEETAH_DEVELOPER_DEBUG)
      Case %XDB_DUMPINDEX:      Function = ctx_DumpIndex_Internal (xdb)
#EndIf
      
      Case Else    
        @xdb.nAction = 0
        Function = %ERR_ACTION  'error occurred b/c no valid action was specified.
      
   End Select
  
  
#If Not %Def(%CHEETAH_CREATE_SLL)
   LeaveCriticalSection gCS
#endif
   
End Function


