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 */
372 #ifndef wxCRT_StrtollA
373 WXDLLIMPEXP_BASE wxLongLong_t
wxCRT_StrtollA(const char* nptr
, char** endptr
, int base
);
374 WXDLLIMPEXP_BASE wxULongLong_t
wxCRT_StrtoullA(const char* nptr
, char** endptr
, int base
);
376 #ifndef wxCRT_StrtollW
377 WXDLLIMPEXP_BASE wxLongLong_t
wxCRT_StrtollW(const wchar_t* nptr
, wchar_t** endptr
, int base
);
378 WXDLLIMPEXP_BASE wxULongLong_t
wxCRT_StrtoullW(const wchar_t* nptr
, wchar_t** endptr
, int base
);
382 /* -------------------------------------------------------------------------
384 ------------------------------------------------------------------------- */
386 #if defined(__UNIX__) || defined(__WXMAC__)
393 /* these functions are only needed in the form used for filenames (i.e. char*
394 on Unix, wchar_t* on Windows), so we don't need to use A/W suffix: */
395 #if wxMBFILES || !wxUSE_UNICODE // ANSI filenames
397 #define wxCRT_Fopen fopen
398 #define wxCRT_Freopen freopen
399 #define wxCRT_Remove remove
400 #define wxCRT_Rename rename
402 #else // Unicode filenames
404 /* special case: these functions are missing under Win9x with Unicows so we
405 have to implement them ourselves */
406 #if wxUSE_UNICODE_MSLU
407 WXDLLIMPEXP_BASE
FILE* wxMSLU__wfopen(const wchar_t *name
, const wchar_t *mode
);
408 WXDLLIMPEXP_BASE
FILE* wxMSLU__wfreopen(const wchar_t *name
, const wchar_t *mode
, FILE *stream
);
409 WXDLLIMPEXP_BASE
int wxMSLU__wrename(const wchar_t *oldname
, const wchar_t *newname
);
410 WXDLLIMPEXP_BASE
int wxMSLU__wremove(const wchar_t *name
);
411 #define wxCRT_Fopen wxMSLU__wfopen
412 #define wxCRT_Freopen wxMSLU__wfreopen
413 #define wxCRT_Remove wxMSLU__wremove
414 #define wxCRT_Rename wxMSLU__wrename
416 #define wxCRT_Rename _wrename
417 #define wxCRT_Fopen _wfopen
418 #define wxCRT_Freopen _wfreopen
420 /* carefully: wxCRT_Remove() must return 0 on success while
421 DeleteFile() returns 0 on error, so don't just define one as
423 int wxCRT_Remove(const wchar_t *path
);
425 #define wxCRT_Remove _wremove
429 #endif // wxMBFILES/!wxMBFILES
431 #define wxCRT_PutsA puts
432 #define wxCRT_FputsA fputs
433 #define wxCRT_FgetsA fgets
434 #define wxCRT_FputcA fputc
435 #define wxCRT_FgetcA fgetc
436 #define wxCRT_UngetcA ungetc
438 #ifdef wxHAVE_TCHAR_SUPPORT
439 #define wxCRT_PutsW _putws
440 #define wxCRT_FputsW fputws
441 #define wxCRT_FputcW fputwc
444 #define wxCRT_FputsW fputws
447 #define wxCRT_PutsW putws
450 #define wxCRT_FputcW fputwc
452 #define wxCRT_FgetsW fgetws
455 WXDLLIMPEXP_BASE
int wxCRT_PutsW(const wchar_t *ws
);
459 WXDLLIMPEXP_BASE
int wxCRT_FputsW(const wchar_t *ch
, FILE *stream
);
463 WXDLLIMPEXP_BASE
int wxCRT_FputcW(wchar_t wc
, FILE *stream
);
466 #define wxCRT_TmpnamA tmpnam
468 #define wxCRT_TmpnamW _wtmpnam
471 #ifndef wxCRT_TmpnamW
472 WXDLLIMPEXP_BASE
wchar_t *wxCRT_TmpnamW(wchar_t *s
);
475 #define wxCRT_PerrorA perror
476 #ifdef wxHAVE_TCHAR_SUPPORT
477 #define wxCRT_PerrorW _wperror
480 /* -------------------------------------------------------------------------
482 ------------------------------------------------------------------------- */
484 /* there are no env vars at all under CE, so no _tgetenv neither */
486 /* can't define as inline function as this is a C file... */
487 #define wxCRT_GetenvA(name) ((char*)NULL)
488 #define wxCRT_GetenvW(name) ((wchar_t*)NULL)
490 #define wxCRT_GetenvA getenv
492 #define wxCRT_GetenvW _wgetenv
496 #ifndef wxCRT_GetenvW
497 WXDLLIMPEXP_BASE
wchar_t * wxCRT_GetenvW(const wchar_t *name
);
501 #define wxCRT_SystemA system
502 /* mingw32 doesn't provide _tsystem() or _wsystem(): */
503 #if defined(_tsystem)
504 #define wxCRT_SystemW _wsystem
507 #define wxCRT_AtofA atof
508 #define wxCRT_AtoiA atoi
509 #define wxCRT_AtolA atol
511 #if defined(__MWERKS__)
513 #define wxCRT_AtofW watof
514 #define wxCRT_AtoiW watoi
515 #define wxCRT_AtolW watol
516 /* else: use ANSI versions */
518 #elif defined(wxHAVE_TCHAR_SUPPORT)
519 #define wxCRT_AtoiW _wtoi
520 #define wxCRT_AtolW _wtol
521 /* _wtof doesn't exist */
524 #define wxCRT_AtofW(s) wcstof(s, NULL)
526 #define wxCRT_AtolW(s) wcstol(s, NULL, 10)
527 /* wcstoi doesn't exist */
531 There are 2 unrelated problems with these functions under Mac:
532 a) Metrowerks MSL CRT implements them strictly in C99 sense and
533 doesn't support (very common) extension of allowing to call
534 mbstowcs(NULL, ...) which makes it pretty useless as you can't
535 know the size of the needed buffer
536 b) OS X <= 10.2 declares and even defined these functions but
537 doesn't really implement them -- they always return an error
539 So use our own replacements in both cases.
541 #if defined(__MWERKS__) && defined(__MSL__)
542 #define wxNEED_WX_MBSTOWCS
546 #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_2
547 #define wxNEED_WX_MBSTOWCS
551 #ifdef wxNEED_WX_MBSTOWCS
552 /* even though they are defined and "implemented", they are bad and just
553 stubs so we need our own - we need these even in ANSI builds!! */
554 WXDLLIMPEXP_BASE
size_t wxMbstowcs(wchar_t *, const char *, size_t);
555 WXDLLIMPEXP_BASE
size_t wxWcstombs(char *, const wchar_t *, size_t);
557 #define wxMbstowcs mbstowcs
558 #define wxWcstombs wcstombs
563 /* -------------------------------------------------------------------------
565 ------------------------------------------------------------------------- */
567 #define wxCRT_StrftimeA strftime
568 /* FIXME-UTF8: when is this available? */
569 #define wxCRT_StrftimeW wcsftime
571 #ifndef wxCRT_StrftimeW
572 WXDLLIMPEXP_BASE
size_t wxCRT_StrftimeW(wchar_t *s
, size_t max
,
574 const struct tm
*tm
);
579 /* -------------------------------------------------------------------------
581 ------------------------------------------------------------------------- */
584 #define WXWCHAR_T_CAST(c) (wint_t)(c)
586 #define WXWCHAR_T_CAST(c) c
589 #define wxCRT_IsalnumW(c) iswalnum(WXWCHAR_T_CAST(c))
590 #define wxCRT_IsalphaW(c) iswalpha(WXWCHAR_T_CAST(c))
591 #define wxCRT_IscntrlW(c) iswcntrl(WXWCHAR_T_CAST(c))
592 #define wxCRT_IsdigitW(c) iswdigit(WXWCHAR_T_CAST(c))
593 #define wxCRT_IsgraphW(c) iswgraph(WXWCHAR_T_CAST(c))
594 #define wxCRT_IslowerW(c) iswlower(WXWCHAR_T_CAST(c))
595 #define wxCRT_IsprintW(c) iswprint(WXWCHAR_T_CAST(c))
596 #define wxCRT_IspunctW(c) iswpunct(WXWCHAR_T_CAST(c))
597 #define wxCRT_IsspaceW(c) iswspace(WXWCHAR_T_CAST(c))
598 #define wxCRT_IsupperW(c) iswupper(WXWCHAR_T_CAST(c))
599 #define wxCRT_IsxdigitW(c) iswxdigit(WXWCHAR_T_CAST(c))
602 #if defined(__GLIBC__) && (__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0)
603 /* /usr/include/wctype.h incorrectly declares translations */
604 /* tables which provokes tons of compile-time warnings -- try */
605 /* to correct this */
606 #define wxCRT_TolowerW(wc) towctrans((wc), (wctrans_t)__ctype_tolower)
607 #define wxCRT_ToupperW(wc) towctrans((wc), (wctrans_t)__ctype_toupper)
608 #else /* !glibc 2.0 */
609 #define wxCRT_TolowerW towlower
610 #define wxCRT_ToupperW towupper
613 /* There is a bug in VC6 C RTL: toxxx() functions dosn't do anything
614 with signed chars < 0, so "fix" it here. */
615 #define wxCRT_TolowerW(c) towlower((wxUChar)(wxChar)(c))
616 #define wxCRT_ToupperW(c) towupper((wxUChar)(wxChar)(c))
617 #endif // __GLIBC__/!__GLIBC__
623 /* -------------------------------------------------------------------------
624 wx wrappers for CRT functions in both char* and wchar_t* versions
625 ------------------------------------------------------------------------- */
629 /* NB: this belongs to wxcrt.h and not this header, but it makes life easier
630 * for buffer.h and stringimpl.h (both of which must be included before
631 * string.h, which is required by wxcrt.h) to have them here: */
633 /* safe version of strlen() (returns 0 if passed NULL pointer) */
634 inline size_t wxStrlen(const char *s
) { return s
? wxCRT_StrlenA(s
) : 0; }
635 inline size_t wxStrlen(const wchar_t *s
) { return s
? wxCRT_StrlenW(s
) : 0; }
636 #define wxWcslen wxCRT_StrlenW
638 #define wxStrdupA wxCRT_StrdupA
639 #define wxStrdupW wxCRT_StrdupW
640 inline char* wxStrdup(const char *s
) { return wxCRT_StrdupA(s
); }
641 inline wchar_t* wxStrdup(const wchar_t *s
) { return wxCRT_StrdupW(s
); }
643 #endif /* __cplusplus */
645 #endif /* _WX_WXCRTBASE_H_ */