| 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. |
|---|
| 4 |
|
|---|
| 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) |
|---|
| 8 |
any later version. |
|---|
| 9 |
|
|---|
| 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. |
|---|
| 14 |
|
|---|
| 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, |
|---|
| 18 |
USA. */ |
|---|
| 19 |
|
|---|
| 20 |
#ifdef HAVE_CONFIG_H |
|---|
| 21 |
# include <config.h> |
|---|
| 22 |
#endif |
|---|
| 23 |
|
|---|
| 24 |
#ifdef __GNUC__ |
|---|
| 25 |
# define alloca __builtin_alloca |
|---|
| 26 |
# define HAVE_ALLOCA 1 |
|---|
| 27 |
#else |
|---|
| 28 |
# ifdef _MSC_VER |
|---|
| 29 |
# include <malloc.h> |
|---|
| 30 |
# define alloca _alloca |
|---|
| 31 |
# else |
|---|
| 32 |
# if defined HAVE_ALLOCA_H || defined _LIBC |
|---|
| 33 |
# include <alloca.h> |
|---|
| 34 |
# else |
|---|
| 35 |
# ifdef _AIX |
|---|
| 36 |
#pragma alloca |
|---|
| 37 |
# else |
|---|
| 38 |
# ifndef alloca |
|---|
| 39 |
char *alloca (); |
|---|
| 40 |
# endif |
|---|
| 41 |
# endif |
|---|
| 42 |
# endif |
|---|
| 43 |
# endif |
|---|
| 44 |
#endif |
|---|
| 45 |
|
|---|
| 46 |
#include <stdio.h> |
|---|
| 47 |
|
|---|
| 48 |
#if !HAVE_POSIX_PRINTF |
|---|
| 49 |
|
|---|
| 50 |
#include <stdlib.h> |
|---|
| 51 |
#include <string.h> |
|---|
| 52 |
|
|---|
| 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) |
|---|
| 58 |
#else |
|---|
| 59 |
# define DLL_EXPORTED |
|---|
| 60 |
#endif |
|---|
| 61 |
|
|---|
| 62 |
#define STATIC static |
|---|
| 63 |
|
|---|
| 64 |
/* Define auxiliary functions declared in "printf-args.h". */ |
|---|
| 65 |
#include "printf-args.c" |
|---|
| 66 |
|
|---|
| 67 |
/* Define auxiliary functions declared in "printf-parse.h". */ |
|---|
| 68 |
#include "printf-parse.c" |
|---|
| 69 |
|
|---|
| 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" |
|---|
| 76 |
#endif |
|---|
| 77 |
|
|---|
| 78 |
DLL_EXPORTED |
|---|
| 79 |
int |
|---|
| 80 |
libintl_vfprintf (FILE *stream, const char *format, va_list args) |
|---|
| 81 |
{ |
|---|
| 82 |
if (strchr (format, '$') == NULL) |
|---|
| 83 |
return vfprintf (stream, format, args); |
|---|
| 84 |
else |
|---|
| 85 |
{ |
|---|
| 86 |
size_t length; |
|---|
| 87 |
char *result = libintl_vasnprintf (NULL, &length, format, args); |
|---|
| 88 |
int retval = -1; |
|---|
| 89 |
if (result != NULL) |
|---|
| 90 |
{ |
|---|
| 91 |
if (fwrite (result, 1, length, stream) == length) |
|---|
| 92 |
retval = length; |
|---|
| 93 |
free (result); |
|---|
| 94 |
} |
|---|
| 95 |
return retval; |
|---|
| 96 |
} |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
DLL_EXPORTED |
|---|
| 100 |
int |
|---|
| 101 |
libintl_fprintf (FILE *stream, const char *format, ...) |
|---|
| 102 |
{ |
|---|
| 103 |
va_list args; |
|---|
| 104 |
int retval; |
|---|
| 105 |
|
|---|
| 106 |
va_start (args, format); |
|---|
| 107 |
retval = libintl_vfprintf (stream, format, args); |
|---|
| 108 |
va_end (args); |
|---|
| 109 |
return retval; |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
DLL_EXPORTED |
|---|
| 113 |
int |
|---|
| 114 |
libintl_vprintf (const char *format, va_list args) |
|---|
| 115 |
{ |
|---|
| 116 |
return libintl_vfprintf (stdout, format, args); |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
DLL_EXPORTED |
|---|
| 120 |
int |
|---|
| 121 |
libintl_printf (const char *format, ...) |
|---|
| 122 |
{ |
|---|
| 123 |
va_list args; |
|---|
| 124 |
int retval; |
|---|
| 125 |
|
|---|
| 126 |
va_start (args, format); |
|---|
| 127 |
retval = libintl_vprintf (format, args); |
|---|
| 128 |
va_end (args); |
|---|
| 129 |
return retval; |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
#if HAVE_SNPRINTF |
|---|
| 133 |
|
|---|
| 134 |
# if HAVE_DECL__SNPRINTF |
|---|
| 135 |
/* Windows. */ |
|---|
| 136 |
# define system_vsnprintf _vsnprintf |
|---|
| 137 |
# else |
|---|
| 138 |
/* Unix. */ |
|---|
| 139 |
# define system_vsnprintf vsnprintf |
|---|
| 140 |
# endif |
|---|
| 141 |
|
|---|
| 142 |
DLL_EXPORTED |
|---|
| 143 |
int |
|---|
| 144 |
libintl_vsnprintf (char *resultbuf, size_t length, const char *format, va_list args) |
|---|
| 145 |
{ |
|---|
| 146 |
if (strchr (format, '$') == NULL) |
|---|
| 147 |
return system_vsnprintf (resultbuf, length, format, args); |
|---|
| 148 |
else |
|---|
| 149 |
{ |
|---|
| 150 |
size_t maxlength = length; |
|---|
| 151 |
char *result = libintl_vasnprintf (resultbuf, &length, format, args); |
|---|
| 152 |
if (result != resultbuf) |
|---|
| 153 |
{ |
|---|
| 154 |
if (maxlength > 0) |
|---|
| 155 |
{ |
|---|
| 156 |
if (length < maxlength) |
|---|
| 157 |
abort (); |
|---|
| 158 |
memcpy (resultbuf, result, maxlength - 1); |
|---|
| 159 |
resultbuf[maxlength - 1] = '\0'; |
|---|
| 160 |
} |
|---|
| 161 |
free (result); |
|---|
| 162 |
return -1; |
|---|
| 163 |
} |
|---|
| 164 |
else |
|---|
| 165 |
return length; |
|---|
| 166 |
} |
|---|
| 167 |
} |
|---|
| 168 |
|
|---|
| 169 |
DLL_EXPORTED |
|---|
| 170 |
int |
|---|
| 171 |
libintl_snprintf (char *resultbuf, size_t length, const char *format, ...) |
|---|
| 172 |
{ |
|---|
| 173 |
va_list args; |
|---|
| 174 |
int retval; |
|---|
| 175 |
|
|---|
| 176 |
va_start (args, format); |
|---|
| 177 |
retval = libintl_vsnprintf (resultbuf, length, format, args); |
|---|
| 178 |
va_end (args); |
|---|
| 179 |
return retval; |
|---|
| 180 |
} |
|---|
| 181 |
|
|---|
| 182 |
#endif |
|---|
| 183 |
|
|---|
| 184 |
#if HAVE_ASPRINTF |
|---|
| 185 |
|
|---|
| 186 |
DLL_EXPORTED |
|---|
| 187 |
int |
|---|
| 188 |
libintl_vasprintf (char **resultp, const char *format, va_list args) |
|---|
| 189 |
{ |
|---|
| 190 |
size_t length; |
|---|
| 191 |
char *result = libintl_vasnprintf (NULL, &length, format, args); |
|---|
| 192 |
if (result == NULL) |
|---|
| 193 |
return -1; |
|---|
| 194 |
*resultp = result; |
|---|
| 195 |
return length; |
|---|
| 196 |
} |
|---|
| 197 |
|
|---|
| 198 |
DLL_EXPORTED |
|---|
| 199 |
int |
|---|
| 200 |
libintl_asprintf (char **resultp, const char *format, ...) |
|---|
| 201 |
{ |
|---|
| 202 |
va_list args; |
|---|
| 203 |
int retval; |
|---|
| 204 |
|
|---|
| 205 |
va_start (args, format); |
|---|
| 206 |
retval = libintl_vasprintf (resultp, format, args); |
|---|
| 207 |
va_end (args); |
|---|
| 208 |
return retval; |
|---|
| 209 |
} |
|---|
| 210 |
|
|---|
| 211 |
#endif |
|---|
| 212 |
|
|---|
| 213 |
#if HAVE_FWPRINTF |
|---|
| 214 |
|
|---|
| 215 |
#include <wchar.h> |
|---|
| 216 |
|
|---|
| 217 |
#define WIDE_CHAR_VERSION 1 |
|---|
| 218 |
|
|---|
| 219 |
/* Define auxiliary functions declared in "wprintf-parse.h". */ |
|---|
| 220 |
#include "printf-parse.c" |
|---|
| 221 |
|
|---|
| 222 |
/* Define functions declared in "vasnprintf.h". */ |
|---|
| 223 |
#define vasnwprintf libintl_vasnwprintf |
|---|
| 224 |
#include "vasnprintf.c" |
|---|
| 225 |
#if 0 /* not needed */ |
|---|
| 226 |
#define asnwprintf libintl_asnwprintf |
|---|
| 227 |
#include "asnprintf.c" |
|---|
| 228 |
#endif |
|---|
| 229 |
|
|---|
| 230 |
# if HAVE_DECL__SNWPRINTF |
|---|
| 231 |
/* Windows. */ |
|---|
| 232 |
# define system_vswprintf _vsnwprintf |
|---|
| 233 |
# else |
|---|
| 234 |
/* Unix. */ |
|---|
| 235 |
# define system_vswprintf vswprintf |
|---|
| 236 |
# endif |
|---|
| 237 |
|
|---|
| 238 |
DLL_EXPORTED |
|---|
| 239 |
int |
|---|
| 240 |
libintl_vfwprintf (FILE *stream, const wchar_t *format, va_list args) |
|---|
| 241 |
{ |
|---|
| 242 |
if (wcschr (format, '$') == NULL) |
|---|
| 243 |
return vfwprintf (stream, format, args); |
|---|
| 244 |
else |
|---|
| 245 |
{ |
|---|
| 246 |
size_t length; |
|---|
| 247 |
wchar_t *result = libintl_vasnwprintf (NULL, &length, format, args); |
|---|
| 248 |
int retval = -1; |
|---|
| 249 |
if (result != NULL) |
|---|
| 250 |
{ |
|---|
| 251 |
size_t i; |
|---|
| 252 |
for (i = 0; i < length; i++) |
|---|
| 253 |
if (fputwc (result[i], stream) == WEOF) |
|---|
| 254 |
break; |
|---|
| 255 |
if (i == length) |
|---|
| 256 |
retval = length; |
|---|
| 257 |
free (result); |
|---|
| 258 |
} |
|---|
| 259 |
return retval; |
|---|
| 260 |
} |
|---|
| 261 |
} |
|---|
| 262 |
|
|---|
| 263 |
DLL_EXPORTED |
|---|
| 264 |
int |
|---|
| 265 |
libintl_fwprintf (FILE *stream, const wchar_t *format, ...) |
|---|
| 266 |
{ |
|---|
| 267 |
va_list args; |
|---|
| 268 |
int retval; |
|---|
| 269 |
|
|---|
| 270 |
va_start (args, format); |
|---|
| 271 |
retval = libintl_vfwprintf (stream, format, args); |
|---|
| 272 |
va_end (args); |
|---|
| 273 |
return retval; |
|---|
| 274 |
} |
|---|
| 275 |
|
|---|
| 276 |
DLL_EXPORTED |
|---|
| 277 |
int |
|---|
| 278 |
libintl_vwprintf (const wchar_t *format, va_list args) |
|---|
| 279 |
{ |
|---|
| 280 |
return libintl_vfwprintf (stdout, format, args); |
|---|
| 281 |
} |
|---|
| 282 |
|
|---|
| 283 |
DLL_EXPORTED |
|---|
| 284 |
int |
|---|
| 285 |
libintl_wprintf (const wchar_t *format, ...) |
|---|
| 286 |
{ |
|---|
| 287 |
va_list args; |
|---|
| 288 |
int retval; |
|---|
| 289 |
|
|---|
| 290 |
va_start (args, format); |
|---|
| 291 |
retval = libintl_vwprintf (format, args); |
|---|
| 292 |
va_end (args); |
|---|
| 293 |
return retval; |
|---|
| 294 |
} |
|---|
| 295 |
|
|---|
| 296 |
DLL_EXPORTED |
|---|
| 297 |
int |
|---|
| 298 |
libintl_vswprintf (wchar_t *resultbuf, size_t length, const wchar_t *format, va_list args) |
|---|
| 299 |
{ |
|---|
| 300 |
if (wcschr (format, '$') == NULL) |
|---|
| 301 |
return system_vswprintf (resultbuf, length, format, args); |
|---|
| 302 |
else |
|---|
| 303 |
{ |
|---|
| 304 |
size_t maxlength = length; |
|---|
| 305 |
wchar_t *result = libintl_vasnwprintf (resultbuf, &length, format, args); |
|---|
| 306 |
if (result != resultbuf) |
|---|
| 307 |
{ |
|---|
| 308 |
if (maxlength > 0) |
|---|
| 309 |
{ |
|---|
| 310 |
if (length < maxlength) |
|---|
| 311 |
abort (); |
|---|
| 312 |
memcpy (resultbuf, result, (maxlength - 1) * sizeof (wchar_t)); |
|---|
| 313 |
resultbuf[maxlength - 1] = 0; |
|---|
| 314 |
} |
|---|
| 315 |
free (result); |
|---|
| 316 |
return -1; |
|---|
| 317 |
} |
|---|
| 318 |
else |
|---|
| 319 |
return length; |
|---|
| 320 |
} |
|---|
| 321 |
} |
|---|
| 322 |
|
|---|
| 323 |
DLL_EXPORTED |
|---|
| 324 |
int |
|---|
| 325 |
libintl_swprintf (wchar_t *resultbuf, size_t length, const wchar_t *format, ...) |
|---|
| 326 |
{ |
|---|
| 327 |
va_list args; |
|---|
| 328 |
int retval; |
|---|
| 329 |
|
|---|
| 330 |
va_start (args, format); |
|---|
| 331 |
retval = libintl_vswprintf (resultbuf, length, format, args); |
|---|
| 332 |
va_end (args); |
|---|
| 333 |
return retval; |
|---|
| 334 |
} |
|---|
| 335 |
|
|---|
| 336 |
#endif |
|---|
| 337 |
|
|---|
| 338 |
#endif |
|---|