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