home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / libsrc87 / prtfld.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-30  |  1.2 KB  |  72 lines

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