' sys_diskio.inc
' all Windows API file access routines are located in this include file

 
' Generic open file function
Function FOpen ( ByVal FilName    As String, _
                 ByVal AccessMode As Long, _
                 ByVal ShareMode  As Long, _
                 ByRef hFile      As Long _
                 ) As Long

    Local zTmp      As Asciiz * 256
    Local AccessIs  As Long
    Local ShareIs   As Long
     
    zTmp = FilName
     
    ' Set the default access and share modes
    AccessIs = %GENERIC_READ Or %GENERIC_WRITE
    ShareIs  = %FILE_SHARE_READ Or %FILE_SHARE_WRITE

    Select Case As Long AccessMode 
       Case 0: AccessIs = %GENERIC_READ
       Case 1: AccessIs = %GENERIC_WRITE
    End Select
     
    Select Case ShareMode 
       Case 1: ShareIs = 0
       Case 2: ShareIs = %FILE_SHARE_READ
       Case 3: ShareIs = %FILE_SHARE_WRITE
    End Select
     
    hFile = CreateFile( zTmp, AccessIs, ShareIs, _
                        ByVal %Null, _
                        %OPEN_ALWAYS, _
                        %FILE_ATTRIBUTE_NORMAL Or %FILE_FLAG_RANDOM_ACCESS, _
                        ByVal %Null )
    
    If hFile = -1 Then          ' -1 Fail to create the file
       Function = GetLastError  ' Set the error code
       hFile = 0                ' Reset handle number
    End If
End Function
 


'Function fLOF( ByVal hFile As Long ) As Quad 
'   Local fSize As Quad
'   
'   If GetFileType(hFile) = %FILE_TYPE_DISK Then
'      fSize = GetFileSize( hFile, ByVal %Null )
'      If fSize > -1 Then Function = fSize
'   End If
'End Function

 
Function FSeek( ByVal hFile As Long, _
                ByVal mPosByte As Quad _
                ) As Long
                
    Decr mPosByte   'b/c API is zero based
 
    'split the quad into two longs to send to the API. This allows for
    'enormous size files!! 
    Dim pp As Quad
    ReDim pm(1 To 2) As Long At VarPtr(pp)
    pp = mPosByte
 
    If SetFilePointer( hFile, ByVal pm(1), pm(2), %FILE_BEGIN ) < 0 Then
       Function = GetLastError
    End If
End Function

 
Function FGet( ByVal hFile As Long, _
               ByVal NumBytes As Long, _
               ByVal MemoryAddress As Dword _
               ) As Long 
               
    Local ByttesReaded As Long
     
    'gets data from the file and stores it at the memory address MemoryAddress???
    If hFile Then
       If NumBytes Then
          If ReadFile( hFile, ByVal MemoryAddress, NumBytes, ByttesReaded, ByVal %Null ) = 0 Then
             Function = GetLastError
          End If
       End If
    End If
End Function   


 
' GET at a specific offset
' Data is written to MemoryAddress???
Function FGetAt( ByVal hFile As Long, _
                 ByVal mPositionByte As Quad, _
                 ByVal vNumBytes As Long, _
                 ByVal MemoryAddress As Dword _
                 ) As Long
                 
    Local ErrCode As Long
    
    ErrCode = FSeek( hFile, mPositionByte )
    If ErrCode = 0 Then ErrCode = FGet( hFile, vNumBytes, MemoryAddress )
    Function = ErrCode
End Function
 
                 
                 
''WRITE data at a the current offset (Paul Squires)
'Function fWrite( ByVal hFile As Long, _
'                 ByVal NumBytes As Long, _
'                 ByVal MemoryAddress As Dword _
'                 ) As Long
'                 
'   Local ByttesReaded As Long
'    
'   If hFile Then
'      If NumBytes Then
'         If WriteFile( hFile, ByVal MemoryAddress, NumBytes, ByttesReaded, ByVal %Null ) = 0 Then
'            Function = GetLastError
'         End If
'      End If
'   End If
'End Function

 
'WRITE AT data at a specific offset in the file
Function fWriteAt( ByVal hFile As Long, _
                   ByVal PosByte As Quad, _
                   ByVal NumBytes As Long, _
                   ByVal MemoryAddress As Dword _
                   ) As Long
                   
    Local ErrCode As Long
    Local ByttesReaded As Long

    ErrCode = FSeek( hFile, PosByte )

    ErrCode = WriteFile( hFile, ByVal MemoryAddress, NumBytes, _
                         ByCopy ByttesReaded, ByVal %Null )

    If errcode = 0 Then
       Function = GetLastError
       Exit Function
    End If
End Function
 
 
Sub FClose( ByRef hFile As Long ) 
    If hFile Then CloseHandle hFile
    hFile = 0
End Sub


                            