home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / DLIBSSRC.ZIP / PRTFLD.C < prev    next >
Encoding:
Text File  |  1987-10-12  |  1.2 KB  |  64 lines

  1. _prtfld(op, put, buf, ljustf, sign, pad, width, preci)
  2. register char *op;
  3. register int (*put)();
  4. register unsigned char *buf;
  5. int ljustf;
  6. register char sign;
  7. char pad;
  8. register int width;
  9. int preci;
  10. /*
  11.  *    Output the given field in the manner specified by the arguments.
  12.  *    Return the number of characters output.
  13.  */
  14. {
  15.     register int cnt = 0, len;
  16.     register unsigned char ch;
  17.  
  18.     len = strlen(buf);
  19.  
  20.     if (*buf == '-')
  21.         sign = *buf++;
  22.     else if (sign)
  23.         len++;
  24.  
  25.     if ((preci != -1) && (len > preci))    /* limit max data width */
  26.         len = preci;
  27.  
  28.     if (width < len)    /* flexible field width or width overflow */
  29.         width = len;
  30.  
  31. /* at this point:
  32.  *    width = total field width
  33.  *    len   = actual data width (including possible sign character)
  34.  */
  35.     cnt = width;
  36.     width -= len;
  37.  
  38.     while (width || len)
  39.         {
  40.         if (!ljustf && width) {        /* left padding */
  41.             if (len && sign && (pad == '0'))
  42.                 goto showsign;
  43.             ch = pad;
  44.             --width;
  45.         }
  46.         else if (len) {
  47.             if (sign) {
  48. showsign:            ch = sign;    /* sign */
  49.                 sign = '\0';
  50.             }
  51.             else
  52.                 ch = *buf++;    /* main field */
  53.             --len;
  54.         }
  55.         else {
  56.             ch = pad;        /* right padding */
  57.             --width;
  58.         }
  59.         (*put)(ch, op);
  60.     }
  61.  
  62.     return(cnt);
  63. }
  64.