1 /* Formatted output to strings, using POSIX/XSI format strings with positions.
2 Copyright (C) 2003 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 /* Define auxiliary functions declared in "printf-args.h". */
65 #include "printf-args.c"
67 /* Define auxiliary functions declared in "printf-parse.h". */
68 #include "printf-parse.c"
70 /* Define functions declared in "vasnprintf.h". */
71 #define vasnprintf libintl_vasnprintf
72 #include "vasnprintf.c"
73 #if 0 /* not needed */
74 #define asnprintf libintl_asnprintf
75 #include "asnprintf.c"
80 libintl_vfprintf (FILE *stream, const char *format, va_list args)
82 if (strchr (format, '$') == NULL)
83 return vfprintf (stream, format, args);
87 char *result = libintl_vasnprintf (NULL, &length, format, args);
91 if (fwrite (result, 1, length, stream) == length)
101 libintl_fprintf (FILE *stream, const char *format, ...)
106 va_start (args, format);
107 retval = libintl_vfprintf (stream, format, args);
114 libintl_vprintf (const char *format, va_list args)
116 return libintl_vfprintf (stdout, format, args);
121 libintl_printf (const char *format, ...)
126 va_start (args, format);
127 retval = libintl_vprintf (format, args);
134 libintl_vsprintf (char *resultbuf, const char *format, va_list args)
136 if (strchr (format, '$') == NULL)
137 return vsprintf (resultbuf, format, args);
140 size_t length = (size_t) ~0 / (4 * sizeof (char));
141 char *result = libintl_vasnprintf (resultbuf, &length, format, args);
142 if (result != resultbuf)
154 libintl_sprintf (char *resultbuf, const char *format, ...)
159 va_start (args, format);
160 retval = libintl_vsprintf (resultbuf, format, args);
167 # if HAVE_DECL__SNPRINTF
169 # define system_vsnprintf _vsnprintf
172 # define system_vsnprintf vsnprintf
177 libintl_vsnprintf (char *resultbuf, size_t length, const char *format, va_list args)
179 if (strchr (format, '$') == NULL)
180 return system_vsnprintf (resultbuf, length, format, args);
183 size_t maxlength = length;
184 char *result = libintl_vasnprintf (resultbuf, &length, format, args);
185 if (result != resultbuf)
189 if (length < maxlength)
191 memcpy (resultbuf, result, maxlength - 1);
192 resultbuf[maxlength - 1] = '\0';
204 libintl_snprintf (char *resultbuf, size_t length, const char *format, ...)
209 va_start (args, format);
210 retval = libintl_vsnprintf (resultbuf, length, format, args);
221 libintl_vasprintf (char **resultp, const char *format, va_list args)
224 char *result = libintl_vasnprintf (NULL, &length, format, args);
233 libintl_asprintf (char **resultp, const char *format, ...)
238 va_start (args, format);
239 retval = libintl_vasprintf (resultp, format, args);
250 #define WIDE_CHAR_VERSION 1
252 /* Define auxiliary functions declared in "wprintf-parse.h". */
253 #include "printf-parse.c"
255 /* Define functions declared in "vasnprintf.h". */
256 #define vasnwprintf libintl_vasnwprintf
257 #include "vasnprintf.c"
258 #if 0 /* not needed */
259 #define asnwprintf libintl_asnwprintf
260 #include "asnprintf.c"
263 # if HAVE_DECL__SNWPRINTF
265 # define system_vswprintf _vsnwprintf
268 # define system_vswprintf vswprintf
273 libintl_vfwprintf (FILE *stream, const wchar_t *format, va_list args)
275 if (wcschr (format, '$') == NULL)
276 return vfwprintf (stream, format, args);
280 wchar_t *result = libintl_vasnwprintf (NULL, &length, format, args);
285 for (i = 0; i < length; i++)
286 if (fputwc (result[i], stream) == WEOF)
298 libintl_fwprintf (FILE *stream, const wchar_t *format, ...)
303 va_start (args, format);
304 retval = libintl_vfwprintf (stream, format, args);
311 libintl_vwprintf (const wchar_t *format, va_list args)
313 return libintl_vfwprintf (stdout, format, args);
318 libintl_wprintf (const wchar_t *format, ...)
323 va_start (args, format);
324 retval = libintl_vwprintf (format, args);
331 libintl_vswprintf (wchar_t *resultbuf, size_t length, const wchar_t *format, va_list args)
333 if (wcschr (format, '$') == NULL)
334 return system_vswprintf (resultbuf, length, format, args);
337 size_t maxlength = length;
338 wchar_t *result = libintl_vasnwprintf (resultbuf, &length, format, args);
339 if (result != resultbuf)
343 if (length < maxlength)
345 memcpy (resultbuf, result, (maxlength - 1) * sizeof (wchar_t));
346 resultbuf[maxlength - 1] = 0;
358 libintl_swprintf (wchar_t *resultbuf, size_t length, const wchar_t *format, ...)
363 va_start (args, format);
364 retval = libintl_vswprintf (resultbuf, length, format, args);