X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/2523e9b70044baa92a1c63ffdfe179c28ad53536..902b679a455b146555607d6123cf8b9159c39888:/src/common/wxcrt.cpp diff --git a/src/common/wxcrt.cpp b/src/common/wxcrt.cpp index 7a1c9363bb..de9012b6d5 100644 --- a/src/common/wxcrt.cpp +++ b/src/common/wxcrt.cpp @@ -20,7 +20,8 @@ #pragma hdrstop #endif -#include "wx/wxchar.h" +#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 +29,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,31 +54,27 @@ #include "wx/log.h" #endif -#if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H) - #include - #include - #include - #include +#ifdef HAVE_LANGINFO_H + #include #endif -#ifndef wxStrtoll - #ifdef __WXWINCE__ - // there is no errno.h under CE apparently - #define wxSET_ERRNO(value) - #else - #include +#ifdef __WXWINCE__ + // there is no errno.h under CE apparently + #define wxSET_ERRNO(value) +#else + #include - #define wxSET_ERRNO(value) errno = value - #endif + #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 @@ -87,20 +94,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/wxchar.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; @@ -121,32 +128,49 @@ 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); + return wxWcstombs(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; + +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 ) + { + // 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 - return true; + if ( locale != NULL /* setting locale, not querying */ && + rv /* call was successful */ ) + { + wxUpdateLocaleIsUtf8(); + } + return rv; +#endif // __WXWINCE__/!__WXWINCE__ } // ============================================================================ @@ -166,32 +190,28 @@ bool WXDLLEXPORT wxOKlibc() #define wxNEED_WPRINTF - int wxVfprintf( FILE *stream, const wxChar *format, va_list argptr ); + int wxCRT_VfprintfW( FILE *stream, const wchar_t *format, va_list argptr ); #endif #if defined(__DMC__) - /* Digital Mars adds count to _stprintf (C99) so convert */ - #if wxUSE_UNICODE - int wxSprintf (wchar_t * __RESTRICT s, const wchar_t * __RESTRICT format, ... ) - { - va_list arglist; - - va_start( arglist, format ); - int iLen = swprintf ( s, -1, format, arglist ); - va_end( arglist ); - return iLen ; - } - - #endif // wxUSE_UNICODE +/* Digital Mars adds count to _stprintf (C99) so convert */ +int wxCRT_SprintfW (wchar_t * __RESTRICT s, const wchar_t * __RESTRICT format, ... ) +{ + va_list arglist; + va_start( arglist, format ); + int iLen = swprintf ( s, -1, format, arglist ); + va_end( arglist ); + return iLen ; +} #endif //__DMC__ // ---------------------------------------------------------------------------- // implement the standard IO functions for wide char if libc doesn't have them // ---------------------------------------------------------------------------- -#ifdef wxNEED_FPUTS -int wxFputs(const wchar_t *ws, FILE *stream) +#ifndef wxCRT_FputsW +int wxCRT_FputsW(const wchar_t *ws, FILE *stream) { wxCharBuffer buf(wxConvLibc.cWC2MB(ws)); if ( !buf ) @@ -199,17 +219,17 @@ int wxFputs(const wchar_t *ws, FILE *stream) // counting the number of wide characters written isn't worth the trouble, // simply distinguish between ok and error - return fputs(buf, stream) == -1 ? -1 : 0; + return wxCRT_FputsA(buf, stream) == -1 ? -1 : 0; } -#endif // wxNEED_FPUTS +#endif // !wxCRT_FputsW -#ifdef wxNEED_PUTS -int wxPuts(const wxChar *ws) +#ifndef wxCRT_PutsW +int wxCRT_PutsW(const wchar_t *ws) { - int rc = wxFputs(ws, stdout); + int rc = wxCRT_FputsW(ws, stdout); if ( rc != -1 ) { - if ( wxFputs(L"\n", stdout) == -1 ) + if ( wxCRT_FputsW(L"\n", stdout) == -1 ) return -1; rc++; @@ -217,16 +237,16 @@ int wxPuts(const wxChar *ws) return rc; } -#endif // wxNEED_PUTS +#endif // !wxCRT_PutsW -#ifdef wxNEED_PUTC -int /* not wint_t */ wxPutc(wchar_t wc, FILE *stream) +#ifndef wxCRT_FputcW +int /* not wint_t */ wxCRT_FputcW(wchar_t wc, FILE *stream) { wchar_t ws[2] = { wc, L'\0' }; - return wxFputs(ws, stream); + return wxCRT_FputsW(ws, stream); } -#endif // wxNEED_PUTC +#endif // !wxCRT_FputcW // NB: we only implement va_list functions here, the ones taking ... are // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse @@ -234,40 +254,23 @@ int /* not wint_t */ wxPutc(wchar_t wc, FILE *stream) #ifdef wxNEED_WPRINTF // TODO: implement the scanf() functions -int vwscanf(const wxChar *format, va_list argptr) +static int vwscanf(const wchar_t *format, va_list argptr) { - wxFAIL_MSG( _T("TODO") ); + wxFAIL_MSG( wxT("TODO") ); return -1; } -int vswscanf(const wxChar *ws, const wxChar *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") ); - - va_list argcopy; - wxVaCopy(argcopy, argptr); - return vsscanf(wxConvLibc.cWX2MB(ws), wxConvLibc.cWX2MB(format), argcopy); -} - -int vfwscanf(FILE *stream, const wxChar *format, va_list argptr) +static int vfwscanf(FILE *stream, const wchar_t *format, va_list argptr) { - wxFAIL_MSG( _T("TODO") ); + wxFAIL_MSG( wxT("TODO") ); return -1; } -#define vswprintf wxVsnprintf_ +#define vswprintf wxCRT_VsnprintfW -int vfwprintf(FILE *stream, const wxChar *format, va_list argptr) +static int vfwprintf(FILE *stream, const wchar_t *format, va_list argptr) { wxString s; int rc = s.PrintfV(format, argptr); @@ -282,355 +285,261 @@ int vfwprintf(FILE *stream, const wxChar *format, va_list argptr) return rc; } -int vwprintf(const wxChar *format, va_list argptr) +static int vwprintf(const wchar_t *format, va_list argptr) { - return wxVfprintf(stdout, format, argptr); + return wxCRT_VfprintfW(stdout, format, argptr); } #endif // wxNEED_WPRINTF -#ifdef wxNEED_PRINTF_CONVERSION - -// ---------------------------------------------------------------------------- -// wxFormatConverter: class doing the "%s" -> "%ls" conversion -// ---------------------------------------------------------------------------- - -/* - Here are the gory details. We want to follow the Windows/MS conventions, - that is to have - - In ANSI mode: - - format specifier results in - ----------------------------------- - %c, %hc, %hC char - %lc, %C, %lC wchar_t - - In Unicode mode: - - format specifier results in - ----------------------------------- - %hc, %C, %hC char - %c, %lc, %lC wchar_t - - - while on POSIX systems we have %C identical to %lc and %c always means char - (in any mode) while %lc always means wchar_t, - - So to use native functions in order to get our semantics we must do the - following translations in Unicode mode (nothing to do in ANSI mode): - - wxWidgets specifier POSIX specifier - ---------------------------------------- - - %hc, %C, %hC %c - %c %lc - - - And, of course, the same should be done for %s as well. -*/ - -class wxFormatConverter +#ifdef wxNEED_VSWSCANF +static int vswscanf(const wchar_t *ws, const wchar_t *format, va_list argptr) { -public: - wxFormatConverter(const wxChar *format); - - // notice that we only translated the string if m_fmtOrig == NULL (as set - // by CopyAllBefore()), otherwise we should simply use the original format - operator const wxChar *() const - { return m_fmtOrig ? m_fmtOrig : m_fmt.c_str(); } - -private: - // copy another character to the translated format: this function does the - // copy if we are translating but doesn't do anything at all if we don't, - // so we don't create the translated format string at all unless we really - // need to (i.e. InsertFmtChar() is called) - wxChar CopyFmtChar(wxChar ch) - { - if ( !m_fmtOrig ) - { - // we're translating, do copy - m_fmt += ch; - } - else - { - // simply increase the count which should be copied by - // CopyAllBefore() later if needed - m_nCopied++; - } - - return ch; - } - - // insert an extra character - void InsertFmtChar(wxChar ch) - { - if ( m_fmtOrig ) - { - // so far we haven't translated anything yet - CopyAllBefore(); - } - - m_fmt += ch; - } + // 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. - void CopyAllBefore() - { - wxASSERT_MSG( m_fmtOrig && m_fmt.empty(), _T("logic error") ); + 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") ); - m_fmt = wxString(m_fmtOrig, m_nCopied); + return vsscanf(static_cast(wxConvLibc.cWX2MB(ws)), + wxConvLibc.cWX2MB(format), argptr); +} +#endif - // we won't need it any longer - m_fmtOrig = NULL; - } +// ---------------------------------------------------------------------------- +// wxPrintf(), wxScanf() and relatives +// ---------------------------------------------------------------------------- - static bool IsFlagChar(wxChar ch) - { - return ch == _T('-') || ch == _T('+') || - ch == _T('0') || ch == _T(' ') || ch == _T('#'); - } +// FIXME-UTF8: do format conversion using (modified) wxFormatConverter in +// template wrappers, not here; note that it will needed to +// translate all forms of string specifiers to %(l)s for wxPrintf(), +// but it only should do what it did in 2.8 for wxScanf()! - void SkipDigits(const wxChar **ptpc) - { - while ( **ptpc >= _T('0') && **ptpc <= _T('9') ) - CopyFmtChar(*(*ptpc)++); - } +#ifndef wxCRT_PrintfW +int wxCRT_PrintfW( const wchar_t *format, ... ) +{ + va_list argptr; + va_start(argptr, format); - // the translated format - wxString m_fmt; + int ret = vwprintf( format, argptr ); - // the original format - const wxChar *m_fmtOrig; + va_end(argptr); - // the number of characters already copied - size_t m_nCopied; -}; + return ret; +} +#endif -wxFormatConverter::wxFormatConverter(const wxChar *format) +#ifndef wxCRT_FprintfW +int wxCRT_FprintfW( FILE *stream, const wchar_t *format, ... ) { - m_fmtOrig = format; - m_nCopied = 0; - - while ( *format ) - { - if ( CopyFmtChar(*format++) == _T('%') ) - { - // skip any flags - while ( IsFlagChar(*format) ) - CopyFmtChar(*format++); - - // and possible width - if ( *format == _T('*') ) - CopyFmtChar(*format++); - else - SkipDigits(&format); - - // precision? - if ( *format == _T('.') ) - { - CopyFmtChar(*format++); - if ( *format == _T('*') ) - CopyFmtChar(*format++); - else - SkipDigits(&format); - } + va_list argptr; + va_start( argptr, format ); - // next we can have a size modifier - enum - { - Default, - Short, - Long - } size; + int ret = vfwprintf( stream, format, argptr ); - switch ( *format ) - { - case _T('h'): - size = Short; - format++; - break; - - case _T('l'): - // "ll" has a different meaning! - if ( format[1] != _T('l') ) - { - size = Long; - format++; - break; - } - //else: fall through - - default: - size = Default; - } + va_end(argptr); - // and finally we should have the type - switch ( *format ) - { - case _T('C'): - case _T('S'): - // %C and %hC -> %c and %lC -> %lc - if ( size == Long ) - CopyFmtChar(_T('l')); - - InsertFmtChar(*format++ == _T('C') ? _T('c') : _T('s')); - break; - - case _T('c'): - case _T('s'): - // %c -> %lc but %hc stays %hc and %lc is still %lc - if ( size == Default) - InsertFmtChar(_T('l')); - // fall through - - default: - // nothing special to do - if ( size != Default ) - CopyFmtChar(*(format - 1)); - CopyFmtChar(*format++); - } - } - } + return ret; } +#endif -#else // !wxNEED_PRINTF_CONVERSION - // no conversion necessary - #define wxFormatConverter(x) (x) -#endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION - -#ifdef __WXDEBUG__ -// For testing the format converter -wxString wxConvertFormat(const wxChar *format) +#ifndef wxCRT_VfprintfW +int wxCRT_VfprintfW( FILE *stream, const wchar_t *format, va_list argptr ) { - return wxString(wxFormatConverter(format)); + return vfwprintf( stream, format, argptr ); } #endif -// ---------------------------------------------------------------------------- -// wxPrintf(), wxScanf() and relatives -// ---------------------------------------------------------------------------- +#ifndef wxCRT_VprintfW +int wxCRT_VprintfW( const wchar_t *format, va_list argptr ) +{ + return vwprintf( format, argptr ); +} +#endif -#if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF) +#ifndef wxCRT_VsprintfW +int wxCRT_VsprintfW( wchar_t *str, const wchar_t *format, va_list argptr ) +{ + // same as for wxSprintf() + return vswprintf(str, INT_MAX / 4, format, argptr); +} +#endif -int wxCRT_Scanf( const wxChar *format, ... ) +#ifndef wxCRT_ScanfW +int wxCRT_ScanfW(const wchar_t *format, ...) { va_list argptr; va_start(argptr, format); - int ret = vwscanf(wxFormatConverter(format), argptr ); +#ifdef __VMS +#if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD ) + int ret = std::vwscanf(format, argptr); +#else + int ret = vwscanf(format, argptr); +#endif +#else + int ret = vwscanf(format, argptr); +#endif va_end(argptr); return ret; } +#endif -int wxCRT_Sscanf( const wxChar *str, const wxChar *format, ... ) +#ifndef wxCRT_SscanfW +int wxCRT_SscanfW(const wchar_t *str, const wchar_t *format, ...) { va_list argptr; va_start(argptr, format); - int ret = vswscanf( str, wxFormatConverter(format), argptr ); +#ifdef __VMS +#if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD ) + int ret = std::vswscanf(str, format, argptr); +#else + int ret = vswscanf(str, format, argptr); +#endif +#else + int ret = vswscanf(str, format, argptr); +#endif va_end(argptr); return ret; } +#endif -int wxCRT_Fscanf( FILE *stream, const wxChar *format, ... ) +#ifndef wxCRT_FscanfW +int wxCRT_FscanfW(FILE *stream, const wchar_t *format, ...) { va_list argptr; va_start(argptr, format); - int ret = vfwscanf(stream, wxFormatConverter(format), argptr); +#ifdef __VMS +#if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD ) + int ret = std::vfwscanf(stream, format, argptr); +#else + int ret = vfwscanf(stream, format, argptr); +#endif +#else + int ret = vfwscanf(stream, format, argptr); +#endif va_end(argptr); return ret; } +#endif -int wxCRT_Printf( const wxChar *format, ... ) +#ifndef wxCRT_VsscanfW +int wxCRT_VsscanfW(const wchar_t *str, const wchar_t *format, va_list argptr) +{ +#ifdef __VMS +#if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD ) + return std::vswscanf(str, format, argptr); +#else + return vswscanf(str, format, argptr); +#endif +#else + return vswscanf(str, format, argptr); +#endif +} +#endif + + +// ---------------------------------------------------------------------------- +// wrappers to printf and scanf function families +// ---------------------------------------------------------------------------- + +#if !wxUSE_UTF8_LOCALE_ONLY +int wxDoSprintfWchar(char *str, const wxChar *format, ...) { va_list argptr; va_start(argptr, format); - int ret = vwprintf( wxFormatConverter(format), argptr ); + int rv = wxVsprintf(str, format, argptr); va_end(argptr); - - return ret; + return rv; } +#endif // !wxUSE_UTF8_LOCALE_ONLY -int wxCRT_Fprintf( FILE *stream, const wxChar *format, ... ) +#if wxUSE_UNICODE_UTF8 +int wxDoSprintfUtf8(char *str, const char *format, ...) { va_list argptr; - va_start( argptr, format ); + va_start(argptr, format); - int ret = vfwprintf( stream, wxFormatConverter(format), argptr ); + int rv = wxVsprintf(str, format, argptr); va_end(argptr); - - return ret; + return rv; } +#endif // wxUSE_UNICODE_UTF8 -int wxCRT_Vsscanf( const wxChar *str, const wxChar *format, va_list argptr ) -{ - return vswscanf( str, wxFormatConverter(format), argptr ); -} +#if wxUSE_UNICODE -int wxCRT_Vfprintf( FILE *stream, const wxChar *format, va_list argptr ) +#if !wxUSE_UTF8_LOCALE_ONLY +int wxDoSprintfWchar(wchar_t *str, const wxChar *format, ...) { - return vfwprintf( stream, wxFormatConverter(format), argptr ); -} + va_list argptr; + va_start(argptr, format); -int wxCRT_Vprintf( const wxChar *format, va_list argptr ) -{ - return vwprintf( wxFormatConverter(format), argptr ); -} + int rv = wxVsprintf(str, format, argptr); -#ifndef wxCRT_Vsnprintf -int wxCRT_Vsnprintf( wxChar *str, size_t size, const wxChar *format, va_list argptr ) -{ - return vswprintf( str, size, wxFormatConverter(format), argptr ); + va_end(argptr); + return rv; } -#endif // wxCRT_Vsnprintf +#endif // !wxUSE_UTF8_LOCALE_ONLY -int wxCRT_Vsprintf( wxChar *str, const wxChar *format, va_list argptr ) +#if wxUSE_UNICODE_UTF8 +int wxDoSprintfUtf8(wchar_t *str, const char *format, ...) { - // same as for wxSprintf() - return vswprintf(str, INT_MAX / 4, wxFormatConverter(format), argptr); -} + va_list argptr; + va_start(argptr, format); -#endif // wxNEED_PRINTF_CONVERSION + int rv = wxVsprintf(str, format, argptr); + va_end(argptr); + return rv; +} +#endif // wxUSE_UNICODE_UTF8 -// ---------------------------------------------------------------------------- -// wrappers to printf and scanf function families -// ---------------------------------------------------------------------------- +#endif // wxUSE_UNICODE -int wxDoSprintf(char *str, const wxString& format, ...) +#if !wxUSE_UTF8_LOCALE_ONLY +int wxDoSnprintfWchar(char *str, size_t size, const wxChar *format, ...) { va_list argptr; va_start(argptr, format); - int rv = wxVsprintf(str, format, argptr); + int rv = wxVsnprintf(str, size, format, argptr); va_end(argptr); return rv; } +#endif // !wxUSE_UTF8_LOCALE_ONLY -#if wxUSE_UNICODE -int wxDoSprintf(wchar_t *str, const wxString& format, ...) +#if wxUSE_UNICODE_UTF8 +int wxDoSnprintfUtf8(char *str, size_t size, const char *format, ...) { va_list argptr; va_start(argptr, format); - int rv = wxVsprintf(str, format, argptr); + int rv = wxVsnprintf(str, size, format, argptr); va_end(argptr); return rv; } -#endif +#endif // wxUSE_UNICODE_UTF8 + +#if wxUSE_UNICODE -int wxDoSnprintf(char *str, size_t size, const wxString& format, ...) +#if !wxUSE_UTF8_LOCALE_ONLY +int wxDoSnprintfWchar(wchar_t *str, size_t size, const wxChar *format, ...) { va_list argptr; va_start(argptr, format); @@ -640,9 +549,10 @@ int wxDoSnprintf(char *str, size_t size, const wxString& format, ...) va_end(argptr); return rv; } +#endif // !wxUSE_UTF8_LOCALE_ONLY -#if wxUSE_UNICODE -int wxDoSnprintf(wchar_t *str, size_t size, const wxString& format, ...) +#if wxUSE_UNICODE_UTF8 +int wxDoSnprintfUtf8(wchar_t *str, size_t size, const char *format, ...) { va_list argptr; va_start(argptr, format); @@ -652,7 +562,9 @@ int wxDoSnprintf(wchar_t *str, size_t size, const wxString& format, ...) va_end(argptr); return rv; } -#endif +#endif // wxUSE_UNICODE_UTF8 + +#endif // wxUSE_UNICODE #ifdef HAVE_BROKEN_VSNPRINTF_DECL @@ -660,38 +572,56 @@ int wxDoSnprintf(wchar_t *str, size_t size, const wxString& format, ...) #endif #if wxUSE_UNICODE -static int ConvertStringToBuf(const wxString& s, char *out, size_t outsize) + +namespace { - const wxWX2WCbuf buf = s.wc_str(); - size_t len = wxConvLibc.FromWChar(out, outsize, buf); - if ( len != wxCONV_FAILED ) - return len-1; - else - return wxConvLibc.FromWChar(NULL, 0, buf); +#if !wxUSE_UTF8_LOCALE_ONLY +int ConvertStringToBuf(const wxString& s, char *out, size_t outsize) +{ + const wxCharBuffer buf(s.mb_str()); + + const size_t len = buf.length(); + if ( outsize > len ) + { + memcpy(out, buf, (len+1) * sizeof(char)); + } + else // not enough space + { + memcpy(out, buf, (outsize-1) * sizeof(char)); + out[outsize-1] = '\0'; + } + + return len; } +#endif // !wxUSE_UTF8_LOCALE_ONLY #if wxUSE_UNICODE_UTF8 -static int ConvertStringToBuf(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 +#endif // wxUSE_UNICODE_UTF8 + +} // anonymous namespace template static size_t PrintfViaString(T *out, size_t outsize, const wxString& format, va_list argptr) { - va_list argcopy; - wxVaCopy(argcopy, argptr); - wxString s; - s.PrintfV(format, argcopy); + s.PrintfV(format, argptr); return ConvertStringToBuf(s, out, outsize); } @@ -699,21 +629,18 @@ static size_t PrintfViaString(T *out, size_t outsize, int wxVsprintf(char *str, const wxString& format, va_list argptr) { - va_list argcopy; - wxVaCopy(argcopy, argptr); - #if wxUSE_UTF8_LOCALE_ONLY - return vsprintf(str, format.wx_str(), argcopy); + return wxCRT_VsprintfA(str, format.wx_str(), argptr); #else #if wxUSE_UNICODE_UTF8 if ( wxLocaleIsUtf8 ) - return vsprintf(str, format.wx_str(), argcopy); + return wxCRT_VsprintfA(str, format.wx_str(), argptr); else #endif #if wxUSE_UNICODE - return PrintfViaString(str, wxNO_LEN, format, argcopy); + return PrintfViaString(str, wxNO_LEN, format, argptr); #else - return wxCRT_Vsprintf(str, format, argcopy); + return wxCRT_VsprintfA(str, format.mb_str(), argptr); #endif #endif } @@ -721,18 +648,28 @@ int wxVsprintf(char *str, const wxString& format, va_list argptr) #if wxUSE_UNICODE int wxVsprintf(wchar_t *str, const wxString& format, va_list argptr) { - va_list argcopy; - wxVaCopy(argcopy, argptr); - #if wxUSE_UNICODE_WCHAR - return wxCRT_Vsprintf(str, format, argcopy); +#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 ) - return wxCRT_Vsprintf(str, format, argcopy); + return wxCRT_VsprintfW(str, format.wc_str(), argptr); else #endif - return PrintfViaString(str, wxNO_LEN, format, argcopy); + return PrintfViaString(str, wxNO_LEN, format, argptr); #endif // wxUSE_UNICODE_UTF8 } #endif // wxUSE_UNICODE @@ -740,15 +677,12 @@ int wxVsprintf(wchar_t *str, const wxString& format, va_list argptr) int wxVsnprintf(char *str, size_t size, const wxString& format, va_list argptr) { int rv; - va_list argcopy; - wxVaCopy(argcopy, argptr); - #if wxUSE_UTF8_LOCALE_ONLY - rv = vsnprintf(str, size, format.wx_str(), argcopy); + rv = wxCRT_VsnprintfA(str, size, format.wx_str(), argptr); #else #if wxUSE_UNICODE_UTF8 if ( wxLocaleIsUtf8 ) - rv = vsnprintf(str, size, format.wx_str(), argcopy); + rv = wxCRT_VsnprintfA(str, size, format.wx_str(), argptr); else #endif #if wxUSE_UNICODE @@ -756,10 +690,10 @@ int wxVsnprintf(char *str, size_t size, const wxString& format, va_list argptr) // NB: if this code is called, then wxString::PrintV() would use the // wchar_t* version of wxVsnprintf(), so it's safe to use PrintV() // from here - rv = PrintfViaString(str, size, format, argcopy); + rv = PrintfViaString(str, size, format, argptr); } #else - rv = wxCRT_Vsnprintf(str, size, format, argcopy); + rv = wxCRT_VsnprintfA(str, size, format.mb_str(), argptr); #endif #endif @@ -774,22 +708,20 @@ int wxVsnprintf(char *str, size_t size, const wxString& format, va_list argptr) int wxVsnprintf(wchar_t *str, size_t size, const wxString& format, va_list argptr) { int rv; - va_list argcopy; - wxVaCopy(argcopy, argptr); #if wxUSE_UNICODE_WCHAR - rv = wxCRT_Vsnprintf(str, size, format, argcopy); + rv = wxCRT_VsnprintfW(str, size, format.wc_str(), argptr); #else // wxUSE_UNICODE_UTF8 #if !wxUSE_UTF8_LOCALE_ONLY if ( !wxLocaleIsUtf8 ) - rv = wxCRT_Vsnprintf(str, size, format, argcopy); + rv = wxCRT_VsnprintfW(str, size, format.wc_str(), argptr); else #endif { // NB: if this code is called, then wxString::PrintV() would use the // char* version of wxVsnprintf(), so it's safe to use PrintV() // from here - rv = PrintfViaString(str, size, format, argcopy); + rv = PrintfViaString(str, size, format, argptr); } #endif // wxUSE_UNICODE_UTF8 @@ -801,38 +733,14 @@ 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) // ---------------------------------------------------------------------------- -#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 wxIscntrl(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 - #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) { @@ -854,7 +762,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) { @@ -878,398 +786,147 @@ WXDLLEXPORT size_t wxWcstombs (char * out, const wchar_t * in, size_t outlen) #endif // wxNEED_WX_MBSTOWCS -#if defined(wxNEED_WX_CTYPE_H) - -#include - -#define cfalnumset CFCharacterSetGetPredefined(kCFCharacterSetAlphaNumeric) -#define cfalphaset CFCharacterSetGetPredefined(kCFCharacterSetLetter) -#define cfcntrlset CFCharacterSetGetPredefined(kCFCharacterSetControl) -#define cfdigitset CFCharacterSetGetPredefined(kCFCharacterSetDecimalDigit) -//CFCharacterSetRef cfgraphset = kCFCharacterSetControl && !' ' -#define cflowerset CFCharacterSetGetPredefined(kCFCharacterSetLowercaseLetter) -//CFCharacterSetRef cfprintset = !kCFCharacterSetControl -#define cfpunctset CFCharacterSetGetPredefined(kCFCharacterSetPunctuation) -#define cfspaceset CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline) -#define cfupperset CFCharacterSetGetPredefined(kCFCharacterSetUppercaseLetter) - -WXDLLEXPORT int wxIsalnum(wxChar ch) { return CFCharacterSetIsCharacterMember(cfalnumset, ch); } -WXDLLEXPORT int wxIsalpha(wxChar ch) { return CFCharacterSetIsCharacterMember(cfalphaset, ch); } -WXDLLEXPORT int wxIscntrl(wxChar ch) { return CFCharacterSetIsCharacterMember(cfcntrlset, ch); } -WXDLLEXPORT int wxIsdigit(wxChar ch) { return CFCharacterSetIsCharacterMember(cfdigitset, ch); } -WXDLLEXPORT int wxIsgraph(wxChar ch) { return !CFCharacterSetIsCharacterMember(cfcntrlset, ch) && ch != ' '; } -WXDLLEXPORT int wxIslower(wxChar ch) { return CFCharacterSetIsCharacterMember(cflowerset, ch); } -WXDLLEXPORT int wxIsprint(wxChar ch) { return !CFCharacterSetIsCharacterMember(cfcntrlset, ch); } -WXDLLEXPORT int wxIspunct(wxChar ch) { return CFCharacterSetIsCharacterMember(cfpunctset, ch); } -WXDLLEXPORT int wxIsspace(wxChar ch) { return CFCharacterSetIsCharacterMember(cfspaceset, ch); } -WXDLLEXPORT int wxIsupper(wxChar ch) { return CFCharacterSetIsCharacterMember(cfupperset, ch); } -WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxIsdigit(ch) || (ch>='a' && ch<='f') || (ch>='A' && ch<='F'); } -WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)tolower((char)(ch)); } -WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)toupper((char)(ch)); } - -#endif // wxNEED_WX_CTYPE_H - -#ifndef wxStrdupA - -WXDLLEXPORT char *wxStrdupA(const char *s) +#ifndef wxCRT_StrdupA +WXDLLIMPEXP_BASE char *wxCRT_StrdupA(const char *s) { return strcpy((char *)malloc(strlen(s) + 1), s); } +#endif // wxCRT_StrdupA -#endif // wxStrdupA - -#ifndef wxStrdupW - -WXDLLEXPORT wchar_t * wxStrdupW(const wchar_t *pwz) +#ifndef wxCRT_StrdupW +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); memcpy(ret, pwz, size); return ret; } +#endif // wxCRT_StrdupW -#endif // wxStrdupW - -#ifndef wxStricmp -int WXDLLEXPORT wxStricmp(const wxChar *psz1, const wxChar *psz2) -{ - register wxChar c1, c2; - do { - c1 = wxTolower(*psz1++); - c2 = wxTolower(*psz2++); - } while ( c1 && (c1 == c2) ); - return c1 - c2; -} -#endif - -#ifndef wxStricmp -int WXDLLEXPORT wxStrnicmp(const wxChar *s1, const wxChar *s2, size_t n) -{ - // initialize the variables just to suppress stupid gcc warning - register wxChar c1 = 0, c2 = 0; - 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 wxSetlocale_ -wxWCharBuffer wxSetlocale_(int category, const wxChar *locale) -{ - char *localeOld = setlocale(category, wxConvLibc.cWX2MB(locale)); - - return wxWCharBuffer(wxConvLibc.cMB2WC(localeOld)); -} - -wxWCharBuffer wxSetlocale(int category, const wxChar *locale) -{ - wxWCharBuffer rv = wxSetlocale_(category, locale); - if ( rv ) - wxUpdateLocaleIsUtf8(); - return rv; -} -#else // defined(wxSetlocale_) -wxChar *wxSetlocale(int category, const wxChar *locale) -{ - wxChar *rv = wxSetlocale_(category, locale); - if ( rv ) - wxUpdateLocaleIsUtf8(); - return rv; -} -#endif // wxSetlocale_ defined or not - -#if wxUSE_WCHAR_T && !defined(HAVE_WCSLEN) -WXDLLEXPORT size_t wxWcslen(const wchar_t *s) +#ifndef wxWCHAR_T_IS_WXCHAR16 +size_t wxStrlen(const wxChar16 *s ) { - size_t n = 0; - while ( *s++ ) - n++; - - return n; + if (!s) return 0; + size_t i=0; + while (*s!=0) { ++i; ++s; }; + return i; } -#endif - -// ---------------------------------------------------------------------------- -// string.h functions -// ---------------------------------------------------------------------------- - -#ifdef wxNEED_WX_STRING_H -// RN: These need to be c externed for the regex lib -#ifdef __cplusplus -extern "C" { -#endif - -WXDLLEXPORT wxChar * wxStrcat(wxChar *dest, const wxChar *src) +wxChar16* wxStrdup(const wxChar16* s) { - wxChar *ret = dest; - while (*dest) dest++; - while ((*dest++ = *src++)); + size_t size = (wxStrlen(s) + 1) * sizeof(wxChar16); + wxChar16 *ret = (wxChar16*) malloc(size); + memcpy(ret, s, size); return ret; } +#endif -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) +#ifndef wxWCHAR_T_IS_WXCHAR32 +size_t wxStrlen(const wxChar32 *s ) { - while ((*s1 == *s2) && *s1) s1++, s2++; - if ((wxUChar)*s1 < (wxUChar)*s2) return -1; - if ((wxUChar)*s1 > (wxUChar)*s2) return 1; - return 0; + if (!s) return 0; + size_t i=0; + while (*s!=0) { ++i; ++s; }; + return i; } -WXDLLEXPORT wxChar * wxStrcpy(wxChar *dest, const wxChar *src) +wxChar32* wxStrdup(const wxChar32* s) { - wxChar *ret = dest; - while ((*dest++ = *src++)); + size_t size = (wxStrlen(s) + 1) * sizeof(wxChar32); + wxChar32 *ret = (wxChar32*) malloc(size); + memcpy(ret, s, size); return ret; } +#endif -WXDLLEXPORT size_t wxStrlen_(const wxChar *s) +#ifndef wxCRT_StricmpA +WXDLLIMPEXP_BASE int wxCRT_StricmpA(const char *psz1, const char *psz2) { - size_t n = 0; - while ( *s++ ) - n++; - - return n; + register char c1, c2; + do { + c1 = wxTolower(*psz1++); + c2 = wxTolower(*psz2++); + } while ( c1 && (c1 == c2) ); + return c1 - c2; } +#endif // !defined(wxCRT_StricmpA) - -WXDLLEXPORT wxChar * wxStrncat(wxChar *dest, const wxChar *src, size_t n) +#ifndef wxCRT_StricmpW +WXDLLIMPEXP_BASE int wxCRT_StricmpW(const wchar_t *psz1, const wchar_t *psz2) { - wxChar *ret = dest; - while (*dest) dest++; - while (n && (*dest++ = *src++)) n--; - return ret; + register wchar_t c1, c2; + do { + c1 = wxTolower(*psz1++); + c2 = wxTolower(*psz2++); + } while ( c1 && (c1 == c2) ); + return c1 - c2; } +#endif // !defined(wxCRT_StricmpW) -WXDLLEXPORT int wxStrncmp(const wxChar *s1, const wxChar *s2, size_t n) +#ifndef wxCRT_StrnicmpA +WXDLLIMPEXP_BASE int wxCRT_StrnicmpA(const char *s1, const char *s2, size_t n) { - while (n && (*s1 == *s2) && *s1) n--, s1++, s2++; + // initialize the variables just to suppress stupid gcc warning + register char c1 = 0, c2 = 0; + while (n && ((c1 = wxTolower(*s1)) == (c2 = wxTolower(*s2)) ) && c1) n--, s1++, s2++; if (n) { - if ((wxUChar)*s1 < (wxUChar)*s2) return -1; - if ((wxUChar)*s1 > (wxUChar)*s2) return 1; + if (c1 < c2) return -1; + if (c1 > c2) return 1; } return 0; } +#endif // !defined(wxCRT_StrnicmpA) -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) -{ - wxASSERT_MSG( 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; -} - -#ifdef __cplusplus -} -#endif - -WXDLLEXPORT double wxStrtod(const wxChar *nptr, wxChar **endptr) +#ifndef wxCRT_StrnicmpW +WXDLLIMPEXP_BASE int wxCRT_StrnicmpW(const wchar_t *s1, const wchar_t *s2, size_t n) { - 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; + // initialize the variables just to suppress stupid gcc warning + register wchar_t c1 = 0, c2 = 0; + while (n && ((c1 = wxTolower(*s1)) == (c2 = wxTolower(*s2)) ) && c1) n--, s1++, s2++; + if (n) { + if (c1 < c2) return -1; + if (c1 > c2) return 1; } - 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(start, 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; -} - -WXDLLEXPORT unsigned long int wxStrtoul(const wxChar *nptr, wxChar **endptr, int base) -{ - return (unsigned long int) wxStrtol(nptr, endptr, base); -} - -#endif // wxNEED_WX_STRING_H - -#ifdef wxNEED_WX_STDIO_H -WXDLLEXPORT FILE * wxFopen(const wxChar *path, const wxChar *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) -{ - 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 0; } +#endif // !defined(wxCRT_StrnicmpW) -WXDLLEXPORT int wxRename(const wxChar *oldpath, const wxChar *newpath) -{ - return rename( wxConvFile.cWX2MB(oldpath), wxConvFile.cWX2MB(newpath) ); -} -#endif +// ---------------------------------------------------------------------------- +// string.h functions +// ---------------------------------------------------------------------------- -#ifndef wxAtof -double WXDLLEXPORT wxAtof(const wxChar *psz) +// 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" WXDLLIMPEXP_BASE size_t wxCRT_StrlenW(const wchar_t *s) { -#ifdef __WXWINCE__ - double d; - wxString str(psz); - if (str.ToDouble(& d)) - return d; + size_t n = 0; + while ( *s++ ) + n++; - return 0.0; -#else - return atof(wxConvLibc.cWX2MB(psz)); -#endif + return n; } #endif -#ifdef wxNEED_WX_STDLIB_H -int WXDLLEXPORT wxAtoi(const wxChar *psz) -{ - return atoi(wxConvLibc.cWX2MB(psz)); -} - -long WXDLLEXPORT wxAtol(const wxChar *psz) -{ - return atol(wxConvLibc.cWX2MB(psz)); -} +// ---------------------------------------------------------------------------- +// stdlib.h functions +// ---------------------------------------------------------------------------- -wxChar * WXDLLEXPORT wxGetenv(const wxChar *name) +#ifndef wxCRT_GetenvW +WXDLLIMPEXP_BASE wchar_t* wxCRT_GetenvW(const wchar_t *name) { -#if wxUSE_UNICODE // 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((wxChar*)NULL); - value = wxConvLibc.cMB2WX(getenv(wxConvLibc.cWX2MB(name))); + static wxWCharBuffer value; + value = wxConvLibc.cMB2WC(getenv(wxConvLibc.cWC2MB(name))); return value.data(); -#else - return getenv(name); -#endif } +#endif // !wxCRT_GetenvW -int WXDLLEXPORT wxSystem(const wxChar *psz) -{ - return system(wxConvLibc.cWX2MB(psz)); -} - -#endif // wxNEED_WX_STDLIB_H - -#ifdef wxNEED_WX_TIME_H -WXDLLEXPORT size_t -wxStrftime(wxChar *s, size_t maxsize, const wxChar *fmt, const struct tm *tm) +#ifndef wxCRT_StrftimeW +WXDLLIMPEXP_BASE size_t +wxCRT_StrftimeW(wchar_t *s, size_t maxsize, const wchar_t *fmt, const struct tm *tm) { if ( !maxsize ) return 0; @@ -1288,31 +945,15 @@ wxStrftime(wxChar *s, size_t maxsize, const wxChar *fmt, const struct tm *tm) if ( !wbuf ) return 0; - wxStrncpy(s, wbuf, maxsize); - return wxStrlen(s); -} -#endif // wxNEED_WX_TIME_H - -#ifndef wxCtime -WXDLLEXPORT wxChar *wxCtime(const time_t *timep) -{ - // normally the string is 26 chars but give one more in case some broken - // DOS compiler decides to use "\r\n" instead of "\n" at the end - static wxChar buf[27]; - - // ctime() is guaranteed to return a string containing only ASCII - // characters, as its format is always the same for any locale - wxStrncpy(buf, wxString::FromAscii(ctime(timep)), WXSIZEOF(buf)); - buf[WXSIZEOF(buf) - 1] = _T('\0'); - - return buf; + wxCRT_StrncpyW(s, wbuf, maxsize); + return wxCRT_StrlenW(s); } -#endif // wxCtime - -#endif // wxUSE_WCHAR_T +#endif // !wxCRT_StrftimeW -#ifndef wxStrtoll -static wxULongLong_t wxStrtoullBase(const wxChar* nptr, wxChar** endptr, int base, wxChar* sign) +#ifdef wxLongLong_t +template +static wxULongLong_t +wxCRT_StrtoullBase(const T* nptr, T** endptr, int base, T* sign) { wxULongLong_t sum = 0; wxString wxstr(nptr); @@ -1320,54 +961,54 @@ static wxULongLong_t wxStrtoullBase(const wxChar* nptr, wxChar** endptr, int bas 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(' '); if ( i != end ) { - wxChar c = *i; + T c = *i; 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 { if ( endptr ) - *endptr = (wxChar*) nptr; + *endptr = (T*) nptr; wxSET_ERRNO(EINVAL); return sum; } } else - i--; + --i; } if ( base == 0 ) base = 10; - for ( ; i != end; i++ ) + for ( ; i != end; ++i ) { unsigned int n; - wxChar c = *i; - if ( c >= wxT('0') ) + T c = *i; + if ( c >= '0' ) { - if ( c <= wxT('9') ) + if ( c <= '9' ) n = c - wxT('0'); else n = wxTolower(c) - wxT('a') + 10; @@ -1391,17 +1032,17 @@ static wxULongLong_t wxStrtoullBase(const wxChar* nptr, wxChar** endptr, int bas if ( endptr ) { - const wxChar& endref = *i; - *endptr = &(wxChar&)endref; + *endptr = (T*)(nptr + (i - wxstr.begin())); } return sum; } -wxULongLong_t wxStrtoull(const wxChar* nptr, wxChar** endptr, int base) +template +static wxULongLong_t wxCRT_DoStrtoull(const T* nptr, T** endptr, int base) { - wxChar sign; - wxULongLong_t uval = wxStrtoullBase(nptr, endptr, base, &sign); + T sign; + wxULongLong_t uval = ::wxCRT_StrtoullBase(nptr, endptr, base, &sign); if ( sign == wxT('-') ) { @@ -1412,20 +1053,18 @@ wxULongLong_t wxStrtoull(const wxChar* nptr, wxChar** endptr, int base) return uval; } -wxLongLong_t wxStrtoll(const wxChar* nptr, wxChar** endptr, int base) +template +static wxLongLong_t wxCRT_DoStrtoll(const T* nptr, T** endptr, int base) { - wxChar sign; - wxULongLong_t uval = wxStrtoullBase(nptr, endptr, base, &sign); + T 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 { @@ -1443,15 +1082,33 @@ wxLongLong_t wxStrtoll(const wxChar* nptr, wxChar** endptr, int base) return val; } -#endif // wxStrtoll + +#ifndef wxCRT_StrtollA +wxLongLong_t wxCRT_StrtollA(const char* nptr, char** endptr, int base) + { return wxCRT_DoStrtoll(nptr, endptr, base); } +#endif +#ifndef wxCRT_StrtollW +wxLongLong_t wxCRT_StrtollW(const wchar_t* nptr, wchar_t** endptr, int base) + { return wxCRT_DoStrtoll(nptr, endptr, base); } +#endif + +#ifndef wxCRT_StrtoullA +wxULongLong_t wxCRT_StrtoullA(const char* nptr, char** endptr, int base) + { return wxCRT_DoStrtoull(nptr, endptr, base); } +#endif +#ifndef wxCRT_StrtoullW +wxULongLong_t wxCRT_StrtoullW(const wchar_t* nptr, wchar_t** endptr, int base) + { return wxCRT_DoStrtoull(nptr, endptr, base); } +#endif + +#endif // wxLongLong_t // ---------------------------------------------------------------------------- -// functions which we may need even if !wxUSE_WCHAR_T +// strtok() functions // ---------------------------------------------------------------------------- -#ifndef wxStrtok - -WXDLLEXPORT wxChar * wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr) +template +static T *wxCRT_DoStrtok(T *psz, const T *delim, T **save_ptr) { if (!psz) { @@ -1463,15 +1120,15 @@ WXDLLEXPORT wxChar * wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_pt psz += wxStrspn(psz, delim); if (!*psz) { - *save_ptr = (wxChar *)NULL; - return (wxChar *)NULL; + *save_ptr = NULL; + return NULL; } - wxChar *ret = psz; + T *ret = psz; psz = wxStrpbrk(psz, delim); if (!psz) { - *save_ptr = (wxChar*)NULL; + *save_ptr = NULL; } else { @@ -1482,7 +1139,14 @@ WXDLLEXPORT wxChar * wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_pt return ret; } -#endif // wxStrtok +#ifndef wxCRT_StrtokA +char *wxCRT_StrtokA(char *psz, const char *delim, char **save_ptr) + { return wxCRT_DoStrtok(psz, delim, save_ptr); } +#endif +#ifndef wxCRT_StrtokW +wchar_t *wxCRT_StrtokW(wchar_t *psz, const wchar_t *delim, wchar_t **save_ptr) + { return wxCRT_DoStrtok(psz, delim, save_ptr); } +#endif // ---------------------------------------------------------------------------- // missing C RTL functions @@ -1510,19 +1174,9 @@ void *calloc( size_t num, size_t size ) #endif // __WXWINCE__ <= 211 -#ifdef __WXWINCE__ - -int wxRemove(const wxChar *path) -{ - return ::DeleteFile(path) == 0; -} - -#endif - - -// ---------------------------------------------------------------------------- +// ============================================================================ // wxLocaleIsUtf8 -// ---------------------------------------------------------------------------- +// ============================================================================ #if wxUSE_UNICODE_UTF8 @@ -1544,18 +1198,28 @@ static bool wxIsLocaleUtf8() { // "UTF-8" is used by modern glibc versions, but test other variants // as well, just in case: - return strcmp(charset, "UTF-8") == 0 || - strcmp(charset, "utf-8") == 0 || - strcmp(charset, "UTF8") == 0 || - strcmp(charset, "utf8") == 0; + if ( strcmp(charset, "UTF-8") == 0 || + strcmp(charset, "utf-8") == 0 || + strcmp(charset, "UTF8") == 0 || + strcmp(charset, "utf8") == 0 ) + { + return true; + } } - else // nl_langinfo() failed -#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: + const char *lc_ctype = setlocale(LC_CTYPE, NULL); + if ( lc_ctype && + (strcmp(lc_ctype, "C") == 0 || strcmp(lc_ctype, "POSIX") == 0) ) { - // we don't know what charset libc is using, so assume the worst - // to be safe: - return false; + return true; } + + // we don't know what charset libc is using, so assume the worst + // to be safe: + return false; } void wxUpdateLocaleIsUtf8() @@ -1563,11 +1227,106 @@ 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(); #endif } -#endif // wxUSE_UTF8_LOCALE_ONLY +#endif // wxUSE_UNICODE_UTF8 + +// ============================================================================ +// wx wrappers for CRT functions +// ============================================================================ + +#if wxUSE_UNICODE_WCHAR + #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callW +#elif wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY + #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) \ + return_kw wxLocaleIsUtf8 ? callA : callW +#else // ANSI or UTF8 only + #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callA +#endif + +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.wchar_str())); +} + +int wxFputs(const wxString& s, FILE *stream) +{ + CALL_ANSI_OR_UNICODE(return, + wxCRT_FputsA(s.mb_str(), stream), + wxCRT_FputsW(s.wc_str(), stream)); +} + +int wxFputc(const wxUniChar& c, FILE *stream) +{ +#if !wxUSE_UNICODE // FIXME-UTF8: temporary, remove this with ANSI build + return wxCRT_FputcA((char)c, stream); +#else + CALL_ANSI_OR_UNICODE(return, + wxCRT_FputsA(c.AsUTF8(), stream), + wxCRT_FputcW((wchar_t)c, stream)); +#endif +} + +#ifdef wxCRT_PerrorA + +void wxPerror(const wxString& s) +{ +#ifdef wxCRT_PerrorW + CALL_ANSI_OR_UNICODE(wxEMPTY_PARAMETER_VALUE, + wxCRT_PerrorA(s.mb_str()), + wxCRT_PerrorW(s.wc_str())); +#else + wxCRT_PerrorA(s.mb_str()); +#endif +} + +#endif // wxCRT_PerrorA + +wchar_t *wxFgets(wchar_t *s, int size, FILE *stream) +{ + wxCHECK_MSG( s, NULL, "empty buffer passed to wxFgets()" ); + + wxCharBuffer buf(size - 1); + // FIXME: this reads too little data if wxConvLibc uses UTF-8 ('size' wide + // characters may be encoded by up to 'size'*4 bytes), but what + // else can we do? + if ( wxFgets(buf.data(), size, stream) == NULL ) + return NULL; + + if ( wxConvLibc.ToWChar(s, size, buf, wxNO_LEN) == wxCONV_FAILED ) + return NULL; + + return s; +} + +// ---------------------------------------------------------------------------- +// wxScanf() and friends +// ---------------------------------------------------------------------------- + +#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(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(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(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 // HAVE_NO_VSSCANF