V.3.1
clsUniFile

It permits you to manage UNICODE/ANSI/UTF8 files. This class supports extended Unicode and UNC path (up to 32000 characters, with Unicode path and filenames).
Documentation for enums used in this class can be found by searching "CreateFile" on MSDN.

Methods:

These methods work on an Open File
CloseFile closes the opened file, automatically called when the class terminates
Eof returns true if the file pointer is at the End Of File
Flush flushes operation
GetFilePos returns the position of the file pointer
IsFileOpen checks if the class is managing a file
Lof gets the size of the opened file
OpenFileAppend uses OpenFileGeneric for opening a file for appending
OpenFileGeneric opens a file, you can use any combination of enum values for input parameters
OpenFileRead uses OpenFileGeneric for opening a file for reading
OpenFileWrite uses OpenFileGeneric for opening a file for writing
OpenedFileHandle returns the handle of the opened file managed by the class
OpenedFileName returns the name of the opened file managed by the class
ReadAnsiLine reads an Ansi line from file (stops when find a LF char).
See the ReadBOM function.
ReadAny reads n bytes from file and fill a certain variable
ReadBOM reads the file encoding identifier contained at the top of the file and returns the file encoding used.

This function must be used  every time you want to read a text file using the ReadAnsiLine and ReadUnicodeLine functions.

Sample:

 'Read each line in file
 If CFile.OpenFileRead(fileName) Then
   fEncoding = CFile.ReadBOM()

   While Not CFile.EOF()
     'Read line from file
     If (fEncoding = fe_ANSI) Then
       rc = CFile.ReadAnsiLine(inStrg)
     Else
       rc = CFile.ReadUnicodeLine(inStrg)
     End If
     
     If (rc = False) Then GoTo FILE_ERR
   Wend
   
   CFile.CloseFile
 End If
 

...OR...


 
'Read each line in file
 If CFile.OpenFileRead(fileName) Then
   fEncoding = CFile.ReadBOM()

   While Not CFile.EOF()
     'Read line from file
     rc = CFile.ReadLine(inStrg,fEncoding)
     If (rc = False) Then GoTo FILE_ERR
   Wend
   
   CFile.CloseFile
 End If
ReadByte reads a byte from file and return true if the operation finished correctly
ReadBytes reads n byte from file and return true if the operation finished correctly
ReadInt reads an integer from file (2 bytes)
ReadLine reads a line with the given file encoding (ansi, unicode, utf8).
ReadLong reads a long from file (4 bytes)
ReadUTF8Line reads an UTF8 line from file (stops when find a LF char).
See the ReadBOM function. (Same as ReadAnsiLine + UTF8ToStr).
ReadUnicodeLine reads an Unicode line from file (stops when find a 00 - LF wchar).
See the ReadBOM function.
SetEndOfFileHere sets the EOF at the current file position
SetFilePos moves the file pointer
WriteAnsiLine writes an Ansi line to file and append a CrLf char.
See the WriteBOM function.
WriteAny writes n bytes to file reading from a certain variable
WriteBOM writes the file encoding identifier in the file;
- for ANSI it is not used, the function returns ok
- for UTF8 it writes 3 bytes
- for UTF16 it writes 2 bytes.

If you want to make a standard Unicode UTF8/UTF16 file you have to write the BOM at the top of the file.

The function "
st_GetFileEncoding" detects the encoding using the BOM.

Sample:

 'Write lines into file
 If CFile.OpenFileWrite(fileName) Then
   If (bWriteAnsi) Then

     CFile.WriteBOM fe_ANSI
     CFile.WriteAnsiLine "Test Line 1"
     CFile.WriteAnsiLine "Test Line 2"
     CFile.WriteAnsiLine "Test Line 3"
     CFile.WriteAnsiLine "Test Line 4"

   Else

     CFile.WriteBOM fe_UTF16LE
     CFile.WriteUnicodeLine "Test Line 1"
     CFile.WriteUnicodeLine "Test Line 2"
     CFile.WriteUnicodeLine "Test Line 3"
     CFile.WriteUnicodeLine "Test Line 4"

   End If
   
   CFile.CloseFile
 End If
 
WriteByte writes a byte to file and return true if the operation finished correctly
WriteBytes writes n bytes to file and return true if the operation finished correctly
WriteInt writes an integer to file (2 bytes)
WriteLine writes a line with the given encoding (ansi, unicode, utf8).
WriteLong writes a long to file (4 bytes)
WriteNumCharsAndString / ReadNumCharsAndString writes/reads a binary string to file
WriteUTF8Line writes a UTF8 line to file and append a CrLf char.
See the WriteBOM function. (Same as StrToUTF8 + WriteAnsiLine).
WriteUnicodeLine writes an Unicode line to file and append a CrLf wchar.
See the WriteBOM function.
These helper methods work without open a file, they are prefixed with "st_" (static)
st_CopyFile copies a file to another file
st_CopyFolderContent copies the content of a folder to another
st_DirExist checks if a directory exists
st_FileExist checks if a file exists (no pattern ?* is supported!)
st_FileExt gets only the extension of a file (without the '.')
st_FileHasAttribute checks if a specified file has a specified attribute
st_FileLen gets the size of a file
st_FileName gets only the name without path of a filename
st_FileNameNoExt gets the name without extension of a file
st_FilePath gets only the path of a file
st_GetCurDir / st_SetCurDir gets/sets Windows current directory (not the application path!)
st_GetFileAttr / st_SetFileAttr gets/sets file attributes
st_GetFileDate gets file dates (creation time, last access, last write)
st_GetFileEncoding gets the encoding of a text file (Ansi, Unicode, UTF8)
st_MakePath makes a specifies path, this can contain many directories
st_MkDir creates a directory
st_MoveFile moves the specified file
st_MoveFolderContent moves the content of a directory to another
st_RmDir removes an empty directory
st_RmFile remove a certain file
st_RmFiles removes files matching a specified pattern
st_StringToTextFile converts a VB6 string to a file using the specified encoding
st_TextFileToString reads a text file and convert in to a string
st_UniAppPath returns the application path in UNICODE.
PS. this function only works when you'll compile your application!!!
st_UniCommandLine returns the command line in UNICODE.
PS. this function only works when you'll compile your application!!!
http://www.hexagora.com
Thu, 22 Sep 2011 17:54:35 UT
Copyright 2003-11 by Lorenzi Davide