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 defined(__WATCOMC__)
96 #define HAVE_VSWPRINTF 1
97 #endif
98
99 #if wxUSE_PRINTF_POS_PARAMS
100 /*
101 The systems where vsnprintf() supports positional parameters should
102 define the HAVE_UNIX98_PRINTF symbol.
103
104 On systems which don't (e.g. Windows) we are forced to use
105 our wxVsnprintf() implementation.
106 */
107 #if defined(HAVE_UNIX98_PRINTF)
108 #ifdef HAVE_VSWPRINTF
109 #define wxCRT_VsnprintfW vswprintf
110 #endif
111 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
112 #define wxCRT_VsnprintfA wx_fixed_vsnprintf
113 #else
114 #define wxCRT_VsnprintfA vsnprintf
115 #endif
116 #else /* !HAVE_UNIX98_PRINTF */
117 /*
118 The only compiler with positional parameters support under Windows
119 is VC++ 8.0 which provides a new xxprintf_p() functions family.
120 The 2003 PSDK includes a slightly earlier version of VC8 than the
121 main release and does not have the printf_p functions.
122 */
123 #if defined _MSC_FULL_VER && _MSC_FULL_VER >= 140050727 && !defined __WXWINCE__
124 #define wxCRT_VsnprintfA _vsprintf_p
125 #define wxCRT_VsnprintfW _vswprintf_p
126 #endif
127 #endif /* HAVE_UNIX98_PRINTF/!HAVE_UNIX98_PRINTF */
128 #else /* !wxUSE_PRINTF_POS_PARAMS */
129 /*
130 We always want to define safe snprintf() function to be used instead of
131 sprintf(). Some compilers already have it (or rather vsnprintf() which
132 we really need...), otherwise we implement it using our own printf()
133 code.
134
135 We define function with a trailing underscore here because the real one
136 is a wrapper around it as explained below
137 */
138
139 #if defined(__VISUALC__) || \
140 (defined(__BORLANDC__) && __BORLANDC__ >= 0x540)
141 #define wxCRT_VsnprintfA _vsnprintf
142 #define wxCRT_VsnprintfW _vsnwprintf
143 #else
144 #if defined(HAVE__VSNWPRINTF)
145 #define wxCRT_VsnprintfW _vsnwprintf
146 #elif defined(HAVE_VSWPRINTF)
147 #define wxCRT_VsnprintfW vswprintf
148 #elif defined(__WATCOMC__)
149 #define wxCRT_VsnprintfW _vsnwprintf
150 #endif
151
152 #if defined(HAVE_VSNPRINTF) \
153 || defined(__WATCOMC__)
154 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
155 #define wxCRT_VsnprintfA wx_fixed_vsnprintf
156 #else
157 #define wxCRT_VsnprintfA vsnprintf
158 #endif
159 #endif
160 #endif
161 #endif /* wxUSE_PRINTF_POS_PARAMS/!wxUSE_PRINTF_POS_PARAMS */
162
163 #ifndef wxCRT_VsnprintfW
164 /* no (suitable) vsnprintf(), cook our own */
165 WXDLLIMPEXP_BASE int
166 wxCRT_VsnprintfW(wchar_t *buf, size_t len, const wchar_t *format, va_list argptr);
167 #define wxUSE_WXVSNPRINTFW 1
168 #else
169 #define wxUSE_WXVSNPRINTFW 0
170 #endif
171
172 #ifndef wxCRT_VsnprintfA
173 /* no (suitable) vsnprintf(), cook our own */
174 WXDLLIMPEXP_BASE int
175 wxCRT_VsnprintfA(char *buf, size_t len, const char *format, va_list argptr);
176 #define wxUSE_WXVSNPRINTFA 1
177 #else
178 #define wxUSE_WXVSNPRINTFA 0
179 #endif
180
181 // for wxString code, define wxUSE_WXVSNPRINTF to indicate that wx
182 // implementation is used no matter what (in UTF-8 build, either *A or *W
183 // version may be called):
184 #if !wxUSE_UNICODE
185 #define wxUSE_WXVSNPRINTF wxUSE_WXVSNPRINTFA
186 #elif wxUSE_UNICODE_WCHAR
187 #define wxUSE_WXVSNPRINTF wxUSE_WXVSNPRINTFW
188 #elif wxUSE_UTF8_LOCALE_ONLY
189 #define wxUSE_WXVSNPRINTF wxUSE_WXVSNPRINTFA
190 #else // UTF-8 under any locale
191 #define wxUSE_WXVSNPRINTF (wxUSE_WXVSNPRINTFA && wxUSE_WXVSNPRINTFW)
192 #endif
193
194 #define wxCRT_FprintfA fprintf
195 #define wxCRT_PrintfA printf
196 #define wxCRT_VfprintfA vfprintf
197 #define wxCRT_VprintfA vprintf
198 #define wxCRT_VsprintfA vsprintf
199
200 /*
201 In Unicode mode we need to have all standard functions such as wprintf() and
202 so on but not all systems have them so use our own implementations in this
203 case.
204 */
205 #if !defined(wxHAVE_TCHAR_SUPPORT) && !defined(HAVE_WPRINTF)
206 #define wxNEED_WPRINTF
207 #endif
208 #if !defined(wxHAVE_TCHAR_SUPPORT) && !defined(HAVE_VSWSCANF) && defined(HAVE_VSSCANF)
209 #define wxNEED_VSWSCANF
210 #endif
211
212
213 #if defined(wxNEED_WPRINTF)
214 /*
215 we need to implement all wide character printf functions either because
216 we don't have them at all or because they don't have the semantics we
217 need
218 */
219 int wxCRT_PrintfW( const wchar_t *format, ... );
220 int wxCRT_FprintfW( FILE *stream, const wchar_t *format, ... );
221 int wxCRT_VfprintfW( FILE *stream, const wchar_t *format, va_list ap );
222 int wxCRT_VprintfW( const wchar_t *format, va_list ap );
223 int wxCRT_VsprintfW( wchar_t *str, const wchar_t *format, va_list ap );
224 #else /* !wxNEED_WPRINTF */
225 #define wxCRT_FprintfW fwprintf
226 #define wxCRT_PrintfW wprintf
227 #define wxCRT_VfprintfW vfwprintf
228 #define wxCRT_VprintfW vwprintf
229
230 #if defined(__WINDOWS__) && !defined(HAVE_VSWPRINTF)
231 // only non-standard vswprintf() without buffer size argument can be used here
232 #define wxCRT_VsprintfW vswprintf
233 #endif
234 #endif /* wxNEED_WPRINTF */
235
236
237 /* Required for wxScanf() etc. */
238 #define wxCRT_ScanfA scanf
239 #define wxCRT_SscanfA sscanf
240 #define wxCRT_FscanfA fscanf
241
242 /* vsscanf() may have a wrong declaration with non-const first parameter, fix
243 * this by wrapping it if necessary. */
244 #if defined __cplusplus && defined HAVE_BROKEN_VSSCANF_DECL
wxCRT_VsscanfA(const char * str,const char * format,va_list ap)245 inline int wxCRT_VsscanfA(const char *str, const char *format, va_list ap)
246 {
247 return vsscanf(const_cast<char *>(str), format, ap);
248 }
249 #else
250 #define wxCRT_VsscanfA vsscanf
251 #endif
252
253 #if defined(wxNEED_WPRINTF)
254 int wxCRT_ScanfW(const wchar_t *format, ...);
255 int wxCRT_SscanfW(const wchar_t *str, const wchar_t *format, ...);
256 int wxCRT_FscanfW(FILE *stream, const wchar_t *format, ...);
257 #else
258 #define wxCRT_ScanfW wxVMS_USE_STD wscanf
259 #define wxCRT_SscanfW wxVMS_USE_STD swscanf
260 #define wxCRT_FscanfW wxVMS_USE_STD fwscanf
261 #endif
262 #ifdef wxNEED_VSWSCANF
263 int wxCRT_VsscanfW(const wchar_t *str, const wchar_t *format, va_list ap);
264 #else
265 #define wxCRT_VsscanfW wxVMS_USE_STD vswscanf
266 #endif
267
268 // ----------------------------------------------------------------------------
269 // user-friendly wrappers to CRT functions
270 // ----------------------------------------------------------------------------
271
272 #ifdef __WATCOMC__
273 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
274 #define wxPrintf wxPrintf_Impl
275 #define wxFprintf wxFprintf_Impl
276 #define wxSprintf wxSprintf_Impl
277 #define wxSnprintf wxSnprintf_Impl
278 #endif
279
280 // FIXME-UTF8: remove this
281 #if wxUSE_UNICODE
282 #define wxCRT_PrintfNative wxCRT_PrintfW
283 #define wxCRT_FprintfNative wxCRT_FprintfW
284 #else
285 #define wxCRT_PrintfNative wxCRT_PrintfA
286 #define wxCRT_FprintfNative wxCRT_FprintfA
287 #endif
288
289
290 WX_DEFINE_VARARG_FUNC_SANS_N0(int, wxPrintf, 1, (const wxFormatString&),
291 wxCRT_PrintfNative, wxCRT_PrintfA)
wxPrintf(const wxFormatString & s)292 inline int wxPrintf(const wxFormatString& s)
293 {
294 return wxPrintf("%s", s.InputAsString());
295 }
296
297 WX_DEFINE_VARARG_FUNC_SANS_N0(int, wxFprintf, 2, (FILE*, const wxFormatString&),
298 wxCRT_FprintfNative, wxCRT_FprintfA)
wxFprintf(FILE * f,const wxFormatString & s)299 inline int wxFprintf(FILE *f, const wxFormatString& s)
300 {
301 return wxFprintf(f, "%s", s.InputAsString());
302 }
303
304 // va_list versions of printf functions simply forward to the respective
305 // CRT function; note that they assume that va_list was created using
306 // wxArgNormalizer<T>!
307 #if wxUSE_UNICODE_UTF8
308 #if wxUSE_UTF8_LOCALE_ONLY
309 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
310 return implA args
311 #else
312 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
313 if ( wxLocaleIsUtf8 ) return implA args; \
314 else return implW args
315 #endif
316 #elif wxUSE_UNICODE_WCHAR
317 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
318 return implW args
319 #else // ANSI
320 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
321 return implA args
322 #endif
323
324 inline int
wxVprintf(const wxString & format,va_list ap)325 wxVprintf(const wxString& format, va_list ap)
326 {
327 WX_VARARG_VFOO_IMPL((wxFormatString(format), ap),
328 wxCRT_VprintfW, wxCRT_VprintfA);
329 }
330
331 inline int
wxVfprintf(FILE * f,const wxString & format,va_list ap)332 wxVfprintf(FILE *f, const wxString& format, va_list ap)
333 {
334 WX_VARARG_VFOO_IMPL((f, wxFormatString(format), ap),
335 wxCRT_VfprintfW, wxCRT_VfprintfA);
336 }
337
338 #undef WX_VARARG_VFOO_IMPL
339
340
341 // wxSprintf() and friends have to be implemented in two forms, one for
342 // writing to char* buffer and one for writing to wchar_t*:
343
344 #if !wxUSE_UTF8_LOCALE_ONLY
345 int WXDLLIMPEXP_BASE wxDoSprintfWchar(char *str, const wxChar *format, ...);
346 #endif
347 #if wxUSE_UNICODE_UTF8
348 int WXDLLIMPEXP_BASE wxDoSprintfUtf8(char *str, const char *format, ...);
349 #endif
350 WX_DEFINE_VARARG_FUNC(int, wxSprintf, 2, (char*, const wxFormatString&),
351 wxDoSprintfWchar, wxDoSprintfUtf8)
352
353 int WXDLLIMPEXP_BASE
354 wxVsprintf(char *str, const wxString& format, va_list argptr);
355
356 #if !wxUSE_UTF8_LOCALE_ONLY
357 int WXDLLIMPEXP_BASE wxDoSnprintfWchar(char *str, size_t size, const wxChar *format, ...);
358 #endif
359 #if wxUSE_UNICODE_UTF8
360 int WXDLLIMPEXP_BASE wxDoSnprintfUtf8(char *str, size_t size, const char *format, ...);
361 #endif
362 WX_DEFINE_VARARG_FUNC(int, wxSnprintf, 3, (char*, size_t, const wxFormatString&),
363 wxDoSnprintfWchar, wxDoSnprintfUtf8)
364
365 int WXDLLIMPEXP_BASE
366 wxVsnprintf(char *str, size_t size, const wxString& format, va_list argptr);
367
368 #if wxUSE_UNICODE
369
370 #if !wxUSE_UTF8_LOCALE_ONLY
371 int WXDLLIMPEXP_BASE wxDoSprintfWchar(wchar_t *str, const wxChar *format, ...);
372 #endif
373 #if wxUSE_UNICODE_UTF8
374 int WXDLLIMPEXP_BASE wxDoSprintfUtf8(wchar_t *str, const char *format, ...);
375 #endif
376 WX_DEFINE_VARARG_FUNC(int, wxSprintf, 2, (wchar_t*, const wxFormatString&),
377 wxDoSprintfWchar, wxDoSprintfUtf8)
378
379 int WXDLLIMPEXP_BASE
380 wxVsprintf(wchar_t *str, const wxString& format, va_list argptr);
381
382 #if !wxUSE_UTF8_LOCALE_ONLY
383 int WXDLLIMPEXP_BASE wxDoSnprintfWchar(wchar_t *str, size_t size, const wxChar *format, ...);
384 #endif
385 #if wxUSE_UNICODE_UTF8
386 int WXDLLIMPEXP_BASE wxDoSnprintfUtf8(wchar_t *str, size_t size, const char *format, ...);
387 #endif
388 WX_DEFINE_VARARG_FUNC(int, wxSnprintf, 3, (wchar_t*, size_t, const wxFormatString&),
389 wxDoSnprintfWchar, wxDoSnprintfUtf8)
390
391 int WXDLLIMPEXP_BASE
392 wxVsnprintf(wchar_t *str, size_t size, const wxString& format, va_list argptr);
393
394 #endif // wxUSE_UNICODE
395
396 #ifdef __WATCOMC__
397 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
398 //
399 // fortunately, OpenWatcom implements __VA_ARGS__, so we can provide macros
400 // that cast the format argument to wxString:
401 #undef wxPrintf
402 #undef wxFprintf
403 #undef wxSprintf
404 #undef wxSnprintf
405
406 #define wxPrintf(fmt, ...) \
407 wxPrintf_Impl(wxFormatString(fmt), __VA_ARGS__)
408 #define wxFprintf(f, fmt, ...) \
409 wxFprintf_Impl(f, wxFormatString(fmt), __VA_ARGS__)
410 #define wxSprintf(s, fmt, ...) \
411 wxSprintf_Impl(s, wxFormatString(fmt), __VA_ARGS__)
412 #define wxSnprintf(s, n, fmt, ...) \
413 wxSnprintf_Impl(s, n, wxFormatString(fmt), __VA_ARGS__)
414 #endif // __WATCOMC__
415
416
417 // We can't use wxArgNormalizer<T> for variadic arguments to wxScanf() etc.
418 // because they are writable, so instead of providing friendly template
419 // vararg-like functions, we just provide both char* and wchar_t* variants
420 // of these functions. The type of output variadic arguments for %s must match
421 // the type of 'str' and 'format' arguments.
422 //
423 // For compatibility with earlier wx versions, we also provide wxSscanf()
424 // version with the first argument (input string) wxString; for this version,
425 // the type of output string values is determined by the type of format string
426 // only.
427
428 #define _WX_SCANFUNC_EXTRACT_ARGS_1(x) x
429 #define _WX_SCANFUNC_EXTRACT_ARGS_2(x,y) x, y
430 #define _WX_SCANFUNC_EXTRACT_ARGS(N, args) _WX_SCANFUNC_EXTRACT_ARGS_##N args
431
432 #define _WX_VARARG_PASS_WRITABLE(i) a##i
433
434 #define _WX_DEFINE_SCANFUNC(N, dummy1, name, impl, passfixed, numfixed, fixed)\
435 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
436 int name(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, fixed), \
437 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
438 { \
439 return impl(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, passfixed), \
440 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WRITABLE)); \
441 }
442
443 #define WX_DEFINE_SCANFUNC(name, numfixed, fixed, impl, passfixed) \
444 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
445 _WX_DEFINE_SCANFUNC, \
446 dummy1, name, impl, passfixed, numfixed, fixed)
447
448 // this is needed to normalize the format string, see src/common/strvararg.cpp
449 // for more details
450 #ifdef __WINDOWS__
451 #define wxScanfConvertFormatW(fmt) fmt
452 #else
453 const wxScopedWCharBuffer
454 WXDLLIMPEXP_BASE wxScanfConvertFormatW(const wchar_t *format);
455 #endif
456
457 WX_DEFINE_SCANFUNC(wxScanf, 1, (const char *format),
458 wxCRT_ScanfA, (format))
459 WX_DEFINE_SCANFUNC(wxScanf, 1, (const wchar_t *format),
460 wxCRT_ScanfW, (wxScanfConvertFormatW(format)))
461
462 WX_DEFINE_SCANFUNC(wxFscanf, 2, (FILE *stream, const char *format),
463 wxCRT_FscanfA, (stream, format))
464 WX_DEFINE_SCANFUNC(wxFscanf, 2, (FILE *stream, const wchar_t *format),
465 wxCRT_FscanfW, (stream, wxScanfConvertFormatW(format)))
466
467 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const char *str, const char *format),
468 wxCRT_SscanfA, (str, format))
469 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wchar_t *str, const wchar_t *format),
470 wxCRT_SscanfW, (str, wxScanfConvertFormatW(format)))
471 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxScopedCharBuffer& str, const char *format),
472 wxCRT_SscanfA, (str.data(), format))
473 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxScopedWCharBuffer& str, const wchar_t *format),
474 wxCRT_SscanfW, (str.data(), wxScanfConvertFormatW(format)))
475 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxString& str, const char *format),
476 wxCRT_SscanfA, (str.mb_str(), format))
477 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxString& str, const wchar_t *format),
478 wxCRT_SscanfW, (str.wc_str(), wxScanfConvertFormatW(format)))
479 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxCStrData& str, const char *format),
480 wxCRT_SscanfA, (str.AsCharBuf(), format))
481 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxCStrData& str, const wchar_t *format),
482 wxCRT_SscanfW, (str.AsWCharBuf(), wxScanfConvertFormatW(format)))
483
484 // Visual C++ doesn't provide vsscanf()
485 #ifndef __VISUALC___
486 int WXDLLIMPEXP_BASE wxVsscanf(const char *str, const char *format, va_list ap);
487 int WXDLLIMPEXP_BASE wxVsscanf(const wchar_t *str, const wchar_t *format, va_list ap);
488 int WXDLLIMPEXP_BASE wxVsscanf(const wxScopedCharBuffer& str, const char *format, va_list ap);
489 int WXDLLIMPEXP_BASE wxVsscanf(const wxScopedWCharBuffer& str, const wchar_t *format, va_list ap);
490 int WXDLLIMPEXP_BASE wxVsscanf(const wxString& str, const char *format, va_list ap);
491 int WXDLLIMPEXP_BASE wxVsscanf(const wxString& str, const wchar_t *format, va_list ap);
492 int WXDLLIMPEXP_BASE wxVsscanf(const wxCStrData& str, const char *format, va_list ap);
493 int WXDLLIMPEXP_BASE wxVsscanf(const wxCStrData& str, const wchar_t *format, va_list ap);
494 #endif // !__VISUALC__
495
496 #endif /* _WX_WXCRTVARARG_H_ */
497