udfArrayFromFile
arr udfArrayFromFile (str, int)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfArrayFromFile (strFilename, intBaseMode) ; The Binary Buffer version.
If strFilename == "" Then Return ArrDimension (0)
intBBSize = FileSize (strFilename)
If !intBBSize Then Return ArrDimension (0)
intBaseMode = !!intBaseMode
hdlBB = BinaryAlloc (intBBSize + intBaseMode)
If intBaseMode Then BinaryPokeStr (hdlBB, 0, @LF)     ; Insert a leading empty line.
BinaryReadEx (hdlBB, intBaseMode, strFilename, 0, -1) ; Read the whole file.
BinaryReplace (hdlBB, @CRLF, @LF, @TRUE)              ; Unify EOL.
BinaryReplace (hdlBB, @CR, " ", @TRUE)                ; Replace lonesome CR's with spaces.
intBBEod = BinaryEodGet (hdlBB)
strString = BinaryPeekStr (hdlBB, 0, intBBEod - (@LF == BinaryPeekStr (hdlBB, intBBEod - 1, 1))) ; Omit trailing @LF.
hdlBB = BinaryFree (hdlBB)
arrArray = Arrayize (strString, @LF)
If intBaseMode Then arrArray [0] = ArrInfo (arrArray, 1) - 1 ; If one based array, then poke number of file lines into array element [0].
Return arrArray
;------------------------------------------------------------------------------------------------------------------------------------------
; This UDF "udfArrayFromFile" reads a textfile and returns a dim1 array.
; Each array element contains one line of the given input file, with EndOfLine characters stripped off.
;
; The intBaseMode parameter controls the creation of a zero based or a one based Array.
; The array contains n elements (zero based) resp. n+1 elements (one based), with n = Number of file lines.
; After returning from this function the number of file lines read can be retrieved
; by 'LineCount = Array [0]' (one based array)
; or 'LineCount = ArrInfo (array, 1)' (zero based array).
;
; If the specified filename is empty or the filesize is zero, then this function returns a valid
; dim0 array with no element. This result has to be checked by the caller for further processing.
;
; strFilename ..... The file to be read into the array.
; intBaseMode=0 ... Creates a zero based array with n elements.
; intBaseMode=1 ... Creates a one based array with n+1 elements.
;
; Detlev Dalitz.20020808.20090510.
;------------------------------------------------------------------------------------------------------------------------------------------
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

strMsgTitle = "Demo: udfArrayFromFile (strFilename, intBaseMode)"
strMsgText = ""

strFilename = IntControl (1004, 0, 0, 0, 0) ; We use this script as test input file.

:Test1
arrArray = udfArrayFromFile ("", 0)
If !ArrInfo (arrArray, -1) Then strMsgText = "Test1. Array is invalid and cannot be used for further processing."
If !ArrInfo (arrArray, 0) Then strMsgText = "Test1. Array has no dimension."
Message (strMsgTitle, strMsgText) ; "Test1. Array has no dimension."

:Test2
intBaseMode = 0
arrArray = udfArrayFromFile (strFilename, intBaseMode)
intLineCount = ArrInfo (arrArray, 1)

strMsgText = "Test2. Elements in array = " : intLineCount
Message (strMsgTitle, strMsgText) ; "Test2. Elements in array = 71"

:Test3
intBaseMode = 1
arrArray = udfArrayFromFile (strFilename, intBaseMode)

intLine = 20
strMsgText = "Test3. This is Line " : intLine : " from " : arrArray [0] : " lines:" : @LF : arrArray [intLine]
Message (strMsgTitle, strMsgText) ; "Test3. This is Line 20 from 71 lines:@LF; Each array element contains one line of the given input file, with EndOfLine characters stripped off."

Exit