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