]> git.saurik.com Git - wxWidgets.git/blob - include/wx/wxcrtvararg.h
added missing #include "wx/buffer.h"
[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_Scanf _tscanf
32
33 #define wxCRT_Sscanf _stscanf
34 #define wxCRT_Vfprintf _vftprintf
35 #define wxCRT_Vprintf _vtprintf
36 #define wxCRT_Vsscanf _vstscanf
37 #define wxCRT_Vsprintf _vstprintf
38
39 #else /* !TCHAR-aware compilers */
40
41 #if !wxUSE_UNICODE /* ASCII */
42 #define wxCRT_Fprintf fprintf
43 #define wxCRT_Fscanf fscanf
44 #define wxCRT_Printf printf
45 #define wxCRT_Scanf scanf
46 #define wxCRT_Sscanf sscanf
47 #define wxCRT_Vfprintf vfprintf
48 #define wxCRT_Vprintf vprintf
49 #define wxCRT_Vsscanf vsscanf
50 #define wxCRT_Vsprintf vsprintf
51 #endif /* ASCII */
52 #endif /* TCHAR-aware compilers/the others */
53
54 /* printf() family saga */
55
56 /*
57 For some systems [v]snprintf() exists in the system libraries but not in the
58 headers, so we need to declare it ourselves to be able to use it.
59 */
60 #if defined(HAVE_VSNPRINTF) && !defined(HAVE_VSNPRINTF_DECL)
61 #ifdef __cplusplus
62 extern "C"
63 #else
64 extern
65 #endif
66 int vsnprintf(char *str, size_t size, const char *format, va_list ap);
67 #endif /* !HAVE_VSNPRINTF_DECL */
68
69 #if defined(HAVE_SNPRINTF) && !defined(HAVE_SNPRINTF_DECL)
70 #ifdef __cplusplus
71 extern "C"
72 #else
73 extern
74 #endif
75 WXDLLIMPEXP_BASE int snprintf(char *str, size_t size, const char *format, ...);
76 #endif /* !HAVE_SNPRINTF_DECL */
77
78 /* Wrapper for vsnprintf if it's 3rd parameter is non-const. Note: the
79 * same isn't done for snprintf below, the builtin wxSnprintf_ is used
80 * instead since it's already a simple wrapper */
81 #if defined __cplusplus && defined HAVE_BROKEN_VSNPRINTF_DECL
82 inline int wx_fixed_vsnprintf(char *str, size_t size, const char *format, va_list ap)
83 {
84 return vsnprintf(str, size, (char*)format, ap);
85 }
86 #endif
87
88 /*
89 MinGW MSVCRT has non-standard vswprintf() (for MSVC compatibility
90 presumably) and normally _vsnwprintf() is used instead
91 */
92 #if defined(HAVE_VSWPRINTF) && defined(__MINGW32__)
93 #undef HAVE_VSWPRINTF
94 #endif
95
96 #if wxUSE_PRINTF_POS_PARAMS
97 /*
98 The systems where vsnprintf() supports positional parameters should
99 define the HAVE_UNIX98_PRINTF symbol.
100
101 On systems which don't (e.g. Windows) we are forced to use
102 our wxVsnprintf() implementation.
103 */
104 #if defined(HAVE_UNIX98_PRINTF)
105 #if wxUSE_UNICODE
106 #ifdef HAVE_VSWPRINTF
107 #define wxVsnprintf_ vswprintf
108 #endif
109 #else /* ASCII */
110 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
111 #define wxVsnprintf_ wx_fixed_vsnprintf
112 #else
113 #define wxVsnprintf_ vsnprintf
114 #endif
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 #if wxUSE_UNICODE
125 #define wxVsnprintf_ _vswprintf_p
126 #else
127 #define wxVsnprintf_ _vsprintf_p
128 #endif
129 #endif
130 #endif /* HAVE_UNIX98_PRINTF/!HAVE_UNIX98_PRINTF */
131 #else /* !wxUSE_PRINTF_POS_PARAMS */
132 /*
133 We always want to define safe snprintf() function to be used instead of
134 sprintf(). Some compilers already have it (or rather vsnprintf() which
135 we really need...), otherwise we implement it using our own printf()
136 code.
137
138 We define function with a trailing underscore here because the real one
139 is a wrapper around it as explained below
140 */
141
142 /* first deal with TCHAR-aware compilers which have _vsntprintf */
143 #ifndef wxVsnprintf_
144 #if defined(__VISUALC__) || \
145 (defined(__BORLANDC__) && __BORLANDC__ >= 0x540)
146 #define wxVsnprintf_ _vsntprintf
147 #endif
148 #endif
149
150 /* if this didn't work, define it separately for Unicode and ANSI builds */
151 #ifndef wxVsnprintf_
152 #if wxUSE_UNICODE
153 #if defined(HAVE__VSNWPRINTF)
154 #define wxVsnprintf_ _vsnwprintf
155 #elif defined(HAVE_VSWPRINTF)
156 #define wxVsnprintf_ vswprintf
157 #elif defined(__WATCOMC__)
158 #define wxVsnprintf_ _vsnwprintf
159 #endif
160 #else /* ASCII */
161 /*
162 All versions of CodeWarrior supported by wxWidgets apparently
163 have both snprintf() and vsnprintf()
164 */
165 #if defined(HAVE_VSNPRINTF) \
166 || defined(__MWERKS__) || defined(__WATCOMC__)
167 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
168 #define wxVsnprintf_ wx_fixed_vsnprintf
169 #else
170 #define wxVsnprintf_ vsnprintf
171 #endif
172 #endif
173 #endif /* Unicode/ASCII */
174 #endif /* wxVsnprintf_ */
175 #endif /* wxUSE_PRINTF_POS_PARAMS/!wxUSE_PRINTF_POS_PARAMS */
176
177 #ifndef wxVsnprintf_
178 /* no (suitable) vsnprintf(), cook our own */
179 WXDLLIMPEXP_BASE int
180 wxVsnprintf_(wxChar *buf, size_t len, const wxChar *format, va_list argptr);
181
182 #define wxUSE_WXVSNPRINTF 1
183 #else
184 #define wxUSE_WXVSNPRINTF 0
185 #endif
186
187 /*
188 In Unicode mode we need to have all standard functions such as wprintf() and
189 so on but not all systems have them so use our own implementations in this
190 case.
191 */
192 #if wxUSE_UNICODE && !defined(wxHAVE_TCHAR_SUPPORT) && !defined(HAVE_WPRINTF)
193 #define wxNEED_WPRINTF
194 #endif
195
196 /*
197 More Unicode complications: although both ANSI C and C++ define a number of
198 wide character functions such as wprintf(), not all environments have them.
199 Worse, those which do have different behaviours: under Windows, %s format
200 specifier changes its meaning in Unicode build and expects a Unicode string
201 while under Unix/POSIX it still means an ASCII string even for wprintf() and
202 %ls has to be used for wide strings.
203
204 We choose to always emulate Windows behaviour as more useful for us so even
205 if we have wprintf() we still must wrap it in a non trivial wxPrintf().
206
207 */
208
209 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
210 /*
211 we need to implement all wide character printf and scanf functions
212 either because we don't have them at all or because they don't have the
213 semantics we need
214 */
215 int wxCRT_Scanf( const wxChar *format, ... ) ATTRIBUTE_PRINTF_1;
216 int wxCRT_Sscanf( const wxChar *str, const wxChar *format, ... ) ATTRIBUTE_PRINTF_2;
217 int wxCRT_Fscanf( FILE *stream, const wxChar *format, ... ) ATTRIBUTE_PRINTF_2;
218 int wxCRT_Vsscanf( const wxChar *str, const wxChar *format, va_list ap );
219 int wxCRT_Printf( const wxChar *format, ... ) ATTRIBUTE_PRINTF_1;
220 int wxCRT_Fprintf( FILE *stream, const wxChar *format, ... ) ATTRIBUTE_PRINTF_2;
221 int wxCRT_Vfprintf( FILE *stream, const wxChar *format, va_list ap );
222 int wxCRT_Vprintf( const wxChar *format, va_list ap );
223 int wxCRT_Vsprintf( wxChar *str, const wxChar *format, va_list ap );
224 #endif /* wxNEED_PRINTF_CONVERSION */
225
226 /* these 2 can be simply mapped to the versions with underscore at the end */
227 /* if we don't have to do the conversion */
228 /*
229 However, if we don't have any vswprintf() at all we don't need to redefine
230 anything as our own wxVsnprintf_() already behaves as needed.
231 */
232 #if defined(wxNEED_PRINTF_CONVERSION) && defined(wxVsnprintf_)
233 int wxCRT_Vsnprintf( wxChar *str, size_t size, const wxChar *format, va_list ap );
234 #else
235 #define wxCRT_Vsnprintf wxVsnprintf_
236 #endif
237
238
239 // ----------------------------------------------------------------------------
240 // user-friendly wrappers to CRT functions
241 // ----------------------------------------------------------------------------
242
243 #ifdef __WATCOMC__
244 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
245 #define wxPrintf wxPrintf_Impl
246 #define wxFprintf wxFprintf_Impl
247 #define wxSprintf wxSprintf_Impl
248 #define wxSnprintf wxSnprintf_Impl
249 #endif
250
251 // FIXME-UTF8: explicit wide-string and short-string format specifiers
252 // (%hs, %ls and variants) are currently broken, only %s works
253 // as expected (regardless of the build)
254
255 // FIXME-UTF8: %c (and %hc, %lc) don't work as expected either: in UTF-8 build,
256 // we should replace them with %s (as some Unicode chars may be
257 // encoded with >1 bytes) and in all builds, we should use wchar_t
258 // for all characters and convert char to it;
259 // we'll also need wxArgNormalizer<T> specializations for char,
260 // wchar_t, wxUniChar and wxUniCharRef to handle this correctly
261
262 WX_DEFINE_VARARG_FUNC2(int, wxPrintf, 1, (const wxString&),
263 wxCRT_Printf, printf)
264 WX_DEFINE_VARARG_FUNC2(int, wxFprintf, 2, (FILE*, const wxString&),
265 wxCRT_Fprintf, fprintf)
266
267 // va_list versions of printf functions simply forward to the respective
268 // CRT function; note that they assume that va_list was created using
269 // wxArgNormalizer<T>!
270 #if wxUSE_UNICODE_UTF8
271 #if wxUSE_UTF8_LOCALE_ONLY
272 #define WX_VARARG_VFOO_IMPL(args, implWchar, implUtf8) \
273 return implUtf8 args
274 #else
275 #define WX_VARARG_VFOO_IMPL(args, implWchar, implUtf8) \
276 if ( wxLocaleIsUtf8 ) return implUtf8 args; \
277 else return implWchar args
278 #endif
279 #else // wxUSE_UNICODE_WCHAR / ANSI
280 #define WX_VARARG_VFOO_IMPL(args, implWchar, implUtf8) \
281 return implWchar args
282 #endif
283
284 inline int
285 wxVprintf(const wxString& format, va_list ap)
286 {
287 WX_VARARG_VFOO_IMPL((format, ap), wxCRT_Vprintf, vprintf);
288 }
289
290 inline int
291 wxVfprintf(FILE *f, const wxString& format, va_list ap)
292 {
293 WX_VARARG_VFOO_IMPL((f, format, ap), wxCRT_Vfprintf, vfprintf);
294 }
295
296 #undef WX_VARARG_VFOO_IMPL
297
298
299 // wxSprintf() and friends have to be implemented in two forms, one for
300 // writing to char* buffer and one for writing to wchar_t*:
301
302 int WXDLLIMPEXP_BASE wxDoSprintf(char *str, const wxString& format, ...);
303 WX_DEFINE_VARARG_FUNC(int, wxSprintf, 2, (char*, const wxString&),
304 wxDoSprintf)
305
306 int WXDLLIMPEXP_BASE
307 wxVsprintf(char *str, const wxString& format, va_list argptr);
308
309 int WXDLLIMPEXP_BASE wxDoSnprintf(char *str, size_t size, const wxString& format, ...);
310 WX_DEFINE_VARARG_FUNC(int, wxSnprintf, 3, (char*, size_t, const wxString&),
311 wxDoSnprintf)
312
313 int WXDLLIMPEXP_BASE
314 wxVsnprintf(char *str, size_t size, const wxString& format, va_list argptr);
315
316 #if wxUSE_UNICODE
317 int WXDLLIMPEXP_BASE wxDoSprintf(wchar_t *str, const wxString& format, ...);
318 WX_DEFINE_VARARG_FUNC(int, wxSprintf, 2, (wchar_t*, const wxString&),
319 wxDoSprintf)
320
321 int WXDLLIMPEXP_BASE
322 wxVsprintf(wchar_t *str, const wxString& format, va_list argptr);
323
324 int WXDLLIMPEXP_BASE wxDoSnprintf(wchar_t *str, size_t size, const wxString& format, ...);
325 WX_DEFINE_VARARG_FUNC(int, wxSnprintf, 3, (wchar_t*, size_t, const wxString&),
326 wxDoSnprintf)
327
328 int WXDLLIMPEXP_BASE
329 wxVsnprintf(wchar_t *str, size_t size, const wxString& format, va_list argptr);
330 #endif // wxUSE_UNICODE
331
332 #ifdef __WATCOMC__
333 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
334 //
335 // fortunately, OpenWatcom implements __VA_ARGS__, so we can provide macros
336 // that cast the format argument to wxString:
337 #undef wxPrintf
338 #undef wxFprintf
339 #undef wxSprintf
340 #undef wxSnprintf
341
342 #define wxPrintf(fmt, ...) \
343 wxPrintf_Impl(wxString(fmt), __VA_ARGS__)
344 #define wxFprintf(f, fmt, ...) \
345 wxFprintf_Impl(f, wxString(fmt), __VA_ARGS__)
346 #define wxSprintf(s, fmt, ...) \
347 wxSprintf_Impl(s, wxString(fmt), __VA_ARGS__)
348 #define wxSnprintf(s, n, fmt, ...) \
349 wxSnprintf_Impl(s, n, wxString(fmt), __VA_ARGS__)
350 #endif // __WATCOMC__
351
352
353 // We can't use wxArgNormalizer<T> for variadic arguments to wxScanf() etc.
354 // because they are writable, so instead of providing friendly template
355 // vararg-like functions, we just provide both char* and wchar_t* variants
356 // of these functions. The type of output variadic arguments for %s must match
357 // the type of 'str' and 'format' arguments.
358
359 // FIXME-UTF8: actually do it, for now we only have wxChar* variants:
360 #define wxScanf wxCRT_Scanf
361 #define wxFscanf wxCRT_Fscanf
362 #define wxSscanf wxCRT_Sscanf
363 #define wxVsscanf wxCRT_Vsscanf
364
365 #endif /* _WX_WXCRTVARARG_H_ */