home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / libg++-2.6-fsf.lha / libg++-2.6 / libio / iovfprintf.c < prev    next >
C/C++ Source or Header  |  1994-06-24  |  22KB  |  886 lines

  1. /* 
  2. Copyright (C) 1993 Free Software Foundation
  3.  
  4. This file is part of the GNU IO Library.  This library is free
  5. software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU CC; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. As a special exception, if you link this library with files
  20. compiled with a GNU compiler to produce an executable, this does not cause
  21. the resulting executable to be covered by the GNU General Public License.
  22. This exception does not however invalidate any other reasons why
  23. the executable file might be covered by the GNU General Public License. */
  24.  
  25. /*
  26.  * Copyright (c) 1990 Regents of the University of California.
  27.  * All rights reserved.
  28.  *
  29.  * Redistribution and use in source and binary forms are permitted
  30.  * provided that the above copyright notice and this paragraph are
  31.  * duplicated in all such forms and that any documentation,
  32.  * advertising materials, and other materials related to such
  33.  * distribution and use acknowledge that the software was developed
  34.  * by the University of California, Berkeley.  The name of the
  35.  * University may not be used to endorse or promote products derived
  36.  * from this software without specific prior written permission.
  37.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  38.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  39.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  40.  */
  41.  
  42.  
  43. #if defined(LIBC_SCCS) && !defined(lint)
  44. static char sccsid[] = "%W% (Berkeley) %G%";
  45. #endif /* LIBC_SCCS and not lint */
  46.  
  47. /*
  48.  * Actual printf innards.
  49.  *
  50.  * This code is large and complicated...
  51.  */
  52.  
  53. #include <sys/types.h>
  54. #include "libioP.h"
  55. #include <string.h>
  56. #ifdef __STDC__
  57. #include <stdarg.h>
  58. #else
  59. #include <varargs.h>
  60. #endif
  61.  
  62. /*
  63.  * Define FLOATING_POINT to get floating point.
  64.  */
  65. #ifndef    NO_FLOATING_POINT
  66. #define    FLOATING_POINT
  67. #endif
  68.  
  69. /* end of configuration stuff */
  70.  
  71.  
  72. /*
  73.  * Helper "class" for `fprintf to unbuffered': creates a
  74.  * temporary buffer. */
  75.  
  76. struct helper_file
  77. {
  78.   struct _IO_FILE_plus _f;
  79.   _IO_FILE *_put_stream;
  80. };
  81.  
  82. static int
  83. _IO_helper_overflow (fp, c)
  84.      _IO_FILE *fp;
  85.      int c;
  86. {
  87.   _IO_FILE *target = ((struct helper_file*)fp)->_put_stream;
  88.   int used = fp->_IO_write_ptr - fp->_IO_write_base;
  89.   if (used)
  90.     {
  91.       _IO_sputn(target, fp->_IO_write_base, used);
  92.       fp->_IO_write_ptr -= used;
  93.     }
  94.   return _IO_putc (c, fp);
  95. }
  96.  
  97. static struct _IO_jump_t _IO_helper_jumps = {
  98.   _IO_helper_overflow,
  99.   _IO_default_underflow,
  100.   _IO_default_xsputn,
  101.   _IO_default_xsgetn,
  102.   _IO_default_read,
  103.   _IO_default_write,
  104.   _IO_default_doallocate,
  105.   _IO_default_pbackfail,
  106.   _IO_default_setbuf,
  107.   _IO_default_sync,
  108.   _IO_default_finish,
  109.   _IO_default_close,
  110.   _IO_default_stat,
  111.   _IO_default_seek,
  112.   _IO_default_seekoff,
  113.   _IO_default_seekpos,
  114.   _IO_default_uflow
  115. };
  116.  
  117. static int
  118. helper_vfprintf(fp, fmt0, ap)
  119.      register _IO_FILE* fp;
  120.      char const *fmt0;
  121.      _IO_va_list ap;
  122. {
  123.   char buf[_IO_BUFSIZ];
  124.   struct helper_file helper;
  125.   register _IO_FILE *hp = (_IO_FILE*)&helper;
  126.   int result, to_flush;
  127.  
  128.   /* initialize helper */
  129.   helper._put_stream = fp;
  130.   hp->_IO_write_base = buf;
  131.   hp->_IO_write_ptr = buf;
  132.   hp->_IO_write_end = buf+_IO_BUFSIZ;
  133.   hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS;
  134.   hp->_jumps = &_IO_helper_jumps;
  135.   
  136.   /* Now print to helper instead. */
  137.   result = _IO_vfprintf(hp, fmt0, ap);
  138.  
  139.   /* Now flush anything from the helper to the fp. */
  140.   if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
  141.     {
  142.       if (_IO_sputn(fp, hp->_IO_write_base, to_flush) != to_flush)
  143.     return EOF;
  144.     }
  145.   return result;
  146. }
  147.  
  148. #ifdef FLOATING_POINT
  149.  
  150. #include "floatio.h"
  151. #define    BUF        (MAXEXP+MAXFRACT+1)    /* + decimal point */
  152. #define    DEFPREC        6
  153. extern double modf __P((double, double*));
  154.  
  155. #else /* no FLOATING_POINT */
  156.  
  157. #define    BUF        40
  158.  
  159. #endif /* FLOATING_POINT */
  160.  
  161.  
  162. /*
  163.  * Macros for converting digits to letters and vice versa
  164.  */
  165. #define    to_digit(c)    ((c) - '0')
  166. #define is_digit(c)    ((unsigned)to_digit(c) <= 9)
  167. #define    to_char(n)    ((n) + '0')
  168.  
  169. /*
  170.  * Flags used during conversion.
  171.  */
  172. #define    LONGINT        0x01        /* long integer */
  173. #define    LONGDBL        0x02        /* long double; unimplemented */
  174. #define    SHORTINT    0x04        /* short integer */
  175. #define    ALT        0x08        /* alternate form */
  176. #define    LADJUST        0x10        /* left adjustment */
  177. #define    ZEROPAD        0x20        /* zero (as opposed to blank) pad */
  178. #define    HEXPREFIX    0x40        /* add 0x or 0X prefix */
  179.  
  180. int
  181. _IO_vfprintf(fp, fmt0, ap)
  182.      register _IO_FILE* fp;
  183.      char const *fmt0;
  184.      _IO_va_list ap;
  185. {
  186.     register const char *fmt; /* format string */
  187.     register int ch;    /* character from fmt */
  188.     register int n;        /* handy integer (short term usage) */
  189.     register char *cp;    /* handy char pointer (short term usage) */
  190.     const char *fmark;    /* for remembering a place in fmt */
  191.     register int flags;    /* flags as above */
  192.     int ret;        /* return value accumulator */
  193.     int width;        /* width from format (%8d), or 0 */
  194.     int prec;        /* precision from format (%.3d), or -1 */
  195.     char sign;        /* sign prefix (' ', '+', '-', or \0) */
  196. #ifdef FLOATING_POINT
  197.     int softsign;        /* temporary negative sign for floats */
  198.     double _double;        /* double precision arguments %[eEfgG] */
  199. #ifndef USE_DTOA
  200.     int fpprec;        /* `extra' floating precision in [eEfgG] */
  201. #endif
  202. #endif
  203.     unsigned long _ulong;    /* integer arguments %[diouxX] */
  204.     enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
  205.     int dprec;        /* a copy of prec if [diouxX], 0 otherwise */
  206.     int dpad;        /* extra 0 padding needed for integers */
  207.     int fieldsz;        /* field size expanded by sign, dpad etc */
  208.     /* The initialization of 'size' is to suppress a warning that
  209.        'size' might be used unitialized.  It seems gcc can't
  210.        quite grok this spaghetti code ... */
  211.     int size = 0;        /* size of converted field or string */
  212.     char buf[BUF];        /* space for %c, %[diouxX], %[eEfgG] */
  213.     char ox[2];        /* space for 0x hex-prefix */
  214.  
  215.     /*
  216.      * BEWARE, these `goto error' on error, and PAD uses `n'.
  217.      */
  218. #define    PRINT(ptr, len) \
  219.   do { if (_IO_sputn(fp,ptr, len) != len) goto error; } while (0)
  220. #define PAD_SP(howmany) if (_IO_padn(fp, ' ', howmany) < (howmany)) goto error;
  221. #define PAD_0(howmany) if (_IO_padn(fp, '0', howmany) < (howmany)) goto error;
  222.  
  223.     /*
  224.      * To extend shorts properly, we need both signed and unsigned
  225.      * argument extraction methods.
  226.      */
  227. #define    SARG() \
  228.     (flags&LONGINT ? va_arg(ap, long) : \
  229.         flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
  230.         (long)va_arg(ap, int))
  231. #define    UARG() \
  232.     (flags&LONGINT ? va_arg(ap, unsigned long) : \
  233.         flags&SHORTINT ? (unsigned long)(unsigned short)va_arg(ap, int) : \
  234.         (unsigned long)va_arg(ap, unsigned int))
  235.  
  236.     /* optimise stderr (and other unbuffered Unix files) */
  237.     if (fp->_IO_file_flags & _IO_UNBUFFERED)
  238.         return helper_vfprintf(fp, fmt0, ap);
  239.  
  240.     fmt = fmt0;
  241.     ret = 0;
  242.  
  243.     /*
  244.      * Scan the format for conversions (`%' character).
  245.      */
  246.     for (;;) {
  247.         for (fmark = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
  248.             /* void */;
  249.         if ((n = fmt - fmark) != 0) {
  250.             PRINT(fmark, n);
  251.             ret += n;
  252.         }
  253.         if (ch == '\0')
  254.             goto done;
  255.         fmt++;        /* skip over '%' */
  256.  
  257.         flags = 0;
  258.         dprec = 0;
  259. #if defined(FLOATING_POINT) && !defined (USE_DTOA)
  260.         fpprec = 0;
  261. #endif
  262.         width = 0;
  263.         prec = -1;
  264.         sign = '\0';
  265.  
  266. rflag:        ch = *fmt++;
  267. reswitch:    switch (ch) {
  268.         case ' ':
  269.             /*
  270.              * ``If the space and + flags both appear, the space
  271.              * flag will be ignored.''
  272.              *    -- ANSI X3J11
  273.              */
  274.             if (!sign)
  275.                 sign = ' ';
  276.             goto rflag;
  277.         case '#':
  278.             flags |= ALT;
  279.             goto rflag;
  280.         case '*':
  281.             /*
  282.              * ``A negative field width argument is taken as a
  283.              * - flag followed by a positive field width.''
  284.              *    -- ANSI X3J11
  285.              * They don't exclude field widths read from args.
  286.              */
  287.             if ((width = va_arg(ap, int)) >= 0)
  288.                 g