]> git.saurik.com Git - wxWidgets.git/blob - include/wx/wxcrt.h
2dc22cd2a25cf6443029fbd9daa47f6a1448f446
[wxWidgets.git] / include / wx / wxcrt.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/wxcrt.h
3 // Purpose: Type-safe ANSI and Unicode builds compatible wrappers for
4 // CRT functions
5 // Author: Joel Farley, Ove Kaaven
6 // Modified by: Vadim Zeitlin, Robert Roebling, Ron Lee, Vaclav Slavik
7 // Created: 1998/06/12
8 // RCS-ID: $Id$
9 // Copyright: (c) 1998-2006 wxWidgets dev team
10 // Licence: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_WXCRT_H_
14 #define _WX_WXCRT_H_
15
16 #include "wx/wxcrtbase.h"
17 #include "wx/string.h"
18
19 #ifndef __WX_SETUP_H__
20 // For non-configure builds assume vsscanf is available, if not Visual C or DMC
21 #if !defined (__VISUALC__) && !defined (__DMC__)
22 #define HAVE_VSSCANF 1
23 #endif
24 #endif
25
26 // ============================================================================
27 // misc functions
28 // ============================================================================
29
30 /* checks whether the passed in pointer is NULL and if the string is empty */
31 inline bool wxIsEmpty(const char *s) { return !s || !*s; }
32 inline bool wxIsEmpty(const wchar_t *s) { return !s || !*s; }
33 inline bool wxIsEmpty(const wxScopedCharBuffer& s) { return wxIsEmpty(s.data()); }
34 inline bool wxIsEmpty(const wxScopedWCharBuffer& s) { return wxIsEmpty(s.data()); }
35 inline bool wxIsEmpty(const wxString& s) { return s.empty(); }
36 inline bool wxIsEmpty(const wxCStrData& s) { return s.AsString().empty(); }
37
38
39
40 /* multibyte to wide char conversion functions and macros */
41
42 /* multibyte<->widechar conversion */
43 WXDLLIMPEXP_BASE size_t wxMB2WC(wchar_t *buf, const char *psz, size_t n);
44 WXDLLIMPEXP_BASE size_t wxWC2MB(char *buf, const wchar_t *psz, size_t n);
45
46 #if wxUSE_UNICODE
47 #define wxMB2WX wxMB2WC
48 #define wxWX2MB wxWC2MB
49 #define wxWC2WX wxStrncpy
50 #define wxWX2WC wxStrncpy
51 #else
52 #define wxMB2WX wxStrncpy
53 #define wxWX2MB wxStrncpy
54 #define wxWC2WX wxWC2MB
55 #define wxWX2WC wxMB2WC
56 #endif
57
58
59 // RN: We could do the usual tricky compiler detection here,
60 // and use their variant (such as wmemchr, etc.). The problem
61 // is that these functions are quite rare, even though they are
62 // part of the current POSIX standard. In addition, most compilers
63 // (including even MSC) inline them just like we do right in their
64 // headers.
65 //
66 #include <string.h>
67
68 #if wxUSE_UNICODE
69 //implement our own wmem variants
70 inline wxChar* wxTmemchr(const wxChar* s, wxChar c, size_t l)
71 {
72 for(;l && *s != c;--l, ++s) {}
73
74 if(l)
75 return const_cast<wxChar*>(s);
76 return NULL;
77 }
78
79 inline int wxTmemcmp(const wxChar* sz1, const wxChar* sz2, size_t len)
80 {
81 for(; *sz1 == *sz2 && len; --len, ++sz1, ++sz2) {}
82
83 if(len)
84 return *sz1 < *sz2 ? -1 : *sz1 > *sz2;
85 else
86 return 0;
87 }
88
89 inline wxChar* wxTmemcpy(wxChar* szOut, const wxChar* szIn, size_t len)
90 {
91 return (wxChar*) memcpy(szOut, szIn, len * sizeof(wxChar));
92 }
93
94 inline wxChar* wxTmemmove(wxChar* szOut, const wxChar* szIn, size_t len)
95 {
96 return (wxChar*) memmove(szOut, szIn, len * sizeof(wxChar));
97 }
98
99 inline wxChar* wxTmemset(wxChar* szOut, const wxChar cIn, size_t len)
100 {
101 wxChar* szRet = szOut;
102
103 while (len--)
104 *szOut++ = cIn;
105
106 return szRet;
107 }
108 #endif /* wxUSE_UNICODE */
109
110 // provide trivial wrappers for char* versions for both ANSI and Unicode builds
111 // (notice that these intentionally return "char *" and not "void *" unlike the
112 // standard memxxx() for symmetry with the wide char versions):
113 inline char* wxTmemchr(const char* s, char c, size_t len)
114 { return (char*)memchr(s, c, len); }
115 inline int wxTmemcmp(const char* sz1, const char* sz2, size_t len)
116 { return memcmp(sz1, sz2, len); }
117 inline char* wxTmemcpy(char* szOut, const char* szIn, size_t len)
118 { return (char*)memcpy(szOut, szIn, len); }
119 inline char* wxTmemmove(char* szOut, const char* szIn, size_t len)
120 { return (char*)memmove(szOut, szIn, len); }
121 inline char* wxTmemset(char* szOut, const char cIn, size_t len)
122 { return (char*)memset(szOut, cIn, len); }
123
124
125 // ============================================================================
126 // wx wrappers for CRT functions in both char* and wchar_t* versions
127 // ============================================================================
128
129 // A few notes on implementation of these wrappers:
130 //
131 // We need both char* and wchar_t* versions of functions like wxStrlen() for
132 // compatibility with both ANSI and Unicode builds.
133 //
134 // This makes passing wxString or c_str()/mb_str()/wc_str() result to them
135 // ambiguous, so we need to provide overrides for that as well (in cases where
136 // it makes sense).
137 //
138 // We can do this without problems for some functions (wxStrlen()), but in some
139 // cases, we can't stay compatible with both ANSI and Unicode builds, e.g. for
140 // wxStrcpy(const wxString&), which can only return either char* or wchar_t*.
141 // In these cases, we preserve ANSI build compatibility by returning char*.
142
143 // ----------------------------------------------------------------------------
144 // locale functions
145 // ----------------------------------------------------------------------------
146
147 // NB: we can't provide const wchar_t* (= wxChar*) overload, because calling
148 // wxSetlocale(category, NULL) -- which is a common thing to do -- would be
149 // ambiguous
150 WXDLLIMPEXP_BASE char* wxSetlocale(int category, const char *locale);
151 inline char* wxSetlocale(int category, const wxScopedCharBuffer& locale)
152 { return wxSetlocale(category, locale.data()); }
153 inline char* wxSetlocale(int category, const wxString& locale)
154 { return wxSetlocale(category, locale.mb_str()); }
155 inline char* wxSetlocale(int category, const wxCStrData& locale)
156 { return wxSetlocale(category, locale.AsCharBuf()); }
157
158 // ----------------------------------------------------------------------------
159 // string functions
160 // ----------------------------------------------------------------------------
161
162 /* safe version of strlen() (returns 0 if passed NULL pointer) */
163 // NB: these are defined in wxcrtbase.h, see the comment there
164 // inline size_t wxStrlen(const char *s) { return s ? strlen(s) : 0; }
165 // inline size_t wxStrlen(const wchar_t *s) { return s ? wxCRT_Strlen_(s) : 0; }
166 inline size_t wxStrlen(const wxScopedCharBuffer& s) { return wxStrlen(s.data()); }
167 inline size_t wxStrlen(const wxScopedWCharBuffer& s) { return wxStrlen(s.data()); }
168 inline size_t wxStrlen(const wxString& s) { return s.length(); }
169 inline size_t wxStrlen(const wxCStrData& s) { return s.AsString().length(); }
170
171 // this is a function new in 2.9 so we don't care about backwards compatibility and
172 // so don't need to support wxScopedCharBuffer/wxScopedWCharBuffer overloads
173 #if defined(wxCRT_StrnlenA)
174 inline size_t wxStrnlen(const char *str, size_t maxlen) { return wxCRT_StrnlenA(str, maxlen); }
175 #else
176 inline size_t wxStrnlen(const char *str, size_t maxlen)
177 {
178 size_t n;
179 for ( n = 0; n < maxlen; n++ )
180 if ( !str[n] )
181 break;
182
183 return n;
184 }
185 #endif
186
187 #if defined(wxCRT_StrnlenW)
188 inline size_t wxStrnlen(const wchar_t *str, size_t maxlen) { return wxCRT_StrnlenW(str, maxlen); }
189 #else
190 inline size_t wxStrnlen(const wchar_t *str, size_t maxlen)
191 {
192 size_t n;
193 for ( n = 0; n < maxlen; n++ )
194 if ( !str[n] )
195 break;
196
197 return n;
198 }
199 #endif
200
201 // NB: these are defined in wxcrtbase.h, see the comment there
202 // inline char* wxStrdup(const char *s) { return wxStrdupA(s); }
203 // inline wchar_t* wxStrdup(const wchar_t *s) { return wxStrdupW(s); }
204 inline char* wxStrdup(const wxScopedCharBuffer& s) { return wxStrdup(s.data()); }
205 inline wchar_t* wxStrdup(const wxScopedWCharBuffer& s) { return wxStrdup(s.data()); }
206 inline char* wxStrdup(const wxString& s) { return wxStrdup(s.mb_str()); }
207 inline char* wxStrdup(const wxCStrData& s) { return wxStrdup(s.AsCharBuf()); }
208
209 inline char *wxStrcpy(char *dest, const char *src)
210 { return wxCRT_StrcpyA(dest, src); }
211 inline wchar_t *wxStrcpy(wchar_t *dest, const wchar_t *src)
212 { return wxCRT_StrcpyW(dest, src); }
213 inline char *wxStrcpy(char *dest, const wxString& src)
214 { return wxCRT_StrcpyA(dest, src.mb_str()); }
215 inline char *wxStrcpy(char *dest, const wxCStrData& src)
216 { return wxCRT_StrcpyA(dest, src.AsCharBuf()); }
217 inline char *wxStrcpy(char *dest, const wxScopedCharBuffer& src)
218 { return wxCRT_StrcpyA(dest, src.data()); }
219 inline wchar_t *wxStrcpy(wchar_t *dest, const wxString& src)
220 { return wxCRT_StrcpyW(dest, src.wc_str()); }
221 inline wchar_t *wxStrcpy(wchar_t *dest, const wxCStrData& src)
222 { return wxCRT_StrcpyW(dest, src.AsWCharBuf()); }
223 inline wchar_t *wxStrcpy(wchar_t *dest, const wxScopedWCharBuffer& src)
224 { return wxCRT_StrcpyW(dest, src.data()); }
225 inline char *wxStrcpy(char *dest, const wchar_t *src)
226 { return wxCRT_StrcpyA(dest, wxConvLibc.cWC2MB(src)); }
227 inline wchar_t *wxStrcpy(wchar_t *dest, const char *src)
228 { return wxCRT_StrcpyW(dest, wxConvLibc.cMB2WC(src)); }
229
230 inline char *wxStrncpy(char *dest, const char *src, size_t n)
231 { return wxCRT_StrncpyA(dest, src, n); }
232 inline wchar_t *wxStrncpy(wchar_t *dest, const wchar_t *src, size_t n)
233 { return wxCRT_StrncpyW(dest, src, n); }
234 inline char *wxStrncpy(char *dest, const wxString& src, size_t n)
235 { return wxCRT_StrncpyA(dest, src.mb_str(), n); }
236 inline char *wxStrncpy(char *dest, const wxCStrData& src, size_t n)
237 { return wxCRT_StrncpyA(dest, src.AsCharBuf(), n); }
238 inline char *wxStrncpy(char *dest, const wxScopedCharBuffer& src, size_t n)
239 { return wxCRT_StrncpyA(dest, src.data(), n); }
240 inline wchar_t *wxStrncpy(wchar_t *dest, const wxString& src, size_t n)
241 { return wxCRT_StrncpyW(dest, src.wc_str(), n); }
242 inline wchar_t *wxStrncpy(wchar_t *dest, const wxCStrData& src, size_t n)
243 { return wxCRT_StrncpyW(dest, src.AsWCharBuf(), n); }
244 inline wchar_t *wxStrncpy(wchar_t *dest, const wxScopedWCharBuffer& src, size_t n)
245 { return wxCRT_StrncpyW(dest, src.data(), n); }
246 inline char *wxStrncpy(char *dest, const wchar_t *src, size_t n)
247 { return wxCRT_StrncpyA(dest, wxConvLibc.cWC2MB(src), n); }
248 inline wchar_t *wxStrncpy(wchar_t *dest, const char *src, size_t n)
249 { return wxCRT_StrncpyW(dest, wxConvLibc.cMB2WC(src), n); }
250
251 // this is a function new in 2.9 so we don't care about backwards compatibility and
252 // so don't need to support wchar_t/char overloads
253 inline size_t wxStrlcpy(char *dest, const char *src, size_t n)
254 {
255 const size_t len = wxCRT_StrlenA(src);
256
257 if ( n )
258 {
259 if ( n-- > len )
260 n = len;
261 wxCRT_StrncpyA(dest, src, n);
262 dest[n] = '\0';
263 }
264
265 return len;
266 }
267 inline size_t wxStrlcpy(wchar_t *dest, const wchar_t *src, size_t n)
268 {
269 const size_t len = wxCRT_StrlenW(src);
270 if ( n )
271 {
272 if ( n-- > len )
273 n = len;
274 wxCRT_StrncpyW(dest, src, n);
275 dest[n] = L'\0';
276 }
277
278 return len;
279 }
280
281 inline char *wxStrcat(char *dest, const char *src)
282 { return wxCRT_StrcatA(dest, src); }
283 inline wchar_t *wxStrcat(wchar_t *dest, const wchar_t *src)
284 { return wxCRT_StrcatW(dest, src); }
285 inline char *wxStrcat(char *dest, const wxString& src)
286 { return wxCRT_StrcatA(dest, src.mb_str()); }
287 inline char *wxStrcat(char *dest, const wxCStrData& src)
288 { return wxCRT_StrcatA(dest, src.AsCharBuf()); }
289 inline char *wxStrcat(char *dest, const wxScopedCharBuffer& src)
290 { return wxCRT_StrcatA(dest, src.data()); }
291 inline wchar_t *wxStrcat(wchar_t *dest, const wxString& src)
292 { return wxCRT_StrcatW(dest, src.wc_str()); }
293 inline wchar_t *wxStrcat(wchar_t *dest, const wxCStrData& src)
294 { return wxCRT_StrcatW(dest, src.AsWCharBuf()); }
295 inline wchar_t *wxStrcat(wchar_t *dest, const wxScopedWCharBuffer& src)
296 { return wxCRT_StrcatW(dest, src.data()); }
297 inline char *wxStrcat(char *dest, const wchar_t *src)
298 { return wxCRT_StrcatA(dest, wxConvLibc.cWC2MB(src)); }
299 inline wchar_t *wxStrcat(wchar_t *dest, const char *src)
300 { return wxCRT_StrcatW(dest, wxConvLibc.cMB2WC(src)); }
301
302 inline char *wxStrncat(char *dest, const char *src, size_t n)
303 { return wxCRT_StrncatA(dest, src, n); }
304 inline wchar_t *wxStrncat(wchar_t *dest, const wchar_t *src, size_t n)
305 { return wxCRT_StrncatW(dest, src, n); }
306 inline char *wxStrncat(char *dest, const wxString& src, size_t n)
307 { return wxCRT_StrncatA(dest, src.mb_str(), n); }
308 inline char *wxStrncat(char *dest, const wxCStrData& src, size_t n)
309 { return wxCRT_StrncatA(dest, src.AsCharBuf(), n); }
310 inline char *wxStrncat(char *dest, const wxScopedCharBuffer& src, size_t n)
311 { return wxCRT_StrncatA(dest, src.data(), n); }
312 inline wchar_t *wxStrncat(wchar_t *dest, const wxString& src, size_t n)
313 { return wxCRT_StrncatW(dest, src.wc_str(), n); }
314 inline wchar_t *wxStrncat(wchar_t *dest, const wxCStrData& src, size_t n)
315 { return wxCRT_StrncatW(dest, src.AsWCharBuf(), n); }
316 inline wchar_t *wxStrncat(wchar_t *dest, const wxScopedWCharBuffer& src, size_t n)
317 { return wxCRT_StrncatW(dest, src.data(), n); }
318 inline char *wxStrncat(char *dest, const wchar_t *src, size_t n)
319 { return wxCRT_StrncatA(dest, wxConvLibc.cWC2MB(src), n); }
320 inline wchar_t *wxStrncat(wchar_t *dest, const char *src, size_t n)
321 { return wxCRT_StrncatW(dest, wxConvLibc.cMB2WC(src), n); }
322
323
324 #define WX_STR_DECL(name, T1, T2) name(T1 s1, T2 s2)
325 #define WX_STR_CALL(func, a1, a2) func(a1, a2)
326
327 // This macro defines string function for all possible variants of arguments,
328 // except for those taking wxString or wxCStrData as second argument.
329 // Parameters:
330 // rettype - return type
331 // name - name of the (overloaded) function to define
332 // crtA - function to call for char* versions (takes two arguments)
333 // crtW - ditto for wchar_t* function
334 // forString - function to call when the *first* argument is wxString;
335 // the second argument can be any string type, so this is
336 // typically a template
337 #define WX_STR_FUNC_NO_INVERT(rettype, name, crtA, crtW, forString) \
338 inline rettype WX_STR_DECL(name, const char *, const char *) \
339 { return WX_STR_CALL(crtA, s1, s2); } \
340 inline rettype WX_STR_DECL(name, const char *, const wchar_t *) \
341 { return WX_STR_CALL(forString, wxString(s1), wxString(s2)); } \
342 inline rettype WX_STR_DECL(name, const char *, const wxScopedCharBuffer&) \
343 { return WX_STR_CALL(crtA, s1, s2.data()); } \
344 inline rettype WX_STR_DECL(name, const char *, const wxScopedWCharBuffer&) \
345 { return WX_STR_CALL(forString, wxString(s1), s2.data()); } \
346 \
347 inline rettype WX_STR_DECL(name, const wchar_t *, const wchar_t *) \
348 { return WX_STR_CALL(crtW, s1, s2); } \
349 inline rettype WX_STR_DECL(name, const wchar_t *, const char *) \
350 { return WX_STR_CALL(forString, wxString(s1), wxString(s2)); } \
351 inline rettype WX_STR_DECL(name, const wchar_t *, const wxScopedWCharBuffer&) \
352 { return WX_STR_CALL(crtW, s1, s2.data()); } \
353 inline rettype WX_STR_DECL(name, const wchar_t *, const wxScopedCharBuffer&) \
354 { return WX_STR_CALL(forString, wxString(s1), s2.data()); } \
355 \
356 inline rettype WX_STR_DECL(name, const wxScopedCharBuffer&, const char *) \
357 { return WX_STR_CALL(crtA, s1.data(), s2); } \
358 inline rettype WX_STR_DECL(name, const wxScopedCharBuffer&, const wchar_t *) \
359 { return WX_STR_CALL(forString, wxString(s1), wxString(s2)); } \
360 inline rettype WX_STR_DECL(name, const wxScopedCharBuffer&, const wxScopedCharBuffer&)\
361 { return WX_STR_CALL(crtA, s1.data(), s2.data()); } \
362 inline rettype WX_STR_DECL(name, const wxScopedCharBuffer&, const wxScopedWCharBuffer&) \
363 { return WX_STR_CALL(forString, wxString(s1), wxString(s2)); } \
364 \
365 inline rettype WX_STR_DECL(name, const wxScopedWCharBuffer&, const wchar_t *) \
366 { return WX_STR_CALL(crtW, s1.data(), s2); } \
367 inline rettype WX_STR_DECL(name, const wxScopedWCharBuffer&, const char *) \
368 { return WX_STR_CALL(forString, wxString(s1), wxString(s2)); } \
369 inline rettype WX_STR_DECL(name, const wxScopedWCharBuffer&, const wxScopedWCharBuffer&) \
370 { return WX_STR_CALL(crtW, s1.data(), s2.data()); } \
371 inline rettype WX_STR_DECL(name, const wxScopedWCharBuffer&, const wxScopedCharBuffer&) \
372 { return WX_STR_CALL(forString, wxString(s1), wxString(s2)); } \
373 \
374 inline rettype WX_STR_DECL(name, const wxString&, const char*) \
375 { return WX_STR_CALL(forString, s1, s2); } \
376 inline rettype WX_STR_DECL(name, const wxString&, const wchar_t*) \
377 { return WX_STR_CALL(forString, s1, s2); } \
378 inline rettype WX_STR_DECL(name, const wxString&, const wxScopedCharBuffer&) \
379 { return WX_STR_CALL(forString, s1, s2); } \
380 inline rettype WX_STR_DECL(name, const wxString&, const wxScopedWCharBuffer&) \
381 { return WX_STR_CALL(forString, s1, s2); } \
382 inline rettype WX_STR_DECL(name, const wxString&, const wxString&) \
383 { return WX_STR_CALL(forString, s1, s2); } \
384 inline rettype WX_STR_DECL(name, const wxString&, const wxCStrData&) \
385 { return WX_STR_CALL(forString, s1, s2); } \
386 \
387 inline rettype WX_STR_DECL(name, const wxCStrData&, const char*) \
388 { return WX_STR_CALL(forString, s1.AsString(), s2); } \
389 inline rettype WX_STR_DECL(name, const wxCStrData&, const wchar_t*) \
390 { return WX_STR_CALL(forString, s1.AsString(), s2); } \
391 inline rettype WX_STR_DECL(name, const wxCStrData&, const wxScopedCharBuffer&) \
392 { return WX_STR_CALL(forString, s1.AsString(), s2); } \
393 inline rettype WX_STR_DECL(name, const wxCStrData&, const wxScopedWCharBuffer&) \
394 { return WX_STR_CALL(forString, s1.AsString(), s2); } \
395 inline rettype WX_STR_DECL(name, const wxCStrData&, const wxString&) \
396 { return WX_STR_CALL(forString, s1.AsString(), s2); } \
397 inline rettype WX_STR_DECL(name, const wxCStrData&, const wxCStrData&) \
398 { return WX_STR_CALL(forString, s1.AsString(), s2); }
399
400 // This defines strcmp-like function, i.e. one returning the result of
401 // comparison; see WX_STR_FUNC_NO_INVERT for explanation of the arguments
402 #define WX_STRCMP_FUNC(name, crtA, crtW, forString) \
403 WX_STR_FUNC_NO_INVERT(int, name, crtA, crtW, forString) \
404 \
405 inline int WX_STR_DECL(name, const char *, const wxCStrData&) \
406 { return -WX_STR_CALL(forString, s2.AsString(), s1); } \
407 inline int WX_STR_DECL(name, const char *, const wxString&) \
408 { return -WX_STR_CALL(forString, s2, s1); } \
409 \
410 inline int WX_STR_DECL(name, const wchar_t *, const wxCStrData&) \
411 { return -WX_STR_CALL(forString, s2.AsString(), s1); } \
412 inline int WX_STR_DECL(name, const wchar_t *, const wxString&) \
413 { return -WX_STR_CALL(forString, s2, s1); } \
414 \
415 inline int WX_STR_DECL(name, const wxScopedCharBuffer&, const wxCStrData&) \
416 { return -WX_STR_CALL(forString, s2.AsString(), s1.data()); } \
417 inline int WX_STR_DECL(name, const wxScopedCharBuffer&, const wxString&) \
418 { return -WX_STR_CALL(forString, s2, s1.data()); } \
419 \
420 inline int WX_STR_DECL(name, const wxScopedWCharBuffer&, const wxCStrData&) \
421 { return -WX_STR_CALL(forString, s2.AsString(), s1.data()); } \
422 inline int WX_STR_DECL(name, const wxScopedWCharBuffer&, const wxString&) \
423 { return -WX_STR_CALL(forString, s2, s1.data()); }
424
425
426 // This defines a string function that is *not* strcmp-like, i.e. doesn't
427 // return the result of comparison and so if the second argument is a string,
428 // it has to be converted to char* or wchar_t*
429 #define WX_STR_FUNC(rettype, name, crtA, crtW, forString) \
430 WX_STR_FUNC_NO_INVERT(rettype, name, crtA, crtW, forString) \
431 \
432 inline rettype WX_STR_DECL(name, const char *, const wxCStrData&) \
433 { return WX_STR_CALL(crtA, s1, s2.AsCharBuf()); } \
434 inline rettype WX_STR_DECL(name, const char *, const wxString&) \
435 { return WX_STR_CALL(crtA, s1, s2.mb_str()); } \
436 \
437 inline rettype WX_STR_DECL(name, const wchar_t *, const wxCStrData&) \
438 { return WX_STR_CALL(crtW, s1, s2.AsWCharBuf()); } \
439 inline rettype WX_STR_DECL(name, const wchar_t *, const wxString&) \
440 { return WX_STR_CALL(crtW, s1, s2.wc_str()); } \
441 \
442 inline rettype WX_STR_DECL(name, const wxScopedCharBuffer&, const wxCStrData&) \
443 { return WX_STR_CALL(crtA, s1.data(), s2.AsCharBuf()); } \
444 inline rettype WX_STR_DECL(name, const wxScopedCharBuffer&, const wxString&) \
445 { return WX_STR_CALL(crtA, s1.data(), s2.mb_str()); } \
446 \
447 inline rettype WX_STR_DECL(name, const wxScopedWCharBuffer&, const wxCStrData&) \
448 { return WX_STR_CALL(crtW, s1.data(), s2.AsWCharBuf()); } \
449 inline rettype WX_STR_DECL(name, const wxScopedWCharBuffer&, const wxString&) \
450 { return WX_STR_CALL(crtW, s1.data(), s2.wc_str()); }
451
452 template<typename T>
453 inline int wxStrcmp_String(const wxString& s1, const T& s2)
454 { return s1.compare(s2); }
455 WX_STRCMP_FUNC(wxStrcmp, wxCRT_StrcmpA, wxCRT_StrcmpW, wxStrcmp_String)
456
457 template<typename T>
458 inline int wxStricmp_String(const wxString& s1, const T& s2)
459 { return s1.CmpNoCase(s2); }
460 WX_STRCMP_FUNC(wxStricmp, wxCRT_StricmpA, wxCRT_StricmpW, wxStricmp_String)
461
462 #if defined(wxCRT_StrcollA) && defined(wxCRT_StrcollW)
463
464 // GCC 3.4 and other compilers have a bug that causes it to fail compilation if
465 // the template's implementation uses overloaded function declared later (see
466 // the wxStrcoll() call in wxStrcoll_String<T>()), so we have to
467 // forward-declare the template and implement it below WX_STRCMP_FUNC. OTOH,
468 // this fails to compile with VC6, so don't do it for VC. It also causes
469 // problems with GCC visibility in newer GCC versions.
470 #if !(defined(__VISUALC__) || (wxCHECK_GCC_VERSION(3,5) && !wxCHECK_GCC_VERSION(4,7))) || defined(__clang__)
471 #define wxNEEDS_DECL_BEFORE_TEMPLATE
472 #endif
473
474 #ifdef wxNEEDS_DECL_BEFORE_TEMPLATE
475 template<typename T>
476 inline int wxStrcoll_String(const wxString& s1, const T& s2);
477 WX_STRCMP_FUNC(wxStrcoll, wxCRT_StrcollA, wxCRT_StrcollW, wxStrcoll_String)
478 #endif // wxNEEDS_DECL_BEFORE_TEMPLATE
479
480 template<typename T>
481 inline int wxStrcoll_String(const wxString& s1, const T& s2)
482 {
483 #if wxUSE_UNICODE
484 // NB: strcoll() doesn't work correctly on UTF-8 strings, so we have to use
485 // wc_str() even if wxUSE_UNICODE_UTF8; the (const wchar_t*) cast is
486 // there just as optimization to avoid going through
487 // wxStrcoll<wxScopedWCharBuffer>:
488 return wxStrcoll((const wchar_t*)s1.wc_str(), s2);
489 #else
490 return wxStrcoll((const char*)s1.mb_str(), s2);
491 #endif
492 }
493
494 #ifndef wxNEEDS_DECL_BEFORE_TEMPLATE
495 // this is exactly the same WX_STRCMP_FUNC line as above, insde the
496 // wxNEEDS_DECL_BEFORE_TEMPLATE case
497 WX_STRCMP_FUNC(wxStrcoll, wxCRT_StrcollA, wxCRT_StrcollW, wxStrcoll_String)
498 #endif
499
500 #endif // defined(wxCRT_Strcoll[AW])
501
502 template<typename T>
503 inline size_t wxStrspn_String(const wxString& s1, const T& s2)
504 {
505 size_t pos = s1.find_first_not_of(s2);
506 return pos == wxString::npos ? s1.length() : pos;
507 }
508 WX_STR_FUNC(size_t, wxStrspn, wxCRT_StrspnA, wxCRT_StrspnW, wxStrspn_String)
509
510 template<typename T>
511 inline size_t wxStrcspn_String(const wxString& s1, const T& s2)
512 {
513 size_t pos = s1.find_first_of(s2);
514 return pos == wxString::npos ? s1.length() : pos;
515 }
516 WX_STR_FUNC(size_t, wxStrcspn, wxCRT_StrcspnA, wxCRT_StrcspnW, wxStrcspn_String)
517
518 #undef WX_STR_DECL
519 #undef WX_STR_CALL
520 #define WX_STR_DECL(name, T1, T2) name(T1 s1, T2 s2, size_t n)
521 #define WX_STR_CALL(func, a1, a2) func(a1, a2, n)
522
523 template<typename T>
524 inline int wxStrncmp_String(const wxString& s1, const T& s2, size_t n)
525 { return s1.compare(0, n, s2, 0, n); }
526 WX_STRCMP_FUNC(wxStrncmp, wxCRT_StrncmpA, wxCRT_StrncmpW, wxStrncmp_String)
527
528 template<typename T>
529 inline int wxStrnicmp_String(const wxString& s1, const T& s2, size_t n)
530 { return s1.substr(0, n).CmpNoCase(wxString(s2).substr(0, n)); }
531 WX_STRCMP_FUNC(wxStrnicmp, wxCRT_StrnicmpA, wxCRT_StrnicmpW, wxStrnicmp_String)
532
533 #undef WX_STR_DECL
534 #undef WX_STR_CALL
535 #undef WX_STRCMP_FUNC
536 #undef WX_STR_FUNC
537 #undef WX_STR_FUNC_NO_INVERT
538
539 #if defined(wxCRT_StrxfrmA) && defined(wxCRT_StrxfrmW)
540
541 inline size_t wxStrxfrm(char *dest, const char *src, size_t n)
542 { return wxCRT_StrxfrmA(dest, src, n); }
543 inline size_t wxStrxfrm(wchar_t *dest, const wchar_t *src, size_t n)
544 { return wxCRT_StrxfrmW(dest, src, n); }
545 template<typename T>
546 inline size_t wxStrxfrm(T *dest, const wxScopedCharTypeBuffer<T>& src, size_t n)
547 { return wxStrxfrm(dest, src.data(), n); }
548 inline size_t wxStrxfrm(char *dest, const wxString& src, size_t n)
549 { return wxCRT_StrxfrmA(dest, src.mb_str(), n); }
550 inline size_t wxStrxfrm(wchar_t *dest, const wxString& src, size_t n)
551 { return wxCRT_StrxfrmW(dest, src.wc_str(), n); }
552 inline size_t wxStrxfrm(char *dest, const wxCStrData& src, size_t n)
553 { return wxCRT_StrxfrmA(dest, src.AsCharBuf(), n); }
554 inline size_t wxStrxfrm(wchar_t *dest, const wxCStrData& src, size_t n)
555 { return wxCRT_StrxfrmW(dest, src.AsWCharBuf(), n); }
556
557 #endif // defined(wxCRT_Strxfrm[AW])
558
559 inline char *wxStrtok(char *str, const char *delim, char **saveptr)
560 { return wxCRT_StrtokA(str, delim, saveptr); }
561 inline wchar_t *wxStrtok(wchar_t *str, const wchar_t *delim, wchar_t **saveptr)
562 { return wxCRT_StrtokW(str, delim, saveptr); }
563 template<typename T>
564 inline T *wxStrtok(T *str, const wxScopedCharTypeBuffer<T>& delim, T **saveptr)
565 { return wxStrtok(str, delim.data(), saveptr); }
566 inline char *wxStrtok(char *str, const wxCStrData& delim, char **saveptr)
567 { return wxCRT_StrtokA(str, delim.AsCharBuf(), saveptr); }
568 inline wchar_t *wxStrtok(wchar_t *str, const wxCStrData& delim, wchar_t **saveptr)
569 { return wxCRT_StrtokW(str, delim.AsWCharBuf(), saveptr); }
570 inline char *wxStrtok(char *str, const wxString& delim, char **saveptr)
571 { return wxCRT_StrtokA(str, delim.mb_str(), saveptr); }
572 inline wchar_t *wxStrtok(wchar_t *str, const wxString& delim, wchar_t **saveptr)
573 { return wxCRT_StrtokW(str, delim.wc_str(), saveptr); }
574
575 inline const char *wxStrstr(const char *haystack, const char *needle)
576 { return wxCRT_StrstrA(haystack, needle); }
577 inline const wchar_t *wxStrstr(const wchar_t *haystack, const wchar_t *needle)
578 { return wxCRT_StrstrW(haystack, needle); }
579 inline const char *wxStrstr(const char *haystack, const wxString& needle)
580 { return wxCRT_StrstrA(haystack, needle.mb_str()); }
581 inline const wchar_t *wxStrstr(const wchar_t *haystack, const wxString& needle)
582 { return wxCRT_StrstrW(haystack, needle.wc_str()); }
583 // these functions return char* pointer into the non-temporary conversion buffer
584 // used by c_str()'s implicit conversion to char*, for ANSI build compatibility
585 inline const char *wxStrstr(const wxString& haystack, const wxString& needle)
586 { return wxCRT_StrstrA(haystack.c_str(), needle.mb_str()); }
587 inline const char *wxStrstr(const wxCStrData& haystack, const wxString& needle)
588 { return wxCRT_StrstrA(haystack, needle.mb_str()); }
589 inline const char *wxStrstr(const wxCStrData& haystack, const wxCStrData& needle)
590 { return wxCRT_StrstrA(haystack, needle.AsCharBuf()); }
591 // if 'needle' is char/wchar_t, then the same is probably wanted as return value
592 inline const char *wxStrstr(const wxString& haystack, const char *needle)
593 { return wxCRT_StrstrA(haystack.c_str(), needle); }
594 inline const char *wxStrstr(const wxCStrData& haystack, const char *needle)
595 { return wxCRT_StrstrA(haystack, needle); }
596 inline const wchar_t *wxStrstr(const wxString& haystack, const wchar_t *needle)
597 { return wxCRT_StrstrW(haystack.c_str(), needle); }
598 inline const wchar_t *wxStrstr(const wxCStrData& haystack, const wchar_t *needle)
599 { return wxCRT_StrstrW(haystack, needle); }
600
601 inline const char *wxStrchr(const char *s, char c)
602 { return wxCRT_StrchrA(s, c); }
603 inline const wchar_t *wxStrchr(const wchar_t *s, wchar_t c)
604 { return wxCRT_StrchrW(s, c); }
605 inline const char *wxStrrchr(const char *s, char c)
606 { return wxCRT_StrrchrA(s, c); }
607 inline const wchar_t *wxStrrchr(const wchar_t *s, wchar_t c)
608 { return wxCRT_StrrchrW(s, c); }
609 inline const char *wxStrchr(const char *s, const wxUniChar& uc)
610 { char c; return uc.GetAsChar(&c) ? wxCRT_StrchrA(s, c) : NULL; }
611 inline const wchar_t *wxStrchr(const wchar_t *s, const wxUniChar& c)
612 { return wxCRT_StrchrW(s, (wchar_t)c); }
613 inline const char *wxStrrchr(const char *s, const wxUniChar& uc)
614 { char c; return uc.GetAsChar(&c) ? wxCRT_StrrchrA(s, c) : NULL; }
615 inline const wchar_t *wxStrrchr(const wchar_t *s, const wxUniChar& c)
616 { return wxCRT_StrrchrW(s, (wchar_t)c); }
617 inline const char *wxStrchr(const char *s, const wxUniCharRef& uc)
618 { char c; return uc.GetAsChar(&c) ? wxCRT_StrchrA(s, c) : NULL; }
619 inline const wchar_t *wxStrchr(const wchar_t *s, const wxUniCharRef& c)
620 { return wxCRT_StrchrW(s, (wchar_t)c); }
621 inline const char *wxStrrchr(const char *s, const wxUniCharRef& uc)
622 { char c; return uc.GetAsChar(&c) ? wxCRT_StrrchrA(s, c) : NULL; }
623 inline const wchar_t *wxStrrchr(const wchar_t *s, const wxUniCharRef& c)
624 { return wxCRT_StrrchrW(s, (wchar_t)c); }
625 template<typename T>
626 inline const T* wxStrchr(const wxScopedCharTypeBuffer<T>& s, T c)
627 { return wxStrchr(s.data(), c); }
628 template<typename T>
629 inline const T* wxStrrchr(const wxScopedCharTypeBuffer<T>& s, T c)
630 { return wxStrrchr(s.data(), c); }
631 template<typename T>
632 inline const T* wxStrchr(const wxScopedCharTypeBuffer<T>& s, const wxUniChar& c)
633 { return wxStrchr(s.data(), (T)c); }
634 template<typename T>
635 inline const T* wxStrrchr(const wxScopedCharTypeBuffer<T>& s, const wxUniChar& c)
636 { return wxStrrchr(s.data(), (T)c); }
637 template<typename T>
638 inline const T* wxStrchr(const wxScopedCharTypeBuffer<T>& s, const wxUniCharRef& c)
639 { return wxStrchr(s.data(), (T)c); }
640 template<typename T>
641 inline const T* wxStrrchr(const wxScopedCharTypeBuffer<T>& s, const wxUniCharRef& c)
642 { return wxStrrchr(s.data(), (T)c); }
643 // these functions return char* pointer into the non-temporary conversion buffer
644 // used by c_str()'s implicit conversion to char*, for ANSI build compatibility
645 inline const char* wxStrchr(const wxString& s, char c)
646 { return wxCRT_StrchrA((const char*)s.c_str(), c); }
647 inline const char* wxStrrchr(const wxString& s, char c)
648 { return wxCRT_StrrchrA((const char*)s.c_str(), c); }
649 inline const char* wxStrchr(const wxString& s, int c)
650 { return wxCRT_StrchrA((const char*)s.c_str(), c); }
651 inline const char* wxStrrchr(const wxString& s, int c)
652 { return wxCRT_StrrchrA((const char*)s.c_str(), c); }
653 inline const char* wxStrchr(const wxString& s, const wxUniChar& uc)
654 { char c; return uc.GetAsChar(&c) ? wxCRT_StrchrA(s.c_str(), c) : NULL; }
655 inline const char* wxStrrchr(const wxString& s, const wxUniChar& uc)
656 { char c; return uc.GetAsChar(&c) ? wxCRT_StrrchrA(s.c_str(), c) : NULL; }
657 inline const char* wxStrchr(const wxString& s, const wxUniCharRef& uc)
658 { char c; return uc.GetAsChar(&c) ? wxCRT_StrchrA(s.c_str(), c) : NULL; }
659 inline const char* wxStrrchr(const wxString& s, const wxUniCharRef& uc)
660 { char c; return uc.GetAsChar(&c) ? wxCRT_StrrchrA(s.c_str(), c) : NULL; }
661 inline const wchar_t* wxStrchr(const wxString& s, wchar_t c)
662 { return wxCRT_StrchrW((const wchar_t*)s.c_str(), c); }
663 inline const wchar_t* wxStrrchr(const wxString& s, wchar_t c)
664 { return wxCRT_StrrchrW((const wchar_t*)s.c_str(), c); }
665 inline const char* wxStrchr(const wxCStrData& s, char c)
666 { return wxCRT_StrchrA(s.AsChar(), c); }
667 inline const char* wxStrrchr(const wxCStrData& s, char c)
668 { return wxCRT_StrrchrA(s.AsChar(), c); }
669 inline const char* wxStrchr(const wxCStrData& s, int c)
670 { return wxCRT_StrchrA(s.AsChar(), c); }
671 inline const char* wxStrrchr(const wxCStrData& s, int c)
672 { return wxCRT_StrrchrA(s.AsChar(), c); }
673 inline const char* wxStrchr(const wxCStrData& s, const wxUniChar& uc)
674 { char c; return uc.GetAsChar(&c) ? wxCRT_StrchrA(s, c) : NULL; }
675 inline const char* wxStrrchr(const wxCStrData& s, const wxUniChar& uc)
676 { char c; return uc.GetAsChar(&c) ? wxCRT_StrrchrA(s, c) : NULL; }
677 inline const char* wxStrchr(const wxCStrData& s, const wxUniCharRef& uc)
678 { char c; return uc.GetAsChar(&c) ? wxCRT_StrchrA(s, c) : NULL; }
679 inline const char* wxStrrchr(const wxCStrData& s, const wxUniCharRef& uc)
680 { char c; return uc.GetAsChar(&c) ? wxCRT_StrrchrA(s, c) : NULL; }
681 inline const wchar_t* wxStrchr(const wxCStrData& s, wchar_t c)
682 { return wxCRT_StrchrW(s.AsWChar(), c); }
683 inline const wchar_t* wxStrrchr(const wxCStrData& s, wchar_t c)
684 { return wxCRT_StrrchrW(s.AsWChar(), c); }
685
686 inline const char *wxStrpbrk(const char *s, const char *accept)
687 { return wxCRT_StrpbrkA(s, accept); }
688 inline const wchar_t *wxStrpbrk(const wchar_t *s, const wchar_t *accept)
689 { return wxCRT_StrpbrkW(s, accept); }
690 inline const char *wxStrpbrk(const char *s, const wxString& accept)
691 { return wxCRT_StrpbrkA(s, accept.mb_str()); }
692 inline const char *wxStrpbrk(const char *s, const wxCStrData& accept)
693 { return wxCRT_StrpbrkA(s, accept.AsCharBuf()); }
694 inline const wchar_t *wxStrpbrk(const wchar_t *s, const wxString& accept)
695 { return wxCRT_StrpbrkW(s, accept.wc_str()); }
696 inline const wchar_t *wxStrpbrk(const wchar_t *s, const wxCStrData& accept)
697 { return wxCRT_StrpbrkW(s, accept.AsWCharBuf()); }
698 inline const char *wxStrpbrk(const wxString& s, const wxString& accept)
699 { return wxCRT_StrpbrkA(s.c_str(), accept.mb_str()); }
700 inline const char *wxStrpbrk(const wxString& s, const char *accept)
701 { return wxCRT_StrpbrkA(s.c_str(), accept); }
702 inline const wchar_t *wxStrpbrk(const wxString& s, const wchar_t *accept)
703 { return wxCRT_StrpbrkW(s.wc_str(), accept); }
704 inline const char *wxStrpbrk(const wxString& s, const wxCStrData& accept)
705 { return wxCRT_StrpbrkA(s.c_str(), accept.AsCharBuf()); }
706 inline const char *wxStrpbrk(const wxCStrData& s, const wxString& accept)
707 { return wxCRT_StrpbrkA(s.AsChar(), accept.mb_str()); }
708 inline const char *wxStrpbrk(const wxCStrData& s, const char *accept)
709 { return wxCRT_StrpbrkA(s.AsChar(), accept); }
710 inline const wchar_t *wxStrpbrk(const wxCStrData& s, const wchar_t *accept)
711 { return wxCRT_StrpbrkW(s.AsWChar(), accept); }
712 inline const char *wxStrpbrk(const wxCStrData& s, const wxCStrData& accept)
713 { return wxCRT_StrpbrkA(s.AsChar(), accept.AsCharBuf()); }
714 template <typename S, typename T>
715 inline const T *wxStrpbrk(const S& s, const wxScopedCharTypeBuffer<T>& accept)
716 { return wxStrpbrk(s, accept.data()); }
717
718
719 /* inlined non-const versions */
720 template <typename T>
721 inline char *wxStrstr(char *haystack, T needle)
722 { return const_cast<char*>(wxStrstr(const_cast<const char*>(haystack), needle)); }
723 template <typename T>
724 inline wchar_t *wxStrstr(wchar_t *haystack, T needle)
725 { return const_cast<wchar_t*>(wxStrstr(const_cast<const wchar_t*>(haystack), needle)); }
726
727 template <typename T>
728 inline char * wxStrchr(char *s, T c)
729 { return const_cast<char*>(wxStrchr(const_cast<const char*>(s), c)); }
730 template <typename T>
731 inline wchar_t * wxStrchr(wchar_t *s, T c)
732 { return (wchar_t *)wxStrchr((const wchar_t *)s, c); }
733 template <typename T>
734 inline char * wxStrrchr(char *s, T c)
735 { return const_cast<char*>(wxStrrchr(const_cast<const char*>(s), c)); }
736 template <typename T>
737 inline wchar_t * wxStrrchr(wchar_t *s, T c)
738 { return const_cast<wchar_t*>(wxStrrchr(const_cast<const wchar_t*>(s), c)); }
739
740 template <typename T>
741 inline char * wxStrpbrk(char *s, T accept)
742 { return const_cast<char*>(wxStrpbrk(const_cast<const char*>(s), accept)); }
743 template <typename T>
744 inline wchar_t * wxStrpbrk(wchar_t *s, T accept)
745 { return const_cast<wchar_t*>(wxStrpbrk(const_cast<const wchar_t*>(s), accept)); }
746
747
748 // ----------------------------------------------------------------------------
749 // stdio.h functions
750 // ----------------------------------------------------------------------------
751
752 // NB: using fn_str() for mode is a hack to get the same type (char*/wchar_t*)
753 // as needed, the conversion itself doesn't matter, it's ASCII
754 inline FILE *wxFopen(const wxString& path, const wxString& mode)
755 { return wxCRT_Fopen(path.fn_str(), mode.fn_str()); }
756 inline FILE *wxFreopen(const wxString& path, const wxString& mode, FILE *stream)
757 { return wxCRT_Freopen(path.fn_str(), mode.fn_str(), stream); }
758 inline int wxRemove(const wxString& path)
759 { return wxCRT_Remove(path.fn_str()); }
760 inline int wxRename(const wxString& oldpath, const wxString& newpath)
761 { return wxCRT_Rename(oldpath.fn_str(), newpath.fn_str()); }
762
763 extern WXDLLIMPEXP_BASE int wxPuts(const wxString& s);
764 extern WXDLLIMPEXP_BASE int wxFputs(const wxString& s, FILE *stream);
765 extern WXDLLIMPEXP_BASE void wxPerror(const wxString& s);
766
767 extern WXDLLIMPEXP_BASE int wxFputc(const wxUniChar& c, FILE *stream);
768
769 #define wxPutc(c, stream) wxFputc(c, stream)
770 #define wxPutchar(c) wxFputc(c, stdout)
771 #define wxFputchar(c) wxPutchar(c)
772
773 // NB: We only provide ANSI version of fgets() because fgetws() interprets the
774 // stream according to current locale, which is rarely what is desired.
775 inline char *wxFgets(char *s, int size, FILE *stream)
776 { return wxCRT_FgetsA(s, size, stream); }
777 // This version calls ANSI version and converts the string using wxConvLibc
778 extern WXDLLIMPEXP_BASE wchar_t *wxFgets(wchar_t *s, int size, FILE *stream);
779
780 #define wxGets(s) wxGets_is_insecure_and_dangerous_use_wxFgets_instead
781
782 // NB: We only provide ANSI versions of this for the same reasons as in the
783 // case of wxFgets() above
784 inline int wxFgetc(FILE *stream) { return wxCRT_FgetcA(stream); }
785 inline int wxUngetc(int c, FILE *stream) { return wxCRT_UngetcA(c, stream); }
786
787 #define wxGetc(stream) wxFgetc(stream)
788 #define wxGetchar() wxFgetc(stdin)
789 #define wxFgetchar() wxGetchar()
790
791 // ----------------------------------------------------------------------------
792 // stdlib.h functions
793 // ----------------------------------------------------------------------------
794
795 #ifdef wxCRT_AtoiW
796 inline int wxAtoi(const wxString& str) { return wxCRT_AtoiW(str.wc_str()); }
797 #else
798 inline int wxAtoi(const wxString& str) { return wxCRT_AtoiA(str.mb_str()); }
799 #endif
800
801 #ifdef wxCRT_AtolW
802 inline long wxAtol(const wxString& str) { return wxCRT_AtolW(str.wc_str()); }
803 #else
804 inline long wxAtol(const wxString& str) { return wxCRT_AtolA(str.mb_str()); }
805 #endif
806
807 #ifdef wxCRT_AtofW
808 inline double wxAtof(const wxString& str) { return wxCRT_AtofW(str.wc_str()); }
809 #else
810 inline double wxAtof(const wxString& str) { return wxCRT_AtofA(str.mb_str()); }
811 #endif
812
813 inline double wxStrtod(const char *nptr, char **endptr)
814 { return wxCRT_StrtodA(nptr, endptr); }
815 inline double wxStrtod(const wchar_t *nptr, wchar_t **endptr)
816 { return wxCRT_StrtodW(nptr, endptr); }
817 template<typename T>
818 inline double wxStrtod(const wxScopedCharTypeBuffer<T>& nptr, T **endptr)
819 { return wxStrtod(nptr.data(), endptr); }
820
821 // We implement wxStrto*() like this so that the code compiles when NULL is
822 // passed in - - if we had just char** and wchar_t** overloads for 'endptr', it
823 // would be ambiguous. The solution is to use a template so that endptr can be
824 // any type: when NULL constant is used, the type will be int and we can handle
825 // that case specially. Otherwise, we infer the type that 'nptr' should be
826 // converted to from the type of 'endptr'. We need wxStrtoxCharType<T> template
827 // to make the code compile even for T=int (that's the case when it's not going
828 // to be ever used, but it still has to compile).
829 template<typename T> struct wxStrtoxCharType {};
830 template<> struct wxStrtoxCharType<char**>
831 {
832 typedef const char* Type;
833 static char** AsPointer(char **p) { return p; }
834 };
835 template<> struct wxStrtoxCharType<wchar_t**>
836 {
837 typedef const wchar_t* Type;
838 static wchar_t** AsPointer(wchar_t **p) { return p; }
839 };
840 template<> struct wxStrtoxCharType<int>
841 {
842 typedef const char* Type; /* this one is never used */
843 static char** AsPointer(int WXUNUSED_UNLESS_DEBUG(p))
844 {
845 wxASSERT_MSG( p == 0, "passing non-NULL int is invalid" );
846 return NULL;
847 }
848 };
849
850 template<typename T>
851 inline double wxStrtod(const wxString& nptr, T endptr)
852 {
853 if ( endptr == 0 )
854 {
855 // when we don't care about endptr, use the string representation that
856 // doesn't require any conversion (it doesn't matter for this function
857 // even if its UTF-8):
858 return wxStrtod(nptr.wx_str(), (wxStringCharType**)NULL);
859 }
860 else
861 {
862 // note that it is important to use c_str() here and not mb_str() or
863 // wc_str(), because we store the pointer into (possibly converted)
864 // buffer in endptr and so it must be valid even when wxStrtod() returns
865 typedef typename wxStrtoxCharType<T>::Type CharType;
866 return wxStrtod((CharType)nptr.c_str(),
867 wxStrtoxCharType<T>::AsPointer(endptr));
868 }
869 }
870 template<typename T>
871 inline double wxStrtod(const wxCStrData& nptr, T endptr)
872 { return wxStrtod(nptr.AsString(), endptr); }
873
874
875 #define WX_STRTOX_FUNC(rettype, name, implA, implW) \
876 /* see wxStrtod() above for explanation of this code: */ \
877 inline rettype name(const char *nptr, char **endptr, int base) \
878 { return implA(nptr, endptr, base); } \
879 inline rettype name(const wchar_t *nptr, wchar_t **endptr, int base) \
880 { return implW(nptr, endptr, base); } \
881 template<typename T> \
882 inline rettype name(const wxScopedCharTypeBuffer<T>& nptr, T **endptr, int)\
883 { return name(nptr.data(), endptr); } \
884 template<typename T> \
885 inline rettype name(const wxString& nptr, T endptr, int base) \
886 { \
887 if ( endptr == 0 ) \
888 return name(nptr.wx_str(), (wxStringCharType**)NULL, base); \
889 else \
890 { \
891 typedef typename wxStrtoxCharType<T>::Type CharType; \
892 return name((CharType)nptr.c_str(), \
893 wxStrtoxCharType<T>::AsPointer(endptr), \
894 base); \
895 } \
896 } \
897 template<typename T> \
898 inline rettype name(const wxCStrData& nptr, T endptr, int base) \
899 { return name(nptr.AsString(), endptr, base); }
900
901 WX_STRTOX_FUNC(long, wxStrtol, wxCRT_StrtolA, wxCRT_StrtolW)
902 WX_STRTOX_FUNC(unsigned long, wxStrtoul, wxCRT_StrtoulA, wxCRT_StrtoulW)
903 #ifdef wxLongLong_t
904 WX_STRTOX_FUNC(wxLongLong_t, wxStrtoll, wxCRT_StrtollA, wxCRT_StrtollW)
905 WX_STRTOX_FUNC(wxULongLong_t, wxStrtoull, wxCRT_StrtoullA, wxCRT_StrtoullW)
906 #endif // wxLongLong_t
907
908 #undef WX_STRTOX_FUNC
909
910
911 // there is no command interpreter under CE, hence no system()
912 #ifndef __WXWINCE__
913
914 // mingw32 doesn't provide _tsystem() even though it provides other stdlib.h
915 // functions in their wide versions
916 #ifdef wxCRT_SystemW
917 inline int wxSystem(const wxString& str) { return wxCRT_SystemW(str.wc_str()); }
918 #else
919 inline int wxSystem(const wxString& str) { return wxCRT_SystemA(str.mb_str()); }
920 #endif
921
922 #endif // !__WXWINCE__/__WXWINCE__
923
924 inline char* wxGetenv(const char *name) { return wxCRT_GetenvA(name); }
925 inline wchar_t* wxGetenv(const wchar_t *name) { return wxCRT_GetenvW(name); }
926 inline char* wxGetenv(const wxString& name) { return wxCRT_GetenvA(name.mb_str()); }
927 inline char* wxGetenv(const wxCStrData& name) { return wxCRT_GetenvA(name.AsCharBuf()); }
928 inline char* wxGetenv(const wxScopedCharBuffer& name) { return wxCRT_GetenvA(name.data()); }
929 inline wchar_t* wxGetenv(const wxScopedWCharBuffer& name) { return wxCRT_GetenvW(name.data()); }
930
931 // ----------------------------------------------------------------------------
932 // time.h functions
933 // ----------------------------------------------------------------------------
934
935 inline size_t wxStrftime(char *s, size_t max,
936 const wxString& format, const struct tm *tm)
937 { return wxCRT_StrftimeA(s, max, format.mb_str(), tm); }
938
939 inline size_t wxStrftime(wchar_t *s, size_t max,
940 const wxString& format, const struct tm *tm)
941 { return wxCRT_StrftimeW(s, max, format.wc_str(), tm); }
942
943 // NB: we can't provide both char* and wchar_t* versions for obvious reasons
944 // and returning wxString wouldn't work either (it would be immediately
945 // destroyed and if assigned to char*/wchar_t*, the pointer would be
946 // invalid), so we only keep ASCII version, because the returned value
947 // is always ASCII anyway
948 #define wxAsctime asctime
949 #define wxCtime ctime
950
951
952 // ----------------------------------------------------------------------------
953 // ctype.h functions
954 // ----------------------------------------------------------------------------
955
956 // FIXME-UTF8: we'd be better off implementing these ourselves, as the CRT
957 // version is locale-dependent
958 // FIXME-UTF8: these don't work when EOF is passed in because of wxUniChar,
959 // is this OK or not?
960
961 inline bool wxIsalnum(const wxUniChar& c) { return wxCRT_IsalnumW(c) != 0; }
962 inline bool wxIsalpha(const wxUniChar& c) { return wxCRT_IsalphaW(c) != 0; }
963 inline bool wxIscntrl(const wxUniChar& c) { return wxCRT_IscntrlW(c) != 0; }
964 inline bool wxIsdigit(const wxUniChar& c) { return wxCRT_IsdigitW(c) != 0; }
965 inline bool wxIsgraph(const wxUniChar& c) { return wxCRT_IsgraphW(c) != 0; }
966 inline bool wxIslower(const wxUniChar& c) { return wxCRT_IslowerW(c) != 0; }
967 inline bool wxIsprint(const wxUniChar& c) { return wxCRT_IsprintW(c) != 0; }
968 inline bool wxIspunct(const wxUniChar& c) { return wxCRT_IspunctW(c) != 0; }
969 inline bool wxIsspace(const wxUniChar& c) { return wxCRT_IsspaceW(c) != 0; }
970 inline bool wxIsupper(const wxUniChar& c) { return wxCRT_IsupperW(c) != 0; }
971 inline bool wxIsxdigit(const wxUniChar& c) { return wxCRT_IsxdigitW(c) != 0; }
972
973 inline wxUniChar wxTolower(const wxUniChar& c) { return wxCRT_TolowerW(c); }
974 inline wxUniChar wxToupper(const wxUniChar& c) { return wxCRT_ToupperW(c); }
975
976 #if WXWIN_COMPATIBILITY_2_8
977 // we had goofed and defined wxIsctrl() instead of (correct) wxIscntrl() in the
978 // initial versions of this header -- now it is too late to remove it so
979 // although we fixed the function/macro name above, still provide the
980 // backwards-compatible synonym.
981 wxDEPRECATED( inline int wxIsctrl(const wxUniChar& c) );
982 inline int wxIsctrl(const wxUniChar& c) { return wxIscntrl(c); }
983 #endif // WXWIN_COMPATIBILITY_2_8
984
985 inline bool wxIsascii(const wxUniChar& c) { return c.IsAscii(); }
986
987 #endif /* _WX_WXCRT_H_ */