udfFileReadToChunk
udfFileReadLineToChunk
int udfFileReadToChunk (str, int)
int udfFileReadLineToChunk (str, int)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfFileReadToChunk_1 (strFilename, intChunkSize)
intChunkSize = Max (1, intChunkSize)
;strOut = "" ; Debug. This line can be commented out or removed.
strBufIn = ""
strChunk = ""
hdlFR = FileOpen (strFilename, "READ")
While @TRUE
   strLine = FileRead (hdlFR)
   If strLine == "*EOF*" Then Break
   strBufIn = StrBufIn : strLine : @CRLF
   If StrLen (strBufIn) < intChunkSize Then Continue
   While @TRUE
      strChunk = StrSub (strBufIn, 1, intChunkSize)
      ;strOut = strOut : strChunk ; Debug. This line can be commented out or removed.
      udfDisplayChunk (strChunk, 1) ; <== Do something with the current chunk.
      strBufIn = StrSub (strBufIn, intChunkSize + 1, -1)
      If StrLen (strBufIn) < intChunkSize Then Break
   EndWhile
EndWhile
hdlFR = FileClose (hdlFR)
If strBufIn != ""
   ;strOut = strOut : strBufIn ; Debug. This line can be commented out or removed.
   udfDisplayChunk (strBufIn, 1)
EndIf
;Return strOut ; Debug. This line can be commented out or removed.
;..........................................................................................................................................
; This UDF "udfFileReadToChunk" reads a text file line by line and splits the text into chunks of intChunkSize.
; The current chunk can be used via calling a helper UDF.
;
; Small quirk:
; Even if the last line in the file is not delimited by @CRLF, then the UDF adds a trailing @CRLF,
; so that the overall filesize will be the original filesize plus 2 Bytes.
;
; Return value is always 0.
;
; (c)Detlev Dalitz.20110114.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------



;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfFileReadToChunk_2 (strFilename, intChunkSize)
intChunkSize = Max (1, intChunkSize)
;strOut = "" ; Debug. This line can be commented out or removed.
intFileSize = FileSize (strFilename)
intChunkLast = (intFileSize / intChunkSize) + ((intFileSize mod intChunkSize) > 0) - 1
strText = FileGet (strFilename)
For intChunk = 0 To intChunkLast
   strChunk = StrSub (strText, intChunk * intChunkSize + 1, intChunkSize)
   udfDisplayChunk (strChunk, 1) ; <== Do something with the current chunk.
   ;strOut = strOut : strChunk ; Debug. This line can be commented out or removed.
Next
;Return strOut ; Debug. This line can be commented out or removed.
;..........................................................................................................................................
; This UDF "udfFileReadToChunk" reads a text file entirely and splits the text into chunks of intChunkSize.
; The current chunk can be used via calling a helper UDF.
;
; Return value is always 0.
;
; (c)Detlev Dalitz.20110114.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------



;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfFileReadLineToChunk (strFilename, intChunkSize)
intChunkSize = Max (1, intChunkSize)
hdlFR = FileOpen (strFilename, "READ")
While @TRUE
   strText = FileRead (hdlFR)
   If strText == "*EOF*" Then Break
   strText = strText : @CRLF
   intLineSize = StrLen (strText)
   intChunkLast = (intLineSize / intChunkSize) + ((intLineSize mod intChunkSize) > 0) - 1
   For intChunk = 0 To intChunkLast
      strChunk = StrSub (strText, intChunk * intChunkSize + 1, intChunkSize)
      udfDisplayChunk (strChunk, 1) ; <== Do something with the current chunk.
   Next
EndWhile
hdlFR = FileClose (hdlFR)
;..........................................................................................................................................
; This UDF "udfFileReadLineToChunk" reads a text file line by line and splits each text line into chunks of intChunkSize.
; The current chunk can be used via calling a helper UDF.
;
; Return value is always 0.
;
; (c)Detlev Dalitz.20110114.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfDisplayChunk (strText, intFlag)
ptrSumChunk = PtrPersistent (p1, 0)
ptrSumLen = PtrPersistent (p2, 0)
ptrSumText = PtrPersistent (p3, 0)
Switch intFlag
Case 1
   intLen = StrLen (strText)
   *ptrSumChunk = *ptrSumChunk + 1
   *ptrSumLen = *ptrSumLen + intLen
   *ptrSumText = *ptrSumText : strText
   BoxTitle ("Chunk=" : *ptrSumChunk : "|ChunkLen=" : intLen : "|Bytes=" : *ptrSumLen)
   BoxText (strText)
   Break
Case 2
   Return *ptrSumChunk
   Break
Case 3
   Return *ptrSumLen
   Break
Case 4
   Return *ptrSumText
   Break
Case 0
   *ptrSumChunk = 0
   *ptrSumLen = 0
   *ptrSumText = ""
EndSwitch
;..........................................................................................................................................
; This UDF "udfDisplayChunk works in conjunction with the UDF "udfSplitFileIntoChunks"
; It displays the given chunk of text, counts the chunks and accumulates their size and text.
;
; The behaviour of the UDF can be controlled by the flag parameter.
; intFlag = 0 ... Reset all persistent variables to initial value.
; intFlag = 1 ... Display current chunk number, length, accumulated length, chunk text.
; intFlag = 2 ... Return current chunk count.
; intFlag = 3 ... Return current accumulated text length.
; intFlag = 4 ... Return current accumulated text.
;
; (c)Detlev Dalitz.20110114.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfViewTestResult (intTest)
strText = udfDisplayChunk ("", 4)
intLen = udfDisplayChunk ("", 3)
intCount = udfDisplayChunk ("", 2)
If intLen > 0
   strFileTemp = FileCreateTemp ("T" : intTest : "_")
   intBytesWritten = FilePut (strFileTemp, strText : @CRLF : "*EOF*" : @CRLF : "Chunks=" : intCount : @CRLF : "Bytes=" : intLen)
   intResult = Run ("notepad.exe", strFileTemp)
   SendKey ("^{END}") ; Scroll down to end of file.
   blnResult = FileDelete (strFileTemp)
EndIf
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------



; Test.
DirChange (DirScript ())
BoxOpen ("Test", "")

strFileThis = IntControl (1004, 0, 0, 0, 0) ; Use this script as test input file.

intChunkSize = 64


;Goto TEST1
;Goto TEST2
;Goto TEST3

:TEST1
WinActivate ("")
udfDisplayChunk ("", 0) ; Init.
udfFileReadToChunk_1 (strFileThis, intChunkSize)
TimeDelay (2)
udfViewTestResult (1)
TimeDelay (2)


:TEST2
WinActivate ("")
udfDisplayChunk ("", 0) ; Init.
udfFileReadToChunk_2 (strFileThis, intChunkSize)
TimeDelay (2)
udfViewTestResult (2)
TimeDelay (2)


:TEST3
WinActivate ("")
udfDisplayChunk ("", 0) ; Init.
udfFileReadLineToChunk (strFileThis, intChunkSize)
TimeDelay (2)
udfViewTestResult (3)
TimeDelay (2)


:CANCEL
BoxShut ()
Exit