]>
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_Scanf _tscanf
33 #define wxCRT_Sscanf _stscanf
34 #define wxCRT_Vfprintf _vftprintf
35 #define wxCRT_Vprintf _vtprintf
36 #define wxCRT_Vsscanf _vstscanf
37 #define wxCRT_Vsprintf _vstprintf
39 #else /* !TCHAR-aware compilers */
41 #if !wxUSE_UNICODE /* ASCII */
42 #define wxCRT_Fprintf fprintf
43 #define wxCRT_Fscanf fscanf
44 #define wxCRT_Printf printf
45 #define wxCRT_Scanf scanf
46 #define wxCRT_Sscanf sscanf
47 #define wxCRT_Vfprintf vfprintf
48 #define wxCRT_Vprintf vprintf
49 #define wxCRT_Vsscanf vsscanf
50 #define wxCRT_Vsprintf vsprintf
52 #endif /* TCHAR-aware compilers/the others */
54 /* printf() family saga */
57 For some systems [v]snprintf() exists in the system libraries but not in the
58 headers, so we need to declare it ourselves to be able to use it.
60 #if defined(HAVE_VSNPRINTF) && !defined(HAVE_VSNPRINTF_DECL)
66 int vsnprintf(char *str
, size_t size
, const char *format
, va_list ap
);
67 #endif /* !HAVE_VSNPRINTF_DECL */
69 #if defined(HAVE_SNPRINTF) && !defined(HAVE_SNPRINTF_DECL)
75 WXDLLIMPEXP_BASE
int snprintf(char *str
, size_t size
, const char *format
, ...);
76 #endif /* !HAVE_SNPRINTF_DECL */
78 /* Wrapper for vsnprintf if it's 3rd parameter is non-const. Note: the
79 * same isn't done for snprintf below, the builtin wxSnprintf_ is used
80 * instead since it's already a simple wrapper */
81 #if defined __cplusplus && defined HAVE_BROKEN_VSNPRINTF_DECL
82 inline int wx_fixed_vsnprintf(char *str
, size_t size
, const char *format
, va_list ap
)
84 return vsnprintf(str
, size
, (char*)format
, ap
);
89 MinGW MSVCRT has non-standard vswprintf() (for MSVC compatibility
90 presumably) and normally _vsnwprintf() is used instead
92 #if defined(HAVE_VSWPRINTF) && defined(__MINGW32__)
96 #if wxUSE_PRINTF_POS_PARAMS
98 The systems where vsnprintf() supports positional parameters should
99 define the HAVE_UNIX98_PRINTF symbol.
101 On systems which don't (e.g. Windows) we are forced to use
102 our wxVsnprintf() implementation.
104 #if defined(HAVE_UNIX98_PRINTF)
106 #ifdef HAVE_VSWPRINTF
107 #define wxVsnprintf_ vswprintf
110 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
111 #define wxVsnprintf_ wx_fixed_vsnprintf
113 #define wxVsnprintf_ 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__
125 #define wxVsnprintf_ _vswprintf_p
127 #define wxVsnprintf_ _vsprintf_p
130 #endif /* HAVE_UNIX98_PRINTF/!HAVE_UNIX98_PRINTF */
131 #else /* !wxUSE_PRINTF_POS_PARAMS */
133 We always want to define safe snprintf() function to be used instead of
134 sprintf(). Some compilers already have it (or rather vsnprintf() which
135 we really need...), otherwise we implement it using our own printf()
138 We define function with a trailing underscore here because the real one
139 is a wrapper around it as explained below
142 /* first deal with TCHAR-aware compilers which have _vsntprintf */
144 #if defined(__VISUALC__) || \
145 (defined(__BORLANDC__) && __BORLANDC__ >= 0x540)
146 #define wxVsnprintf_ _vsntprintf
150 /* if this didn't work, define it separately for Unicode and ANSI builds */
153 #if defined(HAVE__VSNWPRINTF)
154 #define wxVsnprintf_ _vsnwprintf
155 #elif defined(HAVE_VSWPRINTF)
156 #define wxVsnprintf_ vswprintf
157 #elif defined(__WATCOMC__)
158 #define wxVsnprintf_ _vsnwprintf
162 All versions of CodeWarrior supported by wxWidgets apparently
163 have both snprintf() and vsnprintf()
165 #if defined(HAVE_VSNPRINTF) \
166 || defined(__MWERKS__) || defined(__WATCOMC__)
167 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
168 #define wxVsnprintf_ wx_fixed_vsnprintf
170 #define wxVsnprintf_ vsnprintf
173 #endif /* Unicode/ASCII */
174 #endif /* wxVsnprintf_ */
175 #endif /* wxUSE_PRINTF_POS_PARAMS/!wxUSE_PRINTF_POS_PARAMS */
178 /* no (suitable) vsnprintf(), cook our own */
180 wxVsnprintf_(wxChar
*buf
, size_t len
, const wxChar
*format
, va_list argptr
);
182 #define wxUSE_WXVSNPRINTF 1
184 #define wxUSE_WXVSNPRINTF 0
188 In Unicode mode we need to have all standard functions such as wprintf() and
189 so on but not all systems have them so use our own implementations in this
192 #if wxUSE_UNICODE && !defined(wxHAVE_TCHAR_SUPPORT) && !defined(HAVE_WPRINTF)
193 #define wxNEED_WPRINTF
197 More Unicode complications: although both ANSI C and C++ define a number of
198 wide character functions such as wprintf(), not all environments have them.
199 Worse, those which do have different behaviours: under Windows, %s format
200 specifier changes its meaning in Unicode build and expects a Unicode string
201 while under Unix/POSIX it still means an ASCII string even for wprintf() and
202 %ls has to be used for wide strings.
204 We choose to always emulate Windows behaviour as more useful for us so even
205 if we have wprintf() we still must wrap it in a non trivial wxPrintf().
209 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
211 we need to implement all wide character printf and scanf functions
212 either because we don't have them at all or because they don't have the
215 int wxCRT_Scanf( const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_1
;
216 int wxCRT_Sscanf( const wxChar
*str
, const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_2
;
217 int wxCRT_Fscanf( FILE *stream
, const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_2
;
218 int wxCRT_Vsscanf( const wxChar
*str
, const wxChar
*format
, va_list ap
);
219 int wxCRT_Printf( const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_1
;
220 int wxCRT_Fprintf( FILE *stream
, const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_2
;
221 int wxCRT_Vfprintf( FILE *stream
, const wxChar
*format
, va_list ap
);
222 int wxCRT_Vprintf( const wxChar
*format
, va_list ap
);
223 int wxCRT_Vsprintf( wxChar
*str
, const wxChar
*format
, va_list ap
);
224 #endif /* wxNEED_PRINTF_CONVERSION */
226 /* these 2 can be simply mapped to the versions with underscore at the end */
227 /* if we don't have to do the conversion */
229 However, if we don't have any vswprintf() at all we don't need to redefine
230 anything as our own wxVsnprintf_() already behaves as needed.
232 #if defined(wxNEED_PRINTF_CONVERSION) && defined(wxVsnprintf_)
233 int wxCRT_Vsnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, va_list ap
);
235 #define wxCRT_Vsnprintf wxVsnprintf_
239 // ----------------------------------------------------------------------------
240 // user-friendly wrappers to CRT functions
241 // ----------------------------------------------------------------------------
244 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
245 #define wxPrintf wxPrintf_Impl
246 #define wxFprintf wxFprintf_Impl
247 #define wxSprintf wxSprintf_Impl
248 #define wxSnprintf wxSnprintf_Impl
251 // FIXME-UTF8: explicit wide-string and short-string format specifiers
252 // (%hs, %ls and variants) are currently broken, only %s works
253 // as expected (regardless of the build)
255 // FIXME-UTF8: %c (and %hc, %lc) don't work as expected either: in UTF-8 build,
256 // we should replace them with %s (as some Unicode chars may be
257 // encoded with >1 bytes) and in all builds, we should use wchar_t
258 // for all characters and convert char to it;
259 // we'll also need wxArgNormalizer<T> specializations for char,
260 // wchar_t, wxUniChar and wxUniCharRef to handle this correctly
262 WX_DEFINE_VARARG_FUNC2(int, wxPrintf
, 1, (const wxString
&),
263 wxCRT_Printf
, printf
)
264 WX_DEFINE_VARARG_FUNC2(int, wxFprintf
, 2, (FILE*, const wxString
&),
265 wxCRT_Fprintf
, fprintf
)
267 // va_list versions of printf functions simply forward to the respective
268 // CRT function; note that they assume that va_list was created using
269 // wxArgNormalizer<T>!
270 #if wxUSE_UNICODE_UTF8
271 #if wxUSE_UTF8_LOCALE_ONLY
272 #define WX_VARARG_VFOO_IMPL(args, implWchar, implUtf8) \
275 #define WX_VARARG_VFOO_IMPL(args, implWchar, implUtf8) \
276 if ( wxLocaleIsUtf8 ) return implUtf8 args; \
277 else return implWchar args
279 #else // wxUSE_UNICODE_WCHAR / ANSI
280 #define WX_VARARG_VFOO_IMPL(args, implWchar, implUtf8) \
281 return implWchar args
285 wxVprintf(const wxString
& format
, va_list ap
)
287 WX_VARARG_VFOO_IMPL((format
, ap
), wxCRT_Vprintf
, vprintf
);
291 wxVfprintf(FILE *f
, const wxString
& format
, va_list ap
)
293 WX_VARARG_VFOO_IMPL((f
, format
, ap
), wxCRT_Vfprintf
, vfprintf
);
296 #undef WX_VARARG_VFOO_IMPL
299 // wxSprintf() and friends have to be implemented in two forms, one for
300 // writing to char* buffer and one for writing to wchar_t*:
302 int WXDLLIMPEXP_BASE
wxDoSprintf(char *str
, const wxString
& format
, ...);
303 WX_DEFINE_VARARG_FUNC(int, wxSprintf
, 2, (char*, const wxString
&),
307 wxVsprintf(char *str
, const wxString
& format
, va_list argptr
);
309 int WXDLLIMPEXP_BASE
wxDoSnprintf(char *str
, size_t size
, const wxString
& format
, ...);
310 WX_DEFINE_VARARG_FUNC(int, wxSnprintf
, 3, (char*, size_t, const wxString
&),
314 wxVsnprintf(char *str
, size_t size
, const wxString
& format
, va_list argptr
);
317 int WXDLLIMPEXP_BASE
wxDoSprintf(wchar_t *str
, const wxString
& format
, ...);
318 WX_DEFINE_VARARG_FUNC(int, wxSprintf
, 2, (wchar_t*, const wxString
&),
322 wxVsprintf(wchar_t *str
, const wxString
& format
, va_list argptr
);
324 int WXDLLIMPEXP_BASE
wxDoSnprintf(wchar_t *str
, size_t size
, const wxString
& format
, ...);
325 WX_DEFINE_VARARG_FUNC(int, wxSnprintf
, 3, (wchar_t*, size_t, const wxString
&),
329 wxVsnprintf(wchar_t *str
, size_t size
, const wxString
& format
, va_list argptr
);
330 #endif // wxUSE_UNICODE
333 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
335 // fortunately, OpenWatcom implements __VA_ARGS__, so we can provide macros
336 // that cast the format argument to wxString:
342 #define wxPrintf(fmt, ...) \
343 wxPrintf_Impl(wxString(fmt), __VA_ARGS__)
344 #define wxFprintf(f, fmt, ...) \
345 wxFprintf_Impl(f, wxString(fmt), __VA_ARGS__)
346 #define wxSprintf(s, fmt, ...) \
347 wxSprintf_Impl(s, wxString(fmt), __VA_ARGS__)
348 #define wxSnprintf(s, n, fmt, ...) \
349 wxSnprintf_Impl(s, n, wxString(fmt), __VA_ARGS__)
350 #endif // __WATCOMC__
353 // We can't use wxArgNormalizer<T> for variadic arguments to wxScanf() etc.
354 // because they are writable, so instead of providing friendly template
355 // vararg-like functions, we just provide both char* and wchar_t* variants
356 // of these functions. The type of output variadic arguments for %s must match
357 // the type of 'str' and 'format' arguments.
359 // FIXME-UTF8: actually do it, for now we only have wxChar* variants:
360 #define wxScanf wxCRT_Scanf
361 #define wxFscanf wxCRT_Fscanf
362 #define wxSscanf wxCRT_Sscanf
363 #define wxVsscanf wxCRT_Vsscanf
365 #endif /* _WX_WXCRTVARARG_H_ */