xref: /haiku/src/system/libroot/posix/glibc/stdio-common/printf_size.c (revision 97dfeb96704e5dbc5bec32ad7b21379d0125e031)
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 <libioP.h>
23 
24 #include <stdio_private.h>
25 #include <ctype.h>
26 #include <ieee754.h>
27 #include <math.h>
28 #include <printf.h>
29 
30 
31 /* This defines make it possible to use the same code for GNU C library and
32    the GNU I/O library.	 */
33 #ifdef USE_IN_LIBIO
34 # define PUT(f, s, n) _IO_sputn (f, s, n)
35 # define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : INTUSE(_IO_padn) (f, c, n))
36 /* We use this file GNU C library and GNU I/O library.	So make
37    names equal.	 */
38 # undef putc
39 # define putc(c, f) (wide \
40 		     ? (int)_IO_putwc_unlocked (c, f) : _IO_putc_unlocked (c, f))
41 # define size_t	_IO_size_t
42 # define FILE	_IO_FILE
43 #else	/* ! USE_IN_LIBIO */
44 # define PUT(f, s, n) fwrite (s, 1, n, f)
45 # define PAD(f, c, n) __printf_pad (f, c, n)
46 ssize_t __printf_pad __P ((FILE *, char pad, int n)); /* In vfprintf.c.  */
47 #endif	/* USE_IN_LIBIO */
48 
49 /* Macros for doing the actual output.  */
50 
51 #define outchar(ch)							      \
52   do									      \
53     {									      \
54       register const int outc = (ch);					      \
55       if (putc (outc, fp) == EOF)					      \
56 	return -1;							      \
57       ++done;								      \
58     } while (0)
59 
60 #define PRINT(ptr, wptr, len)						      \
61   do									      \
62     {									      \
63       register size_t outlen = (len);					      \
64       if (len > 20)							      \
65 	{								      \
66 	  if (PUT (fp, wide ? (const char *) wptr : ptr, outlen) != outlen)   \
67 	    return -1;							      \
68 	  ptr += outlen;						      \
69 	  done += outlen;						      \
70 	}								      \
71       else								      \
72 	{								      \
73 	  if (wide)							      \
74 	    while (outlen-- > 0)					      \
75 	      outchar (*wptr++);					      \
76 	  else								      \
77 	    while (outlen-- > 0)					      \
78 	      outchar (*ptr++);						      \
79 	}								      \
80     } while (0)
81 
82 #define PADN(ch, len)							      \
83   do									      \
84     {									      \
85       if (PAD (fp, ch, len) != len)					      \
86 	return -1;							      \
87       done += len;							      \
88     }									      \
89   while (0)
90 
91 /* Prototype for helper functions.  */
92 extern int __printf_fp (FILE *fp, const struct printf_info *info,
93 			const void *const *args);
94 
95 
96 int
97 printf_size (FILE *fp, const struct printf_info *info, const void *const *args)
98 {
99   /* Units for the both formats.  */
100 #define BINARY_UNITS	" kmgtpezy"
101 #define DECIMAL_UNITS	" KMGTPEZY"
102   static const char units[2][sizeof (BINARY_UNITS)] =
103   {
104     BINARY_UNITS,	/* For binary format.  */
105     DECIMAL_UNITS	/* For decimal format.  */
106   };
107   const char *tag = units[isupper (info->spec) != 0];
108   int divisor = isupper (info->spec) ? 1000 : 1024;
109 
110   /* The floating-point value to output.  */
111   union
112     {
113       union ieee754_double dbl;
114       union ieee854_long_double ldbl;
115     }
116   fpnum;
117   const void *ptr = &fpnum;
118 
119   int negative = 0;
120 
121   /* "NaN" or "Inf" for the special cases.  */
122   const char *special = NULL;
123   const wchar_t *wspecial = NULL;
124 
125   struct printf_info fp_info;
126   int done = 0;
127   int wide = info->wide;
128 
129 
130   /* Fetch the argument value.	*/
131 #ifndef __NO_LONG_DOUBLE_MATH
132   if (info->is_long_double && sizeof (long double) > sizeof (double))
133     {
134       fpnum.ldbl.d = *(const long double *) args[0];
135 
136       /* Check for special values: not a number or infinity.  */
137       if (__isnanl (fpnum.ldbl.d))
138 	{
139 	  special = "nan";
140 	  wspecial = L"nan";
141 	  negative = 0;
142 	}
143       else if (__isinfl (fpnum.ldbl.d))
144 	{
145 	  special = "inf";
146 	  wspecial = L"inf";
147 
148 	  negative = fpnum.ldbl.d < 0;
149 	}
150       else
151 	while (fpnum.ldbl.d >= divisor && tag[1] != '\0')
152 	  {
153 	    fpnum.ldbl.d /= divisor;
154 	    ++tag;
155 	  }
156     }
157   else
158 #endif	/* no long double */
159     {
160       fpnum.dbl.d = *(const double *) args[0];
161 
162       /* Check for special values: not a number or infinity.  */
163       if (__isnan (fpnum.dbl.d))
164 	{
165 	  special = "nan";
166 	  wspecial = L"nan";
167 	  negative = 0;
168 	}
169       else if (__isinf (fpnum.dbl.d))
170 	{
171 	  special = "inf";
172 	  wspecial = L"inf";
173 
174 	  negative = fpnum.dbl.d < 0;
175 	}
176       else
177 	while (fpnum.dbl.d >= divisor && tag[1] != '\0')
178 	  {
179 	    fpnum.dbl.d /= divisor;
180 	    ++tag;
181 	  }
182     }
183 
184   if (special)
185     {
186       int width = info->prec > width ? info->prec : info->width;
187 
188       if (negative || info->showsign || info->space)
189 	--width;
190       width -= 3;
191 
192       if (!info->left && width > 0)
193 	PADN (' ', width);
194 
195       if (negative)
196 	outchar ('-');
197       else if (info->showsign)
198 	outchar ('+');
199       else if (info->space)
200 	outchar (' ');
201 
202       PRINT (special, wspecial, 3);
203 
204       if (info->left && width > 0)
205 	PADN (' ', width);
206 
207       return done;
208     }
209 
210   /* Prepare to print the number.  We want to use `__printf_fp' so we
211      have to prepare a `printf_info' structure.  */
212   fp_info.spec = 'f';
213   fp_info.prec = info->prec < 0 ? 3 : info->prec;
214   fp_info.is_long_double = info->is_long_double;
215   fp_info.is_short = info->is_short;
216   fp_info.is_long = info->is_long;
217   fp_info.alt = info->alt;
218   fp_info.space = info->space;
219   fp_info.left = info->left;
220   fp_info.showsign = info->showsign;
221   fp_info.group = info->group;
222   fp_info.extra = info->extra;
223   fp_info.pad = info->pad;
224   fp_info.wide = wide;
225 
226   if (fp_info.left && fp_info.pad == L' ')
227     {
228       /* We must do the padding ourself since the unit character must
229 	 be placed before the padding spaces.  */
230       fp_info.width = 0;
231 
232       done = __printf_fp (fp, &fp_info, &ptr);
233       if (done > 0)
234 	{
235 	  outchar (*tag);
236 	  if (info->width > done)
237 	    PADN (' ', info->width - done);
238 	}
239     }
240   else
241     {
242       /* We can let __printf_fp do all the printing and just add our
243 	 unit character afterwards.  */
244       fp_info.width = info->width - 1;
245 
246       done = __printf_fp (fp, &fp_info, &ptr);
247       if (done > 0)
248 	outchar (*tag);
249     }
250 
251   return done;
252 }
253 
254 /* This is the function used by `vfprintf' to determine number and
255    type of the arguments.  */
256 int
257 printf_size_info (const struct printf_info *info, size_t n, int *argtypes)
258 {
259   /* We need only one double or long double argument.  */
260   if (n >= 1)
261     argtypes[0] = PA_DOUBLE | (info->is_long_double ? PA_FLAG_LONG_DOUBLE : 0);
262 
263   return 1;
264 }
265