How to read file regardless whether Unicode or ANSI?
;==========================================================================================================================================
; Read file regardless whether Unicode or ANSI.
;
; How to read file contents into string variable, regardless of knowing
; in advance what text format to expect, whether ASCII / ANSI or Unicode.
;
; Example 1 demonstrates the usage of BinaryPeekStr / BinaryPeekStrW.
; Example 2 demonstrates the usage of FileGet / FileGetW.
; Example 3 demonstrates the usage of FileGet to read Unicode text and return ASCII/ANSI.
;
;(c)Detlev Dalitz.20100204.20110124.
;==========================================================================================================================================

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfIsTextUnicode (strFilename)
dnsBBsize = FileSize (strFilename, 1) ; Possible huge file size, so we use decimal number string.
If dnsBBsize == "0" Then Return @FALSE
hdlBB = BinaryAlloc (dnsBBsize)
intBytesRead = BinaryRead (hdlBB, strFilename)
blnTestPassed = !!DllCall ("Advapi32.dll", long : "IsTextUnicode", lpbinary : hdlBB, long : dnsBBsize, lpnull)
hdlBB = BinaryFree (hdlBB)
Return blnTestPassed
;..........................................................................................................................................
; This Function "udfIsTextUnicode" returns a boolean value which indicates if a given file is likely to contain a form of Unicode text or not.
;
; Alternative DllCall:
; blnTestPassed = DllCall ("Unicows.dll", long : "IsTextUnicode", lpbinary : hdlBB, long : intBBSize, lpnull)
;
; (c)Detlev Dalitz.20030701.20100114.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfFileGetBB (strFilename)
intFileSize = FileSize (strFileName, 1)
If intFileSize == "0" Then Return ""
hdlBB = BinaryAlloc (intFileSize)
intBytesRead = BinaryRead (hdlBB, strFileName)
If udfIsTextUnicode (strFileName)
   strFileContent = BinaryPeekStrW (hdlBB, 0, BinaryEodGet (hdlBB))
Else
   strFileContent = BinaryPeekStr (hdlBB, 0, BinaryEodGet (hdlBB))
EndIf
hdlBB = BinaryFree (hdlBB)
Return strFileContent
;..........................................................................................................................................
; This UDF "udfFileGet" converts a file to ASCII/ANSI string variable resp. Unicode string variable
; accordingly to the file content which can be normal ASCII/ANSI text or Unicode text.
;
; This UDF serves likely the same way as the WinBatch native functions FileGet() and FileGetW().
;
; (c)Detlev Dalitz.20100204.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfFileGet (strFileName)
If udfIsTextUnicode (strFileName) Then Return FileGetW (StrFileName)
Return FileGet (StrFileName)
;..........................................................................................................................................
; This UDF "udfFileGet" converts a file to ASCII/ANSI string variable resp. Unicode string variable
; accordingly to the file content which can be normal ASCII/ANSI text or Unicode text.
;
; (c)Detlev Dalitz.20100204.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Main.
strFilename = IntControl (1004, 0, 0, 0, 0)      ; Take this script as input file.
strThisFile = FileGet (strFilename)              ; Take the content of this script as input.

strFileTxt = FileCreateTemp ("TXT")              ; Create temporary test file for ASCII/ANSI text.
strFileUni = FileCreateTemp ("UNI")              ; Create temporary test file for Unicode text.

intBytesTxt = FilePut (strFileTxt, strThisFile)  ; Write to ASCII/ANSI file.
intBytesUni = FilePutW (strFileUni, strThisFile) ; Write to Unicode file. ; Size is including prepended 2-Byte BOM.

intFileSizeTxt = FileSize (strFileTxt, 1)        ; Must be same value as intBytesTxt.
intFileSizeUni = FileSize (strFileUni, 1)        ; Must be same value as intBytesUni.


; Example 1. Binary Buffer.
strFileContentTxt = udfFileGetBB (strFileTxt)
strFileContentUni = udfFileGetBB (strFileUni)

; View results.
strAppBrowser = DirHome () : "Browser.exe"
intResult = Run (strAppBrowser, strFileTxt)
intResult = Run (strAppBrowser, strFileUni)
blnResult = AppWaitClose (strAppBrowser)


; Example 2. FileGet.
strFileContentTxt = udfFileGet (strFileTxt)
strFileContentUni = udfFileGet (strFileUni)

; View results.
strAppBrowser = DirHome () : "Browser.exe"
intResult = Run (strAppBrowser, strFileTxt)
intResult = Run (strAppBrowser, strFileUni)
blnResult = AppWaitClose (strAppBrowser)


; Example 3. FileGet.
strFileContentUni = FileGetW (strFileUni) ; Read Unicode text.
strFileContentTxt = FileGet (strFileUni)  ; Read Unicode text and convert to ASCII/ANSI text.
intVTUni = VarType (strFileContentUni)    ; 128.
intVTTxt = VarType (strFileContentTxt)    ; 2.
strMsgText = "VarType (strFileContentUni) = " : intVTUni
strMsgText = strMsgText : @LF : "VarType (strFileContentTxt) = " : intVTTxt
strMsgText = strMsgText : @LF : @LF : StrSub (strFileContentTxt, 1, 1000) : " ..."
Message ("Example 3: Read Unicode - Get ANSI.", strMsgText)
; It is somewhat cheating, because Message() can display both Unicode and ANSI automatically.
; But Vartype() shows the difference.


; Cleanup.
blnResult = FileDelete (strFileTxt)
blnResult = FileDelete (strFileUni)
Exit
;==========================================================================================================================================