X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b2dd2f28f3baf9c6db0dfb90e69c3a8f489737bc..62795f413a7222863b4aee76c08764071f94bd87:/src/common/wxcrt.cpp?ds=inline diff --git a/src/common/wxcrt.cpp b/src/common/wxcrt.cpp index a1d235bcfc..d46c08bce4 100644 --- a/src/common/wxcrt.cpp +++ b/src/common/wxcrt.cpp @@ -4,7 +4,6 @@ // Author: Ove Kaven // Modified by: Ron Lee, Francesco Montorsi // Created: 09/04/99 -// RCS-ID: $Id$ // Copyright: (c) wxWidgets copyright // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -21,6 +20,7 @@ #endif #include "wx/crt.h" +#include "wx/strconv.h" // wxMBConv::cWC2MB() #define _ISOC9X_SOURCE 1 // to get vsscanf() #define _BSD_SOURCE 1 // to still get strdup() @@ -28,6 +28,16 @@ #include #include #include +#include + +#ifdef __SGI__ + // wide character functions are declared in std namespace under IRIX + using namespace std; + + // and this one is only declared if __c99 is defined which is not the case + // for C++ builds, so declare it ourselves + extern "C" int vswscanf(const wchar_t *, const wchar_t *, va_list); +#endif #ifndef __WXWINCE__ #include @@ -43,6 +53,10 @@ #include "wx/log.h" #endif +#ifdef HAVE_LANGINFO_H + #include +#endif + #ifdef __WXWINCE__ // there is no errno.h under CE apparently #define wxSET_ERRNO(value) @@ -52,13 +66,14 @@ #define wxSET_ERRNO(value) errno = value #endif -#if defined(__MWERKS__) && __MSL__ >= 0x6000 -namespace std {} -using namespace std ; +#if defined(__DARWIN__) + #include "wx/osx/core/cfref.h" + #include + #include "wx/osx/core/cfstring.h" + #include #endif -#if wxUSE_WCHAR_T -size_t WXDLLEXPORT wxMB2WC(wchar_t *buf, const char *psz, size_t n) +WXDLLIMPEXP_BASE size_t wxMB2WC(wchar_t *buf, const char *psz, size_t n) { // assume that we have mbsrtowcs() too if we have wcsrtombs() #ifdef HAVE_WCSRTOMBS @@ -78,20 +93,20 @@ size_t WXDLLEXPORT wxMB2WC(wchar_t *buf, const char *psz, size_t n) #endif } - // note that we rely on common (and required by Unix98 but unfortunately not + // Note that we rely on common (and required by Unix98 but unfortunately not // C99) extension which allows to call mbs(r)towcs() with NULL output pointer // to just get the size of the needed buffer -- this is needed as otherwise - // we have no idea about how much space we need and if the CRT doesn't - // support it (the only currently known example being Metrowerks, see - // wx/crt.h) we don't use its mbstowcs() at all + // we have no idea about how much space we need. Currently all supported + // compilers do provide it and if they don't, HAVE_WCSRTOMBS shouldn't be + // defined at all. #ifdef HAVE_WCSRTOMBS - return mbsrtowcs((wchar_t *) NULL, &psz, 0, &mbstate); + return mbsrtowcs(NULL, &psz, 0, &mbstate); #else - return wxMbstowcs((wchar_t *) NULL, psz, 0); + return wxMbstowcs(NULL, psz, 0); #endif } -size_t WXDLLEXPORT wxWC2MB(char *buf, const wchar_t *pwz, size_t n) +WXDLLIMPEXP_BASE size_t wxWC2MB(char *buf, const wchar_t *pwz, size_t n) { #ifdef HAVE_WCSRTOMBS mbstate_t mbstate; @@ -112,32 +127,10 @@ size_t WXDLLEXPORT wxWC2MB(char *buf, const wchar_t *pwz, size_t n) } #ifdef HAVE_WCSRTOMBS - return wcsrtombs((char *) NULL, &pwz, 0, &mbstate); + return wcsrtombs(NULL, &pwz, 0, &mbstate); #else - return wxWcstombs((char *) NULL, pwz, 0); -#endif -} -#endif // wxUSE_WCHAR_T - -bool WXDLLEXPORT wxOKlibc() -{ -#if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__) - // 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; - } - } + return wxWcstombs(NULL, pwz, 0); #endif - return true; } char* wxSetlocale(int category, const char *locale) @@ -149,7 +142,27 @@ char* wxSetlocale(int category, const char *locale) return NULL; #else // !__WXWINCE__ +#ifdef __WXMAC__ + char *rv = NULL ; + if ( locale != NULL && locale[0] == 0 ) + { + // the attempt to use newlocale(LC_ALL_MASK, "", NULL); + // here in order to deduce the language along the environment vars rules + // lead to strange crashes later... + + // we have to emulate the behaviour under OS X + wxCFRef userLocaleRef(CFLocaleCopyCurrent()); + wxCFStringRef str(wxCFRetain((CFStringRef)CFLocaleGetValue(userLocaleRef, kCFLocaleLanguageCode))); + wxString langFull = str.AsString()+"_"; + str.reset(wxCFRetain((CFStringRef)CFLocaleGetValue(userLocaleRef, kCFLocaleCountryCode))); + langFull += str.AsString(); + rv = setlocale(category, langFull.c_str()); + } + else + rv = setlocale(category, locale); +#else char *rv = setlocale(category, locale); +#endif if ( locale != NULL /* setting locale, not querying */ && rv /* call was successful */ ) { @@ -242,29 +255,14 @@ int /* not wint_t */ wxCRT_FputcW(wchar_t wc, FILE *stream) // TODO: implement the scanf() functions static int vwscanf(const wchar_t *format, va_list argptr) { - wxFAIL_MSG( _T("TODO") ); + wxFAIL_MSG( wxT("TODO") ); return -1; } -static int vswscanf(const wchar_t *ws, const wchar_t *format, va_list argptr) -{ - // The best we can do without proper Unicode support in glibc is to - // convert the strings into MB representation and run ANSI version - // of the function. This doesn't work with %c and %s because of difference - // in size of char and wchar_t, though. - - wxCHECK_MSG( wxStrstr(format, _T("%s")) == NULL, -1, - _T("incomplete vswscanf implementation doesn't allow %s") ); - wxCHECK_MSG( wxStrstr(format, _T("%c")) == NULL, -1, - _T("incomplete vswscanf implementation doesn't allow %c") ); - - return vsscanf(wxConvLibc.cWX2MB(ws), wxConvLibc.cWX2MB(format), argptr); -} - static int vfwscanf(FILE *stream, const wchar_t *format, va_list argptr) { - wxFAIL_MSG( _T("TODO") ); + wxFAIL_MSG( wxT("TODO") ); return -1; } @@ -293,6 +291,24 @@ static int vwprintf(const wchar_t *format, va_list argptr) #endif // wxNEED_WPRINTF +#ifdef wxNEED_VSWSCANF +static int vswscanf(const wchar_t *ws, const wchar_t *format, va_list argptr) +{ + // The best we can do without proper Unicode support in glibc is to + // convert the strings into MB representation and run ANSI version + // of the function. This doesn't work with %c and %s because of difference + // in size of char and wchar_t, though. + + wxCHECK_MSG( wxStrstr(format, wxT("%s")) == NULL, -1, + wxT("incomplete vswscanf implementation doesn't allow %s") ); + wxCHECK_MSG( wxStrstr(format, wxT("%c")) == NULL, -1, + wxT("incomplete vswscanf implementation doesn't allow %c") ); + + return vsscanf(static_cast(wxConvLibc.cWX2MB(ws)), + wxConvLibc.cWX2MB(format), argptr); +} +#endif + // ---------------------------------------------------------------------------- // wxPrintf(), wxScanf() and relatives // ---------------------------------------------------------------------------- @@ -562,13 +578,20 @@ namespace #if !wxUSE_UTF8_LOCALE_ONLY int ConvertStringToBuf(const wxString& s, char *out, size_t outsize) { - const wxWX2WCbuf buf = s.wc_str(); + const wxCharBuffer buf(s.mb_str()); - size_t len = wxConvLibc.FromWChar(out, outsize, buf); - if ( len != wxCONV_FAILED ) - return len-1; - else - return wxConvLibc.FromWChar(NULL, 0, buf); + const size_t len = buf.length(); + if ( outsize > len ) + { + memcpy(out, buf, len+1); + } + else // not enough space + { + memcpy(out, buf, outsize-1); + out[outsize-1] = '\0'; + } + + return len; } #endif // !wxUSE_UTF8_LOCALE_ONLY @@ -625,7 +648,20 @@ int wxVsprintf(char *str, const wxString& format, va_list argptr) int wxVsprintf(wchar_t *str, const wxString& format, va_list argptr) { #if wxUSE_UNICODE_WCHAR +#ifdef __DMC__ +/* +This fails with a bug similar to +http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=c++.beta&artnum=680 +in DMC 8.49 and 8.50 +I don't see it being used in the wxWidgets sources at present (oct 2007) CE +*/ +#pragma message ( "warning ::::: wxVsprintf(wchar_t *str, const wxString& format, va_list argptr) not yet implemented" ) + wxFAIL_MSG( wxT("TODO") ); + + return -1; +#else return wxCRT_VsprintfW(str, format.wc_str(), argptr); +#endif //DMC #else // wxUSE_UNICODE_UTF8 #if !wxUSE_UTF8_LOCALE_ONLY if ( !wxLocaleIsUtf8 ) @@ -696,7 +732,6 @@ int wxVsnprintf(wchar_t *str, size_t size, const wxString& format, va_list argpt } #endif // wxUSE_UNICODE -#if wxUSE_WCHAR_T // ---------------------------------------------------------------------------- // ctype.h stuff (currently unused) @@ -704,7 +739,7 @@ int wxVsnprintf(wchar_t *str, size_t size, const wxString& format, va_list argpt #ifdef wxNEED_WX_MBSTOWCS -WXDLLEXPORT size_t wxMbstowcs (wchar_t * out, const char * in, size_t outlen) +WXDLLIMPEXP_BASE size_t wxMbstowcs (wchar_t * out, const char * in, size_t outlen) { if (!out) { @@ -726,7 +761,7 @@ WXDLLEXPORT size_t wxMbstowcs (wchar_t * out, const char * in, size_t outlen) return in - origin; } -WXDLLEXPORT size_t wxWcstombs (char * out, const wchar_t * in, size_t outlen) +WXDLLIMPEXP_BASE size_t wxWcstombs (char * out, const wchar_t * in, size_t outlen) { if (!out) { @@ -751,14 +786,14 @@ WXDLLEXPORT size_t wxWcstombs (char * out, const wchar_t * in, size_t outlen) #endif // wxNEED_WX_MBSTOWCS #ifndef wxCRT_StrdupA -WXDLLEXPORT char *wxCRT_StrdupA(const char *s) +WXDLLIMPEXP_BASE char *wxCRT_StrdupA(const char *s) { return strcpy((char *)malloc(strlen(s) + 1), s); } #endif // wxCRT_StrdupA #ifndef wxCRT_StrdupW -WXDLLEXPORT wchar_t * wxCRT_StrdupW(const wchar_t *pwz) +WXDLLIMPEXP_BASE wchar_t * wxCRT_StrdupW(const wchar_t *pwz) { size_t size = (wxWcslen(pwz) + 1) * sizeof(wchar_t); wchar_t *ret = (wchar_t *) malloc(size); @@ -767,8 +802,45 @@ WXDLLEXPORT wchar_t * wxCRT_StrdupW(const wchar_t *pwz) } #endif // wxCRT_StrdupW +#ifndef wxWCHAR_T_IS_WXCHAR16 +size_t wxStrlen(const wxChar16 *s ) +{ + if (!s) return 0; + size_t i=0; + while (*s!=0) { ++i; ++s; }; + return i; +} + +wxChar16* wxStrdup(const wxChar16* s) +{ + size_t size = (wxStrlen(s) + 1) * sizeof(wxChar16); + wxChar16 *ret = (wxChar16*) malloc(size); + memcpy(ret, s, size); + return ret; +} +#endif + +#ifndef wxWCHAR_T_IS_WXCHAR32 +size_t wxStrlen(const wxChar32 *s ) +{ + if (!s) return 0; + size_t i=0; + while (*s!=0) { ++i; ++s; }; + return i; +} + +wxChar32* wxStrdup(const wxChar32* s) +{ + size_t size = (wxStrlen(s) + 1) * sizeof(wxChar32); + wxChar32 *ret = (wxChar32*) malloc(size); + if ( ret ) + memcpy(ret, s, size); + return ret; +} +#endif + #ifndef wxCRT_StricmpA -int WXDLLEXPORT wxCRT_StricmpA(const char *psz1, const char *psz2) +WXDLLIMPEXP_BASE int wxCRT_StricmpA(const char *psz1, const char *psz2) { register char c1, c2; do { @@ -780,7 +852,7 @@ int WXDLLEXPORT wxCRT_StricmpA(const char *psz1, const char *psz2) #endif // !defined(wxCRT_StricmpA) #ifndef wxCRT_StricmpW -int WXDLLEXPORT wxCRT_StricmpW(const wchar_t *psz1, const wchar_t *psz2) +WXDLLIMPEXP_BASE int wxCRT_StricmpW(const wchar_t *psz1, const wchar_t *psz2) { register wchar_t c1, c2; do { @@ -792,7 +864,7 @@ int WXDLLEXPORT wxCRT_StricmpW(const wchar_t *psz1, const wchar_t *psz2) #endif // !defined(wxCRT_StricmpW) #ifndef wxCRT_StrnicmpA -int WXDLLEXPORT wxCRT_StrnicmpA(const char *s1, const char *s2, size_t n) +WXDLLIMPEXP_BASE int wxCRT_StrnicmpA(const char *s1, const char *s2, size_t n) { // initialize the variables just to suppress stupid gcc warning register char c1 = 0, c2 = 0; @@ -806,7 +878,7 @@ int WXDLLEXPORT wxCRT_StrnicmpA(const char *s1, const char *s2, size_t n) #endif // !defined(wxCRT_StrnicmpA) #ifndef wxCRT_StrnicmpW -int WXDLLEXPORT wxCRT_StrnicmpW(const wchar_t *s1, const wchar_t *s2, size_t n) +WXDLLIMPEXP_BASE int wxCRT_StrnicmpW(const wchar_t *s1, const wchar_t *s2, size_t n) { // initialize the variables just to suppress stupid gcc warning register wchar_t c1 = 0, c2 = 0; @@ -826,7 +898,7 @@ int WXDLLEXPORT wxCRT_StrnicmpW(const wchar_t *s1, const wchar_t *s2, size_t n) // this (and wxCRT_StrncmpW below) are extern "C" because they are needed // by regex code, the rest isn't needed, so it's not declared as extern "C" #ifndef wxCRT_StrlenW -extern "C" WXDLLEXPORT size_t wxCRT_StrlenW(const wchar_t *s) +extern "C" WXDLLIMPEXP_BASE size_t wxCRT_StrlenW(const wchar_t *s) { size_t n = 0; while ( *s++ ) @@ -841,19 +913,19 @@ extern "C" WXDLLEXPORT size_t wxCRT_StrlenW(const wchar_t *s) // ---------------------------------------------------------------------------- #ifndef wxCRT_GetenvW -WXDLLEXPORT wchar_t* wxCRT_GetenvW(const wchar_t *name) +WXDLLIMPEXP_BASE wchar_t* wxCRT_GetenvW(const wchar_t *name) { // NB: buffer returned by getenv() is allowed to be overwritten next // time getenv() is called, so it is OK to use static string // buffer to hold the data. - static wxWCharBuffer value((wchar_t*)NULL); + static wxWCharBuffer value; value = wxConvLibc.cMB2WC(getenv(wxConvLibc.cWC2MB(name))); return value.data(); } #endif // !wxCRT_GetenvW #ifndef wxCRT_StrftimeW -WXDLLEXPORT size_t +WXDLLIMPEXP_BASE size_t wxCRT_StrftimeW(wchar_t *s, size_t maxsize, const wchar_t *fmt, const struct tm *tm) { if ( !maxsize ) @@ -878,8 +950,6 @@ wxCRT_StrftimeW(wchar_t *s, size_t maxsize, const wchar_t *fmt, const struct tm } #endif // !wxCRT_StrftimeW -#endif // wxUSE_WCHAR_T - #ifdef wxLongLong_t template static wxULongLong_t @@ -905,23 +975,35 @@ wxCRT_StrtoullBase(const T* nptr, T** endptr, int base, T* sign) } } - // Starts with 0x? + // Starts with octal or hexadecimal prefix? if ( i != end && *i == wxT('0') ) { ++i; if ( i != end ) { - if ( *i == wxT('x') && (base == 16 || base == 0) ) + if ( (*i == wxT('x')) || (*i == wxT('X')) ) { - base = 16; - ++i; + // Hexadecimal prefix: use base 16 if auto-detecting. + if ( base == 0 ) + base = 16; + + // If we do use base 16, just skip "x" as well. + if ( base == 16 ) + { + ++i; + } + else // Not using base 16 + { + // Then it's an error. + if ( endptr ) + *endptr = (T*) nptr; + wxSET_ERRNO(EINVAL); + return sum; + } } - else + else if ( base == 0 ) { - if ( endptr ) - *endptr = (T*) nptr; - wxSET_ERRNO(EINVAL); - return sum; + base = 8; } } else @@ -936,9 +1018,9 @@ wxCRT_StrtoullBase(const T* nptr, T** endptr, int base, T* sign) unsigned int n; T c = *i; - if ( c >= wxT('0') ) + if ( c >= '0' ) { - if ( c <= wxT('9') ) + if ( c <= '9' ) n = c - wxT('0'); else n = wxTolower(c) - wxT('a') + 10; @@ -972,7 +1054,7 @@ template static wxULongLong_t wxCRT_DoStrtoull(const T* nptr, T** endptr, int base) { T sign; - wxULongLong_t uval = wxCRT_StrtoullBase(nptr, endptr, base, &sign); + wxULongLong_t uval = ::wxCRT_StrtoullBase(nptr, endptr, base, &sign); if ( sign == wxT('-') ) { @@ -987,17 +1069,14 @@ template static wxLongLong_t wxCRT_DoStrtoll(const T* nptr, T** endptr, int base) { T sign; - wxULongLong_t uval = wxCRT_StrtoullBase(nptr, endptr, base, &sign); + wxULongLong_t uval = ::wxCRT_StrtoullBase(nptr, endptr, base, &sign); wxLongLong_t val = 0; if ( sign == wxT('-') ) { - if ( uval <= wxULL(wxINT64_MAX+1) ) + if (uval <= (wxULongLong_t)wxINT64_MAX + 1) { - if ( uval == wxULL(wxINT64_MAX+1)) - val = -((wxLongLong_t)wxINT64_MAX) - 1; - else - val = -((wxLongLong_t)uval); + val = -(wxLongLong_t)uval; } else { @@ -1037,7 +1116,7 @@ wxULongLong_t wxCRT_StrtoullW(const wchar_t* nptr, wchar_t** endptr, int base) #endif // wxLongLong_t // ---------------------------------------------------------------------------- -// functions which we may need even if !wxUSE_WCHAR_T +// strtok() functions // ---------------------------------------------------------------------------- template @@ -1053,15 +1132,15 @@ static T *wxCRT_DoStrtok(T *psz, const T *delim, T **save_ptr) psz += wxStrspn(psz, delim); if (!*psz) { - *save_ptr = (T *)NULL; - return (T *)NULL; + *save_ptr = NULL; + return NULL; } T *ret = psz; psz = wxStrpbrk(psz, delim); if (!psz) { - *save_ptr = (T*)NULL; + *save_ptr = NULL; } else { @@ -1139,7 +1218,7 @@ static bool wxIsLocaleUtf8() return true; } } -#endif +#endif // HAVE_LANGINFO_H // check if we're running under the "C" locale: it is 7bit subset // of UTF-8, so it can be safely used with the UTF-8 build: @@ -1160,7 +1239,7 @@ void wxUpdateLocaleIsUtf8() #if wxUSE_UTF8_LOCALE_ONLY if ( !wxIsLocaleUtf8() ) { - wxLogFatalError(_T("This program requires UTF-8 locale to run.")); + wxLogFatalError(wxT("This program requires UTF-8 locale to run.")); } #else // !wxUSE_UTF8_LOCALE_ONLY wxLocaleIsUtf8 = wxIsLocaleUtf8(); @@ -1184,9 +1263,11 @@ void wxUpdateLocaleIsUtf8() int wxPuts(const wxString& s) { + // under IRIX putws() takes a non-const argument so use wchar_str() instead + // of wc_str() CALL_ANSI_OR_UNICODE(return, wxCRT_PutsA(s.mb_str()), - wxCRT_PutsW(s.wc_str())); + wxCRT_PutsW(s.wchar_str())); } int wxFputs(const wxString& s, FILE *stream) @@ -1243,21 +1324,21 @@ wchar_t *wxFgets(wchar_t *s, int size, FILE *stream) // wxScanf() and friends // ---------------------------------------------------------------------------- -#ifndef __VISUALC__ +#ifdef HAVE_VSSCANF // __VISUALC__ and __DMC__ see wx/crt.h int wxVsscanf(const char *str, const char *format, va_list ap) { return wxCRT_VsscanfA(str, format, ap); } int wxVsscanf(const wchar_t *str, const wchar_t *format, va_list ap) { return wxCRT_VsscanfW(str, format, ap); } int wxVsscanf(const wxCharBuffer& str, const char *format, va_list ap) - { return wxCRT_VsscanfA(str, format, ap); } + { return wxCRT_VsscanfA(static_cast(str), format, ap); } int wxVsscanf(const wxWCharBuffer& str, const wchar_t *format, va_list ap) { return wxCRT_VsscanfW(str, format, ap); } int wxVsscanf(const wxString& str, const char *format, va_list ap) - { return wxCRT_VsscanfA(str.mb_str(), format, ap); } + { return wxCRT_VsscanfA(static_cast(str.mb_str()), format, ap); } int wxVsscanf(const wxString& str, const wchar_t *format, va_list ap) { return wxCRT_VsscanfW(str.wc_str(), format, ap); } int wxVsscanf(const wxCStrData& str, const char *format, va_list ap) - { return wxCRT_VsscanfA(str.AsCharBuf(), format, ap); } + { return wxCRT_VsscanfA(static_cast(str.AsCharBuf()), format, ap); } int wxVsscanf(const wxCStrData& str, const wchar_t *format, va_list ap) { return wxCRT_VsscanfW(str.AsWCharBuf(), format, ap); } -#endif // !__VISUALC__ +#endif // HAVE_NO_VSSCANF