1 /* Formatted output to strings, using POSIX/XSI format strings with positions.
2 Copyright (C) 2003, 2006 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2003.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Library General Public License as published
7 by the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
25 # define alloca __builtin_alloca
26 # define HAVE_ALLOCA 1
30 # define alloca _alloca
32 # if defined HAVE_ALLOCA_H || defined _LIBC
48 #if !HAVE_POSIX_PRINTF
53 /* When building a DLL, we must export some functions. Note that because
54 the functions are only defined for binary backward compatibility, we
55 don't need to use __declspec(dllimport) in any case. */
56 #if defined _MSC_VER && BUILDING_DLL
57 # define DLL_EXPORTED __declspec(dllexport)
64 /* This needs to be consistent with libgnuintl.h.in. */
65 #if defined __NetBSD__ || defined __CYGWIN__ || defined __MINGW32__
66 /* Don't break __attribute__((format(printf,M,N))).
67 This redefinition is only possible because the libc in NetBSD, Cygwin,
68 mingw does not have a function __printf__. */
69 # define libintl_printf __printf__
72 /* Define auxiliary functions declared in "printf-args.h". */
73 #include "printf-args.c"
75 /* Define auxiliary functions declared in "printf-parse.h". */
76 #include "printf-parse.c"
78 /* Define functions declared in "vasnprintf.h". */
79 #define vasnprintf libintl_vasnprintf
80 #include "vasnprintf.c"
81 #if 0 /* not needed */
82 #define asnprintf libintl_asnprintf
83 #include "asnprintf.c"
88 libintl_vfprintf (FILE *stream, const char *format, va_list args)
90 if (strchr (format, '$') == NULL)
91 return vfprintf (stream, format, args);
95 char *result = libintl_vasnprintf (NULL, &length, format, args);
99 if (fwrite (result, 1, length, stream) == length)
109 libintl_fprintf (FILE *stream, const char *format, ...)
114 va_start (args, format);
115 retval = libintl_vfprintf (stream, format, args);
122 libintl_vprintf (const char *format, va_list args)
124 return libintl_vfprintf (stdout, format, args);
129 libintl_printf (const char *format, ...)
134 va_start (args, format);
135 retval = libintl_vprintf (format, args);
142 libintl_vsprintf (char *resultbuf, const char *format, va_list args)
144 if (strchr (format, '$') == NULL)
145 return vsprintf (resultbuf, format, args);
148 size_t length = (size_t) ~0 / (4 * sizeof (char));
149 char *result = libintl_vasnprintf (resultbuf, &length, format, args);
150 if (result != resultbuf)
162 libintl_sprintf (char *resultbuf, const char *format, ...)
167 va_start (args, format);
168 retval = libintl_vsprintf (resultbuf, format, args);
175 # if HAVE_DECL__SNPRINTF
177 # define system_vsnprintf _vsnprintf
180 # define system_vsnprintf vsnprintf
185 libintl_vsnprintf (char *resultbuf, size_t length, const char *format, va_list args)
187 if (strchr (format, '$') == NULL)
188 return system_vsnprintf (resultbuf, length, format, args);
191 size_t maxlength = length;
192 char *result = libintl_vasnprintf (resultbuf, &length, format, args);
193 if (result != resultbuf)
197 if (length < maxlength)
199 memcpy (resultbuf, result, maxlength - 1);
200 resultbuf[maxlength - 1] = '\0';
212 libintl_snprintf (char *resultbuf, size_t length, const char *format, ...)
217 va_start (args, format);
218 retval = libintl_vsnprintf (resultbuf, length, format, args);
229 libintl_vasprintf (char **resultp, const char *format, va_list args)
232 char *result = libintl_vasnprintf (NULL, &length, format, args);
241 libintl_asprintf (char **resultp, const char *format, ...)
246 va_start (args, format);
247 retval = libintl_vasprintf (resultp, format, args);
258 #define WIDE_CHAR_VERSION 1
260 /* Define auxiliary functions declared in "wprintf-parse.h". */
261 #include "printf-parse.c"
263 /* Define functions declared in "vasnprintf.h". */
264 #define vasnwprintf libintl_vasnwprintf
265 #include "vasnprintf.c"
266 #if 0 /* not needed */
267 #define asnwprintf libintl_asnwprintf
268 #include "asnprintf.c"
271 # if HAVE_DECL__SNWPRINTF
273 # define system_vswprintf _vsnwprintf
276 # define system_vswprintf vswprintf
281 libintl_vfwprintf (FILE *stream, const wchar_t *format, va_list args)
283 if (wcschr (format, '$') == NULL)
284 return vfwprintf (stream, format, args);
288 wchar_t *result = libintl_vasnwprintf (NULL, &length, format, args);
293 for (i = 0; i < length; i++)
294 if (fputwc (result[i], stream) == WEOF)
306 libintl_fwprintf (FILE *stream, const wchar_t *format, ...)
311 va_start (args, format);
312 retval = libintl_vfwprintf (stream, format, args);
319 libintl_vwprintf (const wchar_t *format, va_list args)
321 return libintl_vfwprintf (stdout, format, args);
326 libintl_wprintf (const wchar_t *format, ...)
331 va_start (args, format);
332 retval = libintl_vwprintf (format, args);
339 libintl_vswprintf (wchar_t *resultbuf, size_t length, const wchar_t *format, va_list args)
341 if (wcschr (format, '$') == NULL)
342 return system_vswprintf (resultbuf, length, format, args);
345 size_t maxlength = length;
346 wchar_t *result = libintl_vasnwprintf (resultbuf, &length, format, args);
347 if (result != resultbuf)
351 if (length < maxlength)
353 memcpy (resultbuf, result, (maxlength - 1) * sizeof (wchar_t));
354 resultbuf[maxlength - 1] = 0;
366 libintl_swprintf (wchar_t *resultbuf, size_t length, const wchar_t *format, ...)
371 va_start (args, format);
372 retval = libintl_vswprintf (resultbuf, length, format, args);