Update.
[glibc.git] / stdio-common / printf_size.c
blob1eaccdf027fa73de7624de6df7089666d0d94b6c
1 /* Print size value using units for orders of magnitude.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5 Based on a proposal by Larry McVoy <lm@sgi.com>.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include <ctype.h>
23 #include <ieee754.h>
24 #include <math.h>
25 #include <printf.h>
26 #ifdef USE_IN_LIBIO
27 # include <libioP.h>
28 #else
29 # include <stdio.h>
30 #endif
33 /* This defines make it possible to use the same code for GNU C library and
34 the GNU I/O library. */
35 #ifdef USE_IN_LIBIO
36 # define PUT(f, s, n) _IO_sputn (f, s, n)
37 # define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : _IO_padn (f, c, n))
38 /* We use this file GNU C library and GNU I/O library. So make
39 names equal. */
40 # undef putc
41 # define putc(c, f) (wide \
42 ? (int)_IO_putwc_unlocked (c, f) : _IO_putc_unlocked (c, f))
43 # define size_t _IO_size_t
44 # define FILE _IO_FILE
45 #else /* ! USE_IN_LIBIO */
46 # define PUT(f, s, n) fwrite (s, 1, n, f)
47 # define PAD(f, c, n) __printf_pad (f, c, n)
48 ssize_t __printf_pad __P ((FILE *, char pad, int n)); /* In vfprintf.c. */
49 #endif /* USE_IN_LIBIO */
51 /* Macros for doing the actual output. */
53 #define outchar(ch) \
54 do \
55 { \
56 register const int outc = (ch); \
57 if (putc (outc, fp) == EOF) \
58 return -1; \
59 ++done; \
60 } while (0)
62 #define PRINT(ptr, wptr, len) \
63 do \
64 { \
65 register size_t outlen = (len); \
66 if (len > 20) \
67 { \
68 if (PUT (fp, wide ? (const char *) wptr : ptr, outlen) != outlen) \
69 return -1; \
70 ptr += outlen; \
71 done += outlen; \
72 } \
73 else \
74 { \
75 if (wide) \
76 while (outlen-- > 0) \
77 outchar (*wptr++); \
78 else \
79 while (outlen-- > 0) \
80 outchar (*ptr++); \
81 } \
82 } while (0)
84 #define PADN(ch, len) \
85 do \
86 { \
87 if (PAD (fp, ch, len) != len) \
88 return -1; \
89 done += len; \
90 } \
91 while (0)
93 /* Prototype for helper functions. */
94 extern int __printf_fp (FILE *fp, const struct printf_info *info,
95 const void *const *args);
98 int
99 printf_size (FILE *fp, const struct printf_info *info, const void *const *args)
101 /* Units for the both formats. */
102 #define BINARY_UNITS " kmgtpezy"
103 #define DECIMAL_UNITS " KMGTPEZY"
104 static const char units[2][sizeof (BINARY_UNITS)] =
106 BINARY_UNITS, /* For binary format. */
107 DECIMAL_UNITS /* For decimal format. */
109 const char *tag = units[isupper (info->spec) != 0];
110 int divisor = isupper (info->spec) ? 1000 : 1024;
112 /* The floating-point value to output. */
113 union
115 union ieee754_double dbl;
116 union ieee854_long_double ldbl;
118 fpnum;
119 const void *ptr = &fpnum;
121 int negative = 0;
123 /* "NaN" or "Inf" for the special cases. */
124 const char *special = NULL;
125 const wchar_t *wspecial = NULL;
127 struct printf_info fp_info;
128 int done = 0;
129 int wide = info->wide;
132 /* Fetch the argument value. */
133 #ifndef __NO_LONG_DOUBLE_MATH
134 if (info->is_long_double && sizeof (long double) > sizeof (double))
136 fpnum.ldbl.d = *(const long double *) args[0];
138 /* Check for special values: not a number or infinity. */
139 if (__isnanl (fpnum.ldbl.d))
141 special = "nan";
142 wspecial = L"nan";
143 negative = 0;
145 else if (__isinfl (fpnum.ldbl.d))
147 special = "inf";
148 wspecial = L"inf";
150 negative = fpnum.ldbl.d < 0;
152 else
153 while (fpnum.ldbl.d >= divisor && tag[1] != '\0')
155 fpnum.ldbl.d /= divisor;
156 ++tag;
159 else
160 #endif /* no long double */
162 fpnum.dbl.d = *(const double *) args[0];
164 /* Check for special values: not a number or infinity. */
165 if (__isnan (fpnum.dbl.d))
167 special = "nan";
168 wspecial = L"nan";
169 negative = 0;
171 else if (__isinf (fpnum.dbl.d))
173 special = "inf";
174 wspecial = L"inf";
176 negative = fpnum.dbl.d < 0;
178 else
179 while (fpnum.dbl.d >= divisor && tag[1] != '\0')
181 fpnum.dbl.d /= divisor;
182 ++tag;
186 if (special)
188 int width = info->prec > width ? info->prec : width;
190 if (negative || info->showsign || info->space)
191 --width;
192 width -= 3;
194 if (!info->left && width > 0)
195 PADN (' ', width);
197 if (negative)
198 outchar ('-');
199 else if (info->showsign)
200 outchar ('+');
201 else if (info->space)
202 outchar (' ');
204 PRINT (special, wspecial, 3);
206 if (info->left && width > 0)
207 PADN (' ', width);
209 return done;
212 /* Prepare to print the number. We want to use `__printf_fp' so we
213 have to prepare a `printf_info' structure. */
214 fp_info.spec = 'f';
215 fp_info.prec = info->prec < 0 ? 3 : info->prec;
216 fp_info.is_long_double = info->is_long_double;
217 fp_info.is_short = info->is_short;
218 fp_info.is_long = info->is_long;
219 fp_info.alt = info->alt;
220 fp_info.space = info->space;
221 fp_info.left = info->left;
222 fp_info.showsign = info->showsign;
223 fp_info.group = info->group;
224 fp_info.extra = info->extra;
225 fp_info.pad = info->pad;
226 fp_info.wide = wide;
228 if (fp_info.left && fp_info.pad == L' ')
230 /* We must do the padding ourself since the unit character must
231 be placed before the padding spaces. */
232 fp_info.width = 0;
234 done = __printf_fp (fp, &fp_info, &ptr);
235 if (done > 0)
237 outchar (*tag);
238 if (info->width > done)
239 PADN (' ', info->width - done);
242 else
244 /* We can let __printf_fp do all the printing and just add our
245 unit character afterwards. */
246 fp_info.width = info->width - 1;
248 done = __printf_fp (fp, &fp_info, &ptr);
249 if (done > 0)
250 outchar (*tag);
253 return done;
256 /* This is the function used by `vfprintf' to determine number and
257 type of the arguments. */
259 printf_size_info (const struct printf_info *info, size_t n, int *argtypes)
261 /* We need only one double or long double argument. */
262 if (n >= 1)
263 argtypes[0] = PA_DOUBLE | (info->is_long_double ? PA_FLAG_LONG_DOUBLE : 0);
265 return 1;