]> git.ipfire.org Git - thirdparty/glibc.git/blob - stdio-common/printf_size.c
Update.
[thirdparty/glibc.git] / stdio-common / printf_size.c
1 /* Print size value using units for orders of magnitude.
2 Copyright (C) 1997,1998,1999,2000,2001,2002 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>.
6
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
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 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
21
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
31
32
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) : INTUSE(_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 */
50 \f
51 /* Macros for doing the actual output. */
52
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)
61
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)
83
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)
92 \f
93 /* Prototype for helper functions. */
94 extern int __printf_fp (FILE *fp, const struct printf_info *info,
95 const void *const *args);
96
97 \f
98 int
99 printf_size (FILE *fp, const struct printf_info *info, const void *const *args)
100 {
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)] =
105 {
106 BINARY_UNITS, /* For binary format. */
107 DECIMAL_UNITS /* For decimal format. */
108 };
109 const char *tag = units[isupper (info->spec) != 0];
110 int divisor = isupper (info->spec) ? 1000 : 1024;
111
112 /* The floating-point value to output. */
113 union
114 {
115 union ieee754_double dbl;
116 union ieee854_long_double ldbl;
117 }
118 fpnum;
119 const void *ptr = &fpnum;
120
121 int negative = 0;
122
123 /* "NaN" or "Inf" for the special cases. */
124 const char *special = NULL;
125 const wchar_t *wspecial = NULL;
126
127 struct printf_info fp_info;
128 int done = 0;
129 int wide = info->wide;
130
131
132 /* Fetch the argument value. */
133 #ifndef __NO_LONG_DOUBLE_MATH
134 if (info->is_long_double && sizeof (long double) > sizeof (double))
135 {
136 fpnum.ldbl.d = *(const long double *) args[0];
137
138 /* Check for special values: not a number or infinity. */
139 if (__isnanl (fpnum.ldbl.d))
140 {
141 special = "nan";
142 wspecial = L"nan";
143 negative = 0;
144 }
145 else if (__isinfl (fpnum.ldbl.d))
146 {
147 special = "inf";
148 wspecial = L"inf";
149
150 negative = fpnum.ldbl.d < 0;
151 }
152 else
153 while (fpnum.ldbl.d >= divisor && tag[1] != '\0')
154 {
155 fpnum.ldbl.d /= divisor;
156 ++tag;
157 }
158 }
159 else
160 #endif /* no long double */
161 {
162 fpnum.dbl.d = *(const double *) args[0];
163
164 /* Check for special values: not a number or infinity. */
165 if (__isnan (fpnum.dbl.d))
166 {
167 special = "nan";
168 wspecial = L"nan";
169 negative = 0;
170 }
171 else if (__isinf (fpnum.dbl.d))
172 {
173 special = "inf";
174 wspecial = L"inf";
175
176 negative = fpnum.dbl.d < 0;
177 }
178 else
179 while (fpnum.dbl.d >= divisor && tag[1] != '\0')
180 {
181 fpnum.dbl.d /= divisor;
182 ++tag;
183 }
184 }
185
186 if (special)
187 {
188 int width = info->prec > width ? info->prec : width;
189
190 if (negative || info->showsign || info->space)
191 --width;
192 width -= 3;
193
194 if (!info->left && width > 0)
195 PADN (' ', width);
196
197 if (negative)
198 outchar ('-');
199 else if (info->showsign)
200 outchar ('+');
201 else if (info->space)
202 outchar (' ');
203
204 PRINT (special, wspecial, 3);
205
206 if (info->left && width > 0)
207 PADN (' ', width);
208
209 return done;
210 }
211
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;
227
228 if (fp_info.left && fp_info.pad == L' ')
229 {
230 /* We must do the padding ourself since the unit character must
231 be placed before the padding spaces. */
232 fp_info.width = 0;
233
234 done = __printf_fp (fp, &fp_info, &ptr);
235 if (done > 0)
236 {
237 outchar (*tag);
238 if (info->width > done)
239 PADN (' ', info->width - done);
240 }
241 }
242 else
243 {
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;
247
248 done = __printf_fp (fp, &fp_info, &ptr);
249 if (done > 0)
250 outchar (*tag);
251 }
252
253 return done;
254 }
255 \f
256 /* This is the function used by `vfprintf' to determine number and
257 type of the arguments. */
258 int
259 printf_size_info (const struct printf_info *info, size_t n, int *argtypes)
260 {
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);
264
265 return 1;
266 }