]>
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
8 // Copyright: (c) 2007 REA Elektronik GmbH
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_WXCRTVARARG_H_
13 #define _WX_WXCRTVARARG_H_
15 // NB: User code should include wx/crt.h instead of including this
19 #include "wx/strvararg.h"
21 #include "wx/string.h"
23 // ----------------------------------------------------------------------------
24 // CRT functions aliases
25 // ----------------------------------------------------------------------------
27 /* Required for wxPrintf() etc */
30 /* printf() family saga */
33 For many old Unix systems [v]snprintf()/vsscanf() exists in the system
34 libraries but not in the headers, so we need to declare it ourselves to be
39 #if defined(HAVE_VSNPRINTF) && !defined(HAVE_VSNPRINTF_DECL)
45 int vsnprintf(char *str
, size_t size
, const char *format
, va_list ap
);
46 #endif /* !HAVE_VSNPRINTF_DECL */
48 #if defined(HAVE_SNPRINTF) && !defined(HAVE_SNPRINTF_DECL)
54 int snprintf(char *str
, size_t size
, const char *format
, ...);
55 #endif /* !HAVE_SNPRINTF_DECL */
57 #if defined(HAVE_VSSCANF) && !defined(HAVE_VSSCANF_DECL)
63 int vsscanf(const char *str
, const char *format
, va_list ap
);
64 #endif /* !HAVE_VSSCANF_DECL */
66 /* Wrapper for vsnprintf if it's 3rd parameter is non-const. Note: the
67 * same isn't done for snprintf below, the builtin wxSnprintf_ is used
68 * instead since it's already a simple wrapper */
69 #if defined __cplusplus && defined HAVE_BROKEN_VSNPRINTF_DECL
70 inline int wx_fixed_vsnprintf(char *str
, size_t size
, const char *format
, va_list ap
)
72 return vsnprintf(str
, size
, (char*)format
, ap
);
79 mingw32 normally uses MSVCRT which has non-standard vswprintf() and so
80 normally _vsnwprintf() is used instead, the only exception is when mingw32
81 is used with STLPort which does have a standard vswprintf() starting from
82 version 5.1 which we can use.
85 #if defined(_STLPORT_VERSION) && _STLPORT_VERSION >= 0x510
86 #ifndef HAVE_VSWPRINTF
87 #define HAVE_VSWPRINTF
89 #elif defined(HAVE_VSWPRINTF)
90 /* can't use non-standard vswprintf() */
93 #endif /* __MINGW32__ */
95 #if defined(__WATCOMC__)
96 #define HAVE_VSWPRINTF 1
99 #if wxUSE_PRINTF_POS_PARAMS
101 The systems where vsnprintf() supports positional parameters should
102 define the HAVE_UNIX98_PRINTF symbol.
104 On systems which don't (e.g. Windows) we are forced to use
105 our wxVsnprintf() implementation.
107 #if defined(HAVE_UNIX98_PRINTF)
108 #ifdef HAVE_VSWPRINTF
109 #define wxCRT_VsnprintfW vswprintf
111 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
112 #define wxCRT_VsnprintfA wx_fixed_vsnprintf
114 #define wxCRT_VsnprintfA vsnprintf
116 #else /* !HAVE_UNIX98_PRINTF */
118 The only compiler with positional parameters support under Windows
119 is VC++ 8.0 which provides a new xxprintf_p() functions family.
120 The 2003 PSDK includes a slightly earlier version of VC8 than the
121 main release and does not have the printf_p functions.
123 #if defined _MSC_FULL_VER && _MSC_FULL_VER >= 140050727 && !defined __WXWINCE__
124 #define wxCRT_VsnprintfA _vsprintf_p
125 #define wxCRT_VsnprintfW _vswprintf_p
127 #endif /* HAVE_UNIX98_PRINTF/!HAVE_UNIX98_PRINTF */
128 #else /* !wxUSE_PRINTF_POS_PARAMS */
130 We always want to define safe snprintf() function to be used instead of
131 sprintf(). Some compilers already have it (or rather vsnprintf() which
132 we really need...), otherwise we implement it using our own printf()
135 We define function with a trailing underscore here because the real one
136 is a wrapper around it as explained below
139 #if defined(__VISUALC__) || \
140 (defined(__BORLANDC__) && __BORLANDC__ >= 0x540)
141 #define wxCRT_VsnprintfA _vsnprintf
142 #define wxCRT_VsnprintfW _vsnwprintf
144 #if defined(HAVE__VSNWPRINTF)
145 #define wxCRT_VsnprintfW _vsnwprintf
146 #elif defined(HAVE_VSWPRINTF)
147 #define wxCRT_VsnprintfW vswprintf
148 #elif defined(__WATCOMC__)
149 #define wxCRT_VsnprintfW _vsnwprintf
152 #if defined(HAVE_VSNPRINTF) \
153 || defined(__WATCOMC__)
154 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
155 #define wxCRT_VsnprintfA wx_fixed_vsnprintf
157 #define wxCRT_VsnprintfA vsnprintf
161 #endif /* wxUSE_PRINTF_POS_PARAMS/!wxUSE_PRINTF_POS_PARAMS */
163 #ifndef wxCRT_VsnprintfW
164 /* no (suitable) vsnprintf(), cook our own */
166 wxCRT_VsnprintfW(wchar_t *buf
, size_t len
, const wchar_t *format
, va_list argptr
);
167 #define wxUSE_WXVSNPRINTFW 1
169 #define wxUSE_WXVSNPRINTFW 0
172 #ifndef wxCRT_VsnprintfA
173 /* no (suitable) vsnprintf(), cook our own */
175 wxCRT_VsnprintfA(char *buf
, size_t len
, const char *format
, va_list argptr
);
176 #define wxUSE_WXVSNPRINTFA 1
178 #define wxUSE_WXVSNPRINTFA 0
181 // for wxString code, define wxUSE_WXVSNPRINTF to indicate that wx
182 // implementation is used no matter what (in UTF-8 build, either *A or *W
183 // version may be called):
185 #define wxUSE_WXVSNPRINTF wxUSE_WXVSNPRINTFA
186 #elif wxUSE_UNICODE_WCHAR
187 #define wxUSE_WXVSNPRINTF wxUSE_WXVSNPRINTFW
188 #elif wxUSE_UTF8_LOCALE_ONLY
189 #define wxUSE_WXVSNPRINTF wxUSE_WXVSNPRINTFA
190 #else // UTF-8 under any locale
191 #define wxUSE_WXVSNPRINTF (wxUSE_WXVSNPRINTFA && wxUSE_WXVSNPRINTFW)
194 #define wxCRT_FprintfA fprintf
195 #define wxCRT_PrintfA printf
196 #define wxCRT_VfprintfA vfprintf
197 #define wxCRT_VprintfA vprintf
198 #define wxCRT_VsprintfA vsprintf
201 In Unicode mode we need to have all standard functions such as wprintf() and
202 so on but not all systems have them so use our own implementations in this
205 #if wxUSE_UNICODE && !defined(wxHAVE_TCHAR_SUPPORT) && !defined(HAVE_WPRINTF)
206 #define wxNEED_WPRINTF
208 #if wxUSE_UNICODE && !defined(wxHAVE_TCHAR_SUPPORT) && !defined(HAVE_VSWSCANF)
209 #define wxNEED_VSWSCANF
213 #if defined(wxNEED_WPRINTF)
215 we need to implement all wide character printf functions either because
216 we don't have them at all or because they don't have the semantics we
219 int wxCRT_PrintfW( const wchar_t *format
, ... );
220 int wxCRT_FprintfW( FILE *stream
, const wchar_t *format
, ... );
221 int wxCRT_VfprintfW( FILE *stream
, const wchar_t *format
, va_list ap
);
222 int wxCRT_VprintfW( const wchar_t *format
, va_list ap
);
223 int wxCRT_VsprintfW( wchar_t *str
, const wchar_t *format
, va_list ap
);
224 #else /* !wxNEED_WPRINTF */
225 #define wxCRT_FprintfW fwprintf
226 #define wxCRT_PrintfW wprintf
227 #define wxCRT_VfprintfW vfwprintf
228 #define wxCRT_VprintfW vwprintf
230 #if defined(__WINDOWS__) && !defined(HAVE_VSWPRINTF)
231 // only non-standard vswprintf() without buffer size argument can be used here
232 #define wxCRT_VsprintfW vswprintf
234 #endif /* wxNEED_WPRINTF */
237 /* Required for wxScanf() etc. */
238 #define wxCRT_ScanfA scanf
239 #define wxCRT_SscanfA sscanf
240 #define wxCRT_FscanfA fscanf
241 #define wxCRT_VsscanfA vsscanf
243 #if defined(wxNEED_WPRINTF)
244 int wxCRT_ScanfW(const wchar_t *format
, ...);
245 int wxCRT_SscanfW(const wchar_t *str
, const wchar_t *format
, ...);
246 int wxCRT_FscanfW(FILE *stream
, const wchar_t *format
, ...);
248 #define wxCRT_ScanfW wxVMS_USE_STD wscanf
249 #define wxCRT_SscanfW wxVMS_USE_STD swscanf
250 #define wxCRT_FscanfW wxVMS_USE_STD fwscanf
252 #ifdef wxNEED_VSWSCANF
253 int wxCRT_VsscanfW(const wchar_t *str
, const wchar_t *format
, va_list ap
);
255 #define wxCRT_VsscanfW wxVMS_USE_STD vswscanf
258 // ----------------------------------------------------------------------------
259 // user-friendly wrappers to CRT functions
260 // ----------------------------------------------------------------------------
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
270 // FIXME-UTF8: remove this
272 #define wxCRT_PrintfNative wxCRT_PrintfW
273 #define wxCRT_FprintfNative wxCRT_FprintfW
275 #define wxCRT_PrintfNative wxCRT_PrintfA
276 #define wxCRT_FprintfNative wxCRT_FprintfA
280 WX_DEFINE_VARARG_FUNC_SANS_N0(int, wxPrintf
, 1, (const wxFormatString
&),
281 wxCRT_PrintfNative
, wxCRT_PrintfA
)
282 inline int wxPrintf(const wxFormatString
& s
)
284 return wxPrintf("%s", s
.InputAsString());
287 WX_DEFINE_VARARG_FUNC_SANS_N0(int, wxFprintf
, 2, (FILE*, const wxFormatString
&),
288 wxCRT_FprintfNative
, wxCRT_FprintfA
)
289 inline int wxFprintf(FILE *f
, const wxFormatString
& s
)
291 return wxFprintf(f
, "%s", s
.InputAsString());
294 // va_list versions of printf functions simply forward to the respective
295 // CRT function; note that they assume that va_list was created using
296 // wxArgNormalizer<T>!
297 #if wxUSE_UNICODE_UTF8
298 #if wxUSE_UTF8_LOCALE_ONLY
299 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
302 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
303 if ( wxLocaleIsUtf8 ) return implA args; \
304 else return implW args
306 #elif wxUSE_UNICODE_WCHAR
307 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
310 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
315 wxVprintf(const wxString
& format
, va_list ap
)
317 WX_VARARG_VFOO_IMPL((wxFormatString(format
), ap
),
318 wxCRT_VprintfW
, wxCRT_VprintfA
);
322 wxVfprintf(FILE *f
, const wxString
& format
, va_list ap
)
324 WX_VARARG_VFOO_IMPL((f
, wxFormatString(format
), ap
),
325 wxCRT_VfprintfW
, wxCRT_VfprintfA
);
328 #undef WX_VARARG_VFOO_IMPL
331 // wxSprintf() and friends have to be implemented in two forms, one for
332 // writing to char* buffer and one for writing to wchar_t*:
334 #if !wxUSE_UTF8_LOCALE_ONLY
335 int WXDLLIMPEXP_BASE
wxDoSprintfWchar(char *str
, const wxChar
*format
, ...);
337 #if wxUSE_UNICODE_UTF8
338 int WXDLLIMPEXP_BASE
wxDoSprintfUtf8(char *str
, const char *format
, ...);
340 WX_DEFINE_VARARG_FUNC(int, wxSprintf
, 2, (char*, const wxFormatString
&),
341 wxDoSprintfWchar
, wxDoSprintfUtf8
)
344 wxVsprintf(char *str
, const wxString
& format
, va_list argptr
);
346 #if !wxUSE_UTF8_LOCALE_ONLY
347 int WXDLLIMPEXP_BASE
wxDoSnprintfWchar(char *str
, size_t size
, const wxChar
*format
, ...);
349 #if wxUSE_UNICODE_UTF8
350 int WXDLLIMPEXP_BASE
wxDoSnprintfUtf8(char *str
, size_t size
, const char *format
, ...);
352 WX_DEFINE_VARARG_FUNC(int, wxSnprintf
, 3, (char*, size_t, const wxFormatString
&),
353 wxDoSnprintfWchar
, wxDoSnprintfUtf8
)
356 wxVsnprintf(char *str
, size_t size
, const wxString
& format
, va_list argptr
);
360 #if !wxUSE_UTF8_LOCALE_ONLY
361 int WXDLLIMPEXP_BASE
wxDoSprintfWchar(wchar_t *str
, const wxChar
*format
, ...);
363 #if wxUSE_UNICODE_UTF8
364 int WXDLLIMPEXP_BASE
wxDoSprintfUtf8(wchar_t *str
, const char *format
, ...);
366 WX_DEFINE_VARARG_FUNC(int, wxSprintf
, 2, (wchar_t*, const wxFormatString
&),
367 wxDoSprintfWchar
, wxDoSprintfUtf8
)
370 wxVsprintf(wchar_t *str
, const wxString
& format
, va_list argptr
);
372 #if !wxUSE_UTF8_LOCALE_ONLY
373 int WXDLLIMPEXP_BASE
wxDoSnprintfWchar(wchar_t *str
, size_t size
, const wxChar
*format
, ...);
375 #if wxUSE_UNICODE_UTF8
376 int WXDLLIMPEXP_BASE
wxDoSnprintfUtf8(wchar_t *str
, size_t size
, const char *format
, ...);
378 WX_DEFINE_VARARG_FUNC(int, wxSnprintf
, 3, (wchar_t*, size_t, const wxFormatString
&),
379 wxDoSnprintfWchar
, wxDoSnprintfUtf8
)
382 wxVsnprintf(wchar_t *str
, size_t size
, const wxString
& format
, va_list argptr
);
384 #endif // wxUSE_UNICODE
387 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
389 // fortunately, OpenWatcom implements __VA_ARGS__, so we can provide macros
390 // that cast the format argument to wxString:
396 #define wxPrintf(fmt, ...) \
397 wxPrintf_Impl(wxFormatString(fmt), __VA_ARGS__)
398 #define wxFprintf(f, fmt, ...) \
399 wxFprintf_Impl(f, wxFormatString(fmt), __VA_ARGS__)
400 #define wxSprintf(s, fmt, ...) \
401 wxSprintf_Impl(s, wxFormatString(fmt), __VA_ARGS__)
402 #define wxSnprintf(s, n, fmt, ...) \
403 wxSnprintf_Impl(s, n, wxFormatString(fmt), __VA_ARGS__)
404 #endif // __WATCOMC__
407 // We can't use wxArgNormalizer<T> for variadic arguments to wxScanf() etc.
408 // because they are writable, so instead of providing friendly template
409 // vararg-like functions, we just provide both char* and wchar_t* variants
410 // of these functions. The type of output variadic arguments for %s must match
411 // the type of 'str' and 'format' arguments.
413 // For compatibility with earlier wx versions, we also provide wxSscanf()
414 // version with the first argument (input string) wxString; for this version,
415 // the type of output string values is determined by the type of format string
418 #define _WX_SCANFUNC_EXTRACT_ARGS_1(x) x
419 #define _WX_SCANFUNC_EXTRACT_ARGS_2(x,y) x, y
420 #define _WX_SCANFUNC_EXTRACT_ARGS(N, args) _WX_SCANFUNC_EXTRACT_ARGS_##N args
422 #define _WX_VARARG_PASS_WRITABLE(i) a##i
424 #define _WX_DEFINE_SCANFUNC(N, dummy1, name, impl, passfixed, numfixed, fixed)\
425 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
426 int name(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, fixed), \
427 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
429 return impl(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, passfixed), \
430 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WRITABLE)); \
433 #define WX_DEFINE_SCANFUNC(name, numfixed, fixed, impl, passfixed) \
434 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
435 _WX_DEFINE_SCANFUNC, \
436 dummy1, name, impl, passfixed, numfixed, fixed)
438 // this is needed to normalize the format string, see src/common/strvararg.cpp
441 #define wxScanfConvertFormatW(fmt) fmt
443 const wxScopedWCharBuffer
444 WXDLLIMPEXP_BASE
wxScanfConvertFormatW(const wchar_t *format
);
447 WX_DEFINE_SCANFUNC(wxScanf
, 1, (const char *format
),
448 wxCRT_ScanfA
, (format
))
449 WX_DEFINE_SCANFUNC(wxScanf
, 1, (const wchar_t *format
),
450 wxCRT_ScanfW
, (wxScanfConvertFormatW(format
)))
452 WX_DEFINE_SCANFUNC(wxFscanf
, 2, (FILE *stream
, const char *format
),
453 wxCRT_FscanfA
, (stream
, format
))
454 WX_DEFINE_SCANFUNC(wxFscanf
, 2, (FILE *stream
, const wchar_t *format
),
455 wxCRT_FscanfW
, (stream
, wxScanfConvertFormatW(format
)))
457 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const char *str
, const char *format
),
458 wxCRT_SscanfA
, (str
, format
))
459 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wchar_t *str
, const wchar_t *format
),
460 wxCRT_SscanfW
, (str
, wxScanfConvertFormatW(format
)))
461 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wxScopedCharBuffer
& str
, const char *format
),
462 wxCRT_SscanfA
, (str
.data(), format
))
463 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wxScopedWCharBuffer
& str
, const wchar_t *format
),
464 wxCRT_SscanfW
, (str
.data(), wxScanfConvertFormatW(format
)))
465 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wxString
& str
, const char *format
),
466 wxCRT_SscanfA
, (str
.mb_str(), format
))
467 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wxString
& str
, const wchar_t *format
),
468 wxCRT_SscanfW
, (str
.wc_str(), wxScanfConvertFormatW(format
)))
469 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wxCStrData
& str
, const char *format
),
470 wxCRT_SscanfA
, (str
.AsCharBuf(), format
))
471 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wxCStrData
& str
, const wchar_t *format
),
472 wxCRT_SscanfW
, (str
.AsWCharBuf(), wxScanfConvertFormatW(format
)))
474 // Visual C++ doesn't provide vsscanf()
476 int WXDLLIMPEXP_BASE
wxVsscanf(const char *str
, const char *format
, va_list ap
);
477 int WXDLLIMPEXP_BASE
wxVsscanf(const wchar_t *str
, const wchar_t *format
, va_list ap
);
478 int WXDLLIMPEXP_BASE
wxVsscanf(const wxScopedCharBuffer
& str
, const char *format
, va_list ap
);
479 int WXDLLIMPEXP_BASE
wxVsscanf(const wxScopedWCharBuffer
& str
, const wchar_t *format
, va_list ap
);
480 int WXDLLIMPEXP_BASE
wxVsscanf(const wxString
& str
, const char *format
, va_list ap
);
481 int WXDLLIMPEXP_BASE
wxVsscanf(const wxString
& str
, const wchar_t *format
, va_list ap
);
482 int WXDLLIMPEXP_BASE
wxVsscanf(const wxCStrData
& str
, const char *format
, va_list ap
);
483 int WXDLLIMPEXP_BASE
wxVsscanf(const wxCStrData
& str
, const wchar_t *format
, va_list ap
);
484 #endif // !__VISUALC__
486 #endif /* _WX_WXCRTVARARG_H_ */