udfFileShrink
int udfFileShrink (str, int, int)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfFileShrink (strFilename, intShrinkSize, intMode)
intFileSize = FileSizeEx (strFilename)
If !intFileSize Then Return 0
intShrinkSize = Min (intFileSize, Max (0, intShrinkSize))
If !intShrinkSize Then Return intFileSize
hdlBB = BinaryAlloc (intShrinkSize)
If !!intMode
   BinaryReadEx (hdlBB, 0, strFilename, 0, intShrinkSize)
Else
   BinaryReadEx (hdlBB, 0, strFilename, intFileSize - intShrinkSize, intShrinkSize)
EndIf
intFileSize = BinaryWrite (hdlBB, strFilename)
hdlBB = BinaryFree (hdlBB)
Return intFileSize
;..........................................................................................................................................
; CAUTION: This function overwrites the original input file strFilename without permission !!!
;
; This UDF udfFileShrink does not respect linebreak sequences.
; The function just cuts the byte stream on the given intShrinkSize byte boundary.
; This can cause an incomplete line at the boundary.
;
; intMode = 0 ... Cut top of file    : The tail resp. bottom of the file will be saved.
; intMode = 1 ... Cut bottom of file : The head resp. top    of the file will be saved.
;
; On success the return value is the new filesize otherwise zero.
;..........................................................................................................................................
; Detlev Dalitz.20010810.20020724
;..........................................................................................................................................
#EndFunction
;--------------------------------------------------------------------------------------------------


; Test.

; Example: How to shrink a logfile.
;
; Create two testfiles, each of 20 byte length.
; Each file contains the string "TOP...........BOTTOM".
; These files will be shrinked to 8 byte sized files.
; The "top" file will be shrinked to the string "TOP....." (intMode = 1).
; The "bot" file will be shrinked to the string "..BOTTOM" (intMode = 0).

; Prepare testcase.
strTop = FileCreateTemp ("top")
strBot = FileCreateTemp ("bot")
intBBSize = 20
hdlBB = BinaryAlloc (intBBSize)
BinaryEodSet (hdlBB, intBBSize)
BinaryPokeStr (hdlBB, 0, "TOP")
BinaryPokeStr (hdlBB, intBBSize - 6, "BOTTOM")
BinaryReplace (hdlBB, "", ".", @TRUE)
BinaryWrite (hdlBB, strTop)
BinaryWrite (hdlBB, strBot)
BinaryFree (hdlBB)

; Test the udf.
RunWait ("notepad.exe", strBot) ; Display original "bot" file and wait for closing notepad.
intBytesWritten = udfFileShrink (strBot, 8, 0) ; "..BOTTOM"
RunWait ("notepad.exe", strBot) ; Display shrinked "bot" file and wait for closing notepad.

RunWait ("notepad.exe", strTop) ; Display original "top" file and wait for closing notepad.
intBytesWritten = udfFileShrink (strTop, 8, 1) ; "TOP....."
RunWait ("notepad.exe", strTop) ; Display shrinked "top" file and wait for closing notepad.

; Cleaning.
FileDelete (strTop)
FileDelete (strBot)

Exit
;------------------------------------------------------------------------------------------------------------------------------------------