]> git.saurik.com Git - wxWidgets.git/blob - include/wx/wxcrtvararg.h
added ReadType convenience functions (patch 1764160)
[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 #define wxCRT_FprintfW fwprintf
179 #define wxCRT_PrintfW wprintf
180 #define wxCRT_VfprintfW vfwprintf
181 #define wxCRT_VprintfW vwprintf
182
183 #if defined(__WINDOWS__) && !defined(HAVE_VSWPRINTF)
184 // only non-standard vswprintf() without buffer size argument can be used here
185 #define wxCRT_VsprintfW vswprintf
186 #endif
187
188 /*
189 In Unicode mode we need to have all standard functions such as wprintf() and
190 so on but not all systems have them so use our own implementations in this
191 case.
192 */
193 #if wxUSE_UNICODE && !defined(wxHAVE_TCHAR_SUPPORT) && !defined(HAVE_WPRINTF)
194 #define wxNEED_WPRINTF
195 #endif
196
197
198 #if defined(wxNEED_WPRINTF)
199 /*
200 we need to implement all wide character printf functions either because
201 we don't have them at all or because they don't have the semantics we
202 need
203 */
204 int wxCRT_PrintfW( const wchar_t *format, ... );
205 int wxCRT_FprintfW( FILE *stream, const wchar_t *format, ... );
206 int wxCRT_VfprintfW( FILE *stream, const wchar_t *format, va_list ap );
207 int wxCRT_VprintfW( const wchar_t *format, va_list ap );
208 int wxCRT_VsprintfW( wchar_t *str, const wchar_t *format, va_list ap );
209 #endif /* wxNEED_WPRINTF */
210
211
212 /* Required for wxScanf() etc. */
213 #define wxCRT_ScanfA scanf
214 #define wxCRT_SscanfA sscanf
215 #define wxCRT_FscanfA fscanf
216 #define wxCRT_VsscanfA vsscanf
217
218 #if defined(wxNEED_WPRINTF)
219 int wxCRT_ScanfW(const wchar_t *format, ...);
220 int wxCRT_SscanfW(const wchar_t *str, const wchar_t *format, ...);
221 int wxCRT_FscanfW(FILE *stream, const wchar_t *format, ...);
222 int wxCRT_VsscanfW(const wchar_t *str, const wchar_t *format, va_list ap);
223 #else
224 #define wxCRT_ScanfW wscanf
225 #define wxCRT_SscanfW swscanf
226 #define wxCRT_FscanfW fwscanf
227 #define wxCRT_VsscanfW vswscanf
228 #endif
229
230 // ----------------------------------------------------------------------------
231 // user-friendly wrappers to CRT functions
232 // ----------------------------------------------------------------------------
233
234 #ifdef __WATCOMC__
235 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
236 #define wxPrintf wxPrintf_Impl
237 #define wxFprintf wxFprintf_Impl
238 #define wxSprintf wxSprintf_Impl
239 #define wxSnprintf wxSnprintf_Impl
240 #endif
241
242 // FIXME-UTF8: remove this
243 #if wxUSE_UNICODE
244 #define wxCRT_PrintfNative wxCRT_PrintfW
245 #define wxCRT_FprintfNative wxCRT_FprintfW
246 #else
247 #define wxCRT_PrintfNative wxCRT_PrintfA
248 #define wxCRT_FprintfNative wxCRT_FprintfA
249 #endif
250
251 WX_DEFINE_VARARG_FUNC(int, wxPrintf, 1, (const wxFormatString&),
252 wxCRT_PrintfNative, wxCRT_PrintfA)
253 WX_DEFINE_VARARG_FUNC(int, wxFprintf, 2, (FILE*, const wxFormatString&),
254 wxCRT_FprintfNative, wxCRT_FprintfA)
255
256 // va_list versions of printf functions simply forward to the respective
257 // CRT function; note that they assume that va_list was created using
258 // wxArgNormalizer<T>!
259 #if wxUSE_UNICODE_UTF8
260 #if wxUSE_UTF8_LOCALE_ONLY
261 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
262 return implA args
263 #else
264 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
265 if ( wxLocaleIsUtf8 ) return implA args; \
266 else return implW args
267 #endif
268 #elif wxUSE_UNICODE_WCHAR
269 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
270 return implW args
271 #else // ANSI
272 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
273 return implA args
274 #endif
275
276 inline int
277 wxVprintf(const wxString& format, va_list ap)
278 {
279 WX_VARARG_VFOO_IMPL((wxFormatString(format), ap),
280 wxCRT_VprintfW, wxCRT_VprintfA);
281 }
282
283 inline int
284 wxVfprintf(FILE *f, const wxString& format, va_list ap)
285 {
286 WX_VARARG_VFOO_IMPL((f, wxFormatString(format), ap),
287 wxCRT_VfprintfW, wxCRT_VfprintfA);
288 }
289
290 #undef WX_VARARG_VFOO_IMPL
291
292
293 // wxSprintf() and friends have to be implemented in two forms, one for
294 // writing to char* buffer and one for writing to wchar_t*:
295
296 #if !wxUSE_UTF8_LOCALE_ONLY
297 int WXDLLIMPEXP_BASE wxDoSprintfWchar(char *str, const wxChar *format, ...);
298 #endif
299 #if wxUSE_UNICODE_UTF8
300 int WXDLLIMPEXP_BASE wxDoSprintfUtf8(char *str, const char *format, ...);
301 #endif
302 WX_DEFINE_VARARG_FUNC(int, wxSprintf, 2, (char*, const wxFormatString&),
303 wxDoSprintfWchar, wxDoSprintfUtf8)
304
305 int WXDLLIMPEXP_BASE
306 wxVsprintf(char *str, const wxString& format, va_list argptr);
307
308 #if !wxUSE_UTF8_LOCALE_ONLY
309 int WXDLLIMPEXP_BASE wxDoSnprintfWchar(char *str, size_t size, const wxChar *format, ...);
310 #endif
311 #if wxUSE_UNICODE_UTF8
312 int WXDLLIMPEXP_BASE wxDoSnprintfUtf8(char *str, size_t size, const char *format, ...);
313 #endif
314 WX_DEFINE_VARARG_FUNC(int, wxSnprintf, 3, (char*, size_t, const wxFormatString&),
315 wxDoSnprintfWchar, wxDoSnprintfUtf8)
316
317 int WXDLLIMPEXP_BASE
318 wxVsnprintf(char *str, size_t size, const wxString& format, va_list argptr);
319
320 #if wxUSE_UNICODE
321
322 #if !wxUSE_UTF8_LOCALE_ONLY
323 int WXDLLIMPEXP_BASE wxDoSprintfWchar(wchar_t *str, const wxChar *format, ...);
324 #endif
325 #if wxUSE_UNICODE_UTF8
326 int WXDLLIMPEXP_BASE wxDoSprintfUtf8(wchar_t *str, const char *format, ...);
327 #endif
328 WX_DEFINE_VARARG_FUNC(int, wxSprintf, 2, (wchar_t*, const wxFormatString&),
329 wxDoSprintfWchar, wxDoSprintfUtf8)
330
331 int WXDLLIMPEXP_BASE
332 wxVsprintf(wchar_t *str, const wxString& format, va_list argptr);
333
334 #if !wxUSE_UTF8_LOCALE_ONLY
335 int WXDLLIMPEXP_BASE wxDoSnprintfWchar(wchar_t *str, size_t size, const wxChar *format, ...);
336 #endif
337 #if wxUSE_UNICODE_UTF8
338 int WXDLLIMPEXP_BASE wxDoSnprintfUtf8(wchar_t *str, size_t size, const char *format, ...);
339 #endif
340 WX_DEFINE_VARARG_FUNC(int, wxSnprintf, 3, (wchar_t*, size_t, const wxFormatString&),
341 wxDoSnprintfWchar, wxDoSnprintfUtf8)
342
343 int WXDLLIMPEXP_BASE
344 wxVsnprintf(wchar_t *str, size_t size, const wxString& format, va_list argptr);
345
346 #endif // wxUSE_UNICODE
347
348 #ifdef __WATCOMC__
349 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
350 //
351 // fortunately, OpenWatcom implements __VA_ARGS__, so we can provide macros
352 // that cast the format argument to wxString:
353 #undef wxPrintf
354 #undef wxFprintf
355 #undef wxSprintf
356 #undef wxSnprintf
357
358 #define wxPrintf(fmt, ...) \
359 wxPrintf_Impl(wxFormatString(fmt), __VA_ARGS__)
360 #define wxFprintf(f, fmt, ...) \
361 wxFprintf_Impl(f, wxFormatString(fmt), __VA_ARGS__)
362 #define wxSprintf(s, fmt, ...) \
363 wxSprintf_Impl(s, wxFormatString(fmt), __VA_ARGS__)
364 #define wxSnprintf(s, n, fmt, ...) \
365 wxSnprintf_Impl(s, n, wxFormatString(fmt), __VA_ARGS__)
366 #endif // __WATCOMC__
367
368
369 // We can't use wxArgNormalizer<T> for variadic arguments to wxScanf() etc.
370 // because they are writable, so instead of providing friendly template
371 // vararg-like functions, we just provide both char* and wchar_t* variants
372 // of these functions. The type of output variadic arguments for %s must match
373 // the type of 'str' and 'format' arguments.
374 //
375 // For compatibility with earlier wx versions, we also provide wxSscanf()
376 // version with the first argument (input string) wxString; for this version,
377 // the type of output string values is determined by the type of format string
378 // only.
379
380 #define _WX_SCANFUNC_EXTRACT_ARGS_1(x) x
381 #define _WX_SCANFUNC_EXTRACT_ARGS_2(x,y) x, y
382 #define _WX_SCANFUNC_EXTRACT_ARGS(N, args) _WX_SCANFUNC_EXTRACT_ARGS_##N args
383
384 #define _WX_VARARG_PASS_WRITABLE(i) a##i
385
386 #define _WX_DEFINE_SCANFUNC(N, dummy1, name, impl, passfixed, numfixed, fixed)\
387 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
388 int name(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, fixed), \
389 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
390 { \
391 return impl(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, passfixed), \
392 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WRITABLE)); \
393 }
394
395 #define WX_DEFINE_SCANFUNC(name, numfixed, fixed, impl, passfixed) \
396 inline int name(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, fixed)) \
397 { \
398 return impl(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, passfixed)); \
399 } \
400 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
401 _WX_DEFINE_SCANFUNC, \
402 dummy1, name, impl, passfixed, numfixed, fixed)
403
404 // this is needed to normalize the format string, see src/common/strvararg.cpp
405 // for more details
406 #ifdef __WINDOWS__
407 #define wxScanfConvertFormatW(fmt) fmt
408 #else
409 const wxWCharBuffer
410 WXDLLIMPEXP_BASE wxScanfConvertFormatW(const wchar_t *format);
411 #endif
412
413 WX_DEFINE_SCANFUNC(wxScanf, 1, (const char *format),
414 wxCRT_ScanfA, (format))
415 WX_DEFINE_SCANFUNC(wxScanf, 1, (const wchar_t *format),
416 wxCRT_ScanfW, (wxScanfConvertFormatW(format)))
417
418 WX_DEFINE_SCANFUNC(wxFscanf, 2, (FILE *stream, const char *format),
419 wxCRT_FscanfA, (stream, format))
420 WX_DEFINE_SCANFUNC(wxFscanf, 2, (FILE *stream, const wchar_t *format),
421 wxCRT_FscanfW, (stream, wxScanfConvertFormatW(format)))
422
423 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const char *str, const char *format),
424 wxCRT_SscanfA, (str, format))
425 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wchar_t *str, const wchar_t *format),
426 wxCRT_SscanfW, (str, wxScanfConvertFormatW(format)))
427 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxCharBuffer& str, const char *format),
428 wxCRT_SscanfA, (str.data(), format))
429 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxWCharBuffer& str, const wchar_t *format),
430 wxCRT_SscanfW, (str.data(), wxScanfConvertFormatW(format)))
431 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxString& str, const char *format),
432 wxCRT_SscanfA, (str.mb_str(), format))
433 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxString& str, const wchar_t *format),
434 wxCRT_SscanfW, (str.wc_str(), wxScanfConvertFormatW(format)))
435 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxCStrData& str, const char *format),
436 wxCRT_SscanfA, (str.AsCharBuf(), format))
437 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxCStrData& str, const wchar_t *format),
438 wxCRT_SscanfW, (str.AsWCharBuf(), wxScanfConvertFormatW(format)))
439
440 // Visual C++ doesn't provide vsscanf()
441 #ifndef __VISUALC___
442 int WXDLLIMPEXP_BASE wxVsscanf(const char *str, const char *format, va_list ap);
443 int WXDLLIMPEXP_BASE wxVsscanf(const wchar_t *str, const wchar_t *format, va_list ap);
444 int WXDLLIMPEXP_BASE wxVsscanf(const wxCharBuffer& str, const char *format, va_list ap);
445 int WXDLLIMPEXP_BASE wxVsscanf(const wxWCharBuffer& str, const wchar_t *format, va_list ap);
446 int WXDLLIMPEXP_BASE wxVsscanf(const wxString& str, const char *format, va_list ap);
447 int WXDLLIMPEXP_BASE wxVsscanf(const wxString& str, const wchar_t *format, va_list ap);
448 int WXDLLIMPEXP_BASE wxVsscanf(const wxCStrData& str, const char *format, va_list ap);
449 int WXDLLIMPEXP_BASE wxVsscanf(const wxCStrData& str, const wchar_t *format, va_list ap);
450 #endif // !__VISUALC__
451
452 #endif /* _WX_WXCRTVARARG_H_ */