WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Binary Operations

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

UDF BinaryPokeStrEx

Keywords: 	 UDFBinaryPokeStrEx.wbt

udfBinaryPokeStrEx(bb, offset, str, padchar, tempfileflag)

See comments in the udf ...

;----------------------------------------------------------------------------------------------------
; udfBinaryPokeStrEx(bb, offset, str, padchar, tempfileflag)                 ; DD.2001:11:15:00:00:00
;----------------------------------------------------------------------------------------------------
If itemlocate("udfbinarypokestrex", IntControl(77,103,0,0,0), @tab) then goto skip_udfbinarypokestrex
#DefineFunction udfBinaryPokeStrEx(bb, offset, str, padchar, tempfileflag)
bbsize = BinaryEodGet(bb)
If (bbsize==0) then return (bb)
offset = max(0,offset)
padchar = StrSub(padchar,1,1)
tempfileflag = max(@false,min(@true,tempfileflag))
bb2size = max(bbsize,offset+StrLen(str))
If tempfileflag
   tempfile = FileCreateTemp("TMP")
   num = BinaryWrite(bb,tempfile)
   BinaryFree(bb)
   bb2 = BinaryAlloc(bb2size)
   num = BinaryRead(bb2,tempfile)
   FileDelete(tempfile)
else
   bb2 = BinaryAlloc(bb2size)
   num = BinaryCopy(bb2,0,bb,0,bbsize)
   BinaryFree(bb)
EndIf
num = BinaryPokeStr(bb2,offset,str)
If (bb2size>bbsize) then if (padchar!="") then num = BinaryReplace(bb2,"",padchar,@false)
Return (bb2)
;
; This UDF returns a handle to a binary buffer.
; Source buffer will be freed and new buffer is created 
; with buffer size enlarged, shrinked or unchanged.
; If parameter "offset" points beyond eod of source buffer,
; then the new created target buffer size will grow.
; Parameter "padchar" can be used to replace the
; upcoming filler null bytes in target buffer.
; Caution: If "offset" points beyond eod of source buffer and
; if padchar remains empty (i.e. StrLen(padchar)==0),
; then following BinaryPeekStr can cause unwanted truncation effect,
; because an existing null byte is treated as an "end of string marker".
;
; Detlev Dalitz.20011115
#EndFunction
:skip_udfbinarypokestrex


;--- test ---

:test1
; enlarge buffer using memory
; from 3 byte  "+.."
;   to 4 byte  "+***"
;
str  = "***"
bb   = BinaryAlloc(3)
num  = BinaryPokeStr(bb,0,"+")
offset  = 1
padchar = " "
tempfileflag = @false
bb   = udfBinaryPokeStrEx(bb,offset,str,padchar,tempfileflag)
str  = BinaryPeekStr(bb,0,BinaryEodGet(bb))
len  = StrLen(str)
BinaryFree(bb)


:test2
; enlarge buffer with padding using tempfile
; from 3 byte  "+.."
;   to 7 byte  "+###***"
;
str  = "***"
bb   = BinaryAlloc(3)
num  = BinaryPokeStr(bb,0,"+")
offset  = 4
padchar = "#"
tempfileflag = @true
bb   = udfBinaryPokeStrEx(bb,offset,str,padchar,tempfileflag)
str  = BinaryPeekStr(bb,0,BinaryEodGet(bb))
len  = StrLen(str)
BinaryFree(bb)


:test3
; enlarge buffer, but empty padchar causes truncation after byte 1
; from 3 byte  "+.."
;   to 7 byte  "+...***"
;
str  = "***"
bb   = BinaryAlloc(3)
num  = BinaryPokeStr(bb,0,"+")
offset  = 4
padchar = ""
tempfileflag = @true
bb   = udfBinaryPokeStrEx(bb,offset,str,padchar,tempfileflag)
str  = BinaryPeekStr(bb,0,BinaryEodGet(bb))
len  = StrLen(str)
BinaryFree(bb)


:test4
; shrink buffer with padding
; from 10 byte  "+........."
;    to 6 byte  "+~~***"
;
str  = "***"
bb   = BinaryAlloc(10)
num  = BinaryPokeStr(bb,0,"+")
offset  = 3
padchar = "~"
tempfileflag = @true
bb   = udfBinaryPokeStrEx(bb,offset,str,padchar,tempfileflag)
str  = BinaryPeekStr(bb,0,BinaryEodGet(bb))
len  = StrLen(str)
BinaryFree(bb)


:test5
; no change in buffersize
; from 9 byte  "+++++++++"
;   to 9 byte  "+++***+++"
;
str  = "***"
bb   = BinaryAlloc(9)
num  = BinaryPokeStr(bb,0,"+++++++++")
offset  = 3
padchar = " "
tempfileflag = @true
bb   = udfBinaryPokeStrEx(bb,offset,str,padchar,tempfileflag)
str  = BinaryPeekStr(bb,0,BinaryEodGet(bb))
len  = StrLen(str)
BinaryFree(bb)

Exit



Article ID:   W15318
File Created: 2002:09:05:13:51:20
Last Updated: 2002:09:05:13:51:20