fixed wxScanf() etc. to compile with Visual C++ again
[wxWidgets.git] / 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
7 // Created: 2007-02-19
8 // RCS-ID: $Id$
9 // Copyright: (c) 2007 REA Elektronik GmbH
10 // Licence: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_WXCRTVARARG_H_
14 #define _WX_WXCRTVARARG_H_
15
16 #include "wx/wxcrt.h"
17 #include "wx/strvararg.h"
18
19 #include "wx/string.h"
20
21 // ----------------------------------------------------------------------------
22 // CRT functions aliases
23 // ----------------------------------------------------------------------------
24
25 /* Required for wxPrintf() etc */
26 #include <stdarg.h>
27
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 */
35
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
42 #endif /* ASCII */
43 #endif /* TCHAR-aware compilers/the others */
44
45
46 /* printf() family saga */
47
48 /*
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.
51 */
52 #if defined(HAVE_VSNPRINTF) && !defined(HAVE_VSNPRINTF_DECL)
53 #ifdef __cplusplus
54 extern "C"
55 #else
56 extern
57 #endif
58 int vsnprintf(char *str, size_t size, const char *format, va_list ap);
59 #endif /* !HAVE_VSNPRINTF_DECL */
60
61 #if defined(HAVE_SNPRINTF) && !defined(HAVE_SNPRINTF_DECL)
62 #ifdef __cplusplus
63 extern "C"
64 #else
65 extern
66 #endif
67 WXDLLIMPEXP_BASE int snprintf(char *str, size_t size, const char *format, ...);
68 #endif /* !HAVE_SNPRINTF_DECL */
69
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)
75 {
76 return vsnprintf(str, size, (char*)format, ap);
77 }
78 #endif
79
80 /*
81 MinGW MSVCRT has non-standard vswprintf() (for MSVC compatibility
82 presumably) and normally _vsnwprintf() is used instead
83 */
84 #if defined(HAVE_VSWPRINTF) && defined(__MINGW32__)
85 #undef HAVE_VSWPRINTF
86 #endif
87
88 #if wxUSE_PRINTF_POS_PARAMS
89 /*
90 The systems where vsnprintf() supports positional parameters should
91 define the HAVE_UNIX98_PRINTF symbol.
92
93 On systems which don't (e.g. Windows) we are forced to use
94 our wxVsnprintf() implementation.
95 */
96 #if defined(HAVE_UNIX98_PRINTF)
97 #if wxUSE_UNICODE
98 #ifdef HAVE_VSWPRINTF
99 #define wxVsnprintf_ vswprintf
100 #endif
101 #else /* ASCII */
102 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
103 #define wxVsnprintf_ wx_fixed_vsnprintf
104 #else
105 #define wxVsnprintf_ vsnprintf
106 #endif
107 #endif
108 #else /* !HAVE_UNIX98_PRINTF */
109 /*
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.
114 */
115 #if defined _MSC_FULL_VER && _MSC_FULL_VER >= 140050727 && !defined __WXWINCE__
116 #if wxUSE_UNICODE
117 #define wxVsnprintf_ _vswprintf_p
118 #else
119 #define wxVsnprintf_ _vsprintf_p
120 #endif
121 #endif
122 #endif /* HAVE_UNIX98_PRINTF/!HAVE_UNIX98_PRINTF */
123 #else /* !wxUSE_PRINTF_POS_PARAMS */
124 /*
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()
128 code.
129
130 We define function with a trailing underscore here because the real one
131 is a wrapper around it as explained below
132 */
133
134 /* first deal with TCHAR-aware compilers which have _vsntprintf */
135 #ifndef wxVsnprintf_
136 #if defined(__VISUALC__) || \
137 (defined(__BORLANDC__) && __BORLANDC__ >= 0x540)
138 #define wxVsnprintf_ _vsntprintf
139 #endif
140 #endif
141
142 /* if this didn't work, define it separately for Unicode and ANSI builds */
143 #ifndef wxVsnprintf_
144 #if wxUSE_UNICODE
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
151 #endif
152 #else /* ASCII */
153 /*
154 All versions of CodeWarrior supported by wxWidgets apparently
155 have both snprintf() and vsnprintf()
156 */
157 #if defined(HAVE_VSNPRINTF) \
158 || defined(__MWERKS__) || defined(__WATCOMC__)
159 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
160 #define wxVsnprintf_ wx_fixed_vsnprintf
161 #else
162 #define wxVsnprintf_ vsnprintf
163 #endif
164 #endif
165 #endif /* Unicode/ASCII */
166 #endif /* wxVsnprintf_ */
167 #endif /* wxUSE_PRINTF_POS_PARAMS/!wxUSE_PRINTF_POS_PARAMS */
168
169 #ifndef wxVsnprintf_
170 /* no (suitable) vsnprintf(), cook our own */
171 WXDLLIMPEXP_BASE int
172 wxVsnprintf_(wxChar *buf, size_t len, const wxChar *format, va_list argptr);
173
174 #define wxUSE_WXVSNPRINTF 1
175 #else
176 #define wxUSE_WXVSNPRINTF 0
177 #endif
178
179 /*
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
182 case.
183 */
184 #if wxUSE_UNICODE && !defined(wxHAVE_TCHAR_SUPPORT) && !defined(HAVE_WPRINTF)
185 #define wxNEED_WPRINTF
186 #endif
187
188 /*
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.
195
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().
198
199 */
200
201 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
202 /*
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
205 need
206 */
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 */
213
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 */
216 /*
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.
219 */
220 #if defined(wxNEED_PRINTF_CONVERSION) && defined(wxVsnprintf_)
221 int wxCRT_Vsnprintf( wxChar *str, size_t size, const wxChar *format, va_list ap );
222 #else
223 #define wxCRT_Vsnprintf wxVsnprintf_
224 #endif
225
226
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
232
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);
238 #else
239 #define wxCRT_ScanfW wscanf
240 #define wxCRT_SscanfW swscanf
241 #define wxCRT_FscanfW fwscanf
242 #define wxCRT_VsscanfW vswscanf
243 #endif
244
245 // ----------------------------------------------------------------------------
246 // user-friendly wrappers to CRT functions
247 // ----------------------------------------------------------------------------
248
249 #ifdef __WATCOMC__
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
255 #endif
256
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)
260
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
267
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)
272
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) \
279 return implUtf8 args
280 #else
281 #define WX_VARARG_VFOO_IMPL(args, implWchar, implUtf8) \
282 if ( wxLocaleIsUtf8 ) return implUtf8 args; \
283 else return implWchar args
284 #endif
285 #else // wxUSE_UNICODE_WCHAR / ANSI
286 #define WX_VARARG_VFOO_IMPL(args, implWchar, implUtf8) \
287 return implWchar args
288 #endif
289
290 inline int
291 wxVprintf(const wxString& format, va_list ap)
292 {
293 WX_VARARG_VFOO_IMPL((wxFormatString(format), ap),
294 wxCRT_Vprintf, vprintf);
295 }
296
297 inline int
298 wxVfprintf(FILE *f, const wxString& format, va_list ap)
299 {
300 WX_VARARG_VFOO_IMPL((f, wxFormatString(format), ap),
301 wxCRT_Vfprintf, vfprintf);
302 }
303
304 #undef WX_VARARG_VFOO_IMPL
305
306
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*:
309
310 #if !wxUSE_UTF8_LOCALE_ONLY
311 int WXDLLIMPEXP_BASE wxDoSprintfWchar(char *str, const wxChar *format, ...);
312 #endif
313 #if wxUSE_UNICODE_UTF8
314 int WXDLLIMPEXP_BASE wxDoSprintfUtf8(char *str, const char *format, ...);
315 #endif
316 WX_DEFINE_VARARG_FUNC(int, wxSprintf, 2, (char*, const wxFormatString&),
317 wxDoSprintfWchar, wxDoSprintfUtf8)
318
319 int WXDLLIMPEXP_BASE
320 wxVsprintf(char *str, const wxString& format, va_list argptr);
321
322 #if !wxUSE_UTF8_LOCALE_ONLY
323 int WXDLLIMPEXP_BASE wxDoSnprintfWchar(char *str, size_t size, const wxChar *format, ...);
324 #endif
325 #if wxUSE_UNICODE_UTF8
326 int WXDLLIMPEXP_BASE wxDoSnprintfUtf8(char *str, size_t size, const char *format, ...);
327 #endif
328 WX_DEFINE_VARARG_FUNC(int, wxSnprintf, 3, (char*, size_t, const wxFormatString&),
329 wxDoSnprintfWchar, wxDoSnprintfUtf8)
330
331 int WXDLLIMPEXP_BASE
332 wxVsnprintf(char *str, size_t size, const wxString& format, va_list argptr);
333
334 #if wxUSE_UNICODE
335
336 #if !wxUSE_UTF8_LOCALE_ONLY
337 int WXDLLIMPEXP_BASE wxDoSprintfWchar(wchar_t *str, const wxChar *format, ...);
338 #endif
339 #if wxUSE_UNICODE_UTF8
340 int WXDLLIMPEXP_BASE wxDoSprintfUtf8(wchar_t *str, const char *format, ...);
341 #endif
342 WX_DEFINE_VARARG_FUNC(int, wxSprintf, 2, (wchar_t*, const wxFormatString&),
343 wxDoSprintfWchar, wxDoSprintfUtf8)
344
345 int WXDLLIMPEXP_BASE
346 wxVsprintf(wchar_t *str, const wxString& format, va_list argptr);
347
348 #if !wxUSE_UTF8_LOCALE_ONLY
349 int WXDLLIMPEXP_BASE wxDoSnprintfWchar(wchar_t *str, size_t size, const wxChar *format, ...);
350 #endif
351 #if wxUSE_UNICODE_UTF8
352 int WXDLLIMPEXP_BASE wxDoSnprintfUtf8(wchar_t *str, size_t size, const char *format, ...);
353 #endif
354 WX_DEFINE_VARARG_FUNC(int, wxSnprintf, 3, (wchar_t*, size_t, const wxFormatString&),
355 wxDoSnprintfWchar, wxDoSnprintfUtf8)
356
357 int WXDLLIMPEXP_BASE
358 wxVsnprintf(wchar_t *str, size_t size, const wxString& format, va_list argptr);
359
360 #endif // wxUSE_UNICODE
361
362 #ifdef __WATCOMC__
363 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
364 //
365 // fortunately, OpenWatcom implements __VA_ARGS__, so we can provide macros
366 // that cast the format argument to wxString:
367 #undef wxPrintf
368 #undef wxFprintf
369 #undef wxSprintf
370 #undef wxSnprintf
371
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__
381
382
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.
388 //
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
392 // only.
393
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
397
398 #define _WX_VARARG_PASS_WRITABLE(i) a##i
399
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)) \
404 { \
405 return impl(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, passfixed), \
406 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WRITABLE)); \
407 }
408
409 #define WX_DEFINE_SCANFUNC(name, numfixed, fixed, impl, passfixed) \
410 inline int name(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, fixed)) \
411 { \
412 return impl(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, passfixed)); \
413 } \
414 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
415 _WX_DEFINE_SCANFUNC, \
416 dummy1, name, impl, passfixed, numfixed, fixed)
417
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))
422
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))
427
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))
444
445 // Visual C++ doesn't provide vsscanf()
446 #ifndef __VISUALC___
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__
456
457 #endif /* _WX_WXCRTVARARG_H_ */