1 /* __gmp_doprnt -- printf style formatted output.
3 THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY. THEY'RE ALMOST
4 CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
5 FUTURE GNU MP RELEASES.
7 Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
9 This file is part of the GNU MP Library.
11 The GNU MP Library is free software; you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
16 The GNU MP Library is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
19 License for more details.
21 You should have received a copy of the GNU Lesser General Public License
22 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
24 #define _GNU_SOURCE /* for DECIMAL_POINT in glibc langinfo.h */
34 #include <ctype.h> /* for isdigit */
35 #include <stddef.h> /* for ptrdiff_t */
37 #include <stdio.h> /* for NULL */
41 # include <inttypes.h> /* for intmax_t */
49 #include <langinfo.h> /* for nl_langinfo */
53 #include <locale.h> /* for localeconv */
57 #include <sys/types.h> /* for quad_t */
64 /* change this to "#define TRACE(x) x" for diagnostics */
68 /* Should be portable, but in any case this is only used under some ASSERTs. */
69 #define va_equal(x, y) \
70 (memcmp (&(x), &(y), sizeof(va_list)) == 0)
73 /* printf is convenient because it allows various types to be printed in one
74 fairly compact call, so having gmp_printf support the standard types as
75 well as the gmp ones is important. This ends up meaning all the standard
76 parsing must be duplicated, to get a new routine recognising the gmp
79 With the currently favoured handling of mpz etc as Z, Q and F type
80 markers, it's not possible to use glibc register_printf_function since
81 that only accepts new conversion characters, not new types. If Z was a
82 conversion there'd be no way to specify hex, decimal or octal, or
83 similarly with F no way to specify fixed point or scientific format.
85 It seems wisest to pass conversions %f, %e and %g of float, double and
86 long double over to the standard printf. It'd be hard to be sure of
87 getting the right handling for NaNs, rounding, etc. Integer conversions
88 %d etc and string conversions %s on the other hand could be easily enough
89 handled within gmp_doprnt, but if floats are going to libc then it's just
90 as easy to send all non-gmp types there.
92 "Z" was a type marker for size_t in old glibc, but there seems no need to
93 provide access to that now "z" is standard.
95 In GMP 4.1.1 we documented "ll" and "L" as being equivalent, but in C99
96 in fact "ll" is just for long long and "L" just for long double.
97 Apparentely GLIBC allows "L" for long long though. This doesn't affect
98 us as such, since both are passed through to the C library. To be
99 consistent with what we said before, the two are treated equivalently
100 here, and it's left to the C library to do what it thinks with them.
104 "b" might be nice for binary output, and could even be supported for the
105 standard C types too if desired.
107 POSIX style "%n$" parameter numbering would be possible, but would need
108 to be handled completely within gmp_doprnt, since the numbering will be
109 all different once the format string it cut into pieces.
111 Some options for mpq formatting would be good. Perhaps a non-zero
112 precision field could give a width for the denominator and mean always
113 put a "/". A form "n+p/q" might interesting too, though perhaps that's
114 better left to applications.
116 Right now there's no way for an application to know whether types like
117 intmax_t are supported here. If configure is doing its job and the same
118 compiler is used for gmp as for the application then there shouldn't be
119 any problem, but perhaps gmp.h should have some preprocessor symbols to
120 say what libgmp can do. */
124 /* If a gmp format is the very first thing or there are two gmp formats with
125 nothing in between then we'll reach here with this_fmt == last_fmt and we
126 can do nothing in that case.
128 last_ap is always replaced after a FLUSH, so it doesn't matter if va_list
129 is a call-by-reference and the funs->format routine modifies it. */
133 if (this_fmt == last_fmt) \
135 TRACE (printf ("nothing to flush\n")); \
136 ASSERT (va_equal (this_ap, last_ap)); \
140 ASSERT (*this_fmt == '%'); \
142 TRACE (printf ("flush \"%s\"\n", last_fmt)); \
143 DOPRNT_FORMAT (last_fmt, last_ap); \
148 /* Parse up the given format string and do the appropriate output using the
149 given "funs" routines. The data parameter is passed through to those
153 __gmp_doprnt (const struct doprnt_funs_t
*funs
, void *data
,
154 const char *orig_fmt
, va_list orig_ap
)
156 va_list ap
, this_ap
, last_ap
;
157 size_t alloc_fmt_size
;
158 char *fmt
, *alloc_fmt
, *last_fmt
, *this_fmt
, *gmp_str
;
160 int type
, fchar
, *value
, seen_precision
;
161 struct doprnt_params_t param
;
163 TRACE (printf ("gmp_doprnt \"%s\"\n", orig_fmt
));
165 /* Don't modify orig_ap, if va_list is actually an array and hence call by
166 reference. It could be argued that it'd be more efficient to leave the
167 caller to make a copy if it cared, but doing so here is going to be a
168 very small part of the total work, and we may as well keep applications
170 va_copy (ap
, orig_ap
);
172 /* The format string is chopped up into pieces to be passed to
173 funs->format. Unfortunately that means it has to be copied so each
174 piece can be null-terminated. We're not going to be very fast here, so
175 use __gmp_allocate_func rather than TMP_ALLOC, to avoid overflowing the
176 stack if a long output string is given. */
177 alloc_fmt_size
= strlen (orig_fmt
) + 1;
179 /* for a long long limb we change %Mx to %llx, so could need an extra 1
180 char for every 3 existing */
181 alloc_fmt_size
+= alloc_fmt_size
/ 3;
183 alloc_fmt
= __GMP_ALLOCATE_FUNC_TYPE (alloc_fmt_size
, char);
185 strcpy (fmt
, orig_fmt
);
187 /* last_fmt and last_ap are just after the last output, and hence where
188 the next output will begin, when that's done */
190 va_copy (last_ap
, ap
);
194 TRACE (printf ("next: \"%s\"\n", fmt
));
196 fmt
= strchr (fmt
, '%');
200 /* this_fmt and this_ap are the current '%' sequence being considered */
202 va_copy (this_ap
, ap
);
203 fmt
++; /* skip the '%' */
205 TRACE (printf ("considering\n");
206 printf (" last: \"%s\"\n", last_fmt
);
207 printf (" this: \"%s\"\n", this_fmt
));
210 value
= ¶m
.width
;
214 param
.expfmt
= "e%c%02d";
217 param
.justify
= DOPRNT_JUSTIFY_RIGHT
;
219 param
.showbase
= DOPRNT_SHOWBASE_NO
;
221 param
.showtrailing
= 1;
226 /* This loop parses a single % sequence. "break" from the switch
227 means continue with this %, "goto next" means the conversion
228 character has been seen and a new % should be sought. */
238 /* %a behaves like %e, but defaults to all significant digits,
239 and there's no leading zeros on the exponent (which is in
242 param
.expfmt
= "p%c%d";
246 param
.expfmt
= "P%c%d";
248 param
.conv
= DOPRNT_CONV_SCIENTIFIC
;
250 if (! seen_precision
)
251 param
.prec
= -1; /* default to all digits */
252 param
.showbase
= DOPRNT_SHOWBASE_YES
;
253 param
.showtrailing
= 1;
257 /* Let's assume wchar_t will be promoted to "int" in the call,
258 the same as char will be. */
259 (void) va_arg (ap
, int);
266 TRACE (printf ("integer, base=%d\n", param
.base
));
267 if (! seen_precision
)
271 /* Let's assume uintmax_t is the same size as intmax_t. */
273 (void) va_arg (ap
, intmax_t);
275 ASSERT_FAIL (intmax_t not available
);
279 (void) va_arg (ap
, long);
283 (void) va_arg (ap
, long long);
285 ASSERT_FAIL (long long not available
);
291 mp_size_t xsize
, abs_xsize
;
294 xp
= va_arg (ap
, mp_ptr
);
296 xsize
= (int) va_arg (ap
, mp_size_t
);
297 abs_xsize
= ABS (xsize
);
298 MPN_NORMALIZE (xp
, abs_xsize
);
299 SIZ(z
) = (xsize
>= 0 ? abs_xsize
: -abs_xsize
);
300 ASSERT_CODE (ALLOC(z
) = abs_xsize
);
301 gmp_str
= mpz_get_str (NULL
, param
.base
, z
);
306 /* quad_t is probably the same as long long, but let's treat
307 it separately just to be sure. Also let's assume u_quad_t
308 will be the same size as quad_t. */
310 (void) va_arg (ap
, quad_t
);
312 ASSERT_FAIL (quad_t
not available
);
317 gmp_str
= mpq_get_str (NULL
, param
.base
, va_arg(ap
, mpq_srcptr
));
321 (void) va_arg (ap
, ptrdiff_t);
323 ASSERT_FAIL (ptrdiff_t not available
);
327 (void) va_arg (ap
, size_t);
333 gmp_str
= mpz_get_str (NULL
, param
.base
,
334 va_arg (ap
, mpz_srcptr
));
336 ret
= __gmp_doprnt_integer (funs
, data
, ¶m
, gmp_str
);
337 (*__gmp_free_func
) (gmp_str
, strlen(gmp_str
)+1);
338 DOPRNT_ACCUMULATE (ret
);
339 va_copy (last_ap
, ap
);
344 /* default is an "int", and this includes h=short and hh=char
345 since they're promoted to int in a function call */
346 (void) va_arg (ap
, int);
353 param
.expfmt
= "E%c%02d";
356 param
.conv
= DOPRNT_CONV_SCIENTIFIC
;
358 if (param
.showbase
== DOPRNT_SHOWBASE_NONZERO
)
360 /* # in %e, %f and %g */
362 param
.showtrailing
= 1;
368 DOPRNT_ACCUMULATE (__gmp_doprnt_mpf (funs
, data
, ¶m
,
370 va_arg (ap
, mpf_srcptr
)));
371 va_copy (last_ap
, ap
);
376 (void) va_arg (ap
, long double);
378 ASSERT_FAIL (long double not available
);
382 (void) va_arg (ap
, double);
388 param
.conv
= DOPRNT_CONV_FIXED
;
391 case 'F': /* mpf_t */
392 case 'j': /* intmax_t */
393 case 'L': /* long long */
395 case 'q': /* quad_t */
396 case 'Q': /* mpq_t */
397 case 't': /* ptrdiff_t */
398 case 'z': /* size_t */
399 case 'Z': /* mpz_t */
406 param
.expfmt
= "E%c%02d";
409 param
.conv
= DOPRNT_CONV_GENERAL
;
410 param
.showtrailing
= 0;
416 type
= 'H'; /* internal code for "hh" */
422 type
= 'L'; /* "ll" means "L" */
426 /* glibc strerror(errno), no argument */
429 case 'M': /* mp_limb_t */
430 /* mung format string to l or ll and let plain printf handle it */
432 memmove (fmt
+1, fmt
, strlen (fmt
)+1);
447 p
= va_arg (ap
, void *);
449 case '\0': * (int *) p
= retval
; break;
450 case 'F': mpf_set_si ((mpf_ptr
) p
, (long) retval
); break;
451 case 'H': * (char *) p
= retval
; break;
452 case 'h': * (short *) p
= retval
; break;
454 case 'j': * (intmax_t *) p
= retval
; break;
456 case 'j': ASSERT_FAIL (intmax_t not available
); break;
458 case 'l': * (long *) p
= retval
; break;
459 #if HAVE_QUAD_T && HAVE_LONG_LONG
461 ASSERT_ALWAYS (sizeof (quad_t
) == sizeof (long long));
464 case 'q': ASSERT_FAIL (quad_t
not available
); break;
467 case 'L': * (long long *) p
= retval
; break;
469 case 'L': ASSERT_FAIL (long long not available
); break;
474 n
= va_arg (ap
, mp_size_t
);
478 * (mp_ptr
) p
= retval
;
479 MPN_ZERO ((mp_ptr
) p
+ 1, n
- 1);
483 case 'Q': mpq_set_si ((mpq_ptr
) p
, (long) retval
, 1L); break;
485 case 't': * (ptrdiff_t *) p
= retval
; break;
487 case 't': ASSERT_FAIL (ptrdiff_t not available
); break;
489 case 'z': * (size_t *) p
= retval
; break;
490 case 'Z': mpz_set_si ((mpz_ptr
) p
, (long) retval
); break;
493 va_copy (last_ap
, ap
);
503 /* "void *" will be good enough for "char *" or "wchar_t *", no
504 need for separate code. */
505 (void) va_arg (ap
, const void *);
519 param
.showbase
= DOPRNT_SHOWBASE_NONZERO
;
523 /* glibc digit grouping, just pass it through, no support for it
533 param
.justify
= DOPRNT_JUSTIFY_LEFT
;
537 param
.prec
= -1; /* "." alone means all necessary digits */
543 int n
= va_arg (ap
, int);
545 if (value
== ¶m
.width
)
547 /* negative width means left justify */
550 param
.justify
= DOPRNT_JUSTIFY_LEFT
;
557 /* don't allow negative precision */
558 param
.prec
= MAX (0, n
);
564 if (value
== ¶m
.width
)
566 /* in width field, set fill */
569 /* for right justify, put the fill after any minus sign */
570 if (param
.justify
== DOPRNT_JUSTIFY_RIGHT
)
571 param
.justify
= DOPRNT_JUSTIFY_INTERNAL
;
575 /* in precision field, set value */
580 case '1': case '2': case '3': case '4': case '5':
581 case '6': case '7': case '8': case '9':
582 /* process all digits to form a value */
586 n
= n
* 10 + (fchar
-'0');
588 } while (isascii (fchar
) && isdigit (fchar
));
589 fmt
--; /* unget the non-digit */
595 /* something invalid */
602 /* Stop parsing the current "%" format, look for a new one. */
606 TRACE (printf ("remainder: \"%s\"\n", last_fmt
));
607 if (*last_fmt
!= '\0')
608 DOPRNT_FORMAT (last_fmt
, last_ap
);
610 if (funs
->final
!= NULL
)
611 if ((*funs
->final
) (data
) == -1)
615 (*__gmp_free_func
) (alloc_fmt
, alloc_fmt_size
);