convert.anubis 4.41 KB

 *Project*                     Anubis
   
 *Title*                  conversion tools 
   
 *Copyright*       Copyright (c) David René 2006-2007. 

   
 *Author*    David René
 *Created*   December 2006
 *Status*    Beta  

read tools/basis.anubis
read system/types.anubis
read system/string.anubis

//TODO manage the real endianness of the host


public define ByteArray reverse(ByteArray source).


public define ByteArray to_ByteArray(Bool value).
public define ByteArray to_ByteArray(Word8 value).

public define String    to_String(Int32 value).

public define Bool
  to_Bool
  (
    String  value
  )=
  if insensitive_equal(value,"true") then
    true
  else
    false.
    

public define String
   to_String
     (
       Bool b
     ) =
   if b then "true" else "false". 

public define Int32
   to_Int32
     (
       Bool b
     ) =
   if b then 1 else 0. 
   
public define ByteArray
  to_ByteArray
  (
    Bool  value
  )=
  with ba = constant_byte_array(1,0),
  if value then
    forget(put(ba,0,1));
    ba
  else
    //already to 0, then return the ByteArray
    ba.
    
public define ByteArray
  to_ByteArray
  (
    Word8  value
  )=
  constant_byte_array(1,value).
  

public define ByteArray
  host_to_lendian_Int16_ByteArray
  (
     Int16 value,
  ) =
  //create the ByteArray which will receive the result
  with  ba = constant_byte_array(2, 0),  
  
  if host_endian is 
  {
    little  then  
      forget(put(ba,1,value.high)); 
      forget(put(ba,0,value.low )),
    big     then
      forget(put(ba,1,value.low)); 
      forget(put(ba,0,value.high))
  };
  //return the result ByteArray
   ba.
   
public define ByteArray
  host_to_lendian_Int32_ByteArray
  (
     Int32 value,
  ) =
  //create the ByteArray which will receive the result
  with  ba = constant_byte_array(4, 0),  
  
  if host_endian is 
  {
    little  then  
      forget(put(ba,3,truncate_to_word8( value>>24) )); 
      forget(put(ba,2,truncate_to_word8((value>>16) & 0xFF)));
      forget(put(ba,1,truncate_to_word8((value>>8)  & 0xFF))); 
      forget(put(ba,0,truncate_to_word8( value      & 0xFF))),
    big     then
      forget(put(ba,0,truncate_to_word8( value>>24) )); 
      forget(put(ba,1,truncate_to_word8((value>>16) & 0xFF)));
      forget(put(ba,2,truncate_to_word8((value>>8)  & 0xFF))); 
      forget(put(ba,3,truncate_to_word8( value      & 0xFF)))
  };
  //return the result ByteArray
   ba.

public define ByteArray
  host_to_lendian_Int64_ByteArray
  (
     Int64 value,
  ) =
  if host_endian is 
  {
    little  then  
      host_to_lendian_Int32_ByteArray(value.low) +
      host_to_lendian_Int32_ByteArray(value.high),
    big     then
      host_to_lendian_Int32_ByteArray(value.high) +
      host_to_lendian_Int32_ByteArray(value.low)
  }.


public define ByteArray
  host_to_lendian_Float64_ByteArray
  (
    Float value
  )=
  with ba = serialize(value),
  if host_endian is
  {
    little  then  ba,
    big     then  reverse(ba)
  }.

public define ByteArray
  host_to_lendian_Float32_ByteArray
  (
    Float32 val
  )=
 host_to_lendian_Int32_ByteArray(val.value).
  
public define ByteArray
  host_to_lendian_B_Point_ByteArray
  (
    B_Point  value
  ) =
  host_to_lendian_Float32_ByteArray(value.x) +
  host_to_lendian_Float32_ByteArray(value.y).
  
public define ByteArray
  host_to_lendian_B_Rect_ByteArray
  (
    B_Rect  value
  ) =
  if value is
  {
     rect(left_top, right_bottom)   then
      host_to_lendian_B_Point_ByteArray(left_top)   +
      host_to_lendian_B_Point_ByteArray(right_bottom),
      
     rect(left, top, right, bottom) then
      host_to_lendian_Float32_ByteArray(left)   +
      host_to_lendian_Float32_ByteArray(top)    +
      host_to_lendian_Float32_ByteArray(right)  +
      host_to_lendian_Float32_ByteArray(bottom)      
  }
  .
  
define ByteArray 
  reverse
  (
    ByteArray source,
    ByteArray dest,
    Int32     src_idx,
    Int32     dest_idx
  ) =
  if src_idx < 0 then
    dest
  else
    forget(put(dest, dest_idx, force_nth(src_idx, source)));
    reverse(source, dest, src_idx -1, dest_idx +1)  
  .
  
/** Reverse the entire content of the given ByteArray
  * if the content is [1|2|3] after reverse it will be [3|2|1] 
  */
public define ByteArray
  reverse
  (
    ByteArray source
  )=
  with len = length(source),
  dest = constant_byte_array(len,0),
  reverse(source, dest, len - 1, 0).

 /************** to_String part *************/
 
public define String
  to_String
  (
    Int32 v
  )=
  integer_to_string(v).