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