1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/wxcrtvararg.h
3 // Purpose: Type-safe ANSI and Unicode builds compatible wrappers for
4 // printf(), scanf() and related CRT functions
5 // Author: Joel Farley, Ove Kåven
6 // Modified by: Vadim Zeitlin, Robert Roebling, Ron Lee
7 // Created: 2007-02-19
8 // Copyright: (c) 2007 REA Elektronik GmbH
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_WXCRTVARARG_H_
13 #define _WX_WXCRTVARARG_H_
14
15 // NB: User code should include wx/crt.h instead of including this
16 // header directly.
17
18 #include "wx/wxcrt.h"
19 #include "wx/strvararg.h"
20
21 #include "wx/string.h"
22
23 // ----------------------------------------------------------------------------
24 // CRT functions aliases
25 // ----------------------------------------------------------------------------
26
27 /* Required for wxPrintf() etc */
28 #include <stdarg.h>
29
30 /* printf() family saga */
31
32 /*
33 For many old Unix systems [v]snprintf()/vsscanf() exists in the system
34 libraries but not in the headers, so we need to declare it ourselves to be
35 able to use it.
36 */
37 #ifdef __UNIX__
38
39 #if defined(HAVE_VSNPRINTF) && !defined(HAVE_VSNPRINTF_DECL)
40 #ifdef __cplusplus
41 extern "C"
42 #else
43 extern
44 #endif
45 int vsnprintf(char *str, size_t size, const char *format, va_list ap);
46 #endif /* !HAVE_VSNPRINTF_DECL */
47
48 #if defined(HAVE_SNPRINTF) && !defined(HAVE_SNPRINTF_DECL)
49 #ifdef __cplusplus
50 extern "C"
51 #else
52 extern
53 #endif
54 int snprintf(char *str, size_t size, const char *format, ...);
55 #endif /* !HAVE_SNPRINTF_DECL */
56
57 #if defined(HAVE_VSSCANF) && !defined(HAVE_VSSCANF_DECL)
58 #ifdef __cplusplus
59 extern "C"
60 #else
61 extern
62 #endif
63 int vsscanf(const char *str, const char *format, va_list ap);
64 #endif /* !HAVE_VSSCANF_DECL */
65
66 /* Wrapper for vsnprintf if it's 3rd parameter is non-const. Note: the
67 * same isn't done for snprintf below, the builtin wxSnprintf_ is used
68 * instead since it's already a simple wrapper */
69 #if defined __cplusplus && defined HAVE_BROKEN_VSNPRINTF_DECL
wx_fixed_vsnprintf(char * str,size_t size,const char * format,va_list ap)70 inline int wx_fixed_vsnprintf(char *str, size_t size, const char *format, va_list ap)
71 {
72 return vsnprintf(str, size, (char*)format, ap);
73 }
74 #endif
75
76 #endif /* __UNIX__ */
77
78 /*
79 mingw32 normally uses MSVCRT which has non-standard vswprintf() and so
80 normally _vsnwprintf() is used instead, the only exception is when mingw32
81 is used with STLPort which does have a standard vswprintf() starting from
82 version 5.1 which we can use.
83 */
84 #ifdef __MINGW32__
85 #if defined(_STLPORT_VERSION) && _STLPORT_VERSION >= 0x510
86 #ifndef HAVE_VSWPRINTF
87 #define HAVE_VSWPRINTF
88 #endif
89 #elif defined(HAVE_VSWPRINTF)
90 /* can't use non-standard vswprintf() */
91 #undef HAVE_VSWPRINTF
92 #endif
93 #endif /* __MINGW32__ */
94
95 #if wxUSE_PRINTF_POS_PARAMS
96 /*
97 The systems where vsnprintf() supports positional parameters should
98 define the HAVE_UNIX98_PRINTF symbol.
99
100 On systems which don't (e.g. Windows) we are forced to use
101 our wxVsnprintf() implementation.
102 */
103 #if defined(HAVE_UNIX98_PRINTF)
104 #ifdef HAVE_VSWPRINTF
105 #define wxCRT_VsnprintfW vswprintf
106 #endif
107 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
108 #define wxCRT_VsnprintfA wx_fixed_vsnprintf
109 #else
110 #define wxCRT_VsnprintfA vsnprintf
111 #endif
112 #else /* !HAVE_UNIX98_PRINTF */
113 /*
114 The only compiler with positional parameters support under Windows
115 is VC++ 8.0 which provides a new xxprintf_p() functions family.
116 The 2003 PSDK includes a slightly earlier version of VC8 than the
117 main release and does not have the printf_p functions.
118 */
119 #if defined _MSC_FULL_VER && _MSC_FULL_VER >= 140050727
120 #define wxCRT_VsnprintfA _vsprintf_p
121 #define wxCRT_VsnprintfW _vswprintf_p
122 #endif
123 #endif /* HAVE_UNIX98_PRINTF/!HAVE_UNIX98_PRINTF */
124 #else /* !wxUSE_PRINTF_POS_PARAMS */
125 /*
126 We always want to define safe snprintf() function to be used instead of
127 sprintf(). Some compilers already have it (or rather vsnprintf() which
128 we really need...), otherwise we implement it using our own printf()
129 code.
130
131 We define function with a trailing underscore here because the real one
132 is a wrapper around it as explained below
133 */
134
135 #if defined(__VISUALC__)
136 #define wxCRT_VsnprintfA _vsnprintf
137 #define wxCRT_VsnprintfW _vsnwprintf
138 #else
139 #if defined(HAVE__VSNWPRINTF)
140 #define wxCRT_VsnprintfW _vsnwprintf
141 #elif defined(HAVE_VSWPRINTF)
142 #define wxCRT_VsnprintfW vswprintf
143 #endif
144
145 #if defined(HAVE_VSNPRINTF)
146 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
147 #define wxCRT_VsnprintfA wx_fixed_vsnprintf
148 #else
149 #define wxCRT_VsnprintfA vsnprintf
150 #endif
151 #endif
152 #endif
153 #endif /* wxUSE_PRINTF_POS_PARAMS/!wxUSE_PRINTF_POS_PARAMS */
154
155 #ifndef wxCRT_VsnprintfW
156 /* no (suitable) vsnprintf(), cook our own */
157 WXDLLIMPEXP_BASE int
158 wxCRT_VsnprintfW(wchar_t *buf, size_t len, const wchar_t *format, va_list argptr);
159 #define wxUSE_WXVSNPRINTFW 1
160 #else
161 #define wxUSE_WXVSNPRINTFW 0
162 #endif
163
164 #ifndef wxCRT_VsnprintfA
165 /* no (suitable) vsnprintf(), cook our own */
166 WXDLLIMPEXP_BASE int
167 wxCRT_VsnprintfA(char *buf, size_t len, const char *format, va_list argptr);
168 #define wxUSE_WXVSNPRINTFA 1
169 #else
170 #define wxUSE_WXVSNPRINTFA 0
171 #endif
172
173 // for wxString code, define wxUSE_WXVSNPRINTF to indicate that wx
174 // implementation is used no matter what (in UTF-8 build, either *A or *W
175 // version may be called):
176 #if !wxUSE_UNICODE
177 #define wxUSE_WXVSNPRINTF wxUSE_WXVSNPRINTFA
178 #elif wxUSE_UNICODE_WCHAR
179 #define wxUSE_WXVSNPRINTF wxUSE_WXVSNPRINTFW
180 #elif wxUSE_UTF8_LOCALE_ONLY
181 #define wxUSE_WXVSNPRINTF wxUSE_WXVSNPRINTFA
182 #else // UTF-8 under any locale
183 #define wxUSE_WXVSNPRINTF (wxUSE_WXVSNPRINTFA && wxUSE_WXVSNPRINTFW)
184 #endif
185
186 #define wxCRT_FprintfA fprintf
187 #define wxCRT_PrintfA printf
188 #define wxCRT_VfprintfA vfprintf
189 #define wxCRT_VprintfA vprintf
190 #define wxCRT_VsprintfA vsprintf
191
192 /*
193 In Unicode mode we need to have all standard functions such as wprintf() and
194 so on but not all systems have them so use our own implementations in this
195 case.
196 */
197 #if !defined(wxHAVE_TCHAR_SUPPORT) && !defined(HAVE_WPRINTF)
198 #define wxNEED_WPRINTF
199 #endif
200 #if !defined(wxHAVE_TCHAR_SUPPORT) && !defined(HAVE_VSWSCANF) && defined(HAVE_VSSCANF)
201 #define wxNEED_VSWSCANF
202 #endif
203
204
205 #if defined(wxNEED_WPRINTF)
206 /*
207 we need to implement all wide character printf functions either because
208 we don't have them at all or because they don't have the semantics we
209 need
210 */
211 int wxCRT_PrintfW( const wchar_t *format, ... );
212 int wxCRT_FprintfW( FILE *stream, const wchar_t *format, ... );
213 int wxCRT_VfprintfW( FILE *stream, const wchar_t *format, va_list ap );
214 int wxCRT_VprintfW( const wchar_t *format, va_list ap );
215 int wxCRT_VsprintfW( wchar_t *str, const wchar_t *format, va_list ap );
216 #else /* !wxNEED_WPRINTF */
217 #define wxCRT_FprintfW fwprintf
218 #define wxCRT_PrintfW wprintf
219 #define wxCRT_VfprintfW vfwprintf
220 #define wxCRT_VprintfW vwprintf
221
222 #if defined(__WINDOWS__) && !defined(HAVE_VSWPRINTF)
223 // only non-standard vswprintf() without buffer size argument can be used here
224 #define wxCRT_VsprintfW vswprintf
225 #endif
226 #endif /* wxNEED_WPRINTF */
227
228
229 /* Required for wxScanf() etc. */
230 #define wxCRT_ScanfA scanf
231 #define wxCRT_SscanfA sscanf
232 #define wxCRT_FscanfA fscanf
233
234 /* vsscanf() may have a wrong declaration with non-const first parameter, fix
235 * this by wrapping it if necessary. */
236 #if defined __cplusplus && defined HAVE_BROKEN_VSSCANF_DECL
wxCRT_VsscanfA(const char * str,const char * format,va_list ap)237 inline int wxCRT_VsscanfA(const char *str, const char *format, va_list ap)
238 {
239 return vsscanf(const_cast<char *>(str), format, ap);
240 }
241 #else
242 wxDECL_FOR_STRICT_MINGW32(int, vsscanf, (const char*, const char*, va_list))
243
244 #define wxCRT_VsscanfA vsscanf
245 #endif
246
247 #if defined(wxNEED_WPRINTF)
248 int wxCRT_ScanfW(const wchar_t *format, ...);
249 int wxCRT_SscanfW(const wchar_t *str, const wchar_t *format, ...);
250 int wxCRT_FscanfW(FILE *stream, const wchar_t *format, ...);
251 #else
252 #define wxCRT_ScanfW wxVMS_USE_STD wscanf
253 #define wxCRT_SscanfW wxVMS_USE_STD swscanf
254 #define wxCRT_FscanfW wxVMS_USE_STD fwscanf
255 #endif
256 #ifdef wxNEED_VSWSCANF
257 int wxCRT_VsscanfW(const wchar_t *str, const wchar_t *format, va_list ap);
258 #else
259 wxDECL_FOR_STRICT_MINGW32(int, vswscanf, (const wchar_t*, const wchar_t*, va_list))
260
261 #define wxCRT_VsscanfW wxVMS_USE_STD vswscanf
262 #endif
263
264 // ----------------------------------------------------------------------------
265 // user-friendly wrappers to CRT functions
266 // ----------------------------------------------------------------------------
267
268 // FIXME-UTF8: remove this
269 #if wxUSE_UNICODE
270 #define wxCRT_PrintfNative wxCRT_PrintfW
271 #define wxCRT_FprintfNative wxCRT_FprintfW
272 #else
273 #define wxCRT_PrintfNative wxCRT_PrintfA
274 #define wxCRT_FprintfNative wxCRT_FprintfA
275 #endif
276
277
278 wxGCC_ONLY_WARNING_SUPPRESS(format-nonliteral)
279
280 WX_DEFINE_VARARG_FUNC_SANS_N0(int, wxPrintf, 1, (const wxFormatString&),
281 wxCRT_PrintfNative, wxCRT_PrintfA)
wxPrintf(const wxFormatString & s)282 inline int wxPrintf(const wxFormatString& s)
283 {
284 return wxPrintf(wxASCII_STR("%s"), s.InputAsString());
285 }
286
287 WX_DEFINE_VARARG_FUNC_SANS_N0(int, wxFprintf, 2, (FILE*, const wxFormatString&),
288 wxCRT_FprintfNative, wxCRT_FprintfA)
wxFprintf(FILE * f,const wxFormatString & s)289 inline int wxFprintf(FILE *f, const wxFormatString& s)
290 {
291 return wxFprintf(f, wxASCII_STR("%s"), s.InputAsString());
292 }
293
294 wxGCC_ONLY_WARNING_RESTORE(format-nonliteral)
295
296 // va_list versions of printf functions simply forward to the respective
297 // CRT function; note that they assume that va_list was created using
298 // wxArgNormalizer<T>!
299 #if wxUSE_UNICODE_UTF8
300 #if wxUSE_UTF8_LOCALE_ONLY
301 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
302 return implA args
303 #else
304 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
305 if ( wxLocaleIsUtf8 ) return implA args; \
306 else return implW args
307 #endif
308 #elif wxUSE_UNICODE_WCHAR
309 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
310 return implW args
311 #else // ANSI
312 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
313 return implA args
314 #endif
315
316 inline int
wxVprintf(const wxString & format,va_list ap)317 wxVprintf(const wxString& format, va_list ap)
318 {
319 WX_VARARG_VFOO_IMPL((wxFormatString(format), ap),
320 wxCRT_VprintfW, wxCRT_VprintfA);
321 }
322
323 inline int
wxVfprintf(FILE * f,const wxString & format,va_list ap)324 wxVfprintf(FILE *f, const wxString& format, va_list ap)
325 {
326 WX_VARARG_VFOO_IMPL((f, wxFormatString(format), ap),
327 wxCRT_VfprintfW, wxCRT_VfprintfA);
328 }
329
330 #undef WX_VARARG_VFOO_IMPL
331
332
333 // wxSprintf() and friends have to be implemented in two forms, one for
334 // writing to char* buffer and one for writing to wchar_t*:
335
336 #if !wxUSE_UTF8_LOCALE_ONLY
337 int WXDLLIMPEXP_BASE wxDoSprintfWchar(char *str, const wxChar *format, ...);
338 #endif
339 #if wxUSE_UNICODE_UTF8
340 int WXDLLIMPEXP_BASE wxDoSprintfUtf8(char *str, const char *format, ...);
341 #endif
342 WX_DEFINE_VARARG_FUNC(int, wxSprintf, 2, (char*, const wxFormatString&),
343 wxDoSprintfWchar, wxDoSprintfUtf8)
344
345 int WXDLLIMPEXP_BASE
346 wxVsprintf(char *str, const wxString& format, va_list argptr);
347
348 #if !wxUSE_UTF8_LOCALE_ONLY
349 int WXDLLIMPEXP_BASE wxDoSnprintfWchar(char *str, size_t size, const wxChar *format, ...);
350 #endif
351 #if wxUSE_UNICODE_UTF8
352 int WXDLLIMPEXP_BASE wxDoSnprintfUtf8(char *str, size_t size, const char *format, ...);
353 #endif
354 WX_DEFINE_VARARG_FUNC(int, wxSnprintf, 3, (char*, size_t, const wxFormatString&),
355 wxDoSnprintfWchar, wxDoSnprintfUtf8)
356
357 int WXDLLIMPEXP_BASE
358 wxVsnprintf(char *str, size_t size, const wxString& format, va_list argptr);
359
360 #if wxUSE_UNICODE
361
362 #if !wxUSE_UTF8_LOCALE_ONLY
363 int WXDLLIMPEXP_BASE wxDoSprintfWchar(wchar_t *str, const wxChar *format, ...);
364 #endif
365 #if wxUSE_UNICODE_UTF8
366 int WXDLLIMPEXP_BASE wxDoSprintfUtf8(wchar_t *str, const char *format, ...);
367 #endif
368 WX_DEFINE_VARARG_FUNC(int, wxSprintf, 2, (wchar_t*, const wxFormatString&),
369 wxDoSprintfWchar, wxDoSprintfUtf8)
370
371 int WXDLLIMPEXP_BASE
372 wxVsprintf(wchar_t *str, const wxString& format, va_list argptr);
373
374 #if !wxUSE_UTF8_LOCALE_ONLY
375 int WXDLLIMPEXP_BASE wxDoSnprintfWchar(wchar_t *str, size_t size, const wxChar *format, ...);
376 #endif
377 #if wxUSE_UNICODE_UTF8
378 int WXDLLIMPEXP_BASE wxDoSnprintfUtf8(wchar_t *str, size_t size, const char *format, ...);
379 #endif
380 WX_DEFINE_VARARG_FUNC(int, wxSnprintf, 3, (wchar_t*, size_t, const wxFormatString&),
381 wxDoSnprintfWchar, wxDoSnprintfUtf8)
382
383 int WXDLLIMPEXP_BASE
384 wxVsnprintf(wchar_t *str, size_t size, const wxString& format, va_list argptr);
385
386 #endif // wxUSE_UNICODE
387
388 // We can't use wxArgNormalizer<T> for variadic arguments to wxScanf() etc.
389 // because they are writable, so instead of providing friendly template
390 // vararg-like functions, we just provide both char* and wchar_t* variants
391 // of these functions. The type of output variadic arguments for %s must match
392 // the type of 'str' and 'format' arguments.
393 //
394 // For compatibility with earlier wx versions, we also provide wxSscanf()
395 // version with the first argument (input string) wxString; for this version,
396 // the type of output string values is determined by the type of format string
397 // only.
398
399 #define _WX_SCANFUNC_EXTRACT_ARGS_1(x) x
400 #define _WX_SCANFUNC_EXTRACT_ARGS_2(x,y) x, y
401 #define _WX_SCANFUNC_EXTRACT_ARGS(N, args) _WX_SCANFUNC_EXTRACT_ARGS_##N args
402
403 #define _WX_VARARG_PASS_WRITABLE(i) a##i
404
405 #define _WX_DEFINE_SCANFUNC(N, dummy1, name, impl, passfixed, numfixed, fixed)\
406 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
407 int name(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, fixed), \
408 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
409 { \
410 return impl(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, passfixed), \
411 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WRITABLE)); \
412 }
413
414 #define WX_DEFINE_SCANFUNC(name, numfixed, fixed, impl, passfixed) \
415 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
416 _WX_DEFINE_SCANFUNC, \
417 dummy1, name, impl, passfixed, numfixed, fixed)
418
419 // this is needed to normalize the format string, see src/common/strvararg.cpp
420 // for more details
421 #ifdef __WINDOWS__
422 #define wxScanfConvertFormatW(fmt) fmt
423 #else
424 const wxScopedWCharBuffer
425 WXDLLIMPEXP_BASE wxScanfConvertFormatW(const wchar_t *format);
426 #endif
427
428 WX_DEFINE_SCANFUNC(wxScanf, 1, (const char *format),
429 wxCRT_ScanfA, (format))
430 WX_DEFINE_SCANFUNC(wxScanf, 1, (const wchar_t *format),
431 wxCRT_ScanfW, (wxScanfConvertFormatW(format)))
432
433 WX_DEFINE_SCANFUNC(wxFscanf, 2, (FILE *stream, const char *format),
434 wxCRT_FscanfA, (stream, format))
435 WX_DEFINE_SCANFUNC(wxFscanf, 2, (FILE *stream, const wchar_t *format),
436 wxCRT_FscanfW, (stream, wxScanfConvertFormatW(format)))
437
438 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const char *str, const char *format),
439 wxCRT_SscanfA, (str, format))
440 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wchar_t *str, const wchar_t *format),
441 wxCRT_SscanfW, (str, wxScanfConvertFormatW(format)))
442 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxScopedCharBuffer& str, const char *format),
443 wxCRT_SscanfA, (str.data(), format))
444 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxScopedWCharBuffer& str, const wchar_t *format),
445 wxCRT_SscanfW, (str.data(), wxScanfConvertFormatW(format)))
446 #ifndef wxNO_IMPLICIT_WXSTRING_ENCODING
447 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxString& str, const char *format),
448 wxCRT_SscanfA, (str.mb_str(), format))
449 #endif
450 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxString& str, const wchar_t *format),
451 wxCRT_SscanfW, (str.wc_str(), wxScanfConvertFormatW(format)))
452 #ifndef wxNO_IMPLICIT_WXSTRING_ENCODING
453 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxCStrData& str, const char *format),
454 wxCRT_SscanfA, (str.AsCharBuf(), format))
455 #endif
456 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxCStrData& str, const wchar_t *format),
457 wxCRT_SscanfW, (str.AsWCharBuf(), wxScanfConvertFormatW(format)))
458
459 // Visual C++ doesn't provide vsscanf()
460 #ifndef __VISUALC___
461 int WXDLLIMPEXP_BASE wxVsscanf(const char *str, const char *format, va_list ap);
462 int WXDLLIMPEXP_BASE wxVsscanf(const wchar_t *str, const wchar_t *format, va_list ap);
463 int WXDLLIMPEXP_BASE wxVsscanf(const wxScopedCharBuffer& str, const char *format, va_list ap);
464 int WXDLLIMPEXP_BASE wxVsscanf(const wxScopedWCharBuffer& str, const wchar_t *format, va_list ap);
465 int WXDLLIMPEXP_BASE wxVsscanf(const wxString& str, const char *format, va_list ap);
466 int WXDLLIMPEXP_BASE wxVsscanf(const wxString& str, const wchar_t *format, va_list ap);
467 int WXDLLIMPEXP_BASE wxVsscanf(const wxCStrData& str, const char *format, va_list ap);
468 int WXDLLIMPEXP_BASE wxVsscanf(const wxCStrData& str, const wchar_t *format, va_list ap);
469 #endif // !__VISUALC__
470
471 #endif /* _WX_WXCRTVARARG_H_ */
472