]>
git.saurik.com Git - wxWidgets.git/blob - 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
9 // Copyright: (c) 2007 REA Elektronik GmbH
10 // Licence: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
13 #ifndef _WX_WXCRTVARARG_H_
14 #define _WX_WXCRTVARARG_H_
16 // NB: User code should include wx/crt.h instead of including this
20 #include "wx/strvararg.h"
22 #include "wx/string.h"
24 // ----------------------------------------------------------------------------
25 // CRT functions aliases
26 // ----------------------------------------------------------------------------
28 /* Required for wxPrintf() etc */
31 /* printf() family saga */
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.
37 #if defined(HAVE_VSNPRINTF) && !defined(HAVE_VSNPRINTF_DECL)
43 int vsnprintf(char *str
, size_t size
, const char *format
, va_list ap
);
44 #endif /* !HAVE_VSNPRINTF_DECL */
46 #if defined(HAVE_SNPRINTF) && !defined(HAVE_SNPRINTF_DECL)
52 int snprintf(char *str
, size_t size
, const char *format
, ...);
53 #endif /* !HAVE_SNPRINTF_DECL */
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
)
61 return vsnprintf(str
, size
, (char*)format
, ap
);
66 MinGW MSVCRT has non-standard vswprintf() (for MSVC compatibility
67 presumably) and normally _vsnwprintf() is used instead
69 #if defined(HAVE_VSWPRINTF) && defined(__MINGW32__)
73 #if wxUSE_PRINTF_POS_PARAMS
75 The systems where vsnprintf() supports positional parameters should
76 define the HAVE_UNIX98_PRINTF symbol.
78 On systems which don't (e.g. Windows) we are forced to use
79 our wxVsnprintf() implementation.
81 #if defined(HAVE_UNIX98_PRINTF)
83 #define wxCRT_VsnprintfW_ vswprintf
85 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
86 #define wxCRT_VsnprintfA wx_fixed_vsnprintf
88 #define wxCRT_VsnprintfA vsnprintf
90 #else /* !HAVE_UNIX98_PRINTF */
92 The only compiler with positional parameters support under Windows
93 is VC++ 8.0 which provides a new xxprintf_p() functions family.
94 The 2003 PSDK includes a slightly earlier version of VC8 than the
95 main release and does not have the printf_p functions.
97 #if defined _MSC_FULL_VER && _MSC_FULL_VER >= 140050727 && !defined __WXWINCE__
98 #define wxCRT_VsnprintfA _vsprintf_p
99 #define wxCRT_VsnprintfW_ _vswprintf_p
101 #endif /* HAVE_UNIX98_PRINTF/!HAVE_UNIX98_PRINTF */
102 #else /* !wxUSE_PRINTF_POS_PARAMS */
104 We always want to define safe snprintf() function to be used instead of
105 sprintf(). Some compilers already have it (or rather vsnprintf() which
106 we really need...), otherwise we implement it using our own printf()
109 We define function with a trailing underscore here because the real one
110 is a wrapper around it as explained below
113 #if defined(__VISUALC__) || \
114 (defined(__BORLANDC__) && __BORLANDC__ >= 0x540)
115 #define wxCRT_VsnprintfA _vsnprintf
116 #define wxCRT_VsnprintfW_ _vsnwprintf
118 #if defined(HAVE__VSNWPRINTF)
119 #define wxCRT_VsnprintfW_ _vsnwprintf
120 #elif defined(HAVE_VSWPRINTF)
121 #define wxCRT_VsnprintfW_ vswprintf
122 #elif defined(__WATCOMC__)
123 #define wxCRT_VsnprintfW_ _vsnwprintf
127 All versions of CodeWarrior supported by wxWidgets apparently
128 have both snprintf() and vsnprintf()
130 #if defined(HAVE_VSNPRINTF) \
131 || defined(__MWERKS__) || defined(__WATCOMC__)
132 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
133 #define wxCRT_VsnprintfA wx_fixed_vsnprintf
135 #define wxCRT_VsnprintfA vsnprintf
139 #endif /* wxUSE_PRINTF_POS_PARAMS/!wxUSE_PRINTF_POS_PARAMS */
141 #ifndef wxCRT_VsnprintfW_
142 /* no (suitable) vsnprintf(), cook our own */
144 wxCRT_VsnprintfW_(wchar_t *buf
, size_t len
, const wchar_t *format
, va_list argptr
);
145 #define wxUSE_WXVSNPRINTFW 1
147 #define wxUSE_WXVSNPRINTFW 0
150 #ifndef wxCRT_VsnprintfA
151 /* no (suitable) vsnprintf(), cook our own */
153 wxCRT_VsnprintfA(char *buf
, size_t len
, const char *format
, va_list argptr
);
154 #define wxUSE_WXVSNPRINTFA 1
156 #define wxUSE_WXVSNPRINTFA 0
159 // for wxString code, define wxUSE_WXVSNPRINTF to indicate that wx
160 // implementation is used no matter what (in UTF-8 build, either *A or *W
161 // version may be called):
163 #define wxUSE_WXVSNPRINTF wxUSE_WXVSNPRINTFA
164 #elif wxUSE_UNICODE_WCHAR
165 #define wxUSE_WXVSNPRINTF wxUSE_WXVSNPRINTFW
166 #elif wxUSE_UTF8_LOCALE_ONLY
167 #define wxUSE_WXVSNPRINTF wxUSE_WXVSNPRINTFA
168 #else // UTF-8 under any locale
169 #define wxUSE_WXVSNPRINTF (wxUSE_WXVSNPRINTFA && wxUSE_WXVSNPRINTFW)
172 #define wxCRT_FprintfA fprintf
173 #define wxCRT_PrintfA printf
174 #define wxCRT_VfprintfA vfprintf
175 #define wxCRT_VprintfA vprintf
176 #define wxCRT_VsprintfA vsprintf
179 More Unicode complications: although both ANSI C and C++ define a number of
180 wide character functions such as wprintf(), not all environments have them.
181 Worse, those which do have different behaviours: under Windows, %s format
182 specifier changes its meaning in Unicode build and expects a Unicode string
183 while under Unix/POSIX it still means an ASCII string even for wprintf() and
184 %ls has to be used for wide strings.
186 We choose to always emulate Windows behaviour as more useful for us so even
187 if we have wprintf() we still must wrap it in a non trivial wxPrintf().
191 #define wxNEED_PRINTF_CONVERSION
194 // FIXME-UTF8: format conversion should be moved outside of wxCRT_* and to the
195 // vararg templates; after then, wxNEED_PRINTF_CONVERSION should
196 // be removed and this code simplified
197 #ifndef wxNEED_PRINTF_CONVERSION
198 #ifdef wxHAVE_TCHAR_SUPPORT
199 #define wxCRT_FprintfW fwprintf
200 #define wxCRT_PrintfW wprintf
201 #define wxCRT_VfprintfW vfwprintf
202 #define wxCRT_VprintfW vwprintf
203 #define wxCRT_VsprintfW vswprintf
205 #endif // !defined(wxNEED_PRINTF_CONVERSION)
208 In Unicode mode we need to have all standard functions such as wprintf() and
209 so on but not all systems have them so use our own implementations in this
212 #if wxUSE_UNICODE && !defined(wxHAVE_TCHAR_SUPPORT) && !defined(HAVE_WPRINTF)
213 #define wxNEED_WPRINTF
217 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
219 we need to implement all wide character printf functions either because
220 we don't have them at all or because they don't have the semantics we
223 int wxCRT_PrintfW( const wchar_t *format
, ... );
224 int wxCRT_FprintfW( FILE *stream
, const wchar_t *format
, ... );
225 int wxCRT_VfprintfW( FILE *stream
, const wchar_t *format
, va_list ap
);
226 int wxCRT_VprintfW( const wchar_t *format
, va_list ap
);
227 int wxCRT_VsprintfW( wchar_t *str
, const wchar_t *format
, va_list ap
);
228 #endif /* wxNEED_PRINTF_CONVERSION */
230 /* these 2 can be simply mapped to the versions with underscore at the end */
231 /* if we don't have to do the conversion */
233 However, if we don't have any vswprintf() at all we don't need to redefine
234 anything as our own wxCRT_VsnprintfW_() already behaves as needed.
236 #if defined(wxNEED_PRINTF_CONVERSION) && defined(wxCRT_VsnprintfW_)
237 int wxCRT_VsnprintfW( wchar_t *str
, size_t size
, const wchar_t *format
, va_list ap
);
239 #define wxCRT_VsnprintfW wxCRT_VsnprintfW_
243 /* Required for wxScanf() etc. */
244 #define wxCRT_ScanfA scanf
245 #define wxCRT_SscanfA sscanf
246 #define wxCRT_FscanfA fscanf
247 #define wxCRT_VsscanfA vsscanf
249 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
250 int wxCRT_ScanfW(const wchar_t *format
, ...);
251 int wxCRT_SscanfW(const wchar_t *str
, const wchar_t *format
, ...);
252 int wxCRT_FscanfW(FILE *stream
, const wchar_t *format
, ...);
253 int wxCRT_VsscanfW(const wchar_t *str
, const wchar_t *format
, va_list ap
);
255 #define wxCRT_ScanfW wscanf
256 #define wxCRT_SscanfW swscanf
257 #define wxCRT_FscanfW fwscanf
258 #define wxCRT_VsscanfW vswscanf
261 // ----------------------------------------------------------------------------
262 // user-friendly wrappers to CRT functions
263 // ----------------------------------------------------------------------------
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
273 // FIXME-UTF8: explicit wide-string and short-string format specifiers
274 // (%hs, %ls and variants) are currently broken, only %s works
275 // as expected (regardless of the build)
277 // FIXME-UTF8: %c (and %hc, %lc) don't work as expected either: in UTF-8 build,
278 // we should replace them with %s (as some Unicode chars may be
279 // encoded with >1 bytes) and in all builds, we should use wchar_t
280 // for all characters and convert char to it;
281 // we'll also need wxArgNormalizer<T> specializations for char,
282 // wchar_t, wxUniChar and wxUniCharRef to handle this correctly
284 // FIXME-UTF8: remove this
286 #define wxCRT_PrintfNative wxCRT_PrintfW
287 #define wxCRT_FprintfNative wxCRT_FprintfW
289 #define wxCRT_PrintfNative wxCRT_PrintfA
290 #define wxCRT_FprintfNative wxCRT_FprintfA
293 WX_DEFINE_VARARG_FUNC(int, wxPrintf
, 1, (const wxFormatString
&),
294 wxCRT_PrintfNative
, wxCRT_PrintfA
)
295 WX_DEFINE_VARARG_FUNC(int, wxFprintf
, 2, (FILE*, const wxFormatString
&),
296 wxCRT_FprintfNative
, wxCRT_FprintfA
)
298 // va_list versions of printf functions simply forward to the respective
299 // CRT function; note that they assume that va_list was created using
300 // wxArgNormalizer<T>!
301 #if wxUSE_UNICODE_UTF8
302 #if wxUSE_UTF8_LOCALE_ONLY
303 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
306 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
307 if ( wxLocaleIsUtf8 ) return implA args; \
308 else return implW args
310 #elif wxUSE_UNICODE_WCHAR
311 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
314 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
319 wxVprintf(const wxString
& format
, va_list ap
)
321 WX_VARARG_VFOO_IMPL((wxFormatString(format
), ap
),
322 wxCRT_VprintfW
, wxCRT_VprintfA
);
326 wxVfprintf(FILE *f
, const wxString
& format
, va_list ap
)
328 WX_VARARG_VFOO_IMPL((f
, wxFormatString(format
), ap
),
329 wxCRT_VfprintfW
, wxCRT_VfprintfA
);
332 #undef WX_VARARG_VFOO_IMPL
335 // wxSprintf() and friends have to be implemented in two forms, one for
336 // writing to char* buffer and one for writing to wchar_t*:
338 #if !wxUSE_UTF8_LOCALE_ONLY
339 int WXDLLIMPEXP_BASE
wxDoSprintfWchar(char *str
, const wxChar
*format
, ...);
341 #if wxUSE_UNICODE_UTF8
342 int WXDLLIMPEXP_BASE
wxDoSprintfUtf8(char *str
, const char *format
, ...);
344 WX_DEFINE_VARARG_FUNC(int, wxSprintf
, 2, (char*, const wxFormatString
&),
345 wxDoSprintfWchar
, wxDoSprintfUtf8
)
348 wxVsprintf(char *str
, const wxString
& format
, va_list argptr
);
350 #if !wxUSE_UTF8_LOCALE_ONLY
351 int WXDLLIMPEXP_BASE
wxDoSnprintfWchar(char *str
, size_t size
, const wxChar
*format
, ...);
353 #if wxUSE_UNICODE_UTF8
354 int WXDLLIMPEXP_BASE
wxDoSnprintfUtf8(char *str
, size_t size
, const char *format
, ...);
356 WX_DEFINE_VARARG_FUNC(int, wxSnprintf
, 3, (char*, size_t, const wxFormatString
&),
357 wxDoSnprintfWchar
, wxDoSnprintfUtf8
)
360 wxVsnprintf(char *str
, size_t size
, const wxString
& format
, va_list argptr
);
364 #if !wxUSE_UTF8_LOCALE_ONLY
365 int WXDLLIMPEXP_BASE
wxDoSprintfWchar(wchar_t *str
, const wxChar
*format
, ...);
367 #if wxUSE_UNICODE_UTF8
368 int WXDLLIMPEXP_BASE
wxDoSprintfUtf8(wchar_t *str
, const char *format
, ...);
370 WX_DEFINE_VARARG_FUNC(int, wxSprintf
, 2, (wchar_t*, const wxFormatString
&),
371 wxDoSprintfWchar
, wxDoSprintfUtf8
)
374 wxVsprintf(wchar_t *str
, const wxString
& format
, va_list argptr
);
376 #if !wxUSE_UTF8_LOCALE_ONLY
377 int WXDLLIMPEXP_BASE
wxDoSnprintfWchar(wchar_t *str
, size_t size
, const wxChar
*format
, ...);
379 #if wxUSE_UNICODE_UTF8
380 int WXDLLIMPEXP_BASE
wxDoSnprintfUtf8(wchar_t *str
, size_t size
, const char *format
, ...);
382 WX_DEFINE_VARARG_FUNC(int, wxSnprintf
, 3, (wchar_t*, size_t, const wxFormatString
&),
383 wxDoSnprintfWchar
, wxDoSnprintfUtf8
)
386 wxVsnprintf(wchar_t *str
, size_t size
, const wxString
& format
, va_list argptr
);
388 #endif // wxUSE_UNICODE
391 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
393 // fortunately, OpenWatcom implements __VA_ARGS__, so we can provide macros
394 // that cast the format argument to wxString:
400 #define wxPrintf(fmt, ...) \
401 wxPrintf_Impl(wxFormatString(fmt), __VA_ARGS__)
402 #define wxFprintf(f, fmt, ...) \
403 wxFprintf_Impl(f, wxFormatString(fmt), __VA_ARGS__)
404 #define wxSprintf(s, fmt, ...) \
405 wxSprintf_Impl(s, wxFormatString(fmt), __VA_ARGS__)
406 #define wxSnprintf(s, n, fmt, ...) \
407 wxSnprintf_Impl(s, n, wxFormatString(fmt), __VA_ARGS__)
408 #endif // __WATCOMC__
411 // We can't use wxArgNormalizer<T> for variadic arguments to wxScanf() etc.
412 // because they are writable, so instead of providing friendly template
413 // vararg-like functions, we just provide both char* and wchar_t* variants
414 // of these functions. The type of output variadic arguments for %s must match
415 // the type of 'str' and 'format' arguments.
417 // For compatibility with earlier wx versions, we also provide wxSscanf()
418 // version with the first argument (input string) wxString; for this version,
419 // the type of output string values is determined by the type of format string
422 #define _WX_SCANFUNC_EXTRACT_ARGS_1(x) x
423 #define _WX_SCANFUNC_EXTRACT_ARGS_2(x,y) x, y
424 #define _WX_SCANFUNC_EXTRACT_ARGS(N, args) _WX_SCANFUNC_EXTRACT_ARGS_##N args
426 #define _WX_VARARG_PASS_WRITABLE(i) a##i
428 #define _WX_DEFINE_SCANFUNC(N, dummy1, name, impl, passfixed, numfixed, fixed)\
429 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
430 int name(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, fixed), \
431 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
433 return impl(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, passfixed), \
434 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WRITABLE)); \
437 #define WX_DEFINE_SCANFUNC(name, numfixed, fixed, impl, passfixed) \
438 inline int name(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, fixed)) \
440 return impl(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, passfixed)); \
442 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
443 _WX_DEFINE_SCANFUNC, \
444 dummy1, name, impl, passfixed, numfixed, fixed)
446 WX_DEFINE_SCANFUNC(wxScanf
, 1, (const char *format
),
447 wxCRT_ScanfA
, (format
))
448 WX_DEFINE_SCANFUNC(wxScanf
, 1, (const wchar_t *format
),
449 wxCRT_ScanfW
, (format
))
451 WX_DEFINE_SCANFUNC(wxFscanf
, 2, (FILE *stream
, const char *format
),
452 wxCRT_FscanfA
, (stream
, format
))
453 WX_DEFINE_SCANFUNC(wxFscanf
, 2, (FILE *stream
, const wchar_t *format
),
454 wxCRT_FscanfW
, (stream
, format
))
456 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const char *str
, const char *format
),
457 wxCRT_SscanfA
, (str
, format
))
458 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wchar_t *str
, const wchar_t *format
),
459 wxCRT_SscanfW
, (str
, format
))
460 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wxCharBuffer
& str
, const char *format
),
461 wxCRT_SscanfA
, (str
.data(), format
))
462 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wxWCharBuffer
& str
, const wchar_t *format
),
463 wxCRT_SscanfW
, (str
.data(), format
))
464 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wxString
& str
, const char *format
),
465 wxCRT_SscanfA
, (str
.mb_str(), format
))
466 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wxString
& str
, const wchar_t *format
),
467 wxCRT_SscanfW
, (str
.wc_str(), format
))
468 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wxCStrData
& str
, const char *format
),
469 wxCRT_SscanfA
, (str
.AsCharBuf(), format
))
470 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wxCStrData
& str
, const wchar_t *format
),
471 wxCRT_SscanfW
, (str
.AsWCharBuf(), format
))
473 // Visual C++ doesn't provide vsscanf()
475 int WXDLLIMPEXP_BASE
wxVsscanf(const char *str
, const char *format
, va_list ap
);
476 int WXDLLIMPEXP_BASE
wxVsscanf(const wchar_t *str
, const wchar_t *format
, va_list ap
);
477 int WXDLLIMPEXP_BASE
wxVsscanf(const wxCharBuffer
& str
, const char *format
, va_list ap
);
478 int WXDLLIMPEXP_BASE
wxVsscanf(const wxWCharBuffer
& str
, const wchar_t *format
, va_list ap
);
479 int WXDLLIMPEXP_BASE
wxVsscanf(const wxString
& str
, const char *format
, va_list ap
);
480 int WXDLLIMPEXP_BASE
wxVsscanf(const wxString
& str
, const wchar_t *format
, va_list ap
);
481 int WXDLLIMPEXP_BASE
wxVsscanf(const wxCStrData
& str
, const char *format
, va_list ap
);
482 int WXDLLIMPEXP_BASE
wxVsscanf(const wxCStrData
& str
, const wchar_t *format
, va_list ap
);
483 #endif // !__VISUALC__
485 #endif /* _WX_WXCRTVARARG_H_ */