3 * Purpose: Type-safe ANSI and Unicode builds compatible wrappers for
5 * Author: Joel Farley, Ove Kåven
6 * Modified by: Vadim Zeitlin, Robert Roebling, Ron Lee
9 * Copyright: (c) 1998-2006 wxWidgets dev team
10 * Licence: wxWindows licence
13 /* THIS IS A C FILE, DON'T USE C++ FEATURES (IN PARTICULAR COMMENTS) IN IT */
15 #ifndef _WX_WXCRTBASE_H_
16 #define _WX_WXCRTBASE_H_
18 /* -------------------------------------------------------------------------
19 headers and missing declarations
20 ------------------------------------------------------------------------- */
22 #include "wx/chartype.h"
25 Standard headers we need here.
27 NB: don't include any wxWidgets headers here because almost all of them
38 #if defined(HAVE_STRTOK_R) && defined(__DARWIN__) && defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS
39 char *strtok_r(char *, const char *, char **);
43 a few compilers don't have the (non standard but common) isascii function,
44 define it ourselves for them
47 #if defined(__MWERKS__)
48 #define wxNEED_ISASCII
49 #elif defined(_WIN32_WCE)
51 #define wxNEED_ISASCII
57 inline int isascii(int c
) { return (unsigned)c
< 0x80; }
62 #define isspace(c) ((c) == _T(' ') || (c) == _T('\t'))
64 #endif /* _WIN32_WCE */
66 /* string.h functions */
68 #if defined(__MWERKS__) && !defined(__MACH__) && (__MSL__ < 0x00008000)
70 #elif defined(__WXWINCE__)
78 WXDLLIMPEXP_BASE
char *strdup(const char* s
);
81 /* missing functions in some WinCE versions */
83 #if (_WIN32_WCE < 300)
84 WXDLLIMPEXP_BASE
void *calloc( size_t num
, size_t size
);
86 #endif /* _WIN32_WCE */
89 #if defined(__MWERKS__)
90 /* Metrowerks only has wide char support for OS X >= 10.3 */
91 #if !defined(__DARWIN__) || \
92 (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3)
93 #define wxHAVE_MWERKS_UNICODE
96 #ifdef wxHAVE_MWERKS_UNICODE
97 #define HAVE_WPRINTF 1
98 #define HAVE_WCSRTOMBS 1
99 #define HAVE_VSWPRINTF 1
101 #endif /* __MWERKS__ */
104 /* -------------------------------------------------------------------------
105 UTF-8 locale handling
106 ------------------------------------------------------------------------- */
109 #if wxUSE_UNICODE_UTF8
110 /* flag indicating whether the current locale uses UTF-8 or not; must be
111 updated every time the locale is changed! */
112 #if wxUSE_UTF8_LOCALE_ONLY
113 #define wxLocaleIsUtf8 true
115 extern WXDLLIMPEXP_BASE
bool wxLocaleIsUtf8
;
117 /* function used to update the flag: */
118 extern WXDLLIMPEXP_BASE
void wxUpdateLocaleIsUtf8();
119 #else /* !wxUSE_UNICODE_UTF8 */
120 inline void wxUpdateLocaleIsUtf8() {}
121 #endif /* wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8 */
122 #endif /* __cplusplus */
125 /* -------------------------------------------------------------------------
127 ------------------------------------------------------------------------- */
129 #define wxCRT_StrcatA strcat
130 #define wxCRT_StrchrA strchr
131 #define wxCRT_StrcmpA strcmp
132 #define wxCRT_StrcollA strcoll
133 #define wxCRT_StrcpyA strcpy
134 #define wxCRT_StrcspnA strcspn
135 #define wxCRT_StrlenA strlen
136 #define wxCRT_StrncatA strncat
137 #define wxCRT_StrncmpA strncmp
138 #define wxCRT_StrncpyA strncpy
139 #define wxCRT_StrpbrkA strpbrk
140 #define wxCRT_StrrchrA strrchr
141 #define wxCRT_StrspnA strspn
142 #define wxCRT_StrstrA strstr
143 #define wxCRT_StrxfrmA strxfrm
145 #define wxCRT_StrcatW wcscat
146 #define wxCRT_StrchrW wcschr
147 #define wxCRT_StrcmpW wcscmp
148 #define wxCRT_StrcollW wcscoll
149 #define wxCRT_StrcpyW wcscpy
150 #define wxCRT_StrcspnW wcscspn
151 #define wxCRT_StrncatW wcsncat
152 #define wxCRT_StrncmpW wcsncmp
153 #define wxCRT_StrncpyW wcsncpy
154 #define wxCRT_StrpbrkW wcspbrk
155 #define wxCRT_StrrchrW wcsrchr
156 #define wxCRT_StrspnW wcsspn
157 #define wxCRT_StrstrW wcsstr
158 #define wxCRT_StrxfrmW wcsxfrm
160 /* Almost all compiler have strdup(), but not quite all: CodeWarrior under
161 Mac and VC++ for Windows CE don't provide it; additionally, gcc under
162 Mac and OpenVMS do not have wcsdup: */
163 #if defined(__VISUALC__) && __VISUALC__ >= 1400
164 #define wxCRT_StrdupA _strdup
165 #elif !(defined(__MWERKS__) && defined(__WXMAC__)) && !defined(__WXWINCE__)
166 #define wxCRT_StrdupA strdup
168 #if defined(__WINDOWS__)
169 #define wxCRT_StrdupW _wcsdup
170 #elif !defined(__DARWIN__) && !defined( __VMS )
171 #define wxCRT_StrdupW wcsdup
174 #ifdef wxHAVE_TCHAR_SUPPORT
175 /* we surely have wchar_t if we have TCHAR have wcslen() */
179 #endif /* wxHAVE_TCHAR_SUPPORT */
182 #define wxCRT_StrlenW wcslen
185 #define wxCRT_StrtodA strtod
186 #define wxCRT_StrtolA strtol
187 #define wxCRT_StrtoulA strtoul
188 #define wxCRT_StrtodW wcstod
189 #define wxCRT_StrtolW wcstol
190 #define wxCRT_StrtoulW wcstoul
193 #if __VISUALC__ >= 1300 && !defined(__WXWINCE__)
194 #define wxCRT_StrtollA _strtoi64
195 #define wxCRT_StrtoullA _strtoui64
196 #define wxCRT_StrtollW _wcstoi64
197 #define wxCRT_StrtoullW _wcstoui64
201 #define wxCRT_StrtollA strtoll
202 #define wxCRT_StrtoullA strtoull
203 #endif /* HAVE_STRTOULL */
205 /* assume that we have wcstoull(), which is also C99, too */
206 #define wxCRT_StrtollW wcstoll
207 #define wxCRT_StrtoullW wcstoull
208 #endif /* HAVE_WCSTOULL */
212 /* define wxCRT_StricmpA/W and wxCRT_StrnicmpA/W for various compilers */
214 /* note that we definitely are going to need our own version for widechar
216 #if !defined(wxCRT_StricmpA)
217 #if defined(__BORLANDC__) || defined(__WATCOMC__) || \
218 defined(__SALFORDC__) || defined(__VISAGECPP__) || \
219 defined(__EMX__) || defined(__DJGPP__)
220 #define wxCRT_StricmpA stricmp
221 #define wxCRT_StrnicmpA strnicmp
222 #elif defined(__WXPALMOS__)
223 /* FIXME: There is no equivalent to strnicmp in the Palm OS API. This
224 * quick hack should do until one can be written.
226 #define wxCRT_StricmpA StrCaselessCompare
227 #define wxCRT_StrnicmpA strnicmp
228 #elif defined(__SYMANTEC__) || defined(__VISUALC__) || \
229 (defined(__MWERKS__) && defined(__INTEL__))
230 #define wxCRT_StricmpA _stricmp
231 #define wxCRT_StrnicmpA _strnicmp
232 #elif defined(__UNIX__) || defined(__GNUWIN32__)
233 #define wxCRT_StricmpA strcasecmp
234 #define wxCRT_StrnicmpA strncasecmp
235 /* #else -- use wxWidgets implementation */
237 #endif /* !defined(wxCRT_StricmpA) */
238 /* FIXME-UTF8: use wcs(n)casecmp if available for *W versions */
241 #define wxCRT_StrtokA(str, sep, last) strtok_r(str, sep, last)
243 /* FIXME-UTF8: detect and use wcstok() if available for wxCRT_StrtokW */
245 /* these are extern "C" because they are used by regex lib: */
250 #ifndef wxCRT_StrlenW
251 WXDLLIMPEXP_BASE
size_t wxCRT_StrlenW(const wchar_t *s
);
254 #ifndef wxCRT_StrncmpW
255 WXDLLIMPEXP_BASE
int wxCRT_StrncmpW(const wchar_t *s1
, const wchar_t *s2
, size_t n
);
262 /* FIXME-UTF8: remove this once we are Unicode only */
264 #define wxCRT_StrlenNative wxCRT_StrlenW
265 #define wxCRT_StrncmpNative wxCRT_StrncmpW
266 #define wxCRT_ToupperNative wxCRT_ToupperW
267 #define wxCRT_TolowerNative wxCRT_TolowerW
269 #define wxCRT_StrlenNative wxCRT_StrlenA
270 #define wxCRT_StrncmpNative wxCRT_StrncmpA
271 #define wxCRT_ToupperNative toupper
272 #define wxCRT_TolowerNative tolower
275 #ifndef wxCRT_StrcatW
276 WXDLLIMPEXP_BASE
wchar_t *wxCRT_StrcatW(wchar_t *dest
, const wchar_t *src
);
279 #ifndef wxCRT_StrchrW
280 WXDLLIMPEXP_BASE
const wchar_t *wxCRT_StrchrW(const wchar_t *s
, wchar_t c
);
283 #ifndef wxCRT_StrcmpW
284 WXDLLIMPEXP_BASE
int wxCRT_StrcmpW(const wchar_t *s1
, const wchar_t *s2
);
287 #ifndef wxCRT_StrcollW
288 WXDLLIMPEXP_BASE
int wxCRT_StrcollW(const wchar_t *s1
, const wchar_t *s2
);
291 #ifndef wxCRT_StrcpyW
292 WXDLLIMPEXP_BASE
wchar_t *wxCRT_StrcpyW(wchar_t *dest
, const wchar_t *src
);
295 #ifndef wxCRT_StrcspnW
296 WXDLLIMPEXP_BASE
size_t wxCRT_StrcspnW(const wchar_t *s
, const wchar_t *reject
);
299 #ifndef wxCRT_StrncatW
300 WXDLLIMPEXP_BASE
wchar_t *wxCRT_StrncatW(wchar_t *dest
, const wchar_t *src
, size_t n
);
303 #ifndef wxCRT_StrncpyW
304 WXDLLIMPEXP_BASE
wchar_t *wxCRT_StrncpyW(wchar_t *dest
, const wchar_t *src
, size_t n
);
307 #ifndef wxCRT_StrpbrkW
308 WXDLLIMPEXP_BASE
const wchar_t *wxCRT_StrpbrkW(const wchar_t *s
, const wchar_t *accept
);
311 #ifndef wxCRT_StrrchrW
312 WXDLLIMPEXP_BASE
const wchar_t *wxCRT_StrrchrW(const wchar_t *s
, wchar_t c
);
315 #ifndef wxCRT_StrspnW
316 WXDLLIMPEXP_BASE
size_t wxCRT_StrspnW(const wchar_t *s
, const wchar_t *accept
);
319 #ifndef wxCRT_StrstrW
320 WXDLLIMPEXP_BASE
const wchar_t *wxCRT_StrstrW(const wchar_t *haystack
, const wchar_t *needle
);
323 #ifndef wxCRT_StrtodW
324 WXDLLIMPEXP_BASE
double wxCRT_StrtodW(const wchar_t *nptr
, wchar_t **endptr
);
327 #ifndef wxCRT_StrtolW
328 WXDLLIMPEXP_BASE
long int wxCRT_StrtolW(const wchar_t *nptr
, wchar_t **endptr
, int base
);
331 #ifndef wxCRT_StrtoulW
332 WXDLLIMPEXP_BASE
unsigned long int wxCRT_StrtoulW(const wchar_t *nptr
, wchar_t **endptr
, int base
);
335 #ifndef wxCRT_StrxfrmW
336 WXDLLIMPEXP_BASE
size_t wxCRT_StrxfrmW(wchar_t *dest
, const wchar_t *src
, size_t n
);
339 #ifndef wxCRT_StrdupA
340 WXDLLIMPEXP_BASE
char *wxCRT_StrdupA(const char *psz
);
343 #ifndef wxCRT_StrdupW
344 WXDLLIMPEXP_BASE
wchar_t *wxCRT_StrdupW(const wchar_t *pwz
);
347 #ifndef wxCRT_StricmpA
348 WXDLLIMPEXP_BASE
int wxCRT_StricmpA(const char *psz1
, const char *psz2
);
351 #ifndef wxCRT_StricmpW
352 WXDLLIMPEXP_BASE
int wxCRT_StricmpW(const wchar_t *psz1
, const wchar_t *psz2
);
355 #ifndef wxCRT_StrnicmpA
356 WXDLLIMPEXP_BASE
int wxCRT_StrnicmpA(const char *psz1
, const char *psz2
, size_t len
);
359 #ifndef wxCRT_StrnicmpW
360 WXDLLIMPEXP_BASE
int wxCRT_StrnicmpW(const wchar_t *psz1
, const wchar_t *psz2
, size_t len
);
363 #ifndef wxCRT_StrtokA
364 WXDLLIMPEXP_BASE
char *wxCRT_StrtokA(char *psz
, const char *delim
, char **save_ptr
);
367 #ifndef wxCRT_StrtokW
368 WXDLLIMPEXP_BASE
wchar_t *wxCRT_StrtokW(wchar_t *psz
, const wchar_t *delim
, wchar_t **save_ptr
);
371 /* supply strtoll and strtoull, if needed */
373 #ifndef wxCRT_StrtollA
374 WXDLLIMPEXP_BASE wxLongLong_t
wxCRT_StrtollA(const char* nptr
,
377 WXDLLIMPEXP_BASE wxULongLong_t
wxCRT_StrtoullA(const char* nptr
,
381 #ifndef wxCRT_StrtollW
382 WXDLLIMPEXP_BASE wxLongLong_t
wxCRT_StrtollW(const wchar_t* nptr
,
385 WXDLLIMPEXP_BASE wxULongLong_t
wxCRT_StrtoullW(const wchar_t* nptr
,
389 #endif // wxLongLong_t
392 /* -------------------------------------------------------------------------
394 ------------------------------------------------------------------------- */
396 #if defined(__UNIX__) || defined(__WXMAC__)
403 /* these functions are only needed in the form used for filenames (i.e. char*
404 on Unix, wchar_t* on Windows), so we don't need to use A/W suffix: */
405 #if wxMBFILES || !wxUSE_UNICODE /* ANSI filenames */
407 #define wxCRT_Fopen fopen
408 #define wxCRT_Freopen freopen
409 #define wxCRT_Remove remove
410 #define wxCRT_Rename rename
412 #else /* Unicode filenames */
414 /* special case: these functions are missing under Win9x with Unicows so we
415 have to implement them ourselves */
416 #if wxUSE_UNICODE_MSLU
417 WXDLLIMPEXP_BASE
FILE* wxMSLU__wfopen(const wchar_t *name
, const wchar_t *mode
);
418 WXDLLIMPEXP_BASE
FILE* wxMSLU__wfreopen(const wchar_t *name
, const wchar_t *mode
, FILE *stream
);
419 WXDLLIMPEXP_BASE
int wxMSLU__wrename(const wchar_t *oldname
, const wchar_t *newname
);
420 WXDLLIMPEXP_BASE
int wxMSLU__wremove(const wchar_t *name
);
421 #define wxCRT_Fopen wxMSLU__wfopen
422 #define wxCRT_Freopen wxMSLU__wfreopen
423 #define wxCRT_Remove wxMSLU__wremove
424 #define wxCRT_Rename wxMSLU__wrename
426 #define wxCRT_Rename _wrename
427 #define wxCRT_Fopen _wfopen
428 #define wxCRT_Freopen _wfreopen
430 /* carefully: wxCRT_Remove() must return 0 on success while
431 DeleteFile() returns 0 on error, so don't just define one as
433 int wxCRT_Remove(const wchar_t *path
);
435 #define wxCRT_Remove _wremove
439 #endif /* wxMBFILES/!wxMBFILES */
441 #define wxCRT_PutsA puts
442 #define wxCRT_FputsA fputs
443 #define wxCRT_FgetsA fgets
444 #define wxCRT_FputcA fputc
445 #define wxCRT_FgetcA fgetc
446 #define wxCRT_UngetcA ungetc
448 #ifdef wxHAVE_TCHAR_SUPPORT
449 #define wxCRT_PutsW _putws
450 #define wxCRT_FputsW fputws
451 #define wxCRT_FputcW fputwc
454 #define wxCRT_FputsW fputws
457 #define wxCRT_PutsW putws
460 #define wxCRT_FputcW fputwc
462 #define wxCRT_FgetsW fgetws
465 WXDLLIMPEXP_BASE
int wxCRT_PutsW(const wchar_t *ws
);
469 WXDLLIMPEXP_BASE
int wxCRT_FputsW(const wchar_t *ch
, FILE *stream
);
473 WXDLLIMPEXP_BASE
int wxCRT_FputcW(wchar_t wc
, FILE *stream
);
476 #define wxCRT_TmpnamA tmpnam
478 #define wxCRT_TmpnamW _wtmpnam
481 #ifndef wxCRT_TmpnamW
482 WXDLLIMPEXP_BASE
wchar_t *wxCRT_TmpnamW(wchar_t *s
);
485 #define wxCRT_PerrorA perror
486 #ifdef wxHAVE_TCHAR_SUPPORT
487 #define wxCRT_PerrorW _wperror
490 /* -------------------------------------------------------------------------
492 ------------------------------------------------------------------------- */
494 /* there are no env vars at all under CE, so no _tgetenv neither */
496 /* can't define as inline function as this is a C file... */
497 #define wxCRT_GetenvA(name) ((char*)NULL)
498 #define wxCRT_GetenvW(name) ((wchar_t*)NULL)
500 #define wxCRT_GetenvA getenv
502 #define wxCRT_GetenvW _wgetenv
506 #ifndef wxCRT_GetenvW
507 WXDLLIMPEXP_BASE
wchar_t * wxCRT_GetenvW(const wchar_t *name
);
511 #define wxCRT_SystemA system
512 /* mingw32 doesn't provide _tsystem() or _wsystem(): */
513 #if defined(_tsystem)
514 #define wxCRT_SystemW _wsystem
517 #define wxCRT_AtofA atof
518 #define wxCRT_AtoiA atoi
519 #define wxCRT_AtolA atol
521 #if defined(__MWERKS__)
523 #define wxCRT_AtofW watof
524 #define wxCRT_AtoiW watoi
525 #define wxCRT_AtolW watol
526 /* else: use ANSI versions */
528 #elif defined(wxHAVE_TCHAR_SUPPORT)
529 #define wxCRT_AtoiW _wtoi
530 #define wxCRT_AtolW _wtol
531 /* _wtof doesn't exist */
534 #define wxCRT_AtofW(s) wcstof(s, NULL)
536 #define wxCRT_AtolW(s) wcstol(s, NULL, 10)
537 /* wcstoi doesn't exist */
541 There are 2 unrelated problems with these functions under Mac:
542 a) Metrowerks MSL CRT implements them strictly in C99 sense and
543 doesn't support (very common) extension of allowing to call
544 mbstowcs(NULL, ...) which makes it pretty useless as you can't
545 know the size of the needed buffer
546 b) OS X <= 10.2 declares and even defined these functions but
547 doesn't really implement them -- they always return an error
549 So use our own replacements in both cases.
551 #if defined(__MWERKS__) && defined(__MSL__)
552 #define wxNEED_WX_MBSTOWCS
556 #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_2
557 #define wxNEED_WX_MBSTOWCS
561 #ifdef wxNEED_WX_MBSTOWCS
562 /* even though they are defined and "implemented", they are bad and just
563 stubs so we need our own - we need these even in ANSI builds!! */
564 WXDLLIMPEXP_BASE
size_t wxMbstowcs(wchar_t *, const char *, size_t);
565 WXDLLIMPEXP_BASE
size_t wxWcstombs(char *, const wchar_t *, size_t);
567 #define wxMbstowcs mbstowcs
568 #define wxWcstombs wcstombs
573 /* -------------------------------------------------------------------------
575 ------------------------------------------------------------------------- */
577 #define wxCRT_StrftimeA strftime
578 /* FIXME-UTF8: when is this available? */
579 #define wxCRT_StrftimeW wcsftime
581 #ifndef wxCRT_StrftimeW
582 WXDLLIMPEXP_BASE
size_t wxCRT_StrftimeW(wchar_t *s
, size_t max
,
584 const struct tm
*tm
);
589 /* -------------------------------------------------------------------------
591 ------------------------------------------------------------------------- */
594 #define WXWCHAR_T_CAST(c) (wint_t)(c)
596 #define WXWCHAR_T_CAST(c) c
599 #define wxCRT_IsalnumW(c) iswalnum(WXWCHAR_T_CAST(c))
600 #define wxCRT_IsalphaW(c) iswalpha(WXWCHAR_T_CAST(c))
601 #define wxCRT_IscntrlW(c) iswcntrl(WXWCHAR_T_CAST(c))
602 #define wxCRT_IsdigitW(c) iswdigit(WXWCHAR_T_CAST(c))
603 #define wxCRT_IsgraphW(c) iswgraph(WXWCHAR_T_CAST(c))
604 #define wxCRT_IslowerW(c) iswlower(WXWCHAR_T_CAST(c))
605 #define wxCRT_IsprintW(c) iswprint(WXWCHAR_T_CAST(c))
606 #define wxCRT_IspunctW(c) iswpunct(WXWCHAR_T_CAST(c))
607 #define wxCRT_IsspaceW(c) iswspace(WXWCHAR_T_CAST(c))
608 #define wxCRT_IsupperW(c) iswupper(WXWCHAR_T_CAST(c))
609 #define wxCRT_IsxdigitW(c) iswxdigit(WXWCHAR_T_CAST(c))
612 #if defined(__GLIBC__) && (__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0)
613 /* /usr/include/wctype.h incorrectly declares translations */
614 /* tables which provokes tons of compile-time warnings -- try */
615 /* to correct this */
616 #define wxCRT_TolowerW(wc) towctrans((wc), (wctrans_t)__ctype_tolower)
617 #define wxCRT_ToupperW(wc) towctrans((wc), (wctrans_t)__ctype_toupper)
618 #else /* !glibc 2.0 */
619 #define wxCRT_TolowerW towlower
620 #define wxCRT_ToupperW towupper
622 #else /* !__GLIBC__ */
623 /* There is a bug in VC6 C RTL: toxxx() functions dosn't do anything
624 with signed chars < 0, so "fix" it here. */
625 #define wxCRT_TolowerW(c) towlower((wxUChar)(wxChar)(c))
626 #define wxCRT_ToupperW(c) towupper((wxUChar)(wxChar)(c))
627 #endif /* __GLIBC__/!__GLIBC__ */
633 /* -------------------------------------------------------------------------
634 wx wrappers for CRT functions in both char* and wchar_t* versions
635 ------------------------------------------------------------------------- */
639 /* NB: this belongs to wxcrt.h and not this header, but it makes life easier
640 * for buffer.h and stringimpl.h (both of which must be included before
641 * string.h, which is required by wxcrt.h) to have them here: */
643 /* safe version of strlen() (returns 0 if passed NULL pointer) */
644 inline size_t wxStrlen(const char *s
) { return s
? wxCRT_StrlenA(s
) : 0; }
645 inline size_t wxStrlen(const wchar_t *s
) { return s
? wxCRT_StrlenW(s
) : 0; }
646 #define wxWcslen wxCRT_StrlenW
648 #define wxStrdupA wxCRT_StrdupA
649 #define wxStrdupW wxCRT_StrdupW
650 inline char* wxStrdup(const char *s
) { return wxCRT_StrdupA(s
); }
651 inline wchar_t* wxStrdup(const wchar_t *s
) { return wxCRT_StrdupW(s
); }
653 #endif /* __cplusplus */
655 #endif /* _WX_WXCRTBASE_H_ */