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