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