How to use FileWrite and FileRead with line terminator CRLF CR LF?
;==========================================================================================================================================
;
; How to use FileWrite() and FileRead() with a text file which have a line terminator of CRLF or CR or LF?
;
; Can the WinBatch function FileRead() work successfully with text files
; from other operating systems, without any adaptive string manipulation?
;
;
; Detlev Dalitz.20110129.
;==========================================================================================================================================
;
; Operating systems and their line terminator:
; MSDOS = @CRLF
; Apple = @CR
; UNIX  = @LF
;
;
; See also WinBatch IntControl():
; IntControl (53, p1, 0, 0, 0) ; Set line terminator type for FileWrite.
; p1  Terminator
; 0   No line terminator
; 1   CR/LF (DOS) (default)
; 2   LF (UNIX)
; 3   CR (Macintosh)
; 4   Tab
;
; Note: The absence of a special IntControl for FileRead() let expect,
; that FileRead() does not need any adjustment for reading text files,
; which have a CRLF or CR or LF line terminator.
;
; The following routine creates test files, which have a CRLF or CR or LF line terminator.
; Then WinBatch function FileRead() will be used to read these test files.
; The differences are shown in the report.
;
; Open questions:
; If a text file contains different delimiters, how will FileRead() handle this case?
; How would other WinBatch functions behave, like ArrayFileGet()?
;==========================================================================================================================================


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfTestFileWrite (strFilename, intLines, intTerm, blnCode)
arrTerm = Arrayize ("NONE,@CRLF,@LF,@CR,@TAB", ",")
blnCode = Min (Max (blnCode, 0), 1)
intTerm = Min (Max (intTerm, 0), 4)
intLastIC53 = IntControl (53, intTerm, 0, 0, 0)
hdlFW = FileOpen (strFilename, "WRITE", blnCode) ; 0=ANSI, 1=Unicode.
For intI = 1 To intLines
   FileWrite (hdlFW, intI : " Line delimited by " : arrTerm[intTerm])
Next
hdlFW = FileClose (hdlFW)
IntControl (53, intLastIC53, 0, 0, 0)
Return FileSize (strFilename)
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfTestFileRead (strFilename, intLines, blnCode)
hdlFR = FileOpen (strFilename, "READ", blnCode)
For intI = 1 To intLines
   If FileRead (hdlFR) == "*EOF*" Then Break
Next
hdlFR = FileClose (hdlFR)
Return intI > intLines ; If true, then five lines have been read.
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


strFolderStart = DirGet ()
strFolderTemp = ShortCutDir ("Local Settings") : "\Temp\"
DirMake (strFolderTemp)
DirChange (strFolderTemp)

strFileResultA = "Result_A.csv.txt"
strFileResultU = "Result_U.csv.txt"

arrTermA = Arrayize ("A_CRLF,A_LF,A_CR,A_TAB", ",")
arrTermU = Arrayize ("U_CRLF,U_LF,U_CR,U_TAB", ",")

arrTestA = ArrDimension (4, 2)
arrTestU = ArrDimension (4, 2)

; Test for ANSI files.
intLines = 5
blnCode = @FALSE
For intI = 0 To 3
   arrTestA[intI, 0] = "Test_" : arrTermA[intI] ; Filename.
   udfTestFileWrite (arrTestA[intI, 0], intLines, 1 + intI, blnCode)
   arrTestA[intI, 1] = udfTestFileRead (arrTestA[intI, 0], intLines, blnCode) ; Result.
Next

; Test for Unicode files (UCS-2 LE).
intLines = 5
blnCode = @TRUE
For intI = 0 To 3
   arrTestU[intI, 0] = "Test_" : arrTermU[intI] ; Filename.
   udfTestFileWrite (arrTestU[intI, 0], intLines, 1 + intI, blnCode)
   arrTestU[intI, 1] = udfTestFileRead (arrTestU[intI, 0], intLines, blnCode) ; Result.
Next

intBW = ArrayFilePutCSV (strFileResultA, arrTestA, ",", @FALSE, 2)
intBW = ArrayFilePutCSV (strFileResultU, arrTestU, ",", @FALSE, 2)

strResult = FileGet (strFileResultA) : @CRLF : FileGet (strFileResultU)
strResult = StrReplace (strResult, ",", @TAB)
strResult = StrReplace (strResult, "1", "OK")
strResult = StrReplace (strResult, "0", "Failure")

strMsgText = strResult
strMsgTitle = "Test Result: FileWrite - FileRead"

ClipPut (strMsgTitle : @CRLF : @CRLF : strMsgText)
Pause (strMsgTitle, strMsgText : @LF : "The test result is on the ClipBoard now.")

;Run (strFileResultA, "")
;Run (strFileResultU, "")

:CANCEL
ErrorMode (@OFF)
For intI = 0 To 3
   FileDelete (arrTestA[intI, 0])
   FileDelete (arrTestU[intI, 0])
Next
FileDelete (strFileResultA)
FileDelete (strFileResultU)
ErrorMode (@ON)
DirChange (strFolderStart)
Exit
;--------------------------------------
;   Test Result
;   FileWrite     FileRead
;
;   Test_A_CRLF   OK
;   Test_A_LF     OK
;   Test_A_CR     Failure
;   Test_A_TAB    Failure
;
;   Test_U_CRLF   OK
;   Test_U_LF     OK
;   Test_U_CR     Failure
;   Test_U_TAB    Failure
;--------------------------------------