Page 1 of 1

Extended functions for Files

Posted: Wed Aug 09, 2006 12:41 pm
by Flype
I made for my own use those extended functions set,
for easier/lazier reading content of files.

They might be included in PureBasic by design.

Code: Select all

;- 
;- File Extended functions
;- Written for PureBasic 4.0
;- 

EnableExplicit

ProcedureDLL.l IsBin(FileID.l)       ; Check if the specified FileID is a binary file.
  
  ; Returns #True if the file contains Non-ASCII chars.
  ; Returns #False if the file contains only ASCII chars.
  
  Protected result.l 
  
  If IsFile(FileID) 
    While Not Eof(FileID) 
      Select ReadByte(FileID) 
        Case #NUL,#SOH,#STX,#ETX,#EOT,#ENQ,#ACK,#BEL,#BS,#VT,#FF,#SO,#SI,#DLE,#DC1,#DC2,#DC3,#DC4,#NAK,#SYN,#ETB,#CAN,#EM,#SUB,#ESC,#FS,#GS,#RS,#US,#DEL 
          result = #True 
          Break 
      EndSelect 
    Wend 
  EndIf 
  
  ProcedureReturn result
  
EndProcedure
ProcedureDLL.l IsBinFile(FileName.s) ; Check if the specified FileName is a binary file.
  
  ; Returns #True if the file contains Non-ASCII chars.
  ; Returns #False if the file contains only ASCII chars.
  
  Protected FileID.l, result.l 
  
  FileID = ReadFile(#PB_Any, FileName) 
  
  If FileID 
    While Not Eof(FileID) 
      Select ReadByte(FileID) 
        Case #NUL,#SOH,#STX,#ETX,#EOT,#ENQ,#ACK,#BEL,#BS,#VT,#FF,#SO,#SI,#DLE,#DC1,#DC2,#DC3,#DC4,#NAK,#SYN,#ETB,#CAN,#EM,#SUB,#ESC,#FS,#GS,#RS,#US,#DEL 
          result = #True 
          Break 
      EndSelect 
    Wend 
    CloseFile(FileID) 
  EndIf 
  
  ProcedureReturn result 
  
EndProcedure

ProcedureDLL.s ReadText(FileID.l)       ; Read the specified FileID to a string.
  
  ; Returns a string if successful.
  ; Returns #Null$ if the file can't be read.
  ; Returns #Null$ if the file isn't a text file.
  
  Protected result.s 
  
  If IsFile(FileID) 
    result = Space(Lof(FileID)) 
    ReadData(FileID, @result, Lof(FileID)) 
  EndIf 
  
  ProcedureReturn result 
  
EndProcedure 
ProcedureDLL.s ReadTextFile(FileName.s) ; Read the specified FileName to a string.
  
  ; Returns a string if successful.
  ; Returns #Null$ if the file can't be read.
  ; Returns #Null$ if the file isn't a text file.
  
  Protected FileID.l, result.s 
  
  FileID = ReadFile(#PB_Any, FileName) 
  
  If FileID 
    result = Space(Lof(FileID)) 
    ReadData(FileID, @result, Lof(FileID)) 
    CloseFile(FileID) 
  EndIf 
  
  ProcedureReturn result 
  
EndProcedure

ProcedureDLL.l ReadBuffer(FileID.l)       ; Read the specified FileID to a buffer.
  
  ; Returns a pointer to a buffer if successful.
  ; Returns #Null if the file can't be read.
  ; Remark: The result must freed with FreeMemory().
  
  Protected *result 
  
  If IsFile(FileID) 
    *result = AllocateMemory(Lof(FileID)) 
    If *result 
      ReadData(FileID, *result, Lof(FileID)) 
    EndIf 
  EndIf 
  
  ProcedureReturn *result 
  
EndProcedure
ProcedureDLL.l ReadBufferFile(FileName.s) ; Read the specified FileName to a buffer.
  
  ; Returns a pointer to a buffer if successful.
  ; Returns #Null if the file can't be read.
  ; Remark: The result must freed with FreeMemory().
  
  Protected FileID.l, *result 
  
  FileID = ReadFile(#PB_Any, FileName) 
  
  If FileID 
    *result = AllocateMemory(Lof(FileID)) 
    If *result 
      ReadData(FileID, *result, Lof(FileID)) 
    EndIf 
    CloseFile(FileID) 
  EndIf 
  
  ProcedureReturn *result 
  
EndProcedure

DisableExplicit