udfBinaryInsert
hdlbb udfBinaryInsert (hdlbb, int, hdlbb, int, int)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfBinaryInsert (hdlBBTarget, intTargetOffset, hdlBBInsert, intInsertOffset, intInsertLength)
intTargetOffset = Min (Max (0, intTargetOffset), BinaryEodGet (hdlBBTarget))
intTargetLength = BinaryEodGet (hdlBBTarget)
intInsertOffset = Min (Max (0, intInsertOffset), BinaryEodGet (hdlBBInsert))
intInsertLength = Min (Max (0, intInsertLength), BinaryEodGet (hdlBBInsert))

intNewLength = intTargetLength + intInsertLength
hdlBBNew = BinaryAlloc (intNewLength)
BinaryEodSet (hdlBBNew, intNewLength)

Select @TRUE
Case intInsertLength == 0
   BinaryCopy (hdlBBNew, 0, hdlBBTarget, 0, intTargetLength)
   Break
Case intTargetOffset >= intTargetLength
   BinaryCopy (hdlBBNew, 0, hdlBBTarget, 0, intTargetLength)
   BinaryCopy (hdlBBNew, intTargetLength, hdlBBInsert, 0, intInsertLength)
   Break
Case intTargetOffset <= 0
   BinaryCopy (hdlBBNew, 0, hdlBBInsert, 0, intInsertLength)
   BinaryCopy (hdlBBNew, intInsertLength, hdlBBTarget, 0, intTargetLength)
   Break
Case @TRUE
   BinaryCopy (hdlBBNew, 0, hdlBBTarget, 0, intTargetOffset)
   BinaryCopy (hdlBBNew, intTargetOffset + intInsertLength, hdlBBTarget, intTargetOffset, intTargetLength - intTargetOffset)
   BinaryCopy (hdlBBNew, intTargetOffset, hdlBBInsert, intInsertOffset, intInsertLength)
   Break
EndSelect

Return (hdlBBNew)
;..........................................................................................................................................
; This UDF returns a handle to a new created binary buffer.
; The new binary buffer contains the content from the target buffer
; along with the inserted part of the insertion binary buffer.
;
; Detlev Dalitz.20020616
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test

strMsgTitle = "Demo: udfBinaryInsert (hdlBBTarget, intTargetOffset, hdlBBInsert, intInsertOffset, intInsertLength)"

strTest1 = "udfBinaryInsert (hdlBB1, 0, hdlBB2, 0, 3)"                      ; "123+++++"
strTest2 = "udfBinaryInsert (hdlBB1, BinaryEodGet (hdlBB1), hdlBB2, 0, 3)"  ; "+++++123"
strTest3 = "udfBinaryInsert (hdlBB1, 3, hdlBB2, 0, 3)"                      ; "+++123++"
strTest4 = "udfBinaryInsert (hdlBB1, 3, hdlBB2, 1, 1)"                      ; "+++2++"
strTest5 = "udfBinaryInsert (hdlBB1, 3, hdlBB2, 1, 0)"                      ; "+++++"

hdlBB1 = BinaryAlloc (5) ; The target buffer.
BinaryPokeStr (hdlBB1, 0, "+++++")

hdlBB2 = BinaryAlloc (3) ; The insertion buffer.
BinaryPokeStr (hdlBB2, 0, "123")

For intTest = 1 To 5
   strTest = strTest%intTest%
   hdlBB3 = %strTest% ; Call the function and get the handle to the new combined buffer.
   strMsgText = ""
   strMsgText = StrCat (strMsgText, strTest%intTest%, @CRLF)
   strMsgText = StrCat (strMsgText, 'hdlBB1', @TAB, '= "', BinaryPeekStr (hdlBB1, 0, BinaryEodGet (hdlBB1)), '"', @CRLF)
   strMsgText = StrCat (strMsgText, 'hdlBB2', @TAB, '= "', BinaryPeekStr (hdlBB2, 0, BinaryEodGet (hdlBB2)), '"', @CRLF)
   strMsgText = StrCat (strMsgText, 'hdlBB3', @TAB, '= "', BinaryPeekStr (hdlBB3, 0, BinaryEodGet (hdlBB3)), '"', @CRLF)
   Pause (strMsgTitle, strMsgText)
Next

hdlBB1 = BinaryFree (hdlBB1)
hdlBB2 = BinaryFree (hdlBB2)
hdlBB3 = BinaryFree (hdlBB3)

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