]>
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
178 #define wxCRT_FprintfW fwprintf
179 #define wxCRT_PrintfW wprintf
180 #define wxCRT_VfprintfW vfwprintf
181 #define wxCRT_VprintfW vwprintf
183 #if defined(__WINDOWS__) && !defined(HAVE_VSWPRINTF)
184 // only non-standard vswprintf() without buffer size argument can be used here
185 #define wxCRT_VsprintfW vswprintf
189 In Unicode mode we need to have all standard functions such as wprintf() and
190 so on but not all systems have them so use our own implementations in this
193 #if wxUSE_UNICODE && !defined(wxHAVE_TCHAR_SUPPORT) && !defined(HAVE_WPRINTF)
194 #define wxNEED_WPRINTF
198 #if defined(wxNEED_WPRINTF)
200 we need to implement all wide character printf functions either because
201 we don't have them at all or because they don't have the semantics we
204 int wxCRT_PrintfW( const wchar_t *format
, ... );
205 int wxCRT_FprintfW( FILE *stream
, const wchar_t *format
, ... );
206 int wxCRT_VfprintfW( FILE *stream
, const wchar_t *format
, va_list ap
);
207 int wxCRT_VprintfW( const wchar_t *format
, va_list ap
);
208 int wxCRT_VsprintfW( wchar_t *str
, const wchar_t *format
, va_list ap
);
209 #endif /* wxNEED_WPRINTF */
212 /* Required for wxScanf() etc. */
213 #define wxCRT_ScanfA scanf
214 #define wxCRT_SscanfA sscanf
215 #define wxCRT_FscanfA fscanf
216 #define wxCRT_VsscanfA vsscanf
218 #if defined(wxNEED_WPRINTF)
219 int wxCRT_ScanfW(const wchar_t *format
, ...);
220 int wxCRT_SscanfW(const wchar_t *str
, const wchar_t *format
, ...);
221 int wxCRT_FscanfW(FILE *stream
, const wchar_t *format
, ...);
222 int wxCRT_VsscanfW(const wchar_t *str
, const wchar_t *format
, va_list ap
);
224 #define wxCRT_ScanfW wscanf
225 #define wxCRT_SscanfW swscanf
226 #define wxCRT_FscanfW fwscanf
227 #define wxCRT_VsscanfW vswscanf
230 // ----------------------------------------------------------------------------
231 // user-friendly wrappers to CRT functions
232 // ----------------------------------------------------------------------------
235 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
236 #define wxPrintf wxPrintf_Impl
237 #define wxFprintf wxFprintf_Impl
238 #define wxSprintf wxSprintf_Impl
239 #define wxSnprintf wxSnprintf_Impl
242 // FIXME-UTF8: remove this
244 #define wxCRT_PrintfNative wxCRT_PrintfW
245 #define wxCRT_FprintfNative wxCRT_FprintfW
247 #define wxCRT_PrintfNative wxCRT_PrintfA
248 #define wxCRT_FprintfNative wxCRT_FprintfA
251 WX_DEFINE_VARARG_FUNC(int, wxPrintf
, 1, (const wxFormatString
&),
252 wxCRT_PrintfNative
, wxCRT_PrintfA
)
253 WX_DEFINE_VARARG_FUNC(int, wxFprintf
, 2, (FILE*, const wxFormatString
&),
254 wxCRT_FprintfNative
, wxCRT_FprintfA
)
256 // va_list versions of printf functions simply forward to the respective
257 // CRT function; note that they assume that va_list was created using
258 // wxArgNormalizer<T>!
259 #if wxUSE_UNICODE_UTF8
260 #if wxUSE_UTF8_LOCALE_ONLY
261 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
264 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
265 if ( wxLocaleIsUtf8 ) return implA args; \
266 else return implW args
268 #elif wxUSE_UNICODE_WCHAR
269 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
272 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
277 wxVprintf(const wxString
& format
, va_list ap
)
279 WX_VARARG_VFOO_IMPL((wxFormatString(format
), ap
),
280 wxCRT_VprintfW
, wxCRT_VprintfA
);
284 wxVfprintf(FILE *f
, const wxString
& format
, va_list ap
)
286 WX_VARARG_VFOO_IMPL((f
, wxFormatString(format
), ap
),
287 wxCRT_VfprintfW
, wxCRT_VfprintfA
);
290 #undef WX_VARARG_VFOO_IMPL
293 // wxSprintf() and friends have to be implemented in two forms, one for
294 // writing to char* buffer and one for writing to wchar_t*:
296 #if !wxUSE_UTF8_LOCALE_ONLY
297 int WXDLLIMPEXP_BASE
wxDoSprintfWchar(char *str
, const wxChar
*format
, ...);
299 #if wxUSE_UNICODE_UTF8
300 int WXDLLIMPEXP_BASE
wxDoSprintfUtf8(char *str
, const char *format
, ...);
302 WX_DEFINE_VARARG_FUNC(int, wxSprintf
, 2, (char*, const wxFormatString
&),
303 wxDoSprintfWchar
, wxDoSprintfUtf8
)
306 wxVsprintf(char *str
, const wxString
& format
, va_list argptr
);
308 #if !wxUSE_UTF8_LOCALE_ONLY
309 int WXDLLIMPEXP_BASE
wxDoSnprintfWchar(char *str
, size_t size
, const wxChar
*format
, ...);
311 #if wxUSE_UNICODE_UTF8
312 int WXDLLIMPEXP_BASE
wxDoSnprintfUtf8(char *str
, size_t size
, const char *format
, ...);
314 WX_DEFINE_VARARG_FUNC(int, wxSnprintf
, 3, (char*, size_t, const wxFormatString
&),
315 wxDoSnprintfWchar
, wxDoSnprintfUtf8
)
318 wxVsnprintf(char *str
, size_t size
, const wxString
& format
, va_list argptr
);
322 #if !wxUSE_UTF8_LOCALE_ONLY
323 int WXDLLIMPEXP_BASE
wxDoSprintfWchar(wchar_t *str
, const wxChar
*format
, ...);
325 #if wxUSE_UNICODE_UTF8
326 int WXDLLIMPEXP_BASE
wxDoSprintfUtf8(wchar_t *str
, const char *format
, ...);
328 WX_DEFINE_VARARG_FUNC(int, wxSprintf
, 2, (wchar_t*, const wxFormatString
&),
329 wxDoSprintfWchar
, wxDoSprintfUtf8
)
332 wxVsprintf(wchar_t *str
, const wxString
& format
, va_list argptr
);
334 #if !wxUSE_UTF8_LOCALE_ONLY
335 int WXDLLIMPEXP_BASE
wxDoSnprintfWchar(wchar_t *str
, size_t size
, const wxChar
*format
, ...);
337 #if wxUSE_UNICODE_UTF8
338 int WXDLLIMPEXP_BASE
wxDoSnprintfUtf8(wchar_t *str
, size_t size
, const char *format
, ...);
340 WX_DEFINE_VARARG_FUNC(int, wxSnprintf
, 3, (wchar_t*, size_t, const wxFormatString
&),
341 wxDoSnprintfWchar
, wxDoSnprintfUtf8
)
344 wxVsnprintf(wchar_t *str
, size_t size
, const wxString
& format
, va_list argptr
);
346 #endif // wxUSE_UNICODE
349 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
351 // fortunately, OpenWatcom implements __VA_ARGS__, so we can provide macros
352 // that cast the format argument to wxString:
358 #define wxPrintf(fmt, ...) \
359 wxPrintf_Impl(wxFormatString(fmt), __VA_ARGS__)
360 #define wxFprintf(f, fmt, ...) \
361 wxFprintf_Impl(f, wxFormatString(fmt), __VA_ARGS__)
362 #define wxSprintf(s, fmt, ...) \
363 wxSprintf_Impl(s, wxFormatString(fmt), __VA_ARGS__)
364 #define wxSnprintf(s, n, fmt, ...) \
365 wxSnprintf_Impl(s, n, wxFormatString(fmt), __VA_ARGS__)
366 #endif // __WATCOMC__
369 // We can't use wxArgNormalizer<T> for variadic arguments to wxScanf() etc.
370 // because they are writable, so instead of providing friendly template
371 // vararg-like functions, we just provide both char* and wchar_t* variants
372 // of these functions. The type of output variadic arguments for %s must match
373 // the type of 'str' and 'format' arguments.
375 // For compatibility with earlier wx versions, we also provide wxSscanf()
376 // version with the first argument (input string) wxString; for this version,
377 // the type of output string values is determined by the type of format string
380 #define _WX_SCANFUNC_EXTRACT_ARGS_1(x) x
381 #define _WX_SCANFUNC_EXTRACT_ARGS_2(x,y) x, y
382 #define _WX_SCANFUNC_EXTRACT_ARGS(N, args) _WX_SCANFUNC_EXTRACT_ARGS_##N args
384 #define _WX_VARARG_PASS_WRITABLE(i) a##i
386 #define _WX_DEFINE_SCANFUNC(N, dummy1, name, impl, passfixed, numfixed, fixed)\
387 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
388 int name(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, fixed), \
389 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
391 return impl(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, passfixed), \
392 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WRITABLE)); \
395 #define WX_DEFINE_SCANFUNC(name, numfixed, fixed, impl, passfixed) \
396 inline int name(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, fixed)) \
398 return impl(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, passfixed)); \
400 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
401 _WX_DEFINE_SCANFUNC, \
402 dummy1, name, impl, passfixed, numfixed, fixed)
404 // this is needed to normalize the format string, see src/common/strvararg.cpp
407 #define wxScanfConvertFormatW(fmt) fmt
410 WXDLLIMPEXP_BASE
wxScanfConvertFormatW(const wchar_t *format
);
413 WX_DEFINE_SCANFUNC(wxScanf
, 1, (const char *format
),
414 wxCRT_ScanfA
, (format
))
415 WX_DEFINE_SCANFUNC(wxScanf
, 1, (const wchar_t *format
),
416 wxCRT_ScanfW
, (wxScanfConvertFormatW(format
)))
418 WX_DEFINE_SCANFUNC(wxFscanf
, 2, (FILE *stream
, const char *format
),
419 wxCRT_FscanfA
, (stream
, format
))
420 WX_DEFINE_SCANFUNC(wxFscanf
, 2, (FILE *stream
, const wchar_t *format
),
421 wxCRT_FscanfW
, (stream
, wxScanfConvertFormatW(format
)))
423 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const char *str
, const char *format
),
424 wxCRT_SscanfA
, (str
, format
))
425 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wchar_t *str
, const wchar_t *format
),
426 wxCRT_SscanfW
, (str
, wxScanfConvertFormatW(format
)))
427 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wxCharBuffer
& str
, const char *format
),
428 wxCRT_SscanfA
, (str
.data(), format
))
429 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wxWCharBuffer
& str
, const wchar_t *format
),
430 wxCRT_SscanfW
, (str
.data(), wxScanfConvertFormatW(format
)))
431 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wxString
& str
, const char *format
),
432 wxCRT_SscanfA
, (str
.mb_str(), format
))
433 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wxString
& str
, const wchar_t *format
),
434 wxCRT_SscanfW
, (str
.wc_str(), wxScanfConvertFormatW(format
)))
435 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wxCStrData
& str
, const char *format
),
436 wxCRT_SscanfA
, (str
.AsCharBuf(), format
))
437 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wxCStrData
& str
, const wchar_t *format
),
438 wxCRT_SscanfW
, (str
.AsWCharBuf(), wxScanfConvertFormatW(format
)))
440 // Visual C++ doesn't provide vsscanf()
442 int WXDLLIMPEXP_BASE
wxVsscanf(const char *str
, const char *format
, va_list ap
);
443 int WXDLLIMPEXP_BASE
wxVsscanf(const wchar_t *str
, const wchar_t *format
, va_list ap
);
444 int WXDLLIMPEXP_BASE
wxVsscanf(const wxCharBuffer
& str
, const char *format
, va_list ap
);
445 int WXDLLIMPEXP_BASE
wxVsscanf(const wxWCharBuffer
& str
, const wchar_t *format
, va_list ap
);
446 int WXDLLIMPEXP_BASE
wxVsscanf(const wxString
& str
, const char *format
, va_list ap
);
447 int WXDLLIMPEXP_BASE
wxVsscanf(const wxString
& str
, const wchar_t *format
, va_list ap
);
448 int WXDLLIMPEXP_BASE
wxVsscanf(const wxCStrData
& str
, const char *format
, va_list ap
);
449 int WXDLLIMPEXP_BASE
wxVsscanf(const wxCStrData
& str
, const wchar_t *format
, va_list ap
);
450 #endif // !__VISUALC__
452 #endif /* _WX_WXCRTVARARG_H_ */