counter.anubis 2.77 KB

   *Project* Anubis
   
   
                           A counter for web pages. 
   
   
   
read tools/basis.anubis   
read web/making_a_web_site.anubis   

   
   
   *Public*
   
   
   
   This is the counter to be inserted into a web page:
   
public define HTML_Off_Form
   web_counter
     (
       String           directory,      // where the counter file will be located
       String           name,           // name of counter
       Int              text_size       // size of characters of counter
     ).
   
   
   Below is the function to call to increment the counter. 
   
public define One
   increment_web_counter
     (
       String           directory,      // where the counter file will be located
       String           name            // name of counter
     ). 
   

   In order to reset the counter to 0, just delete the file:
   
                               directory+"/"+name+".counter"
   
   
   
   
   *Private*
   
   
define List(Word8)
   zero_pad
     (
       List(Word8) l,
       Int         n
     ) =
   if length(l) < n
   then zero_pad(['0' . l],n)
   else l. 
   
      
define HTML_Off_Form
   web_counter
     (
       Int                   n,          // value of counter
       Int           text_size           // size of text
     ) =
   with black = rgb(0,0,0), 
        white = rgb(255,255,255), 
   table([border(1,0,1,black)],[row(
   table([border(0,1,0,black),background_color(black)],[row(
     [cell([background_color(black),width(2)],text([],"")) . 
      map((Word8 c) |-> cell([background_color(black),width(8),h_center],
         text([size(truncate_to_Word32(text_size)),color(white),bold],""+(to_Word32(c)-'0'))),       
                zero_pad(explode(to_decimal(n)),8))]
     )])
   )]).
   

   
public define HTML_Off_Form
   web_counter
     (
       String           directory,      // where the counter file will be located
       String           name,           // name of counter
       Int              text_size       // size of characters of counter
     ) =
   if (RetrieveResult(Int))retrieve(directory+"/"+name+".counter") is 
     {
       cannot_find_file    then web_counter(0,text_size),
       read_error          then web_counter(0,text_size),
       type_error          then web_counter(0,text_size),
       ok(n)               then web_counter(n,text_size),
     }. 
   
   
   
public define One
   increment_web_counter
     (
       String           directory,
       String           name, 
     ) =
   with path = directory+"/"+name+".counter",
   protect
   if (RetrieveResult(Int))retrieve(path) is 
     {
       cannot_find_file    then forget(save((Int)1,path)),
       read_error          then unique,
       type_error          then unique,
       ok(n)               then forget(save((Int)(n+1),path))
     }.