BinaryTagRepl

Replaces a binary tag with text.

Syntax:

BinaryTagRepl ( tag-struct, new-string )

Parameters:

(s) tag-struct: Structure returned from BinaryTagInit.

(s) new-string: text string to replace binary-tag with.

Returns:

(s) returns a binary tag structure string. If BinaryTagRepl fails, the replace operation is ignored and the strings are not replaced.

 

 

Note: This function is useful for editing HTML.

The BinaryTagRepl ignores overruns of the binary buffer, therefore make sure to allocate a large enough binary buffer.

 

Example:


;BinaryTag Example 4 - Replace - passing parameters and other tricks
;Set up test case
;Setup input and output file names
filedata=StrCat(DirGet(),"filedata.txt")
filerslt=StrCat(DirGet(),"filerslt.txt")
;Set up a sample file for this example.
;Presumably you could have a library of these
;to choose from, making the generation of
;customized HTML pages easy to automate.
fhandle=FileOpen(filedata,"WRITE")
FileWrite(fhandle,"<HTML>")
FileWrite(fhandle,"<HEAD><TITLE>{{Title}}</TITLE></HEAD>")
FileWrite(fhandle,"<BODY>")
FileWrite(fhandle,"<p>Random number in range 0 thru 10 = {{RANDOM 0 10}}")
FileWrite(fhandle,"<p>Random number in range 92 thru 114 = {{RANDOM 92 114}}")
FileWrite(fhandle,"<p>Random number in range -20 thru 4 = {{RANDOM -20 4}}")
FileWrite(fhandle,"<p>Value of <b>filedata</b> variable=<br>{{VALUE filedata}}")
FileWrite(fhandle,"<p>Value of <b>filerslt</b> variable=<br>{{VALUE filerslt}}")
FileWrite(fhandle,"<p>Current time/date in YmdHms format = {{DATE YMDHMS}}")
FileWrite(fhandle,"<p>Current time/date in display format = {{DATE DISPLAY}}")
FileWrite(fhandle,"</BODY>")
FileWrite(fhandle,"</HTML>")
FileClose(fhandle)
; Allocate a buffer much larger than required.
bb=BinaryAlloc(10000)
;Using {{ and }} for open and close tags around keywords
structure=BinaryTagInit(bb,"{{","}}")
;Not necessary in this example, but allows reuse of a
;BinaryBuffer if this code is placed in a loop editing
;multiple files.
BinaryEodSet(bb,0)
;Read data into the BinaryBuffer
BinaryRead(bb,filedata)
While 1
   structure=BinaryTagFind(structure)
   If structure=="" Then Break ; All done
   ;Get tag with any extra spaces removed
   extract=StrTrim(BinaryTagExtr(structure, 1))
   ParseData(extract)
   HTMLValue="???" ; keyword not found error value
   GoSub %param1%
   structure = BinaryTagRepl(structure,HTMLValue)
EndWhile
BinaryWrite(bb,filerslt)
BinaryFree(bb)
Message("Result in",filerslt)
Exit
:Value
; Use WinBatch substitution to capture variable contents
HTMLValue = %param2%
Return
:TITLE
HTMLValue="BinaryTagRepl Example"
Return
:DATE
If param2=="YMDHMS"
   HTMLValue=TimeYmdHms()
Else
   If param2=="DISPLAY"
      HTMLValue=TimeDate()
   Else
      HTMLValue="???Unknown DATE format???"
   EndIf
EndIf
Return
:Random
randomlow=param2
randomhigh=param3
randomrange=randomhigh-randomlow
HtmlValue=Random(randomrange)+randomlow
Return
See Also:

BinaryTagInit, BinaryTagFind, BinaryTagExtr