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