home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 11 / 11.iso / m / m111 / 2.img / GRPCT3.EXE / HEIGHT.C next >
Encoding:
C/C++ Source or Header  |  1991-01-28  |  2.4 KB  |  166 lines

  1. /* Height of a string calculation routine, Copyright 1991 John Bridges */
  2.  
  3.  
  4. #define code _based(_segname("_CODE"))
  5.  
  6. unsigned code _acrtused;    /* Dummy variable required by linker */
  7.  
  8. struct
  9. {
  10.     unsigned char widths[256];
  11.     unsigned char gap;
  12.     unsigned char vgap;
  13.     unsigned char xsize;
  14.     unsigned char ysize;
  15. } code cb;
  16.  
  17. /* Called function has standard C Parameters passed to it */
  18.  
  19. unsigned long far main(argc, argv)
  20. int argc;
  21. char **argv;
  22. {
  23.     if (argc == 1)
  24.         return (unsigned long) (void far *) &cb;
  25.     else
  26.         return height(num(argv[1]), argv[2]);
  27. }
  28.  
  29. num(unsigned char *pt)
  30. {
  31.     unsigned int j;
  32.  
  33.     if (((unsigned int *) pt)[-1] == 1)
  34.         return (int) *(unsigned long *) pt;
  35.     j = 0;
  36.     while (*pt >= '0' && *pt <= '9')    /* loop through and convert */
  37.         j = j * 10 + (*pt++ - '0');
  38.     return j;
  39. }
  40.  
  41. height(width, ourstr)
  42. int width;
  43. unsigned char *ourstr;
  44. {
  45.     register int ch;
  46.     register unsigned char *pt;
  47.     int xw, xx, oxx;
  48.     int tx, ty;
  49.     unsigned char *wpt;
  50.     int swpt;
  51.     int pch;
  52.  
  53.     pt = ourstr;
  54.     tx = 0;
  55.     ty = 0;
  56. glp:    wpt = 0;
  57.     swpt = 0;
  58.     pch = 0;
  59.     xx = tx;
  60.     while (ch = *pt)
  61.     {
  62.         if (ch == '\r')
  63.         {
  64.             ++pt;
  65.             continue;
  66.         }
  67.         if (ch == '\n' && pt[1] > ' ' && xx > tx)
  68.         {
  69.             if (pch == ' ')
  70.             {
  71.                 ++pt;
  72.                 continue;
  73.             }
  74.             ch = ' ';
  75.         }
  76.         if (ch == '\n')
  77.         {
  78.             xx = 0;
  79.         }
  80.         else if (ch == 255)
  81.         {
  82.             --xx;
  83.         }
  84.         else
  85.         {
  86.             xx += cb.widths[ch];
  87.         }
  88.         if (xx > width + 1 + cb.gap)
  89.             break;
  90.         if (ch == ' ' || ch == '-' || ch == ',')
  91.         {
  92.             oxx = xx;
  93.             wpt = pt;
  94.             swpt = (ch == ' ');
  95.         }
  96.         ++pt;
  97.         pch = ch;
  98.     }
  99.     if (!wpt)
  100.     {
  101.         if (tx > 0 && ch)
  102.             wpt = ourstr - 1;
  103.         else
  104.             wpt = pt;
  105.     }
  106.     else
  107.     {
  108.         if (xx <= width + 1 + cb.gap)
  109.         {
  110.             wpt = pt;
  111.         }
  112.         else
  113.         {
  114.             xx = oxx;
  115.             if (swpt)
  116.                 xx -= cb.widths[' '];
  117.         }
  118.     }
  119.     pt = ourstr;
  120.     pch = 0;
  121.     while (ch = *pt)
  122.     {
  123.         if (ch == '\r')
  124.         {
  125.             ++pt;
  126.             continue;
  127.         }
  128.         if (ch == 255)
  129.         {
  130.             --tx;
  131.             ++pt;
  132.             continue;
  133.         }
  134.         if (ch == '\n' && pt[1] > ' ' && tx > 0)
  135.         {
  136.             if (pch == ' ')
  137.             {
  138.                 ++pt;
  139.                 continue;
  140.             }
  141.             ch = ' ';
  142.         }
  143.         if (pt > wpt || ch == '\n')
  144.         {
  145.             tx = 0;
  146.             ty += cb.ysize + cb.vgap;
  147.             if (ch == '\n')
  148.             {
  149.                 ++pt;
  150.             }
  151.             while (*pt == ' ')
  152.                 ++pt;
  153.             ourstr = pt;
  154.             goto glp;
  155.         }
  156.         pch = ch;
  157.         ++pt;
  158.         tx += cb.widths[ch];
  159.     }
  160.     if (tx)
  161.         ty += cb.ysize + cb.vgap;
  162.     return ty;
  163. }
  164.  
  165.  
  166.