October 28, 2020, 06:22:19 AM

Author Topic: [bmx] 2GB+ filestream by Azathoth [ 1+ years ago ]  (Read 1148 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] 2GB+ filestream by Azathoth [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : 2GB+ filestream
Author : Azathoth
Posted : 1+ years ago

Description : Allows the creation, reading and writing of files larger than the 32bit limit.
Use PosEx, SizeEx, and SeekEx to handle the 64bit positions and size; the ordinary methods are still only 32bit.

Win32 only.


Code :
Code: BlitzMax
  1. Strict
  2.  
  3. Import Pub.Win32
  4.  
  5. Const INVALID_HANDLE_VALUE = -1
  6.  
  7. Const GENERIC_READ = $80000000
  8. Const GENERIC_WRITE = $40000000
  9.  
  10. Const OPEN_EXISTING = 3
  11.  
  12. Extern "win32"
  13.         Function CreateFileW(lpFileName:Byte Ptr,dwDesiredAccess,dwShareMode,lpSecurityAttributes:Byte Ptr,dwCreationDisposition,dwFlagsAndAttributes,hTemplateFile)
  14.         Function GetFileSizeEx(hFile, lpFileSize:Byte Ptr)
  15.         Function SetFilePointerEx(hFile, liDistanceToMove:Long, lpNewFilePointer:Byte Ptr, dwMoveMethod)
  16.         Function ReadFile(hFile, lpBuffer:Byte Ptr, nNumberOfBytesToRead, lpNumberOfBytesRead:Byte Ptr, lpOverlapped:Byte Ptr)
  17.         Function WriteFile(hFile, lpBuffer:Byte Ptr, nNumberOfBytesToWrite, lpNumberOfBytesWritten:Byte Ptr, lpOverlapped:Byte Ptr)
  18.         Function FlushFileBuffers(hFile)
  19.  
  20.         Function CloseHandle(hObject)
  21.         Function GetLastError()
  22.        
  23. EndExtern
  24.  
  25. Function FileSizeEx:Long(path:String)
  26.         Local size:Long, handle:Int
  27.  
  28.         handle=CreateFileW(path.ToWString(),GENERIC_READ,0,Null,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0)
  29.         If handle<>INVALID_HANDLE_VALUE
  30.                 If GetFileSizeEx(handle, Varptr size)=0 Then size=-1
  31.                 CloseHandle(handle)
  32.         EndIf
  33.         Return size
  34. EndFunction
  35.  
  36. Type TFileStream Extends TStream
  37.         Field _pos:Long, _size:Long, _handle=INVALID_HANDLE_VALUE, _mode
  38.        
  39.         Method PosEx:Long()
  40.                 Return _pos
  41.         EndMethod
  42.        
  43.         Method SizeEx:Long()
  44.                 Return _size
  45.         EndMethod
  46.        
  47.         Method Eof()
  48.                 Return (PosEx()=SizeEx())
  49.         EndMethod
  50.        
  51.         Method SeekEx:Long(pos:Long)
  52.                 Assert _handle<>INVALID_HANDLE_VALUE Else "Attempt to seek closed stream"
  53.                 SetFilePointerEx(_handle,pos,Varptr _pos,0)
  54.                 Return _pos
  55.         EndMethod
  56.        
  57.         Method Read(buf:Byte Ptr, count)
  58.                 Assert _handle<>INVALID_HANDLE_VALUE Else "Attempt to read from closed stream"
  59.                 Assert _mode & GENERIC_READ Else "Attempt to read from write-only stream"
  60.                 If ReadFile(_handle,buf,count, Varptr count, Null)=0 Then Return 0
  61.                 _pos:+count
  62.                 Return count
  63.         EndMethod
  64.        
  65.         Method Write( buf:Byte Ptr,count )
  66.                 Assert _handle<>INVALID_HANDLE_VALUE Else "Attempt to write to closed stream"
  67.                 Assert _mode & GENERIC_WRITE Else "Attempt to write to read-only stream"
  68.                 If WriteFile(_handle,buf,count,Varptr count, Null )=0 Then Return 0
  69.                 _pos:+count
  70.                 If _pos>_size _size=_pos
  71.                 Return count
  72.         End Method
  73.        
  74.         Method Pos()
  75.                 Return Int(_pos)
  76.         EndMethod
  77.        
  78.         Method Size()
  79.                 Return Int(_size)
  80.         EndMethod
  81.        
  82.         Method Seek(pos)
  83.                 Return Int(SeekEx(pos))
  84.         EndMethod
  85.        
  86.         Method Flush()
  87.                 If _handle<>INVALID_HANDLE_VALUE Then FlushFileBuffers(_handle)
  88.         EndMethod      
  89.        
  90.         Method Close()
  91.                 If _handle=INVALID_HANDLE_VALUE Then Return
  92.                 Flush()
  93.                 CloseHandle(_handle)
  94.                 _handle=INVALID_HANDLE_VALUE
  95.                 _pos=0
  96.                 _size=0
  97.         EndMethod
  98.        
  99.         Function OpenFile:TFileStream(path:String,readable,writeable )
  100.                 Local m, handle, wstr:Byte Ptr
  101.                
  102.                 If readable m:|GENERIC_READ
  103.                 If writeable m:|GENERIC_WRITE
  104.                
  105.                 wstr=path.ToWString()
  106.                
  107.                 handle=CreateFileW(wstr,m,0,Null,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0)
  108.                
  109.                 MemFree(wstr)
  110.                 If handle<>INVALID_HANDLE_VALUE
  111.                         Local stream:TFileStream=New TFileStream
  112.                        
  113.                         stream._handle=handle
  114.                         SetFilePointerEx(handle,0,Varptr stream._pos,1)
  115.                         SetFilePointerEx(handle,0,Varptr stream._size,2)
  116.                         SetFilePointerEx(handle,stream._pos,Null,0)
  117.                         stream._mode=m
  118.                         Return stream
  119.                 EndIf
  120.         EndFunction
  121.                
  122. EndType


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal