WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: JTaylor on April 07, 2019, 05:15:51 PM

Title: BinaryInsert()?
Post by: JTaylor on April 07, 2019, 05:15:51 PM
Am I missing something or is there no way to Insert a string into a Binary Buffer.   BinaryPokeStr() seems to overwrite rather than Insert.  Thanks.

Jim
Title: Re: BinaryInsert()?
Post by: kdmoyers on April 08, 2019, 05:34:45 AM
In the past I've used BinaryPoke to put in an unlikely byte, and then BinaryReplace to replace it with the full length string I wanted.  A little roundabout, but effective in some cases.
-K
Title: Re: BinaryInsert()?
Post by: JTaylor on April 08, 2019, 12:31:36 PM
Thanks.  Hopefully they will consider adding such a function.   Seems odd there isn't one???

Jim
Title: Re: BinaryInsert()?
Post by: td on April 09, 2019, 11:12:18 AM
Very lightly tested so contains bugs.
Code (winbatch) Select
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; BinaryInsertStr
;;
;; Inserts text into a binary buffer.
;;   _phBin - In - ptr to target binary buffer.
;;            Out - ptr to new buffer with inserted string.
;;   _hStr  - string to insert in binary buffer.
;;   _nOff  - insertion point of string in buffer.
;;
;; Returns new buffer size
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#DefineFunction BinaryInsertStr(_phBin, _Str, _nOff)
   hSrc = *_phBin
   nStrLen = StrLen(_str)
   nBinLen = BinaryBufInfo(hSrc, 0)
   hTmp = BinaryAlloc(nStrLen+nBinLen)
   if _nOff < nBinLen then nCnt = _nOff + 1
   else nCnt = nBinLen
   BinaryCopy(hTmp, 0, hSrc, 0, nCnt)
   BinaryPokeStr(hTmp, _nOff, _str)
   if nBinLen > _nOff then  BinaryCopy(hTmp, _nOff+nStrLen, hSrc, _nOff, nBinLen-_nOff)
   BinaryFree(hSrc)
   *_phBin = hTmp
   return BinaryBufInfo(hTmp, 0)
#EndFunction


;; Test
str = 'This is the best of all possible worlds.'
hTest = BinaryAlloc(Strlen(str))
BinaryPokeStr(hTest, 0, str)

BinaryInsertStr(&hTest, 'might not be ', 8)

str2 = BinaryPeekStr(hTest, 0 , BinaryBufInfo(hTest, 0))
Pause('BinaryInsertStr Test', str:@lf:str2)
Title: Re: BinaryInsert()?
Post by: JTaylor on April 09, 2019, 01:30:57 PM
Thanks.   This should help until the next release when you add it as a function  ;)

Jim
Title: Re: BinaryInsert()?
Post by: kdmoyers on April 10, 2019, 04:32:49 AM
Quote from: td on April 09, 2019, 11:12:18 AM
Code (winbatch) Select
#DefineFunction BinaryInsertStr(_phBin, _Str, _nOff)

This is super interesting, the way you swap the buffers around.  I'm going to study it carefully.
-Kirby
Title: Re: BinaryInsert()?
Post by: td on April 10, 2019, 09:51:02 AM
Let me know when you find a bug.