&Binvars
Description:
Binary valuables are variables that have binary contents, which means not just plain text.
Advantages:
Doesn’t have an inbuilt size limitation like %vars do.
Isn’t limited to a certain range of characters, and contain end of line characters.
Perfect to handle socket events.
Disadvantages:
No trivial sort command.
Hard to work with.
Access to the data is made by character position.
Storage:
The data is stored in the memory.
Initializing a &Binvar:
/bset &binvar N data
Creates a &Binvar the size of N and inserts intial value.
Adding data:
/bcopy –c &bin1 N &bin2 S M
Adds the data between position S and M (S-M) in &bin2 to place N in &bin1 .
M can be set to –1 which means “position S onwards”.
The –c switch cuts the &Binvar to size N+M.
Deleting data:
/bunset &binvar1 &binvar 2 ...
Unsets the list of &binvars.
Retrieving data:
Set %ascii $bvar(&binvar,N)
Returns the ascii value of the character in position N.
Set %ascii $bvar(&binvar,N,M)
Returns the ascii values of the character in the range N-M .
Set %ascii $bvar(&binvar,N,M).text
Returns the contents of the &binvar in the range N-M in plain text.
Searching for data:
Set %position $bfind(&binvar,N, cc cc cc )
Searches the &binvar for a characters that match the ascii value cc cc starting from position N.
Set %position $bfind(&binvar,N,Match Text).text
Searched the &binvar for the matching text starting at position N
/breplce &binvar oldvalue newvalue
Searches the &binvar for a matching “oldvalue” and replaces it with “newvalue”
Converting to files :
/bread filename S N &binvar
Reads N bytes from filename’s position S and copies them to &binvar.
/bwrite filename S N &binvar
Writes N bytes from &binvar to position S in the file.
If S is –1. it will append to the end of the file.
If N is –1, it will copy the entire &binvar.
Retrieving size:
Set %size $bvar(&binvar,0)
Returns the size of the &binvar.
Example:
Load the example to the remotes, and right click on a channel.
All the code until the sockread is just the basic syntax to open a connection to a website.
I’m gonna show you a site that sends a long line of text which can’t be saved into a %var (you can try to sockread it into a %var and see).
menu channel,status {
examplesock: {
sockopen binvars www.urbandictionary.com 80
}
}
on *:sockopen:binvars: {
if ($sockerr > 0) {
echo -a xdccb 211,711 Timed Out
return
}
sockwrite -n $sockname GET /define.php?term=crack HTTP/1.1
sockwrite -n $sockname Host: www.urbandictionary.com
sockwrite -n $sockname Connection: Close
sockwrite -n $sockname User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)
sockwrite -n $sockname $crlf
}
on *:sockread:binvars:{
if ($sockerr > 0) return
while (online) {
if (!$sock(binvars)) return
sockread &mybinvar
if ($sockbr == 0) return
if ($get-tag(mybinvar,1).P) {
var %begin = $gettok($v1,1,32)
var %end = $gettok($v1,2,32)
window @mybinvar
while ($bfind(&mybinvar,%begin,.)) {
aline @mybinvar $replace($gettok($bvar(&mybinvar,%begin,$v1).text,1,60),$+($chr(38),quot;),")
var %begin = $calc($v1 +1)
}
sockclose binvars
}
}
}
;This alias will gets a property (P in our case) finds the position of the beginning of the tag, and its end.
;I’m looking for the tag <P> here, because the long text I want is inside a paragraph.
alias -l get-tag {
if ( !$2 ) { return 0 }
var %startingbyte = $bfind($+(&,$1),$2,$+(<,$prop))
if ( %startingbyte == 0 ) { return 0 }
var %endstart = $bfind($+(&,$1,),%startingbyte,>)
var %len = $calc(%endstart - %startingbyte + 1)
var %tag = $bvar($+(&,$1,),%startingbyte,%len).text
inc %startingbyte %len
if ( !$regex(%tag,/^<[^<]+>/) ) { return 0 }
var %endbyte = $bfind($+(&,$1),%startingbyte,$+(</,$prop,>))
return %startingbyte %endbyte
}