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