X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/bdcb213757b966746ffc73bf01ec2e838d867cd3..508b10810389bea3ecf7e09389aa1c3fca199a15:/src/common/wxcrt.cpp diff --git a/src/common/wxcrt.cpp b/src/common/wxcrt.cpp index 2b862a043a..197fe9597d 100644 --- a/src/common/wxcrt.cpp +++ b/src/common/wxcrt.cpp @@ -43,6 +43,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) @@ -57,8 +61,15 @@ namespace std {} using namespace std ; #endif +#if defined(__DARWIN__) + #include "wx/mac/corefoundation/cfref.h" + #include + #include "wx/mac/corefoundation/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 @@ -91,7 +102,7 @@ size_t WXDLLEXPORT wxMB2WC(wchar_t *buf, const char *psz, size_t n) #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; @@ -119,7 +130,7 @@ size_t WXDLLEXPORT wxWC2MB(char *buf, const wchar_t *pwz, size_t n) } #endif // wxUSE_WCHAR_T -bool WXDLLEXPORT wxOKlibc() +WXDLLIMPEXP_BASE bool wxOKlibc() { #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__) // glibc 2.0 uses UTF-8 even when it shouldn't @@ -142,13 +153,50 @@ bool WXDLLEXPORT wxOKlibc() char* wxSetlocale(int category, const char *locale) { +#ifdef __WXWINCE__ + // FIXME-CE: there is no setlocale() in CE CRT, use SetThreadLocale()? + wxUnusedVar(category); + wxUnusedVar(locale); + + return NULL; +#else // !__WXWINCE__ +#ifdef __WXMAC__ + char *rv = NULL ; + if ( locale != NULL && locale[0] == 0 ) + { + locale_t lt = newlocale(LC_ALL_MASK, "", NULL); + if ( lt ) + { + rv = (char*) querylocale( LC_ALL_MASK, lt ); + freelocale(lt); + } + if ( rv == NULL || rv[0] == 0 || strcmp( rv , "C" ) == 0 || strcmp( rv, "POSIX" ) == 0 ) + { + // 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, rv); + } + } + else + rv = setlocale(category, locale); +#else char *rv = setlocale(category, locale); +#endif if ( locale != NULL /* setting locale, not querying */ && rv /* call was successful */ ) { wxUpdateLocaleIsUtf8(); } return rv; +#endif // __WXWINCE__/!__WXWINCE__ } // ============================================================================ @@ -359,7 +407,7 @@ int wxCRT_ScanfW(const wchar_t *format, ...) #else int ret = vwscanf(format, argptr); #endif - + va_end(argptr); return ret; @@ -552,7 +600,7 @@ namespace { #if !wxUSE_UTF8_LOCALE_ONLY -int wxInternalConvertStringToBuf(const wxString& s, char *out, size_t outsize) +int ConvertStringToBuf(const wxString& s, char *out, size_t outsize) { const wxWX2WCbuf buf = s.wc_str(); @@ -565,13 +613,19 @@ int wxInternalConvertStringToBuf(const wxString& s, char *out, size_t outsize) #endif // !wxUSE_UTF8_LOCALE_ONLY #if wxUSE_UNICODE_UTF8 -int wxInternalConvertStringToBuf(const wxString& s, wchar_t *out, size_t outsize) +int ConvertStringToBuf(const wxString& s, wchar_t *out, size_t outsize) { const wxWX2WCbuf buf(s.wc_str()); - size_t len = wxWcslen(buf); + size_t len = s.length(); // same as buf length for wchar_t* if ( outsize > len ) + { memcpy(out, buf, (len+1) * sizeof(wchar_t)); - // else: not enough space + } + else // not enough space + { + memcpy(out, buf, (outsize-1) * sizeof(wchar_t)); + out[outsize-1] = 0; + } return len; } #endif // wxUSE_UNICODE_UTF8 @@ -585,7 +639,7 @@ static size_t PrintfViaString(T *out, size_t outsize, wxString s; s.PrintfV(format, argptr); - return wxInternalConvertStringToBuf(s, out, outsize); + return ConvertStringToBuf(s, out, outsize); } #endif // wxUSE_UNICODE @@ -611,7 +665,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( _T("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 ) @@ -690,7 +757,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) { @@ -712,7 +779,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) { @@ -737,14 +804,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); @@ -754,7 +821,7 @@ WXDLLEXPORT wchar_t * wxCRT_StrdupW(const wchar_t *pwz) #endif // wxCRT_StrdupW #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 { @@ -766,7 +833,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 { @@ -778,7 +845,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; @@ -792,7 +859,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; @@ -812,7 +879,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++ ) @@ -827,7 +894,7 @@ extern "C" WXDLLEXPORT size_t wxCRT_StrlenW(const wchar_t *s) // ---------------------------------------------------------------------------- #ifndef wxCRT_GetenvW -wchar_t* WXDLLEXPORT 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 @@ -839,7 +906,7 @@ wchar_t* WXDLLEXPORT wxCRT_GetenvW(const wchar_t *name) #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 ) @@ -877,7 +944,7 @@ wxCRT_StrtoullBase(const T* nptr, T** endptr, int base, T* sign) wxString::const_iterator end = wxstr.end(); // Skip spaces - while ( i != end && wxIsspace(*i) ) i++; + while ( i != end && wxIsspace(*i) ) ++i; // Starts with sign? *sign = wxT(' '); @@ -887,20 +954,20 @@ wxCRT_StrtoullBase(const T* nptr, T** endptr, int base, T* sign) if ( c == wxT('+') || c == wxT('-') ) { *sign = c; - i++; + ++i; } } // Starts with 0x? if ( i != end && *i == wxT('0') ) { - i++; + ++i; if ( i != end ) { if ( *i == wxT('x') && (base == 16 || base == 0) ) { base = 16; - i++; + ++i; } else { @@ -911,20 +978,20 @@ wxCRT_StrtoullBase(const T* nptr, T** endptr, int base, T* sign) } } else - i--; + --i; } if ( base == 0 ) base = 10; - for ( ; i != end; i++ ) + for ( ; i != end; ++i ) { 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; @@ -958,7 +1025,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('-') ) { @@ -973,7 +1040,7 @@ 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('-') ) @@ -1093,31 +1160,6 @@ void *calloc( size_t num, size_t size ) #endif // __WXWINCE__ <= 211 -#ifdef __WXWINCE__ -int wxCRT_RemoveW(const wchar_t *path) -{ - return ::DeleteFile(path) == 0; -} -#endif - -#ifndef wxCRT_TmpnamW -wchar_t *wxCRT_TmpnamW(wchar_t *s) -{ - // tmpnam_r() returns NULL if s=NULL, do the same - wxCHECK_MSG( s, NULL, "wxTmpnam must be called with a buffer" ); - -#ifndef L_tmpnam - #define L_tmpnam 1024 -#endif - wxCharBuffer buf(L_tmpnam); - tmpnam(buf.data()); - - wxConvLibc.ToWChar(s, L_tmpnam+1, buf.data()); - return s; -} -#endif // !wxCRT_TmpnamW - - // ============================================================================ // wxLocaleIsUtf8 // ============================================================================ @@ -1150,7 +1192,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: @@ -1218,6 +1260,8 @@ int wxFputc(const wxUniChar& c, FILE *stream) #endif } +#ifdef wxCRT_PerrorA + void wxPerror(const wxString& s) { #ifdef wxCRT_PerrorW @@ -1229,6 +1273,8 @@ void wxPerror(const wxString& s) #endif } +#endif // wxCRT_PerrorA + wchar_t *wxFgets(wchar_t *s, int size, FILE *stream) { wxCHECK_MSG( s, NULL, "empty buffer passed to wxFgets()" ); @@ -1250,7 +1296,7 @@ wchar_t *wxFgets(wchar_t *s, int size, FILE *stream) // wxScanf() and friends // ---------------------------------------------------------------------------- -#ifndef __VISUALC__ +#ifndef HAVE_NO_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) @@ -1267,4 +1313,4 @@ int wxVsscanf(const wxCStrData& str, const char *format, va_list ap) { return wxCRT_VsscanfA(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