home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / a / armbob / !ArmBob / progs / !Exec / Lib / Templread
Text File  |  1994-06-06  |  5KB  |  213 lines

  1. /*****************************************************
  2.  *                                                   *
  3.  *        Template reader      GCW 6/6/94            *
  4.  *                                                   *
  5.  *  Use as  Templread <template_file>                *
  6.  *  to open an Edit window on a Bob source text      *
  7.  *  for the template file given as argument.         *
  8.  *                                                   *
  9.  *****************************************************/
  10.  
  11. main()
  12. {
  13.  r = newvector(8);
  14.  out = "Pipe:Temp";           // temporary output file
  15.  write(out,read(getarg(1)));
  16.  oscli("Settype "+out+" text");
  17.  oscli("Filer_run "+out);
  18. }
  19.  
  20. read(file)
  21. {
  22.  local result;
  23.  r[0] = 23;
  24.  r[1] = @(file);
  25.  swi("OS_File",r);
  26.  if (r[6] != &fec) quit("Not a template file\n");
  27.  result = r[2] = @(newstring(r[4]));
  28.  r[0] = 16;
  29.  r[3] = 0;
  30.  swi("OS_File",r);
  31.  return result;
  32. }
  33.  
  34. write(file,buffer)
  35. {
  36.  local item, fp, offset; 
  37.  if (fp = fopen(file,"w"))
  38.   {
  39.    item = buffer+16;
  40.    offset = £(item);
  41.    while (offset)
  42.     {
  43.       p = buffer+offset;
  44.       inscribe(item,p,fp,p);
  45.       item += 24;
  46.       offset = £(item);
  47.      }
  48.    fclose(fp);
  49.   }
  50. }
  51.  
  52. inscribe(item,p,fp,buffer)
  53. {
  54.  local size, name, flags, ino, icon, c;
  55.  size = £(item+4); name = $(item+12);
  56.  fprint(fp,"/* "+name+" */\n");
  57.  fprint(fp,"in "+name+"_buf put {\n");
  58.  fprint(fp,string(£(p),10)+";/* xmin */\n"); p += 4;
  59.  fprint(fp,string(£(p),10)+";/* ymin */\n"); p += 4;
  60.  fprint(fp,string(£(p),10)+";/* xmax */\n"); p += 4;
  61.  fprint(fp,string(£(p),10)+";/* ymax */\n"); p += 4;
  62.  fprint(fp,string(£(p),10)+";/* x scroll */\n"); p += 4;
  63.  fprint(fp,string(£(p),10)+";/* y scroll */\n"); p += 4;
  64.  fprint(fp,string(£(p),10)+";/* behind */\n"); p += 4;
  65.  fprint(fp,hex(£(p))+";/* window flags */\n"); p += 4;
  66.  fprint(fp,hex(£(p))+";/* colours */\n"); p += 4;
  67.  fprint(fp,hex(£(p))+";/* scroll colours */\n"); p += 4;
  68.  fprint(fp,string(£(p),10)+";/* work xmin */\n"); p += 4;
  69.  fprint(fp,string(£(p),10)+";/* work ymin */\n"); p += 4;
  70.  fprint(fp,string(£(p),10)+";/* work xmax */\n"); p += 4;
  71.  fprint(fp,string(£(p),10)+";/* work ymax */\n"); p += 4;
  72.  flags = £(p) & (259);
  73.  fprint(fp,hex(£(p))+";/* title icon flags */\n"); p += 4;
  74.  fprint(fp,hex(£(p))+";/* work button flags */\n"); p += 4;
  75.  fprint(fp,string(£(p),10)+";/* sprite area */\n"); p += 4;
  76.  fprint(fp,string(£(p),10)+";/* min width/height */\n"); p += 4;
  77.  p = indir(fp,flags,p);
  78.  fprint(fp,string((ino = £(p)),10)+";/* no. of icons */\n"); p += 4;
  79.  for (icon = 0; icon < ino; icon++)
  80.  {
  81.   fprint(fp,"/* icon "+string(icon,10)+" */\n");
  82.   fprint(fp,string(£(p),10)+";/* xmin */\n"); p += 4;
  83.   fprint(fp,string(£(p),10)+";/* ymin */\n"); p += 4;
  84.   fprint(fp,string(£(p),10)+";/* xmax */\n"); p += 4;
  85.   fprint(fp,string(£(p),10)+";/* ymax */\n"); p += 4;
  86.   flags = £(p) & (259);
  87.   fprint(fp,hex(£(p))+";/* icon flags */\n"); p += 4;
  88.   p = indir(fp,flags,p);
  89.  }
  90.  if (p < buffer+size)
  91.   {
  92.    fprint(fp,"/* Indirected data */\n");
  93.    putc(34,fp);
  94.    while (p < buffer+size)
  95.    {
  96.     c = `(p++);
  97.     if (c==13)
  98.       if (p < buffer+size)
  99.        {
  100.         putc(34,fp);
  101.         fprint(fp,";\n");
  102.         putc(34,fp);
  103.        }
  104.     if (c!=13)
  105.      putc(c,fp);
  106.    }
  107.    putc(34,fp);
  108.    fprint(fp,";\n");
  109.   }
  110.  fprint(fp,"}\n");
  111. }
  112.  
  113. /* deal with possible indirection */
  114. indir(fp,flags,q)
  115. {
  116.  local p;
  117.  p = q;
  118.  switch(flags)
  119.  {
  120.   case 0:
  121.     fprint(fp,"/* no data */\n");
  122.     fprint(fp,string(£(p),10)+";\n"); p += 4;
  123.     fprint(fp,string(£(p),10)+";\n"); p += 4;
  124.     fprint(fp,string(£(p),10)+";\n"); p += 4;
  125.     break;
  126.   case 1:
  127.     fprint(fp,"/* text only */\n");
  128.     fprint(fp,""+34+s12(p)+34+";\n"); p += 12;
  129.     break;
  130.   case 2:
  131.   case 3:
  132.     fprint(fp,""+34+s12(p)+34+";\n"); p += 12;
  133.     break;
  134.   case 256:
  135.     fprint(fp,"/* no data */\n");
  136.     fprint(fp,string(£(p),10)+";\n"); p += 4;
  137.     fprint(fp,string(£(p),10)+";\n"); p += 4;
  138.     fprint(fp,string(£(p),10)+";\n"); p += 4;
  139.     break;
  140.   case 257:
  141.     fprint(fp,"/* text-only */\n");
  142.   case 258:
  143.   case 259:
  144.     fprint(fp,"/* indirected */\n");
  145.     fprint(fp,"@("+name+"_buf+"+string(£(p),10)+");\n"); p += 4;
  146.     fprint(fp,"@("+name+"_buf+"+string(£(p),10)+");\n"); p += 4;
  147.     fprint(fp,string(£(p),10)+";/* buffer length */\n"); p += 4;
  148.     break;
  149.   default:
  150.     fclose(fp);
  151.     quit("bad flags\n");
  152.     break;
  153.  }
  154.  return p;
  155. }
  156.  
  157. fprint(fp,s)
  158. {
  159.  local i;
  160.  for (i = 0; i < sizeof(s); i++)
  161.    putc(s[i],fp);
  162. }
  163.  
  164. /* convert a number n to a string to base b */
  165. string(n,b)  
  166. {
  167.  local neg,s,d;
  168.  if (n == 0) return ("0");
  169.  if (neg = (n<0)) n = -n;
  170.  s = "";
  171.  while (n)
  172.    {
  173.     d = n%b;
  174.     s = ((d<10)?(d +'0'):(d+'W'))+s;
  175.     n /= b;
  176.    }
  177.  return (((neg)?"-":"")+s);
  178. }
  179.  
  180. hex(n)
  181. {
  182.  local s,byte,top,bottom,b,i,zero;
  183.  s = "&";zero = TRUE;i = 4;
  184.  b = @(newstring(4));
  185.  in b put { n; }
  186.  while (i>0 && zero)
  187.    {
  188.     i--;
  189.     if (byte=`(b+i)) zero = FALSE;
  190.    }
  191.  top = byte/16; bottom = byte%16;
  192.  if (top) s += (top<10)?(top+'0'):(top+'W');
  193.  s += (bottom<10)?(bottom+'0'):(bottom+'W'); 
  194.  while (i>0)
  195.    {
  196.     i--; byte = `(b+i); top = byte/16; bottom = byte%16;
  197.     s += (top<10)?(top+'0'):(top+'W');
  198.     s += (bottom<10)?(bottom+'0'):(bottom+'W');
  199.    }
  200.  return s;
  201. }
  202.  
  203. /* 12 bytes of data */
  204. s12(p)
  205. {
  206.  local i,s;
  207.  s = newstring(12);
  208.  for(i=0;i<12;i++)
  209.     s[i] = `(p+i);
  210.  return s;
  211. }
  212.  
  213.