X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/d0bdc3ca5f3e1d2adf70c8630b4b524bc53a798e..277f2e527e33de039146ab94358ffec9c98f6112:/src/common/wxchar.cpp diff --git a/src/common/wxchar.cpp b/src/common/wxchar.cpp index f985d439d1..8faa0c6eab 100644 --- a/src/common/wxchar.cpp +++ b/src/common/wxchar.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #ifndef WX_PRECOMP #include "wx/defs.h" @@ -39,30 +40,34 @@ #include "wx/hash.h" #endif +#if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H) + #include + #include + #include + #include +#endif + #if wxUSE_WCHAR_T -size_t wxMB2WC(wchar_t *buf, const char *psz, size_t n) +size_t WXDLLEXPORT wxMB2WC(wchar_t *buf, const char *psz, size_t n) { if (buf) { if (!n || !*psz) { - if (n) *buf = _T('\0'); + if (n) *buf = wxT('\0'); return 0; } return mbstowcs(buf, psz, n); } - // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't - // honor the 3rd parameter, thus it will happily crash here). -#if wxUSE_WCSRTOMBS - // don't know if it's really needed (or if we can pass NULL), but better safe - // than quick - mbstate_t mbstate; + // assume that we have mbsrtowcs() too if we have wcsrtombs() +#ifdef HAVE_WCSRTOMBS + mbstate_t mbstate; return mbsrtowcs((wchar_t *) NULL, &psz, 0, &mbstate); #else // !GNU libc return mbstowcs((wchar_t *) NULL, psz, 0); #endif // GNU } -size_t wxWC2MB(char *buf, const wchar_t *pwz, size_t n) +size_t WXDLLEXPORT wxWC2MB(char *buf, const wchar_t *pwz, size_t n) { if (buf) { if (!n || !*pwz) { @@ -73,21 +78,70 @@ size_t wxWC2MB(char *buf, const wchar_t *pwz, size_t n) return wcstombs(buf, pwz, n); } - // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't - // honor the 3rd parameter, thus it will happily crash here). -#if wxUSE_WCSRTOMBS - // don't know if it's really needed (or if we can pass NULL), but better safe - // than quick - mbstate_t mbstate; +#if HAVE_WCSRTOMBS + mbstate_t mbstate; return wcsrtombs((char *) NULL, &pwz, 0, &mbstate); #else // !GNU libc return wcstombs((char *) NULL, pwz, 0); #endif // GNU } +#endif // wxUSE_WCHAR_T + +bool WXDLLEXPORT wxOKlibc() +{ +#if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) + // glibc 2.0 uses UTF-8 even when it shouldn't + wchar_t res = 0; + if ((MB_CUR_MAX == 2) && + (wxMB2WC(&res, "\xdd\xa5", 1) == 1) && + (res==0x765)) { + // this is UTF-8 allright, check whether that's what we want + char *cur_locale = setlocale(LC_CTYPE, NULL); + if ((strlen(cur_locale) < 4) || + (strcasecmp(cur_locale + strlen(cur_locale) - 4, "utf8")) || + (strcasecmp(cur_locale + strlen(cur_locale) - 5, "utf-8"))) { + // nope, don't use libc conversion + return FALSE; + } + } +#endif + return TRUE; +} + +#ifndef HAVE_WCSLEN +size_t WXDLLEXPORT wcslen(const wchar_t *s) +{ + size_t len = 0; + while (s[len]) len++; + return len; +} +#endif + +#if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H) +inline WORD wxMSW_ctype(wxChar ch) +{ + WORD ret; + GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, &ch, 1, &ret); + return ret; +} + +WXDLLEXPORT int wxIsalnum(wxChar ch) { return IsCharAlphaNumeric(ch); } +WXDLLEXPORT int wxIsalpha(wxChar ch) { return IsCharAlpha(ch); } +WXDLLEXPORT int wxIsctrl(wxChar ch) { return wxMSW_ctype(ch) & C1_CNTRL; } +WXDLLEXPORT int wxIsdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_DIGIT; } +WXDLLEXPORT int wxIsgraph(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_PUNCT|C1_ALPHA); } +WXDLLEXPORT int wxIslower(wxChar ch) { return IsCharLower(ch); } +WXDLLEXPORT int wxIsprint(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_SPACE|C1_PUNCT|C1_ALPHA); } +WXDLLEXPORT int wxIspunct(wxChar ch) { return wxMSW_ctype(ch) & C1_PUNCT; } +WXDLLEXPORT int wxIsspace(wxChar ch) { return wxMSW_ctype(ch) & C1_SPACE; } +WXDLLEXPORT int wxIsupper(wxChar ch) { return IsCharUpper(ch); } +WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_XDIGIT; } +WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)CharLower((LPTSTR)(ch)); } +WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)CharUpper((LPTSTR)(ch)); } #endif #ifndef wxStrdup -wxChar * WXDLLEXPORT wxStrdup(const wxChar *psz) +WXDLLEXPORT wxChar * wxStrdup(const wxChar *psz) { size_t size = (wxStrlen(psz) + 1) * sizeof(wxChar); wxChar *ret = (wxChar *) malloc(size); @@ -108,8 +162,21 @@ int WXDLLEXPORT wxStricmp(const wxChar *psz1, const wxChar *psz2) } #endif +#ifndef wxStricmp +int WXDLLEXPORT wxStrnicmp(const wxChar *s1, const wxChar *s2, size_t n) +{ + register wxChar c1, c2; + while (n && ((c1 = wxTolower(*s1)) == (c2 = wxTolower(*s2)) ) && c1) n--, s1++, s2++; + if (n) { + if (c1 < c2) return -1; + if (c1 > c2) return 1; + } + return 0; +} +#endif + #ifndef wxStrtok -wxChar * WXDLLEXPORT wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr) +WXDLLEXPORT wxChar * wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr) { if (!psz) psz = *save_ptr; psz += wxStrspn(psz, delim); @@ -121,7 +188,7 @@ wxChar * WXDLLEXPORT wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_pt psz = wxStrpbrk(psz, delim); if (!psz) *save_ptr = (wxChar*)NULL; else { - *psz = _T('\0'); + *psz = wxT('\0'); *save_ptr = psz + 1; } return ret; @@ -129,15 +196,202 @@ wxChar * WXDLLEXPORT wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_pt #endif #ifndef wxSetlocale -wxChar * WXDLLEXPORT wxSetlocale(int category, const wxChar *locale) +WXDLLEXPORT wxWCharBuffer wxSetlocale(int category, const wxChar *locale) +{ + char *localeOld = setlocale(category, wxConvLibc.cWX2MB(locale)); + + return wxWCharBuffer(wxConvLibc.cMB2WC(localeOld)); +} +#endif + +#ifdef wxNEED_WX_STRING_H +WXDLLEXPORT wxChar * wxStrcat(wxChar *dest, const wxChar *src) +{ + wxChar *ret = dest; + while (*dest) dest++; + while ((*dest++ = *src++)); + return ret; +} + +WXDLLEXPORT const wxChar * wxStrchr(const wxChar *s, wxChar c) +{ + // be careful here as the terminating NUL makes part of the string + while ( *s != c ) + { + if ( !*s++ ) + return NULL; + } + + return s; +} + +WXDLLEXPORT int wxStrcmp(const wxChar *s1, const wxChar *s2) +{ + while ((*s1 == *s2) && *s1) s1++, s2++; + if ((wxUChar)*s1 < (wxUChar)*s2) return -1; + if ((wxUChar)*s1 > (wxUChar)*s2) return 1; + return 0; +} + +WXDLLEXPORT wxChar * wxStrcpy(wxChar *dest, const wxChar *src) +{ + wxChar *ret = dest; + while ((*dest++ = *src++)); + return ret; +} + +WXDLLEXPORT wxChar * wxStrncat(wxChar *dest, const wxChar *src, size_t n) +{ + wxChar *ret = dest; + while (*dest) dest++; + while (n && (*dest++ = *src++)) n--; + return ret; +} + +WXDLLEXPORT int wxStrncmp(const wxChar *s1, const wxChar *s2, size_t n) +{ + while (n && (*s1 == *s2) && *s1) n--, s1++, s2++; + if (n) { + if ((wxUChar)*s1 < (wxUChar)*s2) return -1; + if ((wxUChar)*s1 > (wxUChar)*s2) return 1; + } + return 0; +} + +WXDLLEXPORT wxChar * wxStrncpy(wxChar *dest, const wxChar *src, size_t n) +{ + wxChar *ret = dest; + while (n && (*dest++ = *src++)) n--; + while (n) *dest++=0, n--; // the docs specify padding with zeroes + return ret; +} + +WXDLLEXPORT const wxChar * wxStrpbrk(const wxChar *s, const wxChar *accept) +{ + while (*s && !wxStrchr(accept, *s)) + s++; + + return *s ? s : NULL; +} + +WXDLLEXPORT const wxChar * wxStrrchr(const wxChar *s, wxChar c) +{ + const wxChar *ret = NULL; + do + { + if ( *s == c ) + ret = s; + s++; + } + while ( *s ); + + return ret; +} + +WXDLLEXPORT size_t wxStrspn(const wxChar *s, const wxChar *accept) +{ + size_t len = 0; + while (wxStrchr(accept, *s++)) len++; + return len; +} + +WXDLLEXPORT const wxChar *wxStrstr(const wxChar *haystack, const wxChar *needle) { - setlocale(category, wxConv_libc.cWX2MB(locale)); - // FIXME - return (wxChar *)NULL; + wxCHECK_RET( needle, NULL, _T("NULL argument in wxStrstr") ); + + // VZ: this is not exactly the most efficient string search algorithm... + + const size_t len = wxStrlen(needle); + + while ( const wxChar *fnd = wxStrchr(haystack, *needle) ) + { + if ( !wxStrncmp(fnd, needle, len) ) + return fnd; + + haystack = fnd + 1; + } + + return NULL; +} + +WXDLLEXPORT double wxStrtod(const wxChar *nptr, wxChar **endptr) +{ + const wxChar *start = nptr; + + // FIXME: only correct for C locale + while (wxIsspace(*nptr)) nptr++; + if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++; + while (wxIsdigit(*nptr)) nptr++; + if (*nptr == wxT('.')) { + nptr++; + while (wxIsdigit(*nptr)) nptr++; + } + if (*nptr == wxT('E') || *nptr == wxT('e')) { + nptr++; + if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++; + while (wxIsdigit(*nptr)) nptr++; + } + + wxString data(nptr, nptr-start); + wxWX2MBbuf dat = data.mb_str(wxConvLibc); + char *rdat = wxMBSTRINGCAST dat; + double ret = strtod(dat, &rdat); + + if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat)); + + return ret; +} + +WXDLLEXPORT long int wxStrtol(const wxChar *nptr, wxChar **endptr, int base) +{ + const wxChar *start = nptr; + + // FIXME: only correct for C locale + while (wxIsspace(*nptr)) nptr++; + if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++; + if (((base == 0) || (base == 16)) && + (nptr[0] == wxT('0') && nptr[1] == wxT('x'))) { + nptr += 2; + base = 16; + } + else if ((base == 0) && (nptr[0] == wxT('0'))) base = 8; + else if (base == 0) base = 10; + + while ((wxIsdigit(*nptr) && (*nptr - wxT('0') < base)) || + (wxIsalpha(*nptr) && (wxToupper(*nptr) - wxT('A') + 10 < base))) nptr++; + + wxString data(nptr, nptr-start); + wxWX2MBbuf dat = data.mb_str(wxConvLibc); + char *rdat = wxMBSTRINGCAST dat; + long int ret = strtol(dat, &rdat, base); + + if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat)); + + return ret; } #endif #ifdef wxNEED_WX_STDIO_H +WXDLLEXPORT FILE * wxFopen(const wxChar *path, const wxChar *mode) +{ + return fopen(wxConvFile.cWX2MB(path), wxConvLibc.cWX2MB(mode)); +} + +WXDLLEXPORT FILE * wxFreopen(const wxChar *path, const wxChar *mode, FILE *stream) +{ + return freopen(wxConvFile.cWX2MB(path), wxConvLibc.cWX2MB(mode), stream); +} + +WXDLLEXPORT int wxRemove(const wxChar *path) +{ + return remove(wxConvFile.cWX2MB(path)); +} + +WXDLLEXPORT int wxRename(const wxChar *oldpath, const wxChar *newpath) +{ + return rename(wxConvFile.cWX2MB(oldpath), wxConvFile.cWX2MB(newpath)); +} + int WXDLLEXPORT wxPrintf(const wxChar *fmt, ...) { va_list argptr; @@ -213,7 +467,7 @@ int WXDLLEXPORT wxVsscanf(const wxChar *buf, const wxChar *fmt, va_list argptr) int ret; // this will work only for numeric conversion! Strings will not be converted correctly // hopefully this is all we'll need - ret = vsscanf(wxConv_libc.cWX2MB(buf), wxConv_libc.cWX2MB(fmt), argptr); + ret = vsscanf(wxConvLibc.cWX2MB(buf), wxConvLibc.cWX2MB(fmt), argptr); return ret; } #endif @@ -221,19 +475,19 @@ int WXDLLEXPORT wxVsscanf(const wxChar *buf, const wxChar *fmt, va_list argptr) #ifndef wxAtof double WXDLLEXPORT wxAtof(const wxChar *psz) { - return atof(wxConv_libc.cWX2MB(psz)); + return atof(wxConvLibc.cWX2MB(psz)); } #endif #ifdef wxNEED_WX_STDLIB_H int WXDLLEXPORT wxAtoi(const wxChar *psz) { - return atoi(wxConv_libc.cWX2MB(psz)); + return atoi(wxConvLibc.cWX2MB(psz)); } long WXDLLEXPORT wxAtol(const wxChar *psz) { - return atol(wxConv_libc.cWX2MB(psz)); + return atol(wxConvLibc.cWX2MB(psz)); } wxChar * WXDLLEXPORT wxGetenv(const wxChar *name) @@ -243,7 +497,7 @@ wxChar * WXDLLEXPORT wxGetenv(const wxChar *name) wxObject *data = env.Get(name); if (!data) { // nope, retrieve it, - const char *val = getenv(wxConv_libc.cWX2MB(name)); + const char *val = getenv(wxConvLibc.cWX2MB(name)); if (!val) return (wxChar *)NULL; // convert it, data = (wxObject *)new wxString(val); @@ -256,7 +510,25 @@ wxChar * WXDLLEXPORT wxGetenv(const wxChar *name) int WXDLLEXPORT wxSystem(const wxChar *psz) { - return system(wxConv_libc.cWX2MB(psz)); + return system(wxConvLibc.cWX2MB(psz)); } #endif + +#ifdef wxNEED_WX_TIME_H +WXDLLEXPORT size_t wxStrftime(wxChar *s, size_t max, const wxChar *fmt, const struct tm *tm) +{ + if (!max) return 0; + char *buf = (char *)malloc(max); + size_t ret = strftime(buf, max, wxConvLibc.cWX2MB(fmt), tm); + if (ret) { + wxStrcpy(s, wxConvLibc.cMB2WX(buf)); + free(buf); + return wxStrlen(s); + } else { + free(buf); + *s = 0; + return 0; + } +} +#endif