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