OSDN Git Service

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