1 /* mpfr_printf -- printf function and friends.
3 Copyright 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel 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 3 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.LESSER. If not, see
20 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
27 /* The mpfr_printf-like functions are defined only if <stdarg.h> exists */
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 */
44 #include "mpfr-impl.h"
46 #ifdef _MPFR_H_HAVE_FILE
48 /* Each printf-like function calls mpfr_vasprintf which
49 - returns the number of characters in the returned string excluding the
51 - returns -1 and sets the erange flag if the number of produced characters
52 exceeds INT_MAX (in that case, also sets errno to EOVERFLOW in POSIX
55 #define GET_STR_VA(sz, str, fmt, ap) \
58 sz = mpfr_vasprintf (&(str), fmt, ap); \
62 mpfr_free_str (str); \
67 #define GET_STR(sz, str, fmt) \
72 sz = mpfr_vasprintf (&(str), fmt, ap); \
77 mpfr_free_str (str); \
83 mpfr_printf (const char *fmt, ...)
88 GET_STR (ret, str, fmt);
89 ret = printf ("%s", str);
96 mpfr_vprintf (const char *fmt, va_list ap)
101 GET_STR_VA (ret, str, fmt, ap);
102 ret = printf ("%s", str);
110 mpfr_fprintf (FILE *fp, const char *fmt, ...)
115 GET_STR (ret, str, fmt);
116 ret = fprintf (fp, "%s", str);
123 mpfr_vfprintf (FILE *fp, const char *fmt, va_list ap)
128 GET_STR_VA (ret, str, fmt, ap);
129 ret = fprintf (fp, "%s", str);
134 #endif /* _MPFR_H_HAVE_FILE */
137 mpfr_sprintf (char *buf, const char *fmt, ...)
142 GET_STR (ret, str, fmt);
143 ret = sprintf (buf, "%s", str);
150 mpfr_vsprintf (char *buf, const char *fmt, va_list ap)
155 GET_STR_VA (ret, str, fmt, ap);
156 ret = sprintf (buf, "%s", str);
163 mpfr_snprintf (char *buf, size_t size, const char *fmt, ...)
169 GET_STR (ret, str, fmt);
171 /* C99 allows SIZE to be zero */
174 MPFR_ASSERTN (buf != NULL);
175 min_size = (size_t)ret < size ? (size_t)ret : size - 1;
176 strncpy (buf, str, min_size);
177 buf[min_size] = '\0';
185 mpfr_vsnprintf (char *buf, size_t size, const char *fmt, va_list ap)
191 GET_STR_VA (ret, str, fmt, ap);
193 /* C99 allows SIZE to be zero */
196 MPFR_ASSERTN (buf != NULL);
197 min_size = (size_t)ret < size ? (size_t)ret : size - 1;
198 strncpy (buf, str, min_size);
199 buf[min_size] = '\0';
207 mpfr_asprintf (char **pp, const char *fmt, ...)
211 GET_STR (ret, *pp, fmt);
215 #endif /* HAVE_STDARG */