bytearray.anubis 1.55 KB


read int.anubis


      *** (5.5) Dumping a byte array in Anubis language format. 
   
   Since version 1.13, we can lexically write byte arrays into our source files. 
   For example,  
   
     { 0 1 2 3 4 }
   
   represents the byte array containing the five bytes 0, 1, 2, 3 and 4. Bytes can
   be written as decimal integers (for example: 97) or as hexadecimal integers (for
   example: 0x4a). Only the values between 0 and 255 are accepted. There is no
   comma between the bytes.  
   
   The function below dumps a byte array into the above syntax (without the braces). 
   Each byte is written as a three digits decimal integer. The number of bytes per 
   line is parametrizable. The operand 'indent' tells how many spaces are to be put
   at the beginning of each line. 
   
define One anubis_format_aux(WStream s, ByteArray b, Int bpl, Int i, Int c, String ind). 
   
public define One
   anubis_format
     (
       WStream   s,
       ByteArray b, 
       Int       bytes_per_line, 
       Int       indent
     ) =
   anubis_format_aux(s,b,max(bytes_per_line+1,1),0,0,
                     constant_string(max(0,indent-1),' ')).
   
define One 
   anubis_format_aux
   (
     WStream s, 
     ByteArray b, 
     Int bpl, 
     Int i, 
     Int c, 
     String ind
   ) = 
   if i = length(b) then unique else
   if c = 0         then print(s,ind);   anubis_format_aux(s,b,bpl,i,1,ind) else 
   if c = bpl       then print(s,"\n");  anubis_format_aux(s,b,bpl,i,0,ind) else 
   print(s," 0x"+to_hexa(force_nth(i,b)));  
   anubis_format_aux(s,b,bpl,i+1,c+1,ind).