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