home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume6 / lbl / src / printl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  2.9 KB  |  154 lines

  1. /* (C) C.D.F. Miller, Heriot-Watt University, March 1984
  2.  *
  3.  *    Permission is hereby given to reproduce or modify this
  4.  *    software freely, provided that this notice be retained,
  5.  *    and that no use be made of the software for commercial
  6.  *    purposes without the express written permission of the
  7.  *    author.
  8.  */
  9.  
  10. /* printl.c:
  11.  *    print a label according to a format
  12.  */
  13.  
  14. #include    <lbl.h>
  15.  
  16. printl(lab, out)
  17.     rg label    *lab;
  18.     rg FILE        *out;
  19. {
  20.     rg format    f = lab->l_type->t_format;
  21.     rg us int    i;
  22.  
  23.     for (i = 0; i <= lab->l_bottom; i++)
  24.     {
  25.         while (*f)
  26.         {
  27.             if (*f != '%')
  28.                 putc(*f, out);
  29.             else
  30.                 switch (*++f)
  31.                 {
  32.                     default:
  33.                         putc(*f, out);
  34.                         break;
  35.                     case '\0':
  36.                         putc('%', out);
  37.                         break;
  38.                     case '0':
  39.                         printd(lab->l_levels[i]-1, out);
  40.                         f++;
  41.                         goto loopend;
  42.                     case '1':
  43.                         printd(lab->l_levels[i], out);
  44.                         f++;
  45.                         goto loopend;
  46.                     case 'i':
  47.                     case 'I':
  48.                         printr(*f, lab->l_levels[i],
  49.                             out);
  50.                         f++;
  51.                         goto loopend;
  52.                     case 'a':
  53.                     case 'A':
  54.                         printa(*f, lab->l_levels[i],
  55.                             out);
  56.                         f++;
  57.                         goto loopend;
  58.                 }
  59.             f++;
  60.         }
  61.         error("format too short to print label %s", lab->l_name);
  62.         break;
  63.     loopend:
  64.         continue;
  65.     }
  66. }
  67.  
  68. /* Print Decimal */
  69. printd(n, out)
  70.     us int    n;
  71.     FILE    *out;
  72. {
  73.     fprintf(out, "%u", n);
  74. }
  75.  
  76. /* Print Alphabetic 
  77.  *    actually base 26 (digits a-z), displaced by 1!
  78.  *    a is either "a" or "A" for upper or lower case.
  79.  */
  80. printa(a, n, out)
  81.     char    a;
  82.     us int    n;
  83.     FILE    *out;
  84. {
  85.     if (n==0)
  86.         putc('0', out);
  87.     else
  88.         auxprinta(a, n-1, out);
  89. }
  90.     
  91. auxprinta(a, n, out)
  92.     char    a;
  93.     us int    n;
  94.     FILE    *out;
  95. {
  96.     if (n > 25)
  97.         auxprinta(a, n/26 - 1, out);
  98.     putc(a + n%26, out);
  99. }
  100.  
  101. /* Print Roman
  102.  *    a is either "a" or "A" for upper or lower case.
  103.  */
  104. printr(a, n, out)
  105.     char    a;
  106.     us int    n;
  107.     FILE    *out;
  108. {
  109.     if (n==0)
  110.     {
  111.         putc('0', out);
  112.         return;
  113.     }
  114.     if (n >= 50000)
  115.     {
  116.         putc('!', out);
  117.         return;
  118.     }
  119.     while (n >= 10000)
  120.         putc(a+('Z'-'I'), out), n -= 10000;
  121.     if (n >= 9000)
  122.         putc(a+('M'-'I'), out), putc(a+('Z'-'I'), out), n -= 9000;
  123.     if (n >= 5000)
  124.         putc(a+('W'-'I'), out), n -= 5000;
  125.     if (n >= 4000)
  126.         putc(a+('M'-'I'), out), putc(a+('W'-'I'), out), n -= 4000;
  127.     while (n >= 1000)
  128.         putc(a+('M'-'I'), out), n -= 1000;
  129.     if (n >= 900)
  130.         putc(a+('C'-'I'), out), putc(a+('M'-'I'), out), n -= 900;
  131.     if (n >= 500)
  132.         putc(a+('D'-'I'), out), n -= 500;
  133.     if (n >= 400)
  134.         putc(a+('C'-'I'), out), putc(a+('D'-'I'), out), n -= 400;
  135.     while (n >= 100)
  136.         putc(a+('C'-'I'), out), n -= 100;
  137.     if (n >= 90)
  138.         putc(a+('X'-'I'), out), putc(a+('C'-'I'), out), n -= 90;
  139.     if (n >= 50)
  140.         putc(a+('L'-'I'), out), n -= 50;
  141.     if (n >= 40)
  142.         putc(a+('X'-'I'), out), putc(a+('L'-'I'), out), n -= 40;
  143.     while (n >= 10)
  144.         putc(a+('X'-'I'), out), n -= 10;
  145.     if (n >= 9)
  146.         putc(a+('I'-'I'), out), putc(a+('X'-'I'), out), n -= 9;
  147.     if (n >= 5)
  148.         putc(a+('V'-'I'), out), n -= 5;
  149.     if (n >= 4)
  150.         putc(a+('I'-'I'), out), putc(a+('V'-'I'), out), n -= 4;
  151.     while (n >= 1)
  152.         putc(a+('I'-'I'), out), n -= 1;
  153. }
  154.