]>
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_
17 #include "wx/strvararg.h"
19 #include "wx/string.h"
21 // ----------------------------------------------------------------------------
22 // CRT functions aliases
23 // ----------------------------------------------------------------------------
25 /* Required for wxPrintf() etc */
28 #ifdef wxHAVE_TCHAR_SUPPORT
29 #define wxCRT_Fprintf _ftprintf
30 #define wxCRT_Printf _tprintf
31 #define wxCRT_Vfprintf _vftprintf
32 #define wxCRT_Vprintf _vtprintf
33 #define wxCRT_Vsprintf _vstprintf
34 #else /* !TCHAR-aware compilers */
36 #if !wxUSE_UNICODE /* ASCII */
37 #define wxCRT_Fprintf fprintf
38 #define wxCRT_Printf printf
39 #define wxCRT_Vfprintf vfprintf
40 #define wxCRT_Vprintf vprintf
41 #define wxCRT_Vsprintf vsprintf
43 #endif /* TCHAR-aware compilers/the others */
46 /* printf() family saga */
49 For some systems [v]snprintf() exists in the system libraries but not in the
50 headers, so we need to declare it ourselves to be able to use it.
52 #if defined(HAVE_VSNPRINTF) && !defined(HAVE_VSNPRINTF_DECL)
58 int vsnprintf(char *str
, size_t size
, const char *format
, va_list ap
);
59 #endif /* !HAVE_VSNPRINTF_DECL */
61 #if defined(HAVE_SNPRINTF) && !defined(HAVE_SNPRINTF_DECL)
67 WXDLLIMPEXP_BASE
int snprintf(char *str
, size_t size
, const char *format
, ...);
68 #endif /* !HAVE_SNPRINTF_DECL */
70 /* Wrapper for vsnprintf if it's 3rd parameter is non-const. Note: the
71 * same isn't done for snprintf below, the builtin wxSnprintf_ is used
72 * instead since it's already a simple wrapper */
73 #if defined __cplusplus && defined HAVE_BROKEN_VSNPRINTF_DECL
74 inline int wx_fixed_vsnprintf(char *str
, size_t size
, const char *format
, va_list ap
)
76 return vsnprintf(str
, size
, (char*)format
, ap
);
81 MinGW MSVCRT has non-standard vswprintf() (for MSVC compatibility
82 presumably) and normally _vsnwprintf() is used instead
84 #if defined(HAVE_VSWPRINTF) && defined(__MINGW32__)
88 #if wxUSE_PRINTF_POS_PARAMS
90 The systems where vsnprintf() supports positional parameters should
91 define the HAVE_UNIX98_PRINTF symbol.
93 On systems which don't (e.g. Windows) we are forced to use
94 our wxVsnprintf() implementation.
96 #if defined(HAVE_UNIX98_PRINTF)
99 #define wxVsnprintf_ vswprintf
102 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
103 #define wxVsnprintf_ wx_fixed_vsnprintf
105 #define wxVsnprintf_ vsnprintf
108 #else /* !HAVE_UNIX98_PRINTF */
110 The only compiler with positional parameters support under Windows
111 is VC++ 8.0 which provides a new xxprintf_p() functions family.
112 The 2003 PSDK includes a slightly earlier version of VC8 than the
113 main release and does not have the printf_p functions.
115 #if defined _MSC_FULL_VER && _MSC_FULL_VER >= 140050727 && !defined __WXWINCE__
117 #define wxVsnprintf_ _vswprintf_p
119 #define wxVsnprintf_ _vsprintf_p
122 #endif /* HAVE_UNIX98_PRINTF/!HAVE_UNIX98_PRINTF */
123 #else /* !wxUSE_PRINTF_POS_PARAMS */
125 We always want to define safe snprintf() function to be used instead of
126 sprintf(). Some compilers already have it (or rather vsnprintf() which
127 we really need...), otherwise we implement it using our own printf()
130 We define function with a trailing underscore here because the real one
131 is a wrapper around it as explained below
134 /* first deal with TCHAR-aware compilers which have _vsntprintf */
136 #if defined(__VISUALC__) || \
137 (defined(__BORLANDC__) && __BORLANDC__ >= 0x540)
138 #define wxVsnprintf_ _vsntprintf
142 /* if this didn't work, define it separately for Unicode and ANSI builds */
145 #if defined(HAVE__VSNWPRINTF)
146 #define wxVsnprintf_ _vsnwprintf
147 #elif defined(HAVE_VSWPRINTF)
148 #define wxVsnprintf_ vswprintf
149 #elif defined(__WATCOMC__)
150 #define wxVsnprintf_ _vsnwprintf
154 All versions of CodeWarrior supported by wxWidgets apparently
155 have both snprintf() and vsnprintf()
157 #if defined(HAVE_VSNPRINTF) \
158 || defined(__MWERKS__) || defined(__WATCOMC__)
159 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
160 #define wxVsnprintf_ wx_fixed_vsnprintf
162 #define wxVsnprintf_ vsnprintf
165 #endif /* Unicode/ASCII */
166 #endif /* wxVsnprintf_ */
167 #endif /* wxUSE_PRINTF_POS_PARAMS/!wxUSE_PRINTF_POS_PARAMS */
170 /* no (suitable) vsnprintf(), cook our own */
172 wxVsnprintf_(wxChar
*buf
, size_t len
, const wxChar
*format
, va_list argptr
);
174 #define wxUSE_WXVSNPRINTF 1
176 #define wxUSE_WXVSNPRINTF 0
180 In Unicode mode we need to have all standard functions such as wprintf() and
181 so on but not all systems have them so use our own implementations in this
184 #if wxUSE_UNICODE && !defined(wxHAVE_TCHAR_SUPPORT) && !defined(HAVE_WPRINTF)
185 #define wxNEED_WPRINTF
189 More Unicode complications: although both ANSI C and C++ define a number of
190 wide character functions such as wprintf(), not all environments have them.
191 Worse, those which do have different behaviours: under Windows, %s format
192 specifier changes its meaning in Unicode build and expects a Unicode string
193 while under Unix/POSIX it still means an ASCII string even for wprintf() and
194 %ls has to be used for wide strings.
196 We choose to always emulate Windows behaviour as more useful for us so even
197 if we have wprintf() we still must wrap it in a non trivial wxPrintf().
201 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
203 we need to implement all wide character printf functions either because
204 we don't have them at all or because they don't have the semantics we
207 int wxCRT_Printf( const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_1
;
208 int wxCRT_Fprintf( FILE *stream
, const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_2
;
209 int wxCRT_Vfprintf( FILE *stream
, const wxChar
*format
, va_list ap
);
210 int wxCRT_Vprintf( const wxChar
*format
, va_list ap
);
211 int wxCRT_Vsprintf( wxChar
*str
, const wxChar
*format
, va_list ap
);
212 #endif /* wxNEED_PRINTF_CONVERSION */
214 /* these 2 can be simply mapped to the versions with underscore at the end */
215 /* if we don't have to do the conversion */
217 However, if we don't have any vswprintf() at all we don't need to redefine
218 anything as our own wxVsnprintf_() already behaves as needed.
220 #if defined(wxNEED_PRINTF_CONVERSION) && defined(wxVsnprintf_)
221 int wxCRT_Vsnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, va_list ap
);
223 #define wxCRT_Vsnprintf wxVsnprintf_
227 /* Required for wxScanf() etc. */
228 #define wxCRT_ScanfA scanf
229 #define wxCRT_SscanfA sscanf
230 #define wxCRT_FscanfA fscanf
231 #define wxCRT_VsscanfA vsscanf
233 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
234 int wxCRT_ScanfW(const wchar_t *format
, ...) ATTRIBUTE_PRINTF_1
;
235 int wxCRT_SscanfW(const wchar_t *str
, const wchar_t *format
, ...) ATTRIBUTE_PRINTF_2
;
236 int wxCRT_FscanfW(FILE *stream
, const wchar_t *format
, ...) ATTRIBUTE_PRINTF_2
;
237 int wxCRT_VsscanfW(const wchar_t *str
, const wchar_t *format
, va_list ap
);
239 #define wxCRT_ScanfW wscanf
240 #define wxCRT_SscanfW swscanf
241 #define wxCRT_FscanfW fwscanf
242 #define wxCRT_VsscanfW vswscanf
245 // ----------------------------------------------------------------------------
246 // user-friendly wrappers to CRT functions
247 // ----------------------------------------------------------------------------
250 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
251 #define wxPrintf wxPrintf_Impl
252 #define wxFprintf wxFprintf_Impl
253 #define wxSprintf wxSprintf_Impl
254 #define wxSnprintf wxSnprintf_Impl
257 // FIXME-UTF8: explicit wide-string and short-string format specifiers
258 // (%hs, %ls and variants) are currently broken, only %s works
259 // as expected (regardless of the build)
261 // FIXME-UTF8: %c (and %hc, %lc) don't work as expected either: in UTF-8 build,
262 // we should replace them with %s (as some Unicode chars may be
263 // encoded with >1 bytes) and in all builds, we should use wchar_t
264 // for all characters and convert char to it;
265 // we'll also need wxArgNormalizer<T> specializations for char,
266 // wchar_t, wxUniChar and wxUniCharRef to handle this correctly
268 WX_DEFINE_VARARG_FUNC(int, wxPrintf
, 1, (const wxFormatString
&),
269 wxCRT_Printf
, printf
)
270 WX_DEFINE_VARARG_FUNC(int, wxFprintf
, 2, (FILE*, const wxFormatString
&),
271 wxCRT_Fprintf
, fprintf
)
273 // va_list versions of printf functions simply forward to the respective
274 // CRT function; note that they assume that va_list was created using
275 // wxArgNormalizer<T>!
276 #if wxUSE_UNICODE_UTF8
277 #if wxUSE_UTF8_LOCALE_ONLY
278 #define WX_VARARG_VFOO_IMPL(args, implWchar, implUtf8) \
281 #define WX_VARARG_VFOO_IMPL(args, implWchar, implUtf8) \
282 if ( wxLocaleIsUtf8 ) return implUtf8 args; \
283 else return implWchar args
285 #else // wxUSE_UNICODE_WCHAR / ANSI
286 #define WX_VARARG_VFOO_IMPL(args, implWchar, implUtf8) \
287 return implWchar args
291 wxVprintf(const wxString
& format
, va_list ap
)
293 WX_VARARG_VFOO_IMPL((wxFormatString(format
), ap
),
294 wxCRT_Vprintf
, vprintf
);
298 wxVfprintf(FILE *f
, const wxString
& format
, va_list ap
)
300 WX_VARARG_VFOO_IMPL((f
, wxFormatString(format
), ap
),
301 wxCRT_Vfprintf
, vfprintf
);
304 #undef WX_VARARG_VFOO_IMPL
307 // wxSprintf() and friends have to be implemented in two forms, one for
308 // writing to char* buffer and one for writing to wchar_t*:
310 #if !wxUSE_UTF8_LOCALE_ONLY
311 int WXDLLIMPEXP_BASE
wxDoSprintfWchar(char *str
, const wxChar
*format
, ...);
313 #if wxUSE_UNICODE_UTF8
314 int WXDLLIMPEXP_BASE
wxDoSprintfUtf8(char *str
, const char *format
, ...);
316 WX_DEFINE_VARARG_FUNC(int, wxSprintf
, 2, (char*, const wxFormatString
&),
317 wxDoSprintfWchar
, wxDoSprintfUtf8
)
320 wxVsprintf(char *str
, const wxString
& format
, va_list argptr
);
322 #if !wxUSE_UTF8_LOCALE_ONLY
323 int WXDLLIMPEXP_BASE
wxDoSnprintfWchar(char *str
, size_t size
, const wxChar
*format
, ...);
325 #if wxUSE_UNICODE_UTF8
326 int WXDLLIMPEXP_BASE
wxDoSnprintfUtf8(char *str
, size_t size
, const char *format
, ...);
328 WX_DEFINE_VARARG_FUNC(int, wxSnprintf
, 3, (char*, size_t, const wxFormatString
&),
329 wxDoSnprintfWchar
, wxDoSnprintfUtf8
)
332 wxVsnprintf(char *str
, size_t size
, const wxString
& format
, va_list argptr
);
336 #if !wxUSE_UTF8_LOCALE_ONLY
337 int WXDLLIMPEXP_BASE
wxDoSprintfWchar(wchar_t *str
, const wxChar
*format
, ...);
339 #if wxUSE_UNICODE_UTF8
340 int WXDLLIMPEXP_BASE
wxDoSprintfUtf8(wchar_t *str
, const char *format
, ...);
342 WX_DEFINE_VARARG_FUNC(int, wxSprintf
, 2, (wchar_t*, const wxFormatString
&),
343 wxDoSprintfWchar
, wxDoSprintfUtf8
)
346 wxVsprintf(wchar_t *str
, const wxString
& format
, va_list argptr
);
348 #if !wxUSE_UTF8_LOCALE_ONLY
349 int WXDLLIMPEXP_BASE
wxDoSnprintfWchar(wchar_t *str
, size_t size
, const wxChar
*format
, ...);
351 #if wxUSE_UNICODE_UTF8
352 int WXDLLIMPEXP_BASE
wxDoSnprintfUtf8(wchar_t *str
, size_t size
, const char *format
, ...);
354 WX_DEFINE_VARARG_FUNC(int, wxSnprintf
, 3, (wchar_t*, size_t, const wxFormatString
&),
355 wxDoSnprintfWchar
, wxDoSnprintfUtf8
)
358 wxVsnprintf(wchar_t *str
, size_t size
, const wxString
& format
, va_list argptr
);
360 #endif // wxUSE_UNICODE
363 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
365 // fortunately, OpenWatcom implements __VA_ARGS__, so we can provide macros
366 // that cast the format argument to wxString:
372 #define wxPrintf(fmt, ...) \
373 wxPrintf_Impl(wxFormatString(fmt), __VA_ARGS__)
374 #define wxFprintf(f, fmt, ...) \
375 wxFprintf_Impl(f, wxFormatString(fmt), __VA_ARGS__)
376 #define wxSprintf(s, fmt, ...) \
377 wxSprintf_Impl(s, wxFormatString(fmt), __VA_ARGS__)
378 #define wxSnprintf(s, n, fmt, ...) \
379 wxSnprintf_Impl(s, n, wxFormatString(fmt), __VA_ARGS__)
380 #endif // __WATCOMC__
383 // We can't use wxArgNormalizer<T> for variadic arguments to wxScanf() etc.
384 // because they are writable, so instead of providing friendly template
385 // vararg-like functions, we just provide both char* and wchar_t* variants
386 // of these functions. The type of output variadic arguments for %s must match
387 // the type of 'str' and 'format' arguments.
389 // For compatibility with earlier wx versions, we also provide wxSscanf()
390 // version with the first argument (input string) wxString; for this version,
391 // the type of output string values is determined by the type of format string
394 #define _WX_SCANFUNC_EXTRACT_ARGS_1(x) x
395 #define _WX_SCANFUNC_EXTRACT_ARGS_2(x,y) x, y
396 #define _WX_SCANFUNC_EXTRACT_ARGS(N, args) _WX_SCANFUNC_EXTRACT_ARGS_##N args
398 #define _WX_VARARG_PASS_WRITABLE(i) a##i
400 #define _WX_DEFINE_SCANFUNC(N, dummy1, name, impl, passfixed, numfixed, fixed)\
401 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
402 int name(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, fixed), \
403 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
405 return impl(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, passfixed), \
406 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WRITABLE)); \
409 #define WX_DEFINE_SCANFUNC(name, numfixed, fixed, impl, passfixed) \
410 inline int name(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, fixed)) \
412 return impl(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, passfixed)); \
414 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
415 _WX_DEFINE_SCANFUNC, \
416 dummy1, name, impl, passfixed, numfixed, fixed)
418 WX_DEFINE_SCANFUNC(wxScanf
, 1, (const char *format
),
419 wxCRT_ScanfA
, (format
))
420 WX_DEFINE_SCANFUNC(wxScanf
, 1, (const wchar_t *format
),
421 wxCRT_ScanfW
, (format
))
423 WX_DEFINE_SCANFUNC(wxFscanf
, 2, (FILE *stream
, const char *format
),
424 wxCRT_FscanfA
, (stream
, format
))
425 WX_DEFINE_SCANFUNC(wxFscanf
, 2, (FILE *stream
, const wchar_t *format
),
426 wxCRT_FscanfW
, (stream
, format
))
428 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const char *str
, const char *format
),
429 wxCRT_SscanfA
, (str
, format
))
430 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wchar_t *str
, const wchar_t *format
),
431 wxCRT_SscanfW
, (str
, format
))
432 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wxCharBuffer
& str
, const char *format
),
433 wxCRT_SscanfA
, (str
.data(), format
))
434 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wxWCharBuffer
& str
, const wchar_t *format
),
435 wxCRT_SscanfW
, (str
.data(), format
))
436 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wxString
& str
, const char *format
),
437 wxCRT_SscanfA
, (str
.mb_str(), format
))
438 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wxString
& str
, const wchar_t *format
),
439 wxCRT_SscanfW
, (str
.wc_str(), format
))
440 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wxCStrData
& str
, const char *format
),
441 wxCRT_SscanfA
, (str
.AsCharBuf(), format
))
442 WX_DEFINE_SCANFUNC(wxSscanf
, 2, (const wxCStrData
& str
, const wchar_t *format
),
443 wxCRT_SscanfW
, (str
.AsWCharBuf(), format
))
445 // Visual C++ doesn't provide vsscanf()
447 int WXDLLIMPEXP_BASE
wxVsscanf(const char *str
, const char *format
, va_list ap
);
448 int WXDLLIMPEXP_BASE
wxVsscanf(const wchar_t *str
, const wchar_t *format
, va_list ap
);
449 int WXDLLIMPEXP_BASE
wxVsscanf(const wxCharBuffer
& str
, const char *format
, va_list ap
);
450 int WXDLLIMPEXP_BASE
wxVsscanf(const wxWCharBuffer
& str
, const wchar_t *format
, va_list ap
);
451 int WXDLLIMPEXP_BASE
wxVsscanf(const wxString
& str
, const char *format
, va_list ap
);
452 int WXDLLIMPEXP_BASE
wxVsscanf(const wxString
& str
, const wchar_t *format
, va_list ap
);
453 int WXDLLIMPEXP_BASE
wxVsscanf(const wxCStrData
& str
, const char *format
, va_list ap
);
454 int WXDLLIMPEXP_BASE
wxVsscanf(const wxCStrData
& str
, const wchar_t *format
, va_list ap
);
455 #endif // !__VISUALC__
457 #endif /* _WX_WXCRTVARARG_H_ */