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 */
32 #include "mpfr-impl.h"
34 #ifdef _MPFR_H_HAVE_FILE
36 /* Each printf-like function calls mpfr_vasprintf which
37 - returns the number of characters in the returned string excluding the
39 - returns -1 and sets the erange flag if the number of produced characters
40 exceeds INT_MAX (in that case, also sets errno to EOVERFLOW in POSIX
43 #define GET_STR_VA(sz, str, fmt, ap) \
46 sz = mpfr_vasprintf (&(str), fmt, ap); \
50 mpfr_free_str (str); \
55 #define GET_STR(sz, str, fmt) \
60 sz = mpfr_vasprintf (&(str), fmt, ap); \
65 mpfr_free_str (str); \
71 mpfr_printf (const char *fmt
, ...)
76 GET_STR (ret
, str
, fmt
);
77 ret
= printf ("%s", str
);
84 mpfr_vprintf (const char *fmt
, va_list ap
)
89 GET_STR_VA (ret
, str
, fmt
, ap
);
90 ret
= printf ("%s", str
);
98 mpfr_fprintf (FILE *fp
, const char *fmt
, ...)
103 GET_STR (ret
, str
, fmt
);
104 ret
= fprintf (fp
, "%s", str
);
111 mpfr_vfprintf (FILE *fp
, const char *fmt
, va_list ap
)
116 GET_STR_VA (ret
, str
, fmt
, ap
);
117 ret
= fprintf (fp
, "%s", str
);
122 #endif /* _MPFR_H_HAVE_FILE */
125 mpfr_sprintf (char *buf
, const char *fmt
, ...)
130 GET_STR (ret
, str
, fmt
);
131 ret
= sprintf (buf
, "%s", str
);
138 mpfr_vsprintf (char *buf
, const char *fmt
, va_list ap
)
143 GET_STR_VA (ret
, str
, fmt
, ap
);
144 ret
= sprintf (buf
, "%s", str
);
151 mpfr_snprintf (char *buf
, size_t size
, const char *fmt
, ...)
157 GET_STR (ret
, str
, fmt
);
159 /* C99 allows SIZE to be zero */
162 MPFR_ASSERTN (buf
!= NULL
);
163 min_size
= (size_t)ret
< size
? (size_t)ret
: size
- 1;
164 strncpy (buf
, str
, min_size
);
165 buf
[min_size
] = '\0';
173 mpfr_vsnprintf (char *buf
, size_t size
, const char *fmt
, va_list ap
)
179 GET_STR_VA (ret
, str
, fmt
, ap
);
181 /* C99 allows SIZE to be zero */
184 MPFR_ASSERTN (buf
!= NULL
);
185 min_size
= (size_t)ret
< size
? (size_t)ret
: size
- 1;
186 strncpy (buf
, str
, min_size
);
187 buf
[min_size
] = '\0';
195 mpfr_asprintf (char **pp
, const char *fmt
, ...)
199 GET_STR (ret
, *pp
, fmt
);
203 #endif /* HAVE_STDARG */