]> git.saurik.com Git - wxWidgets.git/blob - include/wx/wxcrtvararg.h
compilation fix for newest DirectFB versions (patch #1939443 by Anders Larsen)
[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 // NB: User code should include wx/crt.h instead of including this
17 // header directly.
18
19 #include "wx/wxcrt.h"
20 #include "wx/strvararg.h"
21
22 #include "wx/string.h"
23
24 // ----------------------------------------------------------------------------
25 // CRT functions aliases
26 // ----------------------------------------------------------------------------
27
28 /* Required for wxPrintf() etc */
29 #include <stdarg.h>
30
31 /* printf() family saga */
32
33 /*
34 For some systems [v]snprintf()/vsscanf() exists in the system libraries but
35 not in the headers, so we need to declare it ourselves to be able to use it.
36 */
37 #if defined(HAVE_VSNPRINTF) && !defined(HAVE_VSNPRINTF_DECL)
38 #ifdef __cplusplus
39 extern "C"
40 #else
41 extern
42 #endif
43 int vsnprintf(char *str, size_t size, const char *format, va_list ap);
44 #endif /* !HAVE_VSNPRINTF_DECL */
45
46 #if defined(HAVE_SNPRINTF) && !defined(HAVE_SNPRINTF_DECL)
47 #ifdef __cplusplus
48 extern "C"
49 #else
50 extern
51 #endif
52 int snprintf(char *str, size_t size, const char *format, ...);
53 #endif /* !HAVE_SNPRINTF_DECL */
54
55 #if defined(HAVE_VSSCANF) && !defined(HAVE_VSSCANF_DECL)
56 #ifdef __cplusplus
57 extern "C"
58 #else
59 extern
60 #endif
61 int vsscanf(const char *str, const char *format, va_list ap);
62 #endif /* !HAVE_SNPRINTF_DECL */
63
64 /* Wrapper for vsnprintf if it's 3rd parameter is non-const. Note: the
65 * same isn't done for snprintf below, the builtin wxSnprintf_ is used
66 * instead since it's already a simple wrapper */
67 #if defined __cplusplus && defined HAVE_BROKEN_VSNPRINTF_DECL
68 inline int wx_fixed_vsnprintf(char *str, size_t size, const char *format, va_list ap)
69 {
70 return vsnprintf(str, size, (char*)format, ap);
71 }
72 #endif
73
74 /*
75 mingw32 normally uses MSVCRT which has non-standard vswprintf() and so
76 normally _vsnwprintf() is used instead, the only exception is when mingw32
77 is used with STLPort which does have a standard vswprintf() starting from
78 version 5.1 which we can use.
79 */
80 #ifdef __MINGW32__
81 #if defined(_STLPORT_VERSION) && _STLPORT_VERSION >= 0x510
82 #ifndef HAVE_VSWPRINTF
83 #define HAVE_VSWPRINTF
84 #endif
85 #elif defined(HAVE_VSWPRINTF)
86 /* can't use non-standard vswprintf() */
87 #undef HAVE_VSWPRINTF
88 #endif
89 #endif /* __MINGW32__ */
90
91 #if defined(__WATCOMC__)
92 #define HAVE_VSWPRINTF 1
93 #endif
94
95 #if wxUSE_PRINTF_POS_PARAMS
96 /*
97 The systems where vsnprintf() supports positional parameters should
98 define the HAVE_UNIX98_PRINTF symbol.
99
100 On systems which don't (e.g. Windows) we are forced to use
101 our wxVsnprintf() implementation.
102 */
103 #if defined(HAVE_UNIX98_PRINTF)
104 #ifdef HAVE_VSWPRINTF
105 #define wxCRT_VsnprintfW vswprintf
106 #endif
107 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
108 #define wxCRT_VsnprintfA wx_fixed_vsnprintf
109 #else
110 #define wxCRT_VsnprintfA vsnprintf
111 #endif
112 #else /* !HAVE_UNIX98_PRINTF */
113 /*
114 The only compiler with positional parameters support under Windows
115 is VC++ 8.0 which provides a new xxprintf_p() functions family.
116 The 2003 PSDK includes a slightly earlier version of VC8 than the
117 main release and does not have the printf_p functions.
118 */
119 #if defined _MSC_FULL_VER && _MSC_FULL_VER >= 140050727 && !defined __WXWINCE__
120 #define wxCRT_VsnprintfA _vsprintf_p
121 #define wxCRT_VsnprintfW _vswprintf_p
122 #endif
123 #endif /* HAVE_UNIX98_PRINTF/!HAVE_UNIX98_PRINTF */
124 #else /* !wxUSE_PRINTF_POS_PARAMS */
125 /*
126 We always want to define safe snprintf() function to be used instead of
127 sprintf(). Some compilers already have it (or rather vsnprintf() which
128 we really need...), otherwise we implement it using our own printf()
129 code.
130
131 We define function with a trailing underscore here because the real one
132 is a wrapper around it as explained below
133 */
134
135 #if defined(__VISUALC__) || \
136 (defined(__BORLANDC__) && __BORLANDC__ >= 0x540)
137 #define wxCRT_VsnprintfA _vsnprintf
138 #define wxCRT_VsnprintfW _vsnwprintf
139 #else
140 #if defined(HAVE__VSNWPRINTF)
141 #define wxCRT_VsnprintfW _vsnwprintf
142 #elif defined(HAVE_VSWPRINTF)
143 #define wxCRT_VsnprintfW vswprintf
144 #elif defined(__WATCOMC__)
145 #define wxCRT_VsnprintfW _vsnwprintf
146 #endif
147
148 /*
149 All versions of CodeWarrior supported by wxWidgets apparently
150 have both snprintf() and vsnprintf()
151 */
152 #if defined(HAVE_VSNPRINTF) \
153 || defined(__MWERKS__) || defined(__WATCOMC__)
154 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
155 #define wxCRT_VsnprintfA wx_fixed_vsnprintf
156 #else
157 #define wxCRT_VsnprintfA vsnprintf
158 #endif
159 #endif
160 #endif
161 #endif /* wxUSE_PRINTF_POS_PARAMS/!wxUSE_PRINTF_POS_PARAMS */
162
163 #ifndef wxCRT_VsnprintfW
164 /* no (suitable) vsnprintf(), cook our own */
165 WXDLLIMPEXP_BASE int
166 wxCRT_VsnprintfW(wchar_t *buf, size_t len, const wchar_t *format, va_list argptr);
167 #define wxUSE_WXVSNPRINTFW 1
168 #else
169 #define wxUSE_WXVSNPRINTFW 0
170 #endif
171
172 #ifndef wxCRT_VsnprintfA
173 /* no (suitable) vsnprintf(), cook our own */
174 WXDLLIMPEXP_BASE int
175 wxCRT_VsnprintfA(char *buf, size_t len, const char *format, va_list argptr);
176 #define wxUSE_WXVSNPRINTFA 1
177 #else
178 #define wxUSE_WXVSNPRINTFA 0
179 #endif
180
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):
184 #if !wxUSE_UNICODE
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)
192 #endif
193
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
199
200 /*
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
203 case.
204 */
205 #if wxUSE_UNICODE && !defined(wxHAVE_TCHAR_SUPPORT) && !defined(HAVE_WPRINTF)
206 #define wxNEED_WPRINTF
207 #endif
208 #if wxUSE_UNICODE && !defined(wxHAVE_TCHAR_SUPPORT) && !defined(HAVE_VSWSCANF)
209 #define wxNEED_VSWSCANF
210 #endif
211
212
213 #if defined(wxNEED_WPRINTF)
214 /*
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
217 need
218 */
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
229
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
233 #endif
234 #endif /* wxNEED_WPRINTF */
235
236
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
242
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, ...);
247 #else
248 #define wxCRT_ScanfW wxVMS_USE_STD wscanf
249 #define wxCRT_SscanfW wxVMS_USE_STD swscanf
250 #define wxCRT_FscanfW wxVMS_USE_STD fwscanf
251 #endif
252 #ifdef wxNEED_VSWSCANF
253 int wxCRT_VsscanfW(const wchar_t *str, const wchar_t *format, va_list ap);
254 #else
255 #define wxCRT_VsscanfW wxVMS_USE_STD vswscanf
256 #endif
257
258 // ----------------------------------------------------------------------------
259 // user-friendly wrappers to CRT functions
260 // ----------------------------------------------------------------------------
261
262 #ifdef __WATCOMC__
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
268 #endif
269
270 // FIXME-UTF8: remove this
271 #if wxUSE_UNICODE
272 #define wxCRT_PrintfNative wxCRT_PrintfW
273 #define wxCRT_FprintfNative wxCRT_FprintfW
274 #else
275 #define wxCRT_PrintfNative wxCRT_PrintfA
276 #define wxCRT_FprintfNative wxCRT_FprintfA
277 #endif
278
279 WX_DEFINE_VARARG_FUNC(int, wxPrintf, 1, (const wxFormatString&),
280 wxCRT_PrintfNative, wxCRT_PrintfA)
281 WX_DEFINE_VARARG_FUNC(int, wxFprintf, 2, (FILE*, const wxFormatString&),
282 wxCRT_FprintfNative, wxCRT_FprintfA)
283
284 // va_list versions of printf functions simply forward to the respective
285 // CRT function; note that they assume that va_list was created using
286 // wxArgNormalizer<T>!
287 #if wxUSE_UNICODE_UTF8
288 #if wxUSE_UTF8_LOCALE_ONLY
289 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
290 return implA args
291 #else
292 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
293 if ( wxLocaleIsUtf8 ) return implA args; \
294 else return implW args
295 #endif
296 #elif wxUSE_UNICODE_WCHAR
297 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
298 return implW args
299 #else // ANSI
300 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
301 return implA args
302 #endif
303
304 inline int
305 wxVprintf(const wxString& format, va_list ap)
306 {
307 WX_VARARG_VFOO_IMPL((wxFormatString(format), ap),
308 wxCRT_VprintfW, wxCRT_VprintfA);
309 }
310
311 inline int
312 wxVfprintf(FILE *f, const wxString& format, va_list ap)
313 {
314 WX_VARARG_VFOO_IMPL((f, wxFormatString(format), ap),
315 wxCRT_VfprintfW, wxCRT_VfprintfA);
316 }
317
318 #undef WX_VARARG_VFOO_IMPL
319
320
321 // wxSprintf() and friends have to be implemented in two forms, one for
322 // writing to char* buffer and one for writing to wchar_t*:
323
324 #if !wxUSE_UTF8_LOCALE_ONLY
325 int WXDLLIMPEXP_BASE wxDoSprintfWchar(char *str, const wxChar *format, ...);
326 #endif
327 #if wxUSE_UNICODE_UTF8
328 int WXDLLIMPEXP_BASE wxDoSprintfUtf8(char *str, const char *format, ...);
329 #endif
330 WX_DEFINE_VARARG_FUNC(int, wxSprintf, 2, (char*, const wxFormatString&),
331 wxDoSprintfWchar, wxDoSprintfUtf8)
332
333 int WXDLLIMPEXP_BASE
334 wxVsprintf(char *str, const wxString& format, va_list argptr);
335
336 #if !wxUSE_UTF8_LOCALE_ONLY
337 int WXDLLIMPEXP_BASE wxDoSnprintfWchar(char *str, size_t size, const wxChar *format, ...);
338 #endif
339 #if wxUSE_UNICODE_UTF8
340 int WXDLLIMPEXP_BASE wxDoSnprintfUtf8(char *str, size_t size, const char *format, ...);
341 #endif
342 WX_DEFINE_VARARG_FUNC(int, wxSnprintf, 3, (char*, size_t, const wxFormatString&),
343 wxDoSnprintfWchar, wxDoSnprintfUtf8)
344
345 int WXDLLIMPEXP_BASE
346 wxVsnprintf(char *str, size_t size, const wxString& format, va_list argptr);
347
348 #if wxUSE_UNICODE
349
350 #if !wxUSE_UTF8_LOCALE_ONLY
351 int WXDLLIMPEXP_BASE wxDoSprintfWchar(wchar_t *str, const wxChar *format, ...);
352 #endif
353 #if wxUSE_UNICODE_UTF8
354 int WXDLLIMPEXP_BASE wxDoSprintfUtf8(wchar_t *str, const char *format, ...);
355 #endif
356 WX_DEFINE_VARARG_FUNC(int, wxSprintf, 2, (wchar_t*, const wxFormatString&),
357 wxDoSprintfWchar, wxDoSprintfUtf8)
358
359 int WXDLLIMPEXP_BASE
360 wxVsprintf(wchar_t *str, const wxString& format, va_list argptr);
361
362 #if !wxUSE_UTF8_LOCALE_ONLY
363 int WXDLLIMPEXP_BASE wxDoSnprintfWchar(wchar_t *str, size_t size, const wxChar *format, ...);
364 #endif
365 #if wxUSE_UNICODE_UTF8
366 int WXDLLIMPEXP_BASE wxDoSnprintfUtf8(wchar_t *str, size_t size, const char *format, ...);
367 #endif
368 WX_DEFINE_VARARG_FUNC(int, wxSnprintf, 3, (wchar_t*, size_t, const wxFormatString&),
369 wxDoSnprintfWchar, wxDoSnprintfUtf8)
370
371 int WXDLLIMPEXP_BASE
372 wxVsnprintf(wchar_t *str, size_t size, const wxString& format, va_list argptr);
373
374 #endif // wxUSE_UNICODE
375
376 #ifdef __WATCOMC__
377 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
378 //
379 // fortunately, OpenWatcom implements __VA_ARGS__, so we can provide macros
380 // that cast the format argument to wxString:
381 #undef wxPrintf
382 #undef wxFprintf
383 #undef wxSprintf
384 #undef wxSnprintf
385
386 #define wxPrintf(fmt, ...) \
387 wxPrintf_Impl(wxFormatString(fmt), __VA_ARGS__)
388 #define wxFprintf(f, fmt, ...) \
389 wxFprintf_Impl(f, wxFormatString(fmt), __VA_ARGS__)
390 #define wxSprintf(s, fmt, ...) \
391 wxSprintf_Impl(s, wxFormatString(fmt), __VA_ARGS__)
392 #define wxSnprintf(s, n, fmt, ...) \
393 wxSnprintf_Impl(s, n, wxFormatString(fmt), __VA_ARGS__)
394 #endif // __WATCOMC__
395
396
397 // We can't use wxArgNormalizer<T> for variadic arguments to wxScanf() etc.
398 // because they are writable, so instead of providing friendly template
399 // vararg-like functions, we just provide both char* and wchar_t* variants
400 // of these functions. The type of output variadic arguments for %s must match
401 // the type of 'str' and 'format' arguments.
402 //
403 // For compatibility with earlier wx versions, we also provide wxSscanf()
404 // version with the first argument (input string) wxString; for this version,
405 // the type of output string values is determined by the type of format string
406 // only.
407
408 #define _WX_SCANFUNC_EXTRACT_ARGS_1(x) x
409 #define _WX_SCANFUNC_EXTRACT_ARGS_2(x,y) x, y
410 #define _WX_SCANFUNC_EXTRACT_ARGS(N, args) _WX_SCANFUNC_EXTRACT_ARGS_##N args
411
412 #define _WX_VARARG_PASS_WRITABLE(i) a##i
413
414 #define _WX_DEFINE_SCANFUNC(N, dummy1, name, impl, passfixed, numfixed, fixed)\
415 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
416 int name(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, fixed), \
417 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
418 { \
419 return impl(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, passfixed), \
420 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WRITABLE)); \
421 }
422
423 #define WX_DEFINE_SCANFUNC(name, numfixed, fixed, impl, passfixed) \
424 inline int name(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, fixed)) \
425 { \
426 return impl(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, passfixed)); \
427 } \
428 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
429 _WX_DEFINE_SCANFUNC, \
430 dummy1, name, impl, passfixed, numfixed, fixed)
431
432 // this is needed to normalize the format string, see src/common/strvararg.cpp
433 // for more details
434 #ifdef __WINDOWS__
435 #define wxScanfConvertFormatW(fmt) fmt
436 #else
437 const wxWCharBuffer
438 WXDLLIMPEXP_BASE wxScanfConvertFormatW(const wchar_t *format);
439 #endif
440
441 WX_DEFINE_SCANFUNC(wxScanf, 1, (const char *format),
442 wxCRT_ScanfA, (format))
443 WX_DEFINE_SCANFUNC(wxScanf, 1, (const wchar_t *format),
444 wxCRT_ScanfW, (wxScanfConvertFormatW(format)))
445
446 WX_DEFINE_SCANFUNC(wxFscanf, 2, (FILE *stream, const char *format),
447 wxCRT_FscanfA, (stream, format))
448 WX_DEFINE_SCANFUNC(wxFscanf, 2, (FILE *stream, const wchar_t *format),
449 wxCRT_FscanfW, (stream, wxScanfConvertFormatW(format)))
450
451 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const char *str, const char *format),
452 wxCRT_SscanfA, (str, format))
453 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wchar_t *str, const wchar_t *format),
454 wxCRT_SscanfW, (str, wxScanfConvertFormatW(format)))
455 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxCharBuffer& str, const char *format),
456 wxCRT_SscanfA, (str.data(), format))
457 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxWCharBuffer& str, const wchar_t *format),
458 wxCRT_SscanfW, (str.data(), wxScanfConvertFormatW(format)))
459 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxString& str, const char *format),
460 wxCRT_SscanfA, (str.mb_str(), format))
461 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxString& str, const wchar_t *format),
462 wxCRT_SscanfW, (str.wc_str(), wxScanfConvertFormatW(format)))
463 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxCStrData& str, const char *format),
464 wxCRT_SscanfA, (str.AsCharBuf(), format))
465 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxCStrData& str, const wchar_t *format),
466 wxCRT_SscanfW, (str.AsWCharBuf(), wxScanfConvertFormatW(format)))
467
468 // Visual C++ doesn't provide vsscanf()
469 #ifndef __VISUALC___
470 int WXDLLIMPEXP_BASE wxVsscanf(const char *str, const char *format, va_list ap);
471 int WXDLLIMPEXP_BASE wxVsscanf(const wchar_t *str, const wchar_t *format, va_list ap);
472 int WXDLLIMPEXP_BASE wxVsscanf(const wxCharBuffer& str, const char *format, va_list ap);
473 int WXDLLIMPEXP_BASE wxVsscanf(const wxWCharBuffer& str, const wchar_t *format, va_list ap);
474 int WXDLLIMPEXP_BASE wxVsscanf(const wxString& str, const char *format, va_list ap);
475 int WXDLLIMPEXP_BASE wxVsscanf(const wxString& str, const wchar_t *format, va_list ap);
476 int WXDLLIMPEXP_BASE wxVsscanf(const wxCStrData& str, const char *format, va_list ap);
477 int WXDLLIMPEXP_BASE wxVsscanf(const wxCStrData& str, const wchar_t *format, va_list ap);
478 #endif // !__VISUALC__
479
480 #endif /* _WX_WXCRTVARARG_H_ */