]> git.saurik.com Git - wxWidgets.git/blob - include/wx/wxcrtvararg.h
Export recently added wxRichTextXMLHelper to fix link errors.
[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 // Copyright: (c) 2007 REA Elektronik GmbH
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_WXCRTVARARG_H_
13 #define _WX_WXCRTVARARG_H_
14
15 // NB: User code should include wx/crt.h instead of including this
16 // header directly.
17
18 #include "wx/wxcrt.h"
19 #include "wx/strvararg.h"
20
21 #include "wx/string.h"
22
23 // ----------------------------------------------------------------------------
24 // CRT functions aliases
25 // ----------------------------------------------------------------------------
26
27 /* Required for wxPrintf() etc */
28 #include <stdarg.h>
29
30 /* printf() family saga */
31
32 /*
33 For many old Unix systems [v]snprintf()/vsscanf() exists in the system
34 libraries but not in the headers, so we need to declare it ourselves to be
35 able to use it.
36 */
37 #ifdef __UNIX__
38
39 #if defined(HAVE_VSNPRINTF) && !defined(HAVE_VSNPRINTF_DECL)
40 #ifdef __cplusplus
41 extern "C"
42 #else
43 extern
44 #endif
45 int vsnprintf(char *str, size_t size, const char *format, va_list ap);
46 #endif /* !HAVE_VSNPRINTF_DECL */
47
48 #if defined(HAVE_SNPRINTF) && !defined(HAVE_SNPRINTF_DECL)
49 #ifdef __cplusplus
50 extern "C"
51 #else
52 extern
53 #endif
54 int snprintf(char *str, size_t size, const char *format, ...);
55 #endif /* !HAVE_SNPRINTF_DECL */
56
57 #if defined(HAVE_VSSCANF) && !defined(HAVE_VSSCANF_DECL)
58 #ifdef __cplusplus
59 extern "C"
60 #else
61 extern
62 #endif
63 int vsscanf(const char *str, const char *format, va_list ap);
64 #endif /* !HAVE_VSSCANF_DECL */
65
66 /* Wrapper for vsnprintf if it's 3rd parameter is non-const. Note: the
67 * same isn't done for snprintf below, the builtin wxSnprintf_ is used
68 * instead since it's already a simple wrapper */
69 #if defined __cplusplus && defined HAVE_BROKEN_VSNPRINTF_DECL
70 inline int wx_fixed_vsnprintf(char *str, size_t size, const char *format, va_list ap)
71 {
72 return vsnprintf(str, size, (char*)format, ap);
73 }
74 #endif
75
76 #endif /* __UNIX__ */
77
78 /*
79 mingw32 normally uses MSVCRT which has non-standard vswprintf() and so
80 normally _vsnwprintf() is used instead, the only exception is when mingw32
81 is used with STLPort which does have a standard vswprintf() starting from
82 version 5.1 which we can use.
83 */
84 #ifdef __MINGW32__
85 #if defined(_STLPORT_VERSION) && _STLPORT_VERSION >= 0x510
86 #ifndef HAVE_VSWPRINTF
87 #define HAVE_VSWPRINTF
88 #endif
89 #elif defined(HAVE_VSWPRINTF)
90 /* can't use non-standard vswprintf() */
91 #undef HAVE_VSWPRINTF
92 #endif
93 #endif /* __MINGW32__ */
94
95 #if defined(__WATCOMC__)
96 #define HAVE_VSWPRINTF 1
97 #endif
98
99 #if wxUSE_PRINTF_POS_PARAMS
100 /*
101 The systems where vsnprintf() supports positional parameters should
102 define the HAVE_UNIX98_PRINTF symbol.
103
104 On systems which don't (e.g. Windows) we are forced to use
105 our wxVsnprintf() implementation.
106 */
107 #if defined(HAVE_UNIX98_PRINTF)
108 #ifdef HAVE_VSWPRINTF
109 #define wxCRT_VsnprintfW vswprintf
110 #endif
111 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
112 #define wxCRT_VsnprintfA wx_fixed_vsnprintf
113 #else
114 #define wxCRT_VsnprintfA vsnprintf
115 #endif
116 #else /* !HAVE_UNIX98_PRINTF */
117 /*
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.
122 */
123 #if defined _MSC_FULL_VER && _MSC_FULL_VER >= 140050727 && !defined __WXWINCE__
124 #define wxCRT_VsnprintfA _vsprintf_p
125 #define wxCRT_VsnprintfW _vswprintf_p
126 #endif
127 #endif /* HAVE_UNIX98_PRINTF/!HAVE_UNIX98_PRINTF */
128 #else /* !wxUSE_PRINTF_POS_PARAMS */
129 /*
130 We always want to define safe snprintf() function to be used instead of
131 sprintf(). Some compilers already have it (or rather vsnprintf() which
132 we really need...), otherwise we implement it using our own printf()
133 code.
134
135 We define function with a trailing underscore here because the real one
136 is a wrapper around it as explained below
137 */
138
139 #if defined(__VISUALC__) || \
140 (defined(__BORLANDC__) && __BORLANDC__ >= 0x540)
141 #define wxCRT_VsnprintfA _vsnprintf
142 #define wxCRT_VsnprintfW _vsnwprintf
143 #else
144 #if defined(HAVE__VSNWPRINTF)
145 #define wxCRT_VsnprintfW _vsnwprintf
146 #elif defined(HAVE_VSWPRINTF)
147 #define wxCRT_VsnprintfW vswprintf
148 #elif defined(__WATCOMC__)
149 #define wxCRT_VsnprintfW _vsnwprintf
150 #endif
151
152 #if defined(HAVE_VSNPRINTF) \
153 || 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
280 WX_DEFINE_VARARG_FUNC_SANS_N0(int, wxPrintf, 1, (const wxFormatString&),
281 wxCRT_PrintfNative, wxCRT_PrintfA)
282 inline int wxPrintf(const wxFormatString& s)
283 {
284 return wxPrintf("%s", s.InputAsString());
285 }
286
287 WX_DEFINE_VARARG_FUNC_SANS_N0(int, wxFprintf, 2, (FILE*, const wxFormatString&),
288 wxCRT_FprintfNative, wxCRT_FprintfA)
289 inline int wxFprintf(FILE *f, const wxFormatString& s)
290 {
291 return wxFprintf(f, "%s", s.InputAsString());
292 }
293
294 // va_list versions of printf functions simply forward to the respective
295 // CRT function; note that they assume that va_list was created using
296 // wxArgNormalizer<T>!
297 #if wxUSE_UNICODE_UTF8
298 #if wxUSE_UTF8_LOCALE_ONLY
299 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
300 return implA args
301 #else
302 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
303 if ( wxLocaleIsUtf8 ) return implA args; \
304 else return implW args
305 #endif
306 #elif wxUSE_UNICODE_WCHAR
307 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
308 return implW args
309 #else // ANSI
310 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
311 return implA args
312 #endif
313
314 inline int
315 wxVprintf(const wxString& format, va_list ap)
316 {
317 WX_VARARG_VFOO_IMPL((wxFormatString(format), ap),
318 wxCRT_VprintfW, wxCRT_VprintfA);
319 }
320
321 inline int
322 wxVfprintf(FILE *f, const wxString& format, va_list ap)
323 {
324 WX_VARARG_VFOO_IMPL((f, wxFormatString(format), ap),
325 wxCRT_VfprintfW, wxCRT_VfprintfA);
326 }
327
328 #undef WX_VARARG_VFOO_IMPL
329
330
331 // wxSprintf() and friends have to be implemented in two forms, one for
332 // writing to char* buffer and one for writing to wchar_t*:
333
334 #if !wxUSE_UTF8_LOCALE_ONLY
335 int WXDLLIMPEXP_BASE wxDoSprintfWchar(char *str, const wxChar *format, ...);
336 #endif
337 #if wxUSE_UNICODE_UTF8
338 int WXDLLIMPEXP_BASE wxDoSprintfUtf8(char *str, const char *format, ...);
339 #endif
340 WX_DEFINE_VARARG_FUNC(int, wxSprintf, 2, (char*, const wxFormatString&),
341 wxDoSprintfWchar, wxDoSprintfUtf8)
342
343 int WXDLLIMPEXP_BASE
344 wxVsprintf(char *str, const wxString& format, va_list argptr);
345
346 #if !wxUSE_UTF8_LOCALE_ONLY
347 int WXDLLIMPEXP_BASE wxDoSnprintfWchar(char *str, size_t size, const wxChar *format, ...);
348 #endif
349 #if wxUSE_UNICODE_UTF8
350 int WXDLLIMPEXP_BASE wxDoSnprintfUtf8(char *str, size_t size, const char *format, ...);
351 #endif
352 WX_DEFINE_VARARG_FUNC(int, wxSnprintf, 3, (char*, size_t, const wxFormatString&),
353 wxDoSnprintfWchar, wxDoSnprintfUtf8)
354
355 int WXDLLIMPEXP_BASE
356 wxVsnprintf(char *str, size_t size, const wxString& format, va_list argptr);
357
358 #if wxUSE_UNICODE
359
360 #if !wxUSE_UTF8_LOCALE_ONLY
361 int WXDLLIMPEXP_BASE wxDoSprintfWchar(wchar_t *str, const wxChar *format, ...);
362 #endif
363 #if wxUSE_UNICODE_UTF8
364 int WXDLLIMPEXP_BASE wxDoSprintfUtf8(wchar_t *str, const char *format, ...);
365 #endif
366 WX_DEFINE_VARARG_FUNC(int, wxSprintf, 2, (wchar_t*, const wxFormatString&),
367 wxDoSprintfWchar, wxDoSprintfUtf8)
368
369 int WXDLLIMPEXP_BASE
370 wxVsprintf(wchar_t *str, const wxString& format, va_list argptr);
371
372 #if !wxUSE_UTF8_LOCALE_ONLY
373 int WXDLLIMPEXP_BASE wxDoSnprintfWchar(wchar_t *str, size_t size, const wxChar *format, ...);
374 #endif
375 #if wxUSE_UNICODE_UTF8
376 int WXDLLIMPEXP_BASE wxDoSnprintfUtf8(wchar_t *str, size_t size, const char *format, ...);
377 #endif
378 WX_DEFINE_VARARG_FUNC(int, wxSnprintf, 3, (wchar_t*, size_t, const wxFormatString&),
379 wxDoSnprintfWchar, wxDoSnprintfUtf8)
380
381 int WXDLLIMPEXP_BASE
382 wxVsnprintf(wchar_t *str, size_t size, const wxString& format, va_list argptr);
383
384 #endif // wxUSE_UNICODE
385
386 #ifdef __WATCOMC__
387 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
388 //
389 // fortunately, OpenWatcom implements __VA_ARGS__, so we can provide macros
390 // that cast the format argument to wxString:
391 #undef wxPrintf
392 #undef wxFprintf
393 #undef wxSprintf
394 #undef wxSnprintf
395
396 #define wxPrintf(fmt, ...) \
397 wxPrintf_Impl(wxFormatString(fmt), __VA_ARGS__)
398 #define wxFprintf(f, fmt, ...) \
399 wxFprintf_Impl(f, wxFormatString(fmt), __VA_ARGS__)
400 #define wxSprintf(s, fmt, ...) \
401 wxSprintf_Impl(s, wxFormatString(fmt), __VA_ARGS__)
402 #define wxSnprintf(s, n, fmt, ...) \
403 wxSnprintf_Impl(s, n, wxFormatString(fmt), __VA_ARGS__)
404 #endif // __WATCOMC__
405
406
407 // We can't use wxArgNormalizer<T> for variadic arguments to wxScanf() etc.
408 // because they are writable, so instead of providing friendly template
409 // vararg-like functions, we just provide both char* and wchar_t* variants
410 // of these functions. The type of output variadic arguments for %s must match
411 // the type of 'str' and 'format' arguments.
412 //
413 // For compatibility with earlier wx versions, we also provide wxSscanf()
414 // version with the first argument (input string) wxString; for this version,
415 // the type of output string values is determined by the type of format string
416 // only.
417
418 #define _WX_SCANFUNC_EXTRACT_ARGS_1(x) x
419 #define _WX_SCANFUNC_EXTRACT_ARGS_2(x,y) x, y
420 #define _WX_SCANFUNC_EXTRACT_ARGS(N, args) _WX_SCANFUNC_EXTRACT_ARGS_##N args
421
422 #define _WX_VARARG_PASS_WRITABLE(i) a##i
423
424 #define _WX_DEFINE_SCANFUNC(N, dummy1, name, impl, passfixed, numfixed, fixed)\
425 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
426 int name(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, fixed), \
427 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
428 { \
429 return impl(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, passfixed), \
430 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WRITABLE)); \
431 }
432
433 #define WX_DEFINE_SCANFUNC(name, numfixed, fixed, impl, passfixed) \
434 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
435 _WX_DEFINE_SCANFUNC, \
436 dummy1, name, impl, passfixed, numfixed, fixed)
437
438 // this is needed to normalize the format string, see src/common/strvararg.cpp
439 // for more details
440 #ifdef __WINDOWS__
441 #define wxScanfConvertFormatW(fmt) fmt
442 #else
443 const wxScopedWCharBuffer
444 WXDLLIMPEXP_BASE wxScanfConvertFormatW(const wchar_t *format);
445 #endif
446
447 WX_DEFINE_SCANFUNC(wxScanf, 1, (const char *format),
448 wxCRT_ScanfA, (format))
449 WX_DEFINE_SCANFUNC(wxScanf, 1, (const wchar_t *format),
450 wxCRT_ScanfW, (wxScanfConvertFormatW(format)))
451
452 WX_DEFINE_SCANFUNC(wxFscanf, 2, (FILE *stream, const char *format),
453 wxCRT_FscanfA, (stream, format))
454 WX_DEFINE_SCANFUNC(wxFscanf, 2, (FILE *stream, const wchar_t *format),
455 wxCRT_FscanfW, (stream, wxScanfConvertFormatW(format)))
456
457 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const char *str, const char *format),
458 wxCRT_SscanfA, (str, format))
459 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wchar_t *str, const wchar_t *format),
460 wxCRT_SscanfW, (str, wxScanfConvertFormatW(format)))
461 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxScopedCharBuffer& str, const char *format),
462 wxCRT_SscanfA, (str.data(), format))
463 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxScopedWCharBuffer& str, const wchar_t *format),
464 wxCRT_SscanfW, (str.data(), wxScanfConvertFormatW(format)))
465 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxString& str, const char *format),
466 wxCRT_SscanfA, (str.mb_str(), format))
467 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxString& str, const wchar_t *format),
468 wxCRT_SscanfW, (str.wc_str(), wxScanfConvertFormatW(format)))
469 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxCStrData& str, const char *format),
470 wxCRT_SscanfA, (str.AsCharBuf(), format))
471 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxCStrData& str, const wchar_t *format),
472 wxCRT_SscanfW, (str.AsWCharBuf(), wxScanfConvertFormatW(format)))
473
474 // Visual C++ doesn't provide vsscanf()
475 #ifndef __VISUALC___
476 int WXDLLIMPEXP_BASE wxVsscanf(const char *str, const char *format, va_list ap);
477 int WXDLLIMPEXP_BASE wxVsscanf(const wchar_t *str, const wchar_t *format, va_list ap);
478 int WXDLLIMPEXP_BASE wxVsscanf(const wxScopedCharBuffer& str, const char *format, va_list ap);
479 int WXDLLIMPEXP_BASE wxVsscanf(const wxScopedWCharBuffer& str, const wchar_t *format, va_list ap);
480 int WXDLLIMPEXP_BASE wxVsscanf(const wxString& str, const char *format, va_list ap);
481 int WXDLLIMPEXP_BASE wxVsscanf(const wxString& str, const wchar_t *format, va_list ap);
482 int WXDLLIMPEXP_BASE wxVsscanf(const wxCStrData& str, const char *format, va_list ap);
483 int WXDLLIMPEXP_BASE wxVsscanf(const wxCStrData& str, const wchar_t *format, va_list ap);
484 #endif // !__VISUALC__
485
486 #endif /* _WX_WXCRTVARARG_H_ */