Home | History | Annotate | Line # | Download | only in printf
      1  1.1  mrg /* GCC Quad-Precision Math Library
      2  1.1  mrg    Copyright (C) 2011 Free Software Foundation, Inc.
      3  1.1  mrg    Written by Jakub Jelinek  <jakub (at) redhat.com>
      4  1.1  mrg 
      5  1.1  mrg This file is part of the libquadmath library.
      6  1.1  mrg Libquadmath is free software; you can redistribute it and/or
      7  1.1  mrg modify it under the terms of the GNU Library General Public
      8  1.1  mrg License as published by the Free Software Foundation; either
      9  1.1  mrg version 2 of the License, or (at your option) any later version.
     10  1.1  mrg 
     11  1.1  mrg Libquadmath is distributed in the hope that it will be useful,
     12  1.1  mrg but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  1.1  mrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14  1.1  mrg Library General Public License for more details.
     15  1.1  mrg 
     16  1.1  mrg You should have received a copy of the GNU Library General Public
     17  1.1  mrg License along with libquadmath; see the file COPYING.LIB.  If
     18  1.1  mrg not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
     19  1.1  mrg Boston, MA 02110-1301, USA.  */
     20  1.1  mrg 
     21  1.1  mrg #include <stdlib.h>
     22  1.1  mrg #include <stdio.h>
     23  1.1  mrg #ifdef HAVE_LIMITS_H
     24  1.1  mrg #include <limits.h>
     25  1.1  mrg #endif
     26  1.1  mrg #ifdef HAVE_LANGINFO_H
     27  1.1  mrg #include <langinfo.h>
     28  1.1  mrg #endif
     29  1.1  mrg #ifdef HAVE_CTYPE_H
     30  1.1  mrg #include <ctype.h>
     31  1.1  mrg #endif
     32  1.1  mrg #ifdef HAVE_WCHAR_H
     33  1.1  mrg #include <wchar.h>
     34  1.1  mrg #endif
     35  1.1  mrg #ifdef HAVE_WCTYPE_H
     36  1.1  mrg #include <wctype.h>
     37  1.1  mrg #endif
     38  1.1  mrg #ifdef HAVE_PRINTF_HOOKS
     39  1.1  mrg #include <printf.h>
     40  1.1  mrg #endif
     41  1.1  mrg #ifdef HAVE_LOCALE_H
     42  1.1  mrg #include <locale.h>
     43  1.1  mrg #endif
     44  1.1  mrg #include "quadmath-imp.h"
     45  1.1  mrg #include "gmp-impl.h"
     46  1.1  mrg 
     47  1.1  mrg #ifdef HAVE_WCHAR_H
     48  1.1  mrg #define L_(x) L##x
     49  1.1  mrg #else
     50  1.1  mrg #define L_(x) x
     51  1.1  mrg #undef wchar_t
     52  1.1  mrg #undef wint_t
     53  1.1  mrg #undef putwc
     54  1.1  mrg #undef WEOF
     55  1.1  mrg #define wchar_t char
     56  1.1  mrg #define wint_t int
     57  1.1  mrg #define putwc(c,f) putc(c,f)
     58  1.1  mrg #define WEOF EOF
     59  1.1  mrg #endif
     60  1.1  mrg 
     61  1.1  mrg #ifndef HAVE_CTYPE_H
     62  1.1  mrg /* Won't work for EBCDIC.  */
     63  1.1  mrg #undef isupper
     64  1.1  mrg #undef isdigit
     65  1.1  mrg #undef isxdigit
     66  1.1  mrg #undef tolower
     67  1.1  mrg #define isupper(x) \
     68  1.1  mrg   ({__typeof(x) __is_x = (x); __is_x >= 'A' && __is_x <= 'Z'; })
     69  1.1  mrg #define isdigit(x) \
     70  1.1  mrg   ({__typeof(x) __is_x = (x); __is_x >= '0' && __is_x <= '9'; })
     71  1.1  mrg #define isxdigit(x) \
     72  1.1  mrg   ({__typeof(x) __is_x = (x); \
     73  1.1  mrg     (__is_x >= '0' && __is_x <= '9') \
     74  1.1  mrg     || ((x) >= 'A' && (x) <= 'F') \
     75  1.1  mrg     || ((x) >= 'a' && (x) <= 'f'); })
     76  1.1  mrg #define tolower(x) \
     77  1.1  mrg   ({__typeof(x) __is_x = (x); \
     78  1.1  mrg     (__is_x >= 'A' && __is_x <= 'Z') ? __is_x - 'A' + 'a' : __is_x; })
     79  1.1  mrg #endif
     80  1.1  mrg 
     81  1.1  mrg #ifndef CHAR_MAX
     82  1.1  mrg #ifdef __CHAR_UNSIGNED__
     83  1.1  mrg #define CHAR_MAX (2 * __SCHAR_MAX__ + 1)
     84  1.1  mrg #else
     85  1.1  mrg #define CHAR_MAX __SCHAR_MAX__
     86  1.1  mrg #endif
     87  1.1  mrg #endif
     88  1.1  mrg 
     89  1.1  mrg #ifndef HAVE_PRINTF_HOOKS
     90  1.1  mrg #define printf_info __quadmath_printf_info
     91  1.1  mrg struct printf_info
     92  1.1  mrg {
     93  1.1  mrg   int prec;			/* Precision.  */
     94  1.1  mrg   int width;			/* Width.  */
     95  1.1  mrg   wchar_t spec;			/* Format letter.  */
     96  1.1  mrg   unsigned int is_long_double:1;/* L flag.  */
     97  1.1  mrg   unsigned int is_short:1;	/* h flag.  */
     98  1.1  mrg   unsigned int is_long:1;	/* l flag.  */
     99  1.1  mrg   unsigned int alt:1;		/* # flag.  */
    100  1.1  mrg   unsigned int space:1;		/* Space flag.  */
    101  1.1  mrg   unsigned int left:1;		/* - flag.  */
    102  1.1  mrg   unsigned int showsign:1;	/* + flag.  */
    103  1.1  mrg   unsigned int group:1;		/* ' flag.  */
    104  1.1  mrg   unsigned int extra:1;		/* For special use.  */
    105  1.1  mrg   unsigned int is_char:1;	/* hh flag.  */
    106  1.1  mrg   unsigned int wide:1;		/* Nonzero for wide character streams.  */
    107  1.1  mrg   unsigned int i18n:1;		/* I flag.  */
    108  1.1  mrg   unsigned short int user;	/* Bits for user-installed modifiers.  */
    109  1.1  mrg   wchar_t pad;			/* Padding character.  */
    110  1.1  mrg };
    111  1.1  mrg #endif
    112  1.1  mrg 
    113  1.1  mrg struct __quadmath_printf_file
    114  1.1  mrg {
    115  1.1  mrg   FILE *fp;
    116  1.1  mrg   char *str;
    117  1.1  mrg   size_t size;
    118  1.1  mrg   size_t len;
    119  1.1  mrg   int file_p;
    120  1.1  mrg };
    121  1.1  mrg 
    122  1.1  mrg int
    123  1.1  mrg __quadmath_printf_fp (struct __quadmath_printf_file *fp,
    124  1.1  mrg 		      const struct printf_info *info,
    125  1.1  mrg 		      const void *const *args) attribute_hidden;
    126  1.1  mrg int
    127  1.1  mrg __quadmath_printf_fphex (struct __quadmath_printf_file *fp,
    128  1.1  mrg 			 const struct printf_info *info,
    129  1.1  mrg 			 const void *const *args) attribute_hidden;
    130  1.1  mrg 
    131  1.1  mrg size_t __quadmath_do_pad (struct __quadmath_printf_file *fp, int wide,
    132  1.1  mrg 			  int c, size_t n) attribute_hidden;
    133  1.1  mrg 
    134  1.1  mrg static inline __attribute__((__unused__)) size_t
    135  1.1  mrg __quadmath_do_put (struct __quadmath_printf_file *fp, int wide,
    136  1.1  mrg 		   const char *s, size_t n)
    137  1.1  mrg {
    138  1.1  mrg   size_t len;
    139  1.1  mrg   if (fp->file_p)
    140  1.1  mrg     {
    141  1.1  mrg       if (wide)
    142  1.1  mrg 	{
    143  1.1  mrg 	  size_t cnt;
    144  1.1  mrg 	  const wchar_t *ls = (const wchar_t *) s;
    145  1.1  mrg 	  for (cnt = 0; cnt < n; cnt++)
    146  1.1  mrg 	    if (putwc (ls[cnt], fp->fp) == WEOF)
    147  1.1  mrg 	      break;
    148  1.1  mrg 	  return cnt;
    149  1.1  mrg 	}
    150  1.1  mrg       return fwrite (s, 1, n, fp->fp);
    151  1.1  mrg     }
    152  1.1  mrg   len = MIN (fp->size, n);
    153  1.1  mrg   memcpy (fp->str, s, len);
    154  1.1  mrg   fp->str += len;
    155  1.1  mrg   fp->size -= len;
    156  1.1  mrg   fp->len += n;
    157  1.1  mrg   return n;
    158  1.1  mrg }
    159  1.1  mrg 
    160  1.1  mrg static inline __attribute__((__unused__)) int
    161  1.1  mrg __quadmath_do_putc (struct __quadmath_printf_file *fp, int wide,
    162  1.1  mrg 		    wchar_t c)
    163  1.1  mrg {
    164  1.1  mrg   if (fp->file_p)
    165  1.1  mrg     return wide ? (int) putwc (c, fp->fp) : putc (c, fp->fp);
    166  1.1  mrg   if (fp->size)
    167  1.1  mrg     {
    168  1.1  mrg       *(fp->str++) = c;
    169  1.1  mrg       fp->size--;
    170  1.1  mrg     }
    171  1.1  mrg   fp->len++;
    172  1.1  mrg   return (unsigned char) c;
    173  1.1  mrg }
    174  1.1  mrg 
    175  1.1  mrg #define PUT(f, s, n) __quadmath_do_put (f, wide, s, n)
    176  1.1  mrg #define PAD(f, c, n) __quadmath_do_pad (f, wide, c, n)
    177  1.1  mrg #define PUTC(c, f) __quadmath_do_putc (f, wide, c)
    178  1.1  mrg 
    179  1.1  mrg #define nl_langinfo_wc(x) \
    180  1.1  mrg   ({ union { const char *mb; wchar_t wc; } u; u.mb = nl_langinfo (x); u.wc; })
    181  1.1  mrg 
    182  1.1  mrg #undef _itoa
    183  1.1  mrg #define _itoa __quadmath_itoa
    184  1.1  mrg 
    185  1.1  mrg #undef NAN
    186  1.1  mrg #define NAN __builtin_nanf ("")
    187