1 /* mpfr_printf -- printf function and friends.
3 Copyright 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Cacao projects, INRIA.
6 This file is part of the GNU MPFR Library.
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or (at your
11 option) any later version.
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 MA 02110-1301, USA. */
27 /* The mpfr_printf-like functions are defined only if stdarg.h exist */
33 # ifdef HAVE___VA_COPY
34 # define va_copy(dst,src) __va_copy(dst, src)
36 /* autoconf manual advocates this fallback.
37 This is also the solution chosen by gmp */
38 # define va_copy(dst,src) \
39 do { memcpy(&(dst), &(src), sizeof(va_list)); } while (0)
40 # endif /* HAVE___VA_COPY */
41 #endif /* HAVE_VA_COPY */
45 #include "mpfr-impl.h"
47 #ifdef _MPFR_H_HAVE_FILE
49 /* Each printf-like function calls mpfr_vasprintf which
50 - returns the number of characters in the returned string excluding the
52 - returns -1 and sets the erange flag if the number of produced characters
53 exceeds INT_MAX (in that case, also sets errno to EOVERFLOW in POSIX
56 #define GET_STR_VA(sz, str, fmt, ap) \
59 sz = mpfr_vasprintf (&(str), fmt, ap); \
63 mpfr_free_str (str); \
68 #define GET_STR(sz, str, fmt) \
73 sz = mpfr_vasprintf (&(str), fmt, ap); \
78 mpfr_free_str (str); \
84 mpfr_printf (const char *fmt, ...)
89 GET_STR (ret, str, fmt);
90 ret = printf ("%s", str);
97 mpfr_vprintf (const char *fmt, va_list ap)
102 GET_STR_VA (ret, str, fmt, ap);
103 ret = printf ("%s", str);
111 mpfr_fprintf (FILE *fp, const char *fmt, ...)
116 GET_STR (ret, str, fmt);
117 ret = fprintf (fp, "%s", str);
124 mpfr_vfprintf (FILE *fp, const char *fmt, va_list ap)
129 GET_STR_VA (ret, str, fmt, ap);
130 ret = fprintf (fp, "%s", str);
135 #endif /* _MPFR_H_HAVE_FILE */
138 mpfr_sprintf (char *buf, const char *fmt, ...)
143 GET_STR (ret, str, fmt);
144 ret = sprintf (buf, "%s", str);
151 mpfr_vsprintf (char *buf, const char *fmt, va_list ap)
156 GET_STR_VA (ret, str, fmt, ap);
157 ret = sprintf (buf, "%s", str);
164 mpfr_snprintf (char *buf, size_t size, const char *fmt, ...)
170 GET_STR (ret, str, fmt);
172 /* C99 allows SIZE to be zero */
175 MPFR_ASSERTN (buf != NULL);
176 min_size = (size_t)ret < size ? (size_t)ret : size - 1;
177 strncpy (buf, str, min_size);
178 buf[min_size] = '\0';
186 mpfr_vsnprintf (char *buf, size_t size, const char *fmt, va_list ap)
192 GET_STR_VA (ret, str, fmt, ap);
194 /* C99 allows SIZE to be zero */
197 MPFR_ASSERTN (buf != NULL);
198 min_size = (size_t)ret < size ? (size_t)ret : size - 1;
199 strncpy (buf, str, min_size);
200 buf[min_size] = '\0';
208 mpfr_asprintf (char **pp, const char *fmt, ...)
212 GET_STR (ret, *pp, fmt);
216 #endif /* HAVE_STDARG */