]> git.saurik.com Git - wxWidgets.git/blob - include/wx/wxcrtvararg.h
added missing #includes
[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() exists in the system libraries but not in the
35 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 /* 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)
60 {
61 return vsnprintf(str, size, (char*)format, ap);
62 }
63 #endif
64
65 /*
66 MinGW MSVCRT has non-standard vswprintf() (for MSVC compatibility
67 presumably) and normally _vsnwprintf() is used instead
68 */
69 #if defined(HAVE_VSWPRINTF) && defined(__MINGW32__)
70 #undef HAVE_VSWPRINTF
71 #endif
72
73 #if wxUSE_PRINTF_POS_PARAMS
74 /*
75 The systems where vsnprintf() supports positional parameters should
76 define the HAVE_UNIX98_PRINTF symbol.
77
78 On systems which don't (e.g. Windows) we are forced to use
79 our wxVsnprintf() implementation.
80 */
81 #if defined(HAVE_UNIX98_PRINTF)
82 #ifdef HAVE_VSWPRINTF
83 #define wxCRT_VsnprintfW_ vswprintf
84 #endif
85 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
86 #define wxCRT_VsnprintfA wx_fixed_vsnprintf
87 #else
88 #define wxCRT_VsnprintfA vsnprintf
89 #endif
90 #else /* !HAVE_UNIX98_PRINTF */
91 /*
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.
96 */
97 #if defined _MSC_FULL_VER && _MSC_FULL_VER >= 140050727 && !defined __WXWINCE__
98 #define wxCRT_VsnprintfA _vsprintf_p
99 #define wxCRT_VsnprintfW_ _vswprintf_p
100 #endif
101 #endif /* HAVE_UNIX98_PRINTF/!HAVE_UNIX98_PRINTF */
102 #else /* !wxUSE_PRINTF_POS_PARAMS */
103 /*
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()
107 code.
108
109 We define function with a trailing underscore here because the real one
110 is a wrapper around it as explained below
111 */
112
113 #if defined(__VISUALC__) || \
114 (defined(__BORLANDC__) && __BORLANDC__ >= 0x540)
115 #define wxCRT_VsnprintfA _vsnprintf
116 #define wxCRT_VsnprintfW_ _vsnwprintf
117 #else
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
124 #endif
125
126 /*
127 All versions of CodeWarrior supported by wxWidgets apparently
128 have both snprintf() and vsnprintf()
129 */
130 #if defined(HAVE_VSNPRINTF) \
131 || defined(__MWERKS__) || defined(__WATCOMC__)
132 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
133 #define wxCRT_VsnprintfA wx_fixed_vsnprintf
134 #else
135 #define wxCRT_VsnprintfA vsnprintf
136 #endif
137 #endif
138 #endif
139 #endif /* wxUSE_PRINTF_POS_PARAMS/!wxUSE_PRINTF_POS_PARAMS */
140
141 #ifndef wxCRT_VsnprintfW_
142 /* no (suitable) vsnprintf(), cook our own */
143 WXDLLIMPEXP_BASE int
144 wxCRT_VsnprintfW_(wchar_t *buf, size_t len, const wchar_t *format, va_list argptr);
145 #define wxUSE_WXVSNPRINTFW 1
146 #else
147 #define wxUSE_WXVSNPRINTFW 0
148 #endif
149
150 #ifndef wxCRT_VsnprintfA
151 /* no (suitable) vsnprintf(), cook our own */
152 WXDLLIMPEXP_BASE int
153 wxCRT_VsnprintfA(char *buf, size_t len, const char *format, va_list argptr);
154 #define wxUSE_WXVSNPRINTFA 1
155 #else
156 #define wxUSE_WXVSNPRINTFA 0
157 #endif
158
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):
162 #if !wxUSE_UNICODE
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)
170 #endif
171
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
177
178 /*
179 More Unicode complications: although both ANSI C and C++ define a number of
180 wide character functions such as wprintf(), not all environments have them.
181 Worse, those which do have different behaviours: under Windows, %s format
182 specifier changes its meaning in Unicode build and expects a Unicode string
183 while under Unix/POSIX it still means an ASCII string even for wprintf() and
184 %ls has to be used for wide strings.
185
186 We choose to always emulate Windows behaviour as more useful for us so even
187 if we have wprintf() we still must wrap it in a non trivial wxPrintf().
188
189 */
190 #ifndef __WINDOWS__
191 #define wxNEED_PRINTF_CONVERSION
192 #endif
193
194 // FIXME-UTF8: format conversion should be moved outside of wxCRT_* and to the
195 // vararg templates; after then, wxNEED_PRINTF_CONVERSION should
196 // be removed and this code simplified
197 #ifndef wxNEED_PRINTF_CONVERSION
198 #ifdef wxHAVE_TCHAR_SUPPORT
199 #define wxCRT_FprintfW fwprintf
200 #define wxCRT_PrintfW wprintf
201 #define wxCRT_VfprintfW vfwprintf
202 #define wxCRT_VprintfW vwprintf
203 #define wxCRT_VsprintfW vswprintf
204 #endif
205 #endif // !defined(wxNEED_PRINTF_CONVERSION)
206
207 /*
208 In Unicode mode we need to have all standard functions such as wprintf() and
209 so on but not all systems have them so use our own implementations in this
210 case.
211 */
212 #if wxUSE_UNICODE && !defined(wxHAVE_TCHAR_SUPPORT) && !defined(HAVE_WPRINTF)
213 #define wxNEED_WPRINTF
214 #endif
215
216
217 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
218 /*
219 we need to implement all wide character printf functions either because
220 we don't have them at all or because they don't have the semantics we
221 need
222 */
223 int wxCRT_PrintfW( const wchar_t *format, ... );
224 int wxCRT_FprintfW( FILE *stream, const wchar_t *format, ... );
225 int wxCRT_VfprintfW( FILE *stream, const wchar_t *format, va_list ap );
226 int wxCRT_VprintfW( const wchar_t *format, va_list ap );
227 int wxCRT_VsprintfW( wchar_t *str, const wchar_t *format, va_list ap );
228 #endif /* wxNEED_PRINTF_CONVERSION */
229
230 /* these 2 can be simply mapped to the versions with underscore at the end */
231 /* if we don't have to do the conversion */
232 /*
233 However, if we don't have any vswprintf() at all we don't need to redefine
234 anything as our own wxCRT_VsnprintfW_() already behaves as needed.
235 */
236 #if defined(wxNEED_PRINTF_CONVERSION) && defined(wxCRT_VsnprintfW_)
237 int wxCRT_VsnprintfW( wchar_t *str, size_t size, const wchar_t *format, va_list ap );
238 #else
239 #define wxCRT_VsnprintfW wxCRT_VsnprintfW_
240 #endif
241
242
243 /* Required for wxScanf() etc. */
244 #define wxCRT_ScanfA scanf
245 #define wxCRT_SscanfA sscanf
246 #define wxCRT_FscanfA fscanf
247 #define wxCRT_VsscanfA vsscanf
248
249 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
250 int wxCRT_ScanfW(const wchar_t *format, ...);
251 int wxCRT_SscanfW(const wchar_t *str, const wchar_t *format, ...);
252 int wxCRT_FscanfW(FILE *stream, const wchar_t *format, ...);
253 int wxCRT_VsscanfW(const wchar_t *str, const wchar_t *format, va_list ap);
254 #else
255 #define wxCRT_ScanfW wscanf
256 #define wxCRT_SscanfW swscanf
257 #define wxCRT_FscanfW fwscanf
258 #define wxCRT_VsscanfW vswscanf
259 #endif
260
261 // ----------------------------------------------------------------------------
262 // user-friendly wrappers to CRT functions
263 // ----------------------------------------------------------------------------
264
265 #ifdef __WATCOMC__
266 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
267 #define wxPrintf wxPrintf_Impl
268 #define wxFprintf wxFprintf_Impl
269 #define wxSprintf wxSprintf_Impl
270 #define wxSnprintf wxSnprintf_Impl
271 #endif
272
273 // FIXME-UTF8: explicit wide-string and short-string format specifiers
274 // (%hs, %ls and variants) are currently broken, only %s works
275 // as expected (regardless of the build)
276
277 // FIXME-UTF8: %c (and %hc, %lc) don't work as expected either: in UTF-8 build,
278 // we should replace them with %s (as some Unicode chars may be
279 // encoded with >1 bytes) and in all builds, we should use wchar_t
280 // for all characters and convert char to it;
281 // we'll also need wxArgNormalizer<T> specializations for char,
282 // wchar_t, wxUniChar and wxUniCharRef to handle this correctly
283
284 // FIXME-UTF8: remove this
285 #if wxUSE_UNICODE
286 #define wxCRT_PrintfNative wxCRT_PrintfW
287 #define wxCRT_FprintfNative wxCRT_FprintfW
288 #else
289 #define wxCRT_PrintfNative wxCRT_PrintfA
290 #define wxCRT_FprintfNative wxCRT_FprintfA
291 #endif
292
293 WX_DEFINE_VARARG_FUNC(int, wxPrintf, 1, (const wxFormatString&),
294 wxCRT_PrintfNative, wxCRT_PrintfA)
295 WX_DEFINE_VARARG_FUNC(int, wxFprintf, 2, (FILE*, const wxFormatString&),
296 wxCRT_FprintfNative, wxCRT_FprintfA)
297
298 // va_list versions of printf functions simply forward to the respective
299 // CRT function; note that they assume that va_list was created using
300 // wxArgNormalizer<T>!
301 #if wxUSE_UNICODE_UTF8
302 #if wxUSE_UTF8_LOCALE_ONLY
303 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
304 return implA args
305 #else
306 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
307 if ( wxLocaleIsUtf8 ) return implA args; \
308 else return implW args
309 #endif
310 #elif wxUSE_UNICODE_WCHAR
311 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
312 return implW args
313 #else // ANSI
314 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
315 return implA args
316 #endif
317
318 inline int
319 wxVprintf(const wxString& format, va_list ap)
320 {
321 WX_VARARG_VFOO_IMPL((wxFormatString(format), ap),
322 wxCRT_VprintfW, wxCRT_VprintfA);
323 }
324
325 inline int
326 wxVfprintf(FILE *f, const wxString& format, va_list ap)
327 {
328 WX_VARARG_VFOO_IMPL((f, wxFormatString(format), ap),
329 wxCRT_VfprintfW, wxCRT_VfprintfA);
330 }
331
332 #undef WX_VARARG_VFOO_IMPL
333
334
335 // wxSprintf() and friends have to be implemented in two forms, one for
336 // writing to char* buffer and one for writing to wchar_t*:
337
338 #if !wxUSE_UTF8_LOCALE_ONLY
339 int WXDLLIMPEXP_BASE wxDoSprintfWchar(char *str, const wxChar *format, ...);
340 #endif
341 #if wxUSE_UNICODE_UTF8
342 int WXDLLIMPEXP_BASE wxDoSprintfUtf8(char *str, const char *format, ...);
343 #endif
344 WX_DEFINE_VARARG_FUNC(int, wxSprintf, 2, (char*, const wxFormatString&),
345 wxDoSprintfWchar, wxDoSprintfUtf8)
346
347 int WXDLLIMPEXP_BASE
348 wxVsprintf(char *str, const wxString& format, va_list argptr);
349
350 #if !wxUSE_UTF8_LOCALE_ONLY
351 int WXDLLIMPEXP_BASE wxDoSnprintfWchar(char *str, size_t size, const wxChar *format, ...);
352 #endif
353 #if wxUSE_UNICODE_UTF8
354 int WXDLLIMPEXP_BASE wxDoSnprintfUtf8(char *str, size_t size, const char *format, ...);
355 #endif
356 WX_DEFINE_VARARG_FUNC(int, wxSnprintf, 3, (char*, size_t, const wxFormatString&),
357 wxDoSnprintfWchar, wxDoSnprintfUtf8)
358
359 int WXDLLIMPEXP_BASE
360 wxVsnprintf(char *str, size_t size, const wxString& format, va_list argptr);
361
362 #if wxUSE_UNICODE
363
364 #if !wxUSE_UTF8_LOCALE_ONLY
365 int WXDLLIMPEXP_BASE wxDoSprintfWchar(wchar_t *str, const wxChar *format, ...);
366 #endif
367 #if wxUSE_UNICODE_UTF8
368 int WXDLLIMPEXP_BASE wxDoSprintfUtf8(wchar_t *str, const char *format, ...);
369 #endif
370 WX_DEFINE_VARARG_FUNC(int, wxSprintf, 2, (wchar_t*, const wxFormatString&),
371 wxDoSprintfWchar, wxDoSprintfUtf8)
372
373 int WXDLLIMPEXP_BASE
374 wxVsprintf(wchar_t *str, const wxString& format, va_list argptr);
375
376 #if !wxUSE_UTF8_LOCALE_ONLY
377 int WXDLLIMPEXP_BASE wxDoSnprintfWchar(wchar_t *str, size_t size, const wxChar *format, ...);
378 #endif
379 #if wxUSE_UNICODE_UTF8
380 int WXDLLIMPEXP_BASE wxDoSnprintfUtf8(wchar_t *str, size_t size, const char *format, ...);
381 #endif
382 WX_DEFINE_VARARG_FUNC(int, wxSnprintf, 3, (wchar_t*, size_t, const wxFormatString&),
383 wxDoSnprintfWchar, wxDoSnprintfUtf8)
384
385 int WXDLLIMPEXP_BASE
386 wxVsnprintf(wchar_t *str, size_t size, const wxString& format, va_list argptr);
387
388 #endif // wxUSE_UNICODE
389
390 #ifdef __WATCOMC__
391 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
392 //
393 // fortunately, OpenWatcom implements __VA_ARGS__, so we can provide macros
394 // that cast the format argument to wxString:
395 #undef wxPrintf
396 #undef wxFprintf
397 #undef wxSprintf
398 #undef wxSnprintf
399
400 #define wxPrintf(fmt, ...) \
401 wxPrintf_Impl(wxFormatString(fmt), __VA_ARGS__)
402 #define wxFprintf(f, fmt, ...) \
403 wxFprintf_Impl(f, wxFormatString(fmt), __VA_ARGS__)
404 #define wxSprintf(s, fmt, ...) \
405 wxSprintf_Impl(s, wxFormatString(fmt), __VA_ARGS__)
406 #define wxSnprintf(s, n, fmt, ...) \
407 wxSnprintf_Impl(s, n, wxFormatString(fmt), __VA_ARGS__)
408 #endif // __WATCOMC__
409
410
411 // We can't use wxArgNormalizer<T> for variadic arguments to wxScanf() etc.
412 // because they are writable, so instead of providing friendly template
413 // vararg-like functions, we just provide both char* and wchar_t* variants
414 // of these functions. The type of output variadic arguments for %s must match
415 // the type of 'str' and 'format' arguments.
416 //
417 // For compatibility with earlier wx versions, we also provide wxSscanf()
418 // version with the first argument (input string) wxString; for this version,
419 // the type of output string values is determined by the type of format string
420 // only.
421
422 #define _WX_SCANFUNC_EXTRACT_ARGS_1(x) x
423 #define _WX_SCANFUNC_EXTRACT_ARGS_2(x,y) x, y
424 #define _WX_SCANFUNC_EXTRACT_ARGS(N, args) _WX_SCANFUNC_EXTRACT_ARGS_##N args
425
426 #define _WX_VARARG_PASS_WRITABLE(i) a##i
427
428 #define _WX_DEFINE_SCANFUNC(N, dummy1, name, impl, passfixed, numfixed, fixed)\
429 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
430 int name(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, fixed), \
431 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
432 { \
433 return impl(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, passfixed), \
434 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WRITABLE)); \
435 }
436
437 #define WX_DEFINE_SCANFUNC(name, numfixed, fixed, impl, passfixed) \
438 inline int name(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, fixed)) \
439 { \
440 return impl(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, passfixed)); \
441 } \
442 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
443 _WX_DEFINE_SCANFUNC, \
444 dummy1, name, impl, passfixed, numfixed, fixed)
445
446 WX_DEFINE_SCANFUNC(wxScanf, 1, (const char *format),
447 wxCRT_ScanfA, (format))
448 WX_DEFINE_SCANFUNC(wxScanf, 1, (const wchar_t *format),
449 wxCRT_ScanfW, (format))
450
451 WX_DEFINE_SCANFUNC(wxFscanf, 2, (FILE *stream, const char *format),
452 wxCRT_FscanfA, (stream, format))
453 WX_DEFINE_SCANFUNC(wxFscanf, 2, (FILE *stream, const wchar_t *format),
454 wxCRT_FscanfW, (stream, format))
455
456 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const char *str, const char *format),
457 wxCRT_SscanfA, (str, format))
458 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wchar_t *str, const wchar_t *format),
459 wxCRT_SscanfW, (str, format))
460 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxCharBuffer& str, const char *format),
461 wxCRT_SscanfA, (str.data(), format))
462 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxWCharBuffer& str, const wchar_t *format),
463 wxCRT_SscanfW, (str.data(), format))
464 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxString& str, const char *format),
465 wxCRT_SscanfA, (str.mb_str(), format))
466 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxString& str, const wchar_t *format),
467 wxCRT_SscanfW, (str.wc_str(), format))
468 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxCStrData& str, const char *format),
469 wxCRT_SscanfA, (str.AsCharBuf(), format))
470 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxCStrData& str, const wchar_t *format),
471 wxCRT_SscanfW, (str.AsWCharBuf(), format))
472
473 // Visual C++ doesn't provide vsscanf()
474 #ifndef __VISUALC___
475 int WXDLLIMPEXP_BASE wxVsscanf(const char *str, const char *format, va_list ap);
476 int WXDLLIMPEXP_BASE wxVsscanf(const wchar_t *str, const wchar_t *format, va_list ap);
477 int WXDLLIMPEXP_BASE wxVsscanf(const wxCharBuffer& str, const char *format, va_list ap);
478 int WXDLLIMPEXP_BASE wxVsscanf(const wxWCharBuffer& str, const wchar_t *format, va_list ap);
479 int WXDLLIMPEXP_BASE wxVsscanf(const wxString& str, const char *format, va_list ap);
480 int WXDLLIMPEXP_BASE wxVsscanf(const wxString& str, const wchar_t *format, va_list ap);
481 int WXDLLIMPEXP_BASE wxVsscanf(const wxCStrData& str, const char *format, va_list ap);
482 int WXDLLIMPEXP_BASE wxVsscanf(const wxCStrData& str, const wchar_t *format, va_list ap);
483 #endif // !__VISUALC__
484
485 #endif /* _WX_WXCRTVARARG_H_ */