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