Attribute VB_Name = "mod_ctPackage_TOCCache"
' Copyright (C) 2003-2006 Jan Vorel
'
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 2 of the License, or
' (at your option) any later version.

' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.

' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

' This is a shared modul, class modul, usercontrol or form. It is also
' published under the GNU General Public License.

' If you have any questions, suggestions or bug reports about this product,
' contact me: info@ctuser.net; Do not contact me about general B.A.S.I.C.
' language or A.P.I. issues

Option Explicit
Option Base 0

' requires ctPackage_UseGetRawData!!!!
'ctPackage_UseGetRawData

Private Const xFilesMax As Long = 65535
Private xFile(xFilesMax - 1&) As String
Private xFileCount As Long
Private sLong1 As Long
Private Const FilenameOffset As Long = 14&
Private Const FilePosOffset As Long = 2&
Private Const FileIntOffset As Long = 6&
Private Const FileExtOffset As Long = 10&

Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Long
Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long

Public Sub ctPackage_FlushCache(ByVal ctPackagePointer As Long)
  xFileCount = ctPackage_GetFileList(ctPackagePointer, xFile(), xFilesMax)
  End Sub


'#If ctPackage_CachedTOC_GetRawFileData Then
  Public Function ctPackage_CachedTOC_GetRawFileData(ByVal ctPackagePointer As Long, ByRef PutFilenameGetData As String, Optional ByRef ReturnSize As Long, Optional ByVal ProgressControl As Long = 0&, Optional ByVal ProgressStart As Long = 0&, Optional ByVal ProgressTotal As Long = 0&, Optional ByVal ProgressQueue As Long = 0&) As Boolean
    Dim xLong1 As Long
    Dim xLong2 As Long
    Dim xChar1 As String
    Dim ctPIType As ctPackage_InfoType
    
    If ctPackage_GetInfoEx(ctPackagePointer, ctPIType) Then
      xChar1 = LCase(PutFilenameGetData)
      PutFilenameGetData = ""
      For sLong1 = 0& To xFileCount - 1&
        If LCase(Mid(xFile(sLong1), FilenameOffset)) = xChar1 Then
          xLong2 = LenCharToLong(xFile(sLong1), FileIntOffset)
          PutFilenameGetData = String(xLong2, vbNullChar)
          
          'Get FileID, LenCharToLong(xFile(sLong1), FilePosOffset) + 1&, PutFilenameGetData
          If Not SetFilePointer(ctPIType.FileHandle, LenCharToLong(xFile(sLong1), FilePosOffset), 0&, 0&) = -1& Then
            If Not ReadFile(ctPIType.FileHandle, ByVal PutFilenameGetData, xLong2, 0&, ByVal 0&) = 0& Then
          
              ReturnSize = LenCharToLong(xFile(sLong1), FileExtOffset)
              xLong1 = Asc(Left(xFile(sLong1), 1))
              If (xLong1 And 1&) = 1& Then
                'PutFilenameGetData = ZLibDecompress(PutFilenameGetData, ReturnSize)
                If ProgressQueue = -1& Then
                  ProgressQueue = ReturnSize
                  End If
                PutFilenameGetData = ctCompressA_Decompress(PutFilenameGetData, 0&, ProgressControl, ProgressStart, ProgressTotal, ProgressQueue)
                End If
              ctPackage_CachedTOC_GetRawFileData = True
              End If
            End If
          Exit For
          End If
        Next
      End If
    End Function
'  #End If





  Public Function ctPackage_CachedTOC_GetRawDataInfos(ByVal FileName As String, Optional ByRef Offset As Long, Optional ByRef Length As Long, Optional ByRef ExtSize As Long) As Boolean
    FileName = LCase(FileName)
    For sLong1 = 0& To xFileCount - 1&
      If LCase(Mid(xFile(sLong1), FilenameOffset)) = FileName Then
        Length = LenCharToLong(xFile(sLong1), FileIntOffset)
        Offset = LenCharToLong(xFile(sLong1), FilePosOffset)
        ExtSize = LenCharToLong(xFile(sLong1), FileExtOffset)
        ctPackage_CachedTOC_GetRawDataInfos = True
        Exit For
        End If
      Next
    End Function
