udfBinaryPokeStrEx
hdlbb udfBinaryPokeStrEx (hdlbb, int, str, str, bln)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfBinaryPokeStrEx (hdlBB, intOffset, strString, strPadchar, blnTempfileFlag)
intBBSize = BinaryEodGet (hdlBB)
If intBBSize == 0 Then Return hdlBB
intOffset = Max (0, intOffset)
strPadchar = StrSub (strPadchar, 1, 1)
blnTempfileFlag = Max (@FALSE, Min (@TRUE, blnTempfileFlag))
intBB2Size = Max (intBBSize, intOffset + StrLen (strString))
If blnTempfileFlag
   strTempfile = FileCreateTemp ("TMP")
   intBytes = BinaryWrite (hdlBB, strTempfile)
   hdlBB = BinaryFree (hdlBB)
   hdlBB2 = BinaryAlloc (intBB2Size)
   intBytes = BinaryRead (hdlBB2, strTempfile)
   FileDelete (strTempfile)
Else
   hdlBB2 = BinaryAlloc (intBB2Size)
   intBytes = BinaryCopy (hdlBB2, 0, hdlBB, 0, intBBSize)
   hdlBB = BinaryFree (hdlBB)
EndIf
intBytes = BinaryPokeStr (hdlBB2, intOffset, strString)
If intBB2Size > intBBSize Then If strPadchar != "" Then intBytes = BinaryReplace (hdlBB2, "", strPadchar, @FALSE)
Return hdlBB2
;..........................................................................................................................................
; This UDF returns a handle to a binary buffer.
; The given source buffer will be freed during the process and a new buffer will be created with buffer size enlarged, shrinked or unchanged.
; If parameter "intOffset" points beyond end of source buffer, then the new created target buffer size will grow.
; Parameter "strPadchar" can be used to replace the upcoming null bytes in the new part of the buffer.
; Caution:
; If parameter "intOffset" points beyond end of source buffer and if parameter "strPadchar" remains as an empty string
; (i. e. StrLen (strPadchar) == 0), then a following BinaryPeekStr can produce unwanted truncation,
; because the first null byte from the left in the buffer is treated as the end of string.
;..........................................................................................................................................
; Detlev Dalitz.20011115
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

:Test1
; Enlarge buffer using memory.
; From 3 byte  "+.."
;   To 4 byte  "+***"
;
strString = "***"
hdlBB = BinaryAlloc (3)
intBytes = BinaryPokeStr (hdlBB, 0, "+")
intOffset = 1
strPadchar = " "
blnTempfileFlag = @FALSE
hdlBB = udfBinaryPokeStrEx (hdlBB, intOffset, strString, strPadchar, blnTempfileFlag)
strString = BinaryPeekStr (hdlBB, 0, BinaryEodGet (hdlBB))
len = StrLen (strString)
hdlBB = BinaryFree (hdlBB)


:Test2
; Enlarge buffer with padding using tempfile.
; From 3 byte  "+.."
;   To 7 byte  "+###***"
;
strString = "***"
hdlBB = BinaryAlloc (3)
intBytes = BinaryPokeStr(hdlBB, 0, "+")
intOffset = 4
strPadchar = "#"
blnTempfileFlag = @TRUE
hdlBB = udfBinaryPokeStrEx (hdlBB, intOffset, strString, strPadchar, blnTempfileFlag)
strString = BinaryPeekStr(hdlBB, 0, BinaryEodGet (hdlBB))
len = StrLen (strString)
hdlBB = BinaryFree (hdlBB)


:Test3
; Enlarge buffer, but empty strPadchar causes truncation after byte 1.
; From 3 byte  "+.."
;   To 7 byte  "+...***"
;
strString = "***"
hdlBB = BinaryAlloc (3)
intBytes = BinaryPokeStr(hdlBB, 0, "+")
intOffset = 4
strPadchar = ""
blnTempfileFlag = @TRUE
hdlBB = udfBinaryPokeStrEx (hdlBB, intOffset, strString, strPadchar, blnTempfileFlag)
strString = BinaryPeekStr (hdlBB, 0, BinaryEodGet (hdlBB))
len = StrLen (strString)
hdlBB = BinaryFree (hdlBB)


:Test4
; Shrink buffer with padding.
; From 10 byte  "+........."
;    To 6 byte  "+~~***"
;
strString = "***"
hdlBB = BinaryAlloc (10)
intBytes = BinaryPokeStr (hdlBB, 0, "+")
intOffset = 3
strPadchar = "~"
blnTempfileFlag = @TRUE
hdlBB = udfBinaryPokeStrEx (hdlBB, intOffset, strString, strPadchar, blnTempfileFlag)
strString = BinaryPeekStr (hdlBB, 0, BinaryEodGet (hdlBB))
len = StrLen (strString)
hdlBB = BinaryFree (hdlBB)


:Test5
; No change in buffersize.
; From 9 byte  "+++++++++"
;   To 9 byte  "+++***+++"
;
strString = "***"
hdlBB = BinaryAlloc (9)
intBytes = BinaryPokeStr (hdlBB, 0, "+++++++++")
intOffset = 3
strPadchar = " "
blnTempfileFlag = @TRUE
hdlBB = udfBinaryPokeStrEx (hdlBB, intOffset, strString, strPadchar, blnTempfileFlag)
strString = BinaryPeekStr (hdlBB, 0, BinaryEodGet (hdlBB))
len = StrLen (strString)
hdlBB = BinaryFree (hdlBB)

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