home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / prog_c / zc.lzh / ZC / ZCSRC.LZH / IOLib / Math / printf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-06  |  3.3 KB  |  174 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #define    FALSE    (0)
  4. #define TRUE    (!FALSE)
  5.  
  6. _printf(op, put, fmt, args)
  7.     char *op;
  8.     unsigned int (*put)();
  9.     register unsigned char *fmt;
  10.     register unsigned int *args;
  11.     {
  12.     register int i, cnt = 0, ljustf, lval;
  13.     int preci, dpoint, width;
  14.     char pad, sign, radix;
  15.     register char *ptmp;
  16.     char tmp[64], *ltoa(), *ultoa();
  17. #if FLOATS
  18.     double fx;
  19. #endif
  20.  
  21.     while(*fmt)
  22.         {
  23.         if(*fmt == '%')
  24.             {
  25.             ljustf = FALSE;    /* left justify flag */
  26.             sign = '\0';    /* sign char & status */
  27.             pad = ' ';    /* justification padding char */
  28.             width = -1;    /* min field width */
  29.             dpoint = FALSE;    /* found decimal point */
  30.             preci = -1;    /* max data width */
  31.             radix = 10;    /* number base */
  32.             ptmp = tmp;    /* pointer to area to print */
  33.             lval = FALSE;    /* long value flaged */
  34. fmtnxt:
  35.             i = 0;
  36.             while (isdigit(*++fmt))
  37.                 {
  38.                 i = (i * 10) + (*fmt - '0');
  39.                 if (dpoint)
  40.                     preci = i;
  41.                 else if (!i && (pad == ' '))
  42.                     {
  43.                     pad = '0';
  44.                     goto fmtnxt;
  45.                     }
  46.                 else
  47.                     width = i;
  48.                 }
  49.  
  50.             switch(*fmt)
  51.                 {
  52.                 case '\0':    /* early EOS */
  53.                     --fmt;
  54.                     goto charout;
  55.  
  56.                 case '-':    /* left justification */
  57.                     ljustf = TRUE;
  58.                     goto fmtnxt;
  59.  
  60.                 case ' ':
  61.                 case '+':    /* leading sign flag */
  62.                     sign = *fmt;
  63.                     goto fmtnxt;
  64.  
  65.                 case '*':    /* parameter width value */
  66.                     i = *args++;
  67.                     if (dpoint)
  68.                         preci = i;
  69.                     else
  70.                         width = i;
  71.                     goto fmtnxt;
  72.  
  73.                 case '.':    /* secondary width field */
  74.                     dpoint = TRUE;
  75.                     goto fmtnxt;
  76.  
  77.                 case 'l':    /* long data */
  78.                     lval = TRUE;
  79.                     goto fmtnxt;
  80.  
  81.                 case 'd':    /* Signed decimal */
  82.                 case 'i':
  83.                     ltoa((long)((lval)
  84.                         ?(*((long *) args))
  85.                         :(*((int  *) args))),
  86.                           ptmp, 10);
  87.                     if(lval)
  88.                         args = ((unsigned int *)
  89.                             (((long *) args) + 1));
  90.                     else
  91.                         args = ((unsigned int *)
  92.                             (((int *) args) + 1));
  93.                     goto printit;
  94.  
  95.                 case 'b':    /* Unsigned binary */
  96.                     radix = 2;
  97.                     goto usproc;
  98.  
  99.                 case 'o':    /* Unsigned octal */
  100.                     radix = 8;
  101.                     goto usproc;
  102.  
  103.                 case 'p':    /* Pointer */
  104.                     lval = TRUE;
  105.                     pad = '0';
  106.                     width = 6;
  107.                     preci = 8;
  108.                     /* fall thru */
  109.  
  110.                 case 'x':    /* Unsigned hexadecimal */
  111.                 case 'X':
  112.                     radix = 16;
  113.                     /* fall thru */
  114.  
  115.                 case 'u':    /* Unsigned decimal */
  116. usproc:
  117.                     ultoa((unsigned long)((lval)
  118.                         ?(*((unsigned long *) args))
  119.                         : *args++ ),
  120.                           ptmp, radix);
  121.                     if(lval)
  122.                         args = ((unsigned int *)
  123.                         (((unsigned long *) args) + 1));
  124.                     if (*fmt == 'x')
  125.                         strlwr(ptmp, ptmp);
  126.                     goto printit;
  127.  
  128. #if FLOATS
  129.                 case 'e':    /* float */
  130.                 case 'f':
  131.                 case 'g':
  132.                 case 'E':
  133.                 case 'G':
  134.                     fx = *((double *) args);
  135.                     args=(unsigned int *)
  136.                          (((double *) args)+1);
  137.  
  138.                     fp_print(fx, *fmt, preci, ptmp);
  139.                     preci = -1;
  140.                     goto printit;
  141. #endif
  142.  
  143.                 case 'c':    /* Character */
  144.                     ptmp[0] = *args++;
  145.                     ptmp[1] = '\0';
  146.                     goto nopad;
  147.  
  148.                 case 's':    /* String */
  149.                     ptmp = *((char **) args);
  150.                     args = ((unsigned int *)
  151.                         (((char **) args) + 1));
  152. nopad:
  153.                     sign = '\0';
  154.                     pad  = ' ';
  155. printit:
  156.                     cnt += _prtfld(op, put, ptmp, ljustf,
  157.                                sign, pad, width, preci);
  158.                     break;
  159.  
  160.                 default:    /* unknown character */
  161.                     goto charout;
  162.                 }
  163.             }
  164.         else
  165.             {
  166. charout:
  167.             (*put)(*fmt, op);        /* normal char out */
  168.             ++cnt;
  169.             }
  170.         ++fmt;
  171.         }
  172.     return(cnt);
  173.     }
  174.