X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/5707316c9cbc2b44bb6b0246d766e5bb0cfdc2e7..83afe21166ad23a402185eef5dd47782597e8f91:/src/common/wxchar.cpp diff --git a/src/common/wxchar.cpp b/src/common/wxchar.cpp index 72a879991f..39efe204fa 100644 --- a/src/common/wxchar.cpp +++ b/src/common/wxchar.cpp @@ -40,11 +40,13 @@ #include "wx/hash.h" #endif +#include "wx/msgdlg.h" + #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H) -#include -#include -#include -#include + #include + #include + #include + #include #endif #if wxUSE_WCHAR_T @@ -58,11 +60,8 @@ size_t WXDLLEXPORT wxMB2WC(wchar_t *buf, const char *psz, size_t n) 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 + // 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 @@ -81,18 +80,14 @@ size_t WXDLLEXPORT 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 +#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 +#endif // wxUSE_WCHAR_T bool WXDLLEXPORT wxOKlibc() { @@ -205,9 +200,9 @@ WXDLLEXPORT wxChar * wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_pt #ifndef wxSetlocale WXDLLEXPORT wxWCharBuffer wxSetlocale(int category, const wxChar *locale) { - char *localeOld = setlocale(category, wxConvLibc.cWX2MB(locale)); + char *localeOld = setlocale(category, wxConvLocal.cWX2MB(locale)); - return wxWCharBuffer(wxConvLibc.cMB2WC(localeOld)); + return wxWCharBuffer(wxConvLocal.cMB2WC(localeOld)); } #endif @@ -220,10 +215,16 @@ WXDLLEXPORT wxChar * wxStrcat(wxChar *dest, const wxChar *src) return ret; } -WXDLLEXPORT wxChar * wxStrchr(const wxChar *s, wxChar c) +WXDLLEXPORT const wxChar * wxStrchr(const wxChar *s, wxChar c) { - while (*s && *s != c) s++; - return (*s) ? (wxChar *)s : (wxChar *)NULL; + // 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) @@ -267,20 +268,26 @@ WXDLLEXPORT wxChar * wxStrncpy(wxChar *dest, const wxChar *src, size_t n) return ret; } -WXDLLEXPORT wxChar * wxStrpbrk(const wxChar *s, const wxChar *accept) +WXDLLEXPORT const wxChar * wxStrpbrk(const wxChar *s, const wxChar *accept) { - while (*s && !wxStrchr(accept, *s)) s++; - return (*s) ? (wxChar *)s : (wxChar *)NULL; + while (*s && !wxStrchr(accept, *s)) + s++; + + return *s ? s : NULL; } -WXDLLEXPORT wxChar * wxStrrchr(const wxChar *s, wxChar c) +WXDLLEXPORT const wxChar * wxStrrchr(const wxChar *s, wxChar c) { - wxChar *ret = (wxChar *)NULL; - while (*s) { - if (*s == c) ret = (wxChar *)s; - s++; - } - return ret; + 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) @@ -290,14 +297,23 @@ WXDLLEXPORT size_t wxStrspn(const wxChar *s, const wxChar *accept) return len; } -WXDLLEXPORT wxChar * wxStrstr(const wxChar *haystack, const wxChar *needle) +WXDLLEXPORT const wxChar *wxStrstr(const wxChar *haystack, const wxChar *needle) { - wxChar *fnd; - while ((fnd = wxStrchr(haystack, *needle))) { - if (!wxStrcmp(fnd, needle)) return fnd; - haystack = fnd + 1; - } - 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) @@ -319,7 +335,7 @@ WXDLLEXPORT double wxStrtod(const wxChar *nptr, wxChar **endptr) } wxString data(nptr, nptr-start); - wxWX2MBbuf dat = data.mb_str(wxConvLibc); + wxWX2MBbuf dat = data.mb_str(wxConvLocal); char *rdat = wxMBSTRINGCAST dat; double ret = strtod(dat, &rdat); @@ -347,7 +363,7 @@ WXDLLEXPORT long int wxStrtol(const wxChar *nptr, wxChar **endptr, int base) (wxIsalpha(*nptr) && (wxToupper(*nptr) - wxT('A') + 10 < base))) nptr++; wxString data(nptr, nptr-start); - wxWX2MBbuf dat = data.mb_str(wxConvLibc); + wxWX2MBbuf dat = data.mb_str(wxConvLocal); char *rdat = wxMBSTRINGCAST dat; long int ret = strtol(dat, &rdat, base); @@ -360,133 +376,78 @@ WXDLLEXPORT long int wxStrtol(const wxChar *nptr, wxChar **endptr, int base) #ifdef wxNEED_WX_STDIO_H WXDLLEXPORT FILE * wxFopen(const wxChar *path, const wxChar *mode) { - return fopen(wxConvFile.cWX2MB(path), wxConvLibc.cWX2MB(mode)); + char mode_buffer[10]; + for (size_t i = 0; i < wxStrlen(mode)+1; i++) + mode_buffer[i] = (char) mode[i]; + + return fopen( wxConvFile.cWX2MB(path), mode_buffer ); } WXDLLEXPORT FILE * wxFreopen(const wxChar *path, const wxChar *mode, FILE *stream) { - return freopen(wxConvFile.cWX2MB(path), wxConvLibc.cWX2MB(mode), stream); + char mode_buffer[10]; + for (size_t i = 0; i < wxStrlen(mode)+1; i++) + mode_buffer[i] = (char) mode[i]; + + return freopen( wxConvFile.cWX2MB(path), mode_buffer, stream ); } WXDLLEXPORT int wxRemove(const wxChar *path) { - return remove(wxConvFile.cWX2MB(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; - int ret; - - va_start(argptr, fmt); - ret = wxVprintf(fmt, argptr); - va_end(argptr); - return ret; -} - -int WXDLLEXPORT wxVprintf(const wxChar *fmt, va_list argptr) -{ - wxString str; - str.PrintfV(fmt,argptr); - printf("%s", (const char*)str.mb_str()); - return str.Len(); -} - -int WXDLLEXPORT wxFprintf(FILE *stream, const wxChar *fmt, ...) -{ - va_list argptr; - int ret; - - va_start(argptr, fmt); - ret = wxVfprintf(stream, fmt, argptr); - va_end(argptr); - return ret; -} - -int WXDLLEXPORT wxVfprintf(FILE *stream, const wxChar *fmt, va_list argptr) -{ - wxString str; - str.PrintfV(fmt,argptr); - fprintf(stream, "%s", (const char*)str.mb_str()); - return str.Len(); -} - -int WXDLLEXPORT wxSprintf(wxChar *buf, const wxChar *fmt, ...) -{ - va_list argptr; - int ret; - - va_start(argptr, fmt); - ret = wxVsprintf(buf, fmt, argptr); - va_end(argptr); - return ret; -} - -int WXDLLEXPORT wxVsprintf(wxChar *buf, const wxChar *fmt, va_list argptr) -{ - // this might be sort of inefficient, but it doesn't matter since - // we'd prefer people to use wxString::Printf directly instead anyway - wxString str; - str.PrintfV(fmt,argptr); - wxStrcpy(buf,str.c_str()); - return str.Len(); -} - -int WXDLLEXPORT wxSscanf(const wxChar *buf, const wxChar *fmt, ...) -{ - va_list argptr; - int ret; - - va_start(argptr, fmt); - ret = wxVsscanf(buf, fmt, argptr); - va_end(argptr); - return ret; -} - -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(wxConvLibc.cWX2MB(buf), wxConvLibc.cWX2MB(fmt), argptr); - return ret; + return rename( wxConvFile.cWX2MB(oldpath), wxConvFile.cWX2MB(newpath) ); } #endif #ifndef wxAtof double WXDLLEXPORT wxAtof(const wxChar *psz) { - return atof(wxConvLibc.cWX2MB(psz)); + return atof(wxConvLocal.cWX2MB(psz)); } #endif #ifdef wxNEED_WX_STDLIB_H int WXDLLEXPORT wxAtoi(const wxChar *psz) { - return atoi(wxConvLibc.cWX2MB(psz)); + return atoi(wxConvLocal.cWX2MB(psz)); } long WXDLLEXPORT wxAtol(const wxChar *psz) { - return atol(wxConvLibc.cWX2MB(psz)); + return atol(wxConvLocal.cWX2MB(psz)); } wxChar * WXDLLEXPORT wxGetenv(const wxChar *name) { static wxHashTable env; + // check if we already have stored the converted env var wxObject *data = env.Get(name); - if (!data) { + if (!data) + { // nope, retrieve it, - const char *val = getenv(wxConvLibc.cWX2MB(name)); +#if wxUSE_UNICODE + wxCharBuffer buffer = wxConvLocal.cWX2MB(name); + // printf( "buffer %s\n", (const char*) buffer ); + const char *val = getenv( (const char *)buffer ); +#else + const char *val = getenv( name ); +#endif + if (!val) return (wxChar *)NULL; + // printf( "home %s\n", val ); + // convert it, +#ifdef wxUSE_UNICODE + data = (wxObject *)new wxString(val, wxConvLocal); +#else data = (wxObject *)new wxString(val); +#endif + // and store it env.Put(name, data); } @@ -494,9 +455,9 @@ wxChar * WXDLLEXPORT wxGetenv(const wxChar *name) return (wxChar *)((wxString *)data)->c_str(); } -int WXDLLEXPORT wxSystem(const wxChar *psz) +int WXDLLEXPORT wxSystem(const wxChar *psz) { - return system(wxConvLibc.cWX2MB(psz)); + return system(wxConvLocal.cWX2MB(psz)); } #endif @@ -504,17 +465,21 @@ int WXDLLEXPORT wxSystem(const wxChar *psz) #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; + if (!max) return 0; + + char *buf = (char *)malloc(max); + size_t ret = strftime(buf, max, wxConvLocal.cWX2MB(fmt), tm); + if (ret) + { + wxStrcpy(s, wxConvLocal.cMB2WX(buf)); + free(buf); + return wxStrlen(s); + } + else + { + free(buf); + *s = 0; + return 0; } } #endif