X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/dd0ef3324b56a108aa3850e4240d3f7160965c01..1fee6e257780f17b4d53b48aa0e7d2ac69664595:/src/common/wxcrt.cpp?ds=sidebyside diff --git a/src/common/wxcrt.cpp b/src/common/wxcrt.cpp index 5f08d6d4d4..6f3377d6cd 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() @@ -29,12 +30,14 @@ #include #include +#ifndef __WXPALMOS5__ #ifndef __WXWINCE__ #include #include #else #include "wx/msw/wince/time.h" #endif +#endif // !__WXPALMOS5__ #ifndef WX_PRECOMP #include "wx/string.h" @@ -43,11 +46,17 @@ #include "wx/log.h" #endif -#if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H) - #include - #include - #include - #include +#ifdef HAVE_LANGINFO_H + #include +#endif + +#ifdef __WXWINCE__ + // there is no errno.h under CE apparently + #define wxSET_ERRNO(value) +#else + #include + + #define wxSET_ERRNO(value) errno = value #endif #if defined(__MWERKS__) && __MSL__ >= 0x6000 @@ -55,8 +64,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 @@ -81,7 +97,7 @@ size_t WXDLLEXPORT wxMB2WC(wchar_t *buf, const char *psz, size_t n) // 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 + // wx/crt.h) we don't use its mbstowcs() at all #ifdef HAVE_WCSRTOMBS return mbsrtowcs((wchar_t *) NULL, &psz, 0, &mbstate); #else @@ -89,7 +105,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; @@ -117,7 +133,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 @@ -138,6 +154,45 @@ bool WXDLLEXPORT wxOKlibc() return true; } +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 + if ( locale != NULL /* setting locale, not querying */ && + rv /* call was successful */ ) + { + wxUpdateLocaleIsUtf8(); + } + return rv; +#endif // __WXWINCE__/!__WXWINCE__ +} + // ============================================================================ // printf() functions business // ============================================================================ @@ -152,50 +207,31 @@ bool WXDLLEXPORT wxOKlibc() #undef wxVsprintf #undef wxVprintf #undef wxVsnprintf_ - #undef wxSnprintf_ #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(wxSnprintf_) -int WXDLLEXPORT wxDoSnprintf_(wxChar *buf, size_t len, const wxChar *format, ...) +#if defined(__DMC__) +/* Digital Mars adds count to _stprintf (C99) so convert */ +int wxCRT_SprintfW (wchar_t * __RESTRICT s, const wchar_t * __RESTRICT format, ... ) { - va_list argptr; - va_start(argptr, format); - - int iLen = wxVsnprintf_(buf, len, format, argptr); + va_list arglist; - va_end(argptr); - - return iLen; + va_start( arglist, format ); + int iLen = swprintf ( s, -1, format, arglist ); + va_end( arglist ); + return iLen ; } -#endif // wxSnprintf_ - -#if defined(__DMC__) - /* Digital Mars adds count to _stprintf (C99) so convert */ - #if wxUSE_UNICODE - int wxDoSprintf (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 - #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 ) @@ -203,17 +239,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++; @@ -221,16 +257,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 @@ -238,40 +274,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") ); 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") ); 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); @@ -286,357 +305,446 @@ 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: +#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. - format specifier results in - ----------------------------------- - %hc, %C, %hC char - %c, %lc, %lC wchar_t + 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(wx_static_cast(const char*, wxConvLibc.cWX2MB(ws)), + wxConvLibc.cWX2MB(format), argptr); +} +#endif - while on POSIX systems we have %C identical to %lc and %c always means char - (in any mode) while %lc always means wchar_t, +// ---------------------------------------------------------------------------- +// wxPrintf(), wxScanf() and relatives +// ---------------------------------------------------------------------------- - 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): +// 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()! - wxWidgets specifier POSIX specifier - ---------------------------------------- +#ifndef wxCRT_PrintfW +int wxCRT_PrintfW( const wchar_t *format, ... ) +{ + va_list argptr; + va_start(argptr, format); - %hc, %C, %hC %c - %c %lc + int ret = vwprintf( format, argptr ); + va_end(argptr); - And, of course, the same should be done for %s as well. -*/ + return ret; +} +#endif -class wxFormatConverter +#ifndef wxCRT_FprintfW +int wxCRT_FprintfW( FILE *stream, const wchar_t *format, ... ) { -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; - } + va_list argptr; + va_start( argptr, format ); - // insert an extra character - void InsertFmtChar(wxChar ch) - { - if ( m_fmtOrig ) - { - // so far we haven't translated anything yet - CopyAllBefore(); - } + int ret = vfwprintf( stream, format, argptr ); - m_fmt += ch; - } + va_end(argptr); - void CopyAllBefore() - { - wxASSERT_MSG( m_fmtOrig && m_fmt.empty(), _T("logic error") ); + return ret; +} +#endif - m_fmt = wxString(m_fmtOrig, m_nCopied); +#ifndef wxCRT_VfprintfW +int wxCRT_VfprintfW( FILE *stream, const wchar_t *format, va_list argptr ) +{ + return vfwprintf( stream, format, argptr ); +} +#endif - // we won't need it any longer - m_fmtOrig = NULL; - } +#ifndef wxCRT_VprintfW +int wxCRT_VprintfW( const wchar_t *format, va_list argptr ) +{ + return vwprintf( format, argptr ); +} +#endif - static bool IsFlagChar(wxChar ch) - { - return ch == _T('-') || ch == _T('+') || - ch == _T('0') || ch == _T(' ') || ch == _T('#'); - } +#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 - void SkipDigits(const wxChar **ptpc) - { - while ( **ptpc >= _T('0') && **ptpc <= _T('9') ) - CopyFmtChar(*(*ptpc)++); - } +#ifndef wxCRT_ScanfW +int wxCRT_ScanfW(const wchar_t *format, ...) +{ + va_list argptr; + va_start(argptr, format); - // the translated format - wxString m_fmt; +#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 - // 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_SscanfW +int wxCRT_SscanfW(const wchar_t *str, const wchar_t *format, ...) { - m_fmtOrig = format; - m_nCopied = 0; + va_list argptr; + va_start(argptr, format); - while ( *format ) - { - if ( CopyFmtChar(*format++) == _T('%') ) - { - // skip any flags - while ( IsFlagChar(*format) ) - CopyFmtChar(*format++); +#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 - // and possible width - if ( *format == _T('*') ) - CopyFmtChar(*format++); - else - SkipDigits(&format); + va_end(argptr); - // precision? - if ( *format == _T('.') ) - { - CopyFmtChar(*format++); - if ( *format == _T('*') ) - CopyFmtChar(*format++); - else - SkipDigits(&format); - } + return ret; +} +#endif - // next we can have a size modifier - enum - { - Default, - Short, - Long - } size; +#ifndef wxCRT_FscanfW +int wxCRT_FscanfW(FILE *stream, const wchar_t *format, ...) +{ + va_list argptr; + va_start(argptr, format); +#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 - 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_VsscanfW +int wxCRT_VsscanfW(const wchar_t *str, const wchar_t *format, va_list argptr) { - return wxString(wxFormatConverter(format)); +#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 + // ---------------------------------------------------------------------------- -// wxPrintf(), wxScanf() and relatives +// wrappers to printf and scanf function families // ---------------------------------------------------------------------------- -#if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF) - -int wxDoScanf( const wxChar *format, ... ) +#if !wxUSE_UTF8_LOCALE_ONLY +int wxDoSprintfWchar(char *str, const wxChar *format, ...) { va_list argptr; va_start(argptr, format); - int ret = vwscanf(wxFormatConverter(format), argptr ); + int rv = wxVsprintf(str, format, argptr); va_end(argptr); - - return ret; + return rv; } +#endif // !wxUSE_UTF8_LOCALE_ONLY -int wxDoSscanf( const wxChar *str, const wxChar *format, ... ) +#if wxUSE_UNICODE_UTF8 +int wxDoSprintfUtf8(char *str, const char *format, ...) { va_list argptr; va_start(argptr, format); - int ret = vswscanf( str, wxFormatConverter(format), argptr ); + int rv = wxVsprintf(str, format, argptr); va_end(argptr); - - return ret; + return rv; } +#endif // wxUSE_UNICODE_UTF8 + +#if wxUSE_UNICODE -int wxDoFscanf( FILE *stream, const wxChar *format, ... ) +#if !wxUSE_UTF8_LOCALE_ONLY +int wxDoSprintfWchar(wchar_t *str, const wxChar *format, ...) { va_list argptr; va_start(argptr, format); - int ret = vfwscanf(stream, wxFormatConverter(format), argptr); - va_end(argptr); + int rv = wxVsprintf(str, format, argptr); - return ret; + va_end(argptr); + return rv; } +#endif // !wxUSE_UTF8_LOCALE_ONLY -int wxDoPrintf( const wxChar *format, ... ) +#if wxUSE_UNICODE_UTF8 +int wxDoSprintfUtf8(wchar_t *str, const char *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_UNICODE_UTF8 -#ifndef wxSnprintf -int wxDoSnprintf( wxChar *str, size_t size, const wxChar *format, ... ) +#endif // wxUSE_UNICODE + +#if !wxUSE_UTF8_LOCALE_ONLY +int wxDoSnprintfWchar(char *str, size_t size, const wxChar *format, ...) { va_list argptr; va_start(argptr, format); - int ret = vswprintf( str, size, wxFormatConverter(format), argptr ); - - // VsnprintfTestCase reveals that glibc's implementation of vswprintf - // doesn't nul terminate on truncation. - str[size - 1] = 0; + int rv = wxVsnprintf(str, size, format, argptr); va_end(argptr); - - return ret; + return rv; } -#endif // wxSnprintf +#endif // !wxUSE_UTF8_LOCALE_ONLY -int wxDoSprintf( wxChar *str, const wxChar *format, ... ) +#if wxUSE_UNICODE_UTF8 +int wxDoSnprintfUtf8(char *str, size_t size, const char *format, ...) { va_list argptr; va_start(argptr, format); - // note that wxString::FormatV() uses wxVsnprintf(), not wxSprintf(), so - // it's safe to implement this one in terms of it - wxString s(wxString::FormatV(format, argptr)); - wxStrcpy(str, s); + int rv = wxVsnprintf(str, size, format, argptr); va_end(argptr); + return rv; +} +#endif // wxUSE_UNICODE_UTF8 + +#if wxUSE_UNICODE - return s.length(); +#if !wxUSE_UTF8_LOCALE_ONLY +int wxDoSnprintfWchar(wchar_t *str, size_t size, const wxChar *format, ...) +{ + va_list argptr; + va_start(argptr, format); + + int rv = wxVsnprintf(str, size, format, argptr); + + va_end(argptr); + return rv; } +#endif // !wxUSE_UTF8_LOCALE_ONLY -int wxDoFprintf( FILE *stream, const wxChar *format, ... ) +#if wxUSE_UNICODE_UTF8 +int wxDoSnprintfUtf8(wchar_t *str, size_t size, const char *format, ...) { va_list argptr; - va_start( argptr, format ); + va_start(argptr, format); - int ret = vfwprintf( stream, wxFormatConverter(format), argptr ); + int rv = wxVsnprintf(str, size, format, argptr); va_end(argptr); + return rv; +} +#endif // wxUSE_UNICODE_UTF8 - return ret; +#endif // wxUSE_UNICODE + + +#ifdef HAVE_BROKEN_VSNPRINTF_DECL + #define vsnprintf wx_fixed_vsnprintf +#endif + +#if wxUSE_UNICODE + +namespace +{ + +#if !wxUSE_UTF8_LOCALE_ONLY +int ConvertStringToBuf(const wxString& s, char *out, size_t outsize) +{ + 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); } +#endif // !wxUSE_UTF8_LOCALE_ONLY -int wxVsscanf( const wxChar *str, const wxChar *format, va_list argptr ) +#if wxUSE_UNICODE_UTF8 +int ConvertStringToBuf(const wxString& s, wchar_t *out, size_t outsize) { - return vswscanf( str, wxFormatConverter(format), argptr ); + const wxWX2WCbuf buf(s.wc_str()); + 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 + { + memcpy(out, buf, (outsize-1) * sizeof(wchar_t)); + out[outsize-1] = 0; + } + return len; } +#endif // wxUSE_UNICODE_UTF8 + +} // anonymous namespace -int wxVfprintf( FILE *stream, const wxChar *format, va_list argptr ) +template +static size_t PrintfViaString(T *out, size_t outsize, + const wxString& format, va_list argptr) { - return vfwprintf( stream, wxFormatConverter(format), argptr ); + wxString s; + s.PrintfV(format, argptr); + + return ConvertStringToBuf(s, out, outsize); } +#endif // wxUSE_UNICODE -int wxVprintf( const wxChar *format, va_list argptr ) +int wxVsprintf(char *str, const wxString& format, va_list argptr) { - return vwprintf( wxFormatConverter(format), argptr ); +#if wxUSE_UTF8_LOCALE_ONLY + return wxCRT_VsprintfA(str, format.wx_str(), argptr); +#else + #if wxUSE_UNICODE_UTF8 + if ( wxLocaleIsUtf8 ) + return wxCRT_VsprintfA(str, format.wx_str(), argptr); + else + #endif + #if wxUSE_UNICODE + return PrintfViaString(str, wxNO_LEN, format, argptr); + #else + return wxCRT_VsprintfA(str, format.mb_str(), argptr); + #endif +#endif } -#ifndef wxVsnprintf -int wxVsnprintf( wxChar *str, size_t size, const wxChar *format, va_list argptr ) +#if wxUSE_UNICODE +int wxVsprintf(wchar_t *str, const wxString& format, va_list argptr) { - return vswprintf( str, size, wxFormatConverter(format), 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 ) + return wxCRT_VsprintfW(str, format.wc_str(), argptr); + else + #endif + return PrintfViaString(str, wxNO_LEN, format, argptr); +#endif // wxUSE_UNICODE_UTF8 } -#endif // wxVsnprintf +#endif // wxUSE_UNICODE -int wxVsprintf( wxChar *str, const wxChar *format, va_list argptr ) +int wxVsnprintf(char *str, size_t size, const wxString& format, va_list argptr) { - // same as for wxSprintf() - return vswprintf(str, INT_MAX / 4, wxFormatConverter(format), argptr); + int rv; +#if wxUSE_UTF8_LOCALE_ONLY + rv = wxCRT_VsnprintfA(str, size, format.wx_str(), argptr); +#else + #if wxUSE_UNICODE_UTF8 + if ( wxLocaleIsUtf8 ) + rv = wxCRT_VsnprintfA(str, size, format.wx_str(), argptr); + else + #endif + #if wxUSE_UNICODE + { + // 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, argptr); + } + #else + rv = wxCRT_VsnprintfA(str, size, format.mb_str(), argptr); + #endif +#endif + + // VsnprintfTestCase reveals that glibc's implementation of vswprintf + // doesn't nul terminate on truncation. + str[size - 1] = 0; + + return rv; } -#endif // wxNEED_PRINTF_CONVERSION +#if wxUSE_UNICODE +int wxVsnprintf(wchar_t *str, size_t size, const wxString& format, va_list argptr) +{ + int rv; + +#if wxUSE_UNICODE_WCHAR + rv = wxCRT_VsnprintfW(str, size, format.wc_str(), argptr); +#else // wxUSE_UNICODE_UTF8 + #if !wxUSE_UTF8_LOCALE_ONLY + if ( !wxLocaleIsUtf8 ) + 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, argptr); + } +#endif // wxUSE_UNICODE_UTF8 + + // VsnprintfTestCase reveals that glibc's implementation of vswprintf + // doesn't nul terminate on truncation. + str[size - 1] = 0; + + return rv; +} +#endif // wxUSE_UNICODE #if wxUSE_WCHAR_T @@ -644,32 +752,9 @@ int wxVsprintf( wxChar *str, const wxChar *format, va_list argptr ) // 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) { @@ -691,7 +776,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) { @@ -715,75 +800,52 @@ 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 wxCRT_StricmpA +WXDLLIMPEXP_BASE int wxCRT_StricmpA(const char *psz1, const char *psz2) +{ + register char c1, c2; + do { + c1 = wxTolower(*psz1++); + c2 = wxTolower(*psz2++); + } while ( c1 && (c1 == c2) ); + return c1 - c2; +} +#endif // !defined(wxCRT_StricmpA) -#ifndef wxStricmp -int WXDLLEXPORT wxStricmp(const wxChar *psz1, const wxChar *psz2) +#ifndef wxCRT_StricmpW +WXDLLIMPEXP_BASE int wxCRT_StricmpW(const wchar_t *psz1, const wchar_t *psz2) { - register wxChar c1, c2; + register wchar_t c1, c2; do { c1 = wxTolower(*psz1++); c2 = wxTolower(*psz2++); } while ( c1 && (c1 == c2) ); return c1 - c2; } -#endif +#endif // !defined(wxCRT_StricmpW) -#ifndef wxStricmp -int WXDLLEXPORT wxStrnicmp(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) { // initialize the variables just to suppress stupid gcc warning - register wxChar c1 = 0, c2 = 0; + register char c1 = 0, c2 = 0; while (n && ((c1 = wxTolower(*s1)) == (c2 = wxTolower(*s2)) ) && c1) n--, s1++, s2++; if (n) { if (c1 < c2) return -1; @@ -791,19 +853,30 @@ int WXDLLEXPORT wxStrnicmp(const wxChar *s1, const wxChar *s2, size_t n) } return 0; } -#endif +#endif // !defined(wxCRT_StrnicmpA) -#ifndef wxSetlocale -WXDLLEXPORT wxWCharBuffer wxSetlocale(int category, const wxChar *locale) +#ifndef wxCRT_StrnicmpW +WXDLLIMPEXP_BASE int wxCRT_StrnicmpW(const wchar_t *s1, const wchar_t *s2, size_t n) { - char *localeOld = setlocale(category, wxConvLibc.cWX2MB(locale)); - - return wxWCharBuffer(wxConvLibc.cMB2WC(localeOld)); + // 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; + } + return 0; } -#endif +#endif // !defined(wxCRT_StrnicmpW) + +// ---------------------------------------------------------------------------- +// string.h functions +// ---------------------------------------------------------------------------- -#if wxUSE_WCHAR_T && !defined(HAVE_WCSLEN) -WXDLLEXPORT size_t wxWcslen(const wchar_t *s) +// 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) { size_t n = 0; while ( *s++ ) @@ -814,331 +887,208 @@ WXDLLEXPORT size_t wxWcslen(const wchar_t *s) #endif // ---------------------------------------------------------------------------- -// string.h functions +// stdlib.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) -{ - wxChar *ret = dest; - while (*dest) dest++; - while ((*dest++ = *src++)); - return ret; -} - -WXDLLEXPORT const wxChar * wxStrchr(const wxChar *s, wxChar c) -{ - // be careful here as the terminating NUL makes part of the string - while ( *s != c ) - { - if ( !*s++ ) - return NULL; - } - - return s; -} - -WXDLLEXPORT int wxStrcmp(const wxChar *s1, const wxChar *s2) +#ifndef wxCRT_GetenvW +WXDLLIMPEXP_BASE wchar_t* wxCRT_GetenvW(const wchar_t *name) { - while ((*s1 == *s2) && *s1) s1++, s2++; - if ((wxUChar)*s1 < (wxUChar)*s2) return -1; - if ((wxUChar)*s1 > (wxUChar)*s2) return 1; - return 0; + // 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); + value = wxConvLibc.cMB2WC(getenv(wxConvLibc.cWC2MB(name))); + return value.data(); } +#endif // !wxCRT_GetenvW -WXDLLEXPORT wxChar * wxStrcpy(wxChar *dest, const wxChar *src) +#ifndef wxCRT_StrftimeW +WXDLLIMPEXP_BASE size_t +wxCRT_StrftimeW(wchar_t *s, size_t maxsize, const wchar_t *fmt, const struct tm *tm) { - wxChar *ret = dest; - while ((*dest++ = *src++)); - return ret; -} + if ( !maxsize ) + return 0; -WXDLLEXPORT size_t wxStrlen_(const wxChar *s) -{ - size_t n = 0; - while ( *s++ ) - n++; + wxCharBuffer buf(maxsize); - return n; -} + wxCharBuffer bufFmt(wxConvLibc.cWX2MB(fmt)); + if ( !bufFmt ) + return 0; + size_t ret = strftime(buf.data(), maxsize, bufFmt, tm); + if ( !ret ) + return 0; -WXDLLEXPORT wxChar * wxStrncat(wxChar *dest, const wxChar *src, size_t n) -{ - wxChar *ret = dest; - while (*dest) dest++; - while (n && (*dest++ = *src++)) n--; - return ret; -} + wxWCharBuffer wbuf = wxConvLibc.cMB2WX(buf); + if ( !wbuf ) + return 0; -WXDLLEXPORT int wxStrncmp(const wxChar *s1, const wxChar *s2, size_t n) -{ - while (n && (*s1 == *s2) && *s1) n--, s1++, s2++; - if (n) { - if ((wxUChar)*s1 < (wxUChar)*s2) return -1; - if ((wxUChar)*s1 > (wxUChar)*s2) return 1; - } - return 0; + wxCRT_StrncpyW(s, wbuf, maxsize); + return wxCRT_StrlenW(s); } +#endif // !wxCRT_StrftimeW -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; -} +#endif // wxUSE_WCHAR_T -WXDLLEXPORT const wxChar * wxStrpbrk(const wxChar *s, const wxChar *accept) +#ifdef wxLongLong_t +template +static wxULongLong_t +wxCRT_StrtoullBase(const T* nptr, T** endptr, int base, T* sign) { - while (*s && !wxStrchr(accept, *s)) - s++; + wxULongLong_t sum = 0; + wxString wxstr(nptr); + wxString::const_iterator i = wxstr.begin(); + wxString::const_iterator end = wxstr.end(); - return *s ? s : NULL; -} + // Skip spaces + while ( i != end && wxIsspace(*i) ) ++i; -WXDLLEXPORT const wxChar * wxStrrchr(const wxChar *s, wxChar c) -{ - const wxChar *ret = NULL; - do + // Starts with sign? + *sign = wxT(' '); + if ( i != end ) { - if ( *s == c ) - ret = s; - s++; + T c = *i; + if ( c == wxT('+') || c == wxT('-') ) + { + *sign = c; + ++i; + } } - 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) ) + // Starts with 0x? + if ( i != end && *i == wxT('0') ) { - if ( !wxStrncmp(fnd, needle, len) ) - return fnd; - - haystack = fnd + 1; + ++i; + if ( i != end ) + { + if ( *i == wxT('x') && (base == 16 || base == 0) ) + { + base = 16; + ++i; + } + else + { + if ( endptr ) + *endptr = (T*) nptr; + wxSET_ERRNO(EINVAL); + return sum; + } + } + else + --i; } - return NULL; -} - -#ifdef __cplusplus -} -#endif - -WXDLLEXPORT double wxStrtod(const wxChar *nptr, wxChar **endptr) -{ - const wxChar *start = nptr; - - // FIXME: only correct for C locale - while (wxIsspace(*nptr)) nptr++; - if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++; - while (wxIsdigit(*nptr)) nptr++; - if (*nptr == wxT('.')) { - nptr++; - while (wxIsdigit(*nptr)) nptr++; - } - if (*nptr == wxT('E') || *nptr == wxT('e')) { - nptr++; - if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++; - while (wxIsdigit(*nptr)) nptr++; - } - - wxString data(nptr, nptr-start); - wxWX2MBbuf dat = data.mb_str(wxConvLibc); - char *rdat = wxMBSTRINGCAST dat; - double ret = strtod(dat, &rdat); + if ( base == 0 ) + base = 10; - if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat)); + for ( ; i != end; ++i ) + { + unsigned int n; - return ret; -} + T c = *i; + if ( c >= '0' ) + { + if ( c <= '9' ) + n = c - wxT('0'); + else + n = wxTolower(c) - wxT('a') + 10; + } + else + break; -WXDLLEXPORT long int wxStrtol(const wxChar *nptr, wxChar **endptr, int base) -{ - const wxChar *start = nptr; - - // FIXME: only correct for C locale - while (wxIsspace(*nptr)) nptr++; - if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++; - if (((base == 0) || (base == 16)) && - (nptr[0] == wxT('0') && nptr[1] == wxT('x'))) { - nptr += 2; - base = 16; - } - else if ((base == 0) && (nptr[0] == wxT('0'))) base = 8; - else if (base == 0) base = 10; + if ( n >= (unsigned int)base ) + // Invalid character (for this base) + break; - while ((wxIsdigit(*nptr) && (*nptr - wxT('0') < base)) || - (wxIsalpha(*nptr) && (wxToupper(*nptr) - wxT('A') + 10 < base))) nptr++; + wxULongLong_t prevsum = sum; + sum = (sum * base) + n; - wxString data(start, nptr-start); - wxWX2MBbuf dat = data.mb_str(wxConvLibc); - char *rdat = wxMBSTRINGCAST dat; - long int ret = strtol(dat, &rdat, base); + if ( sum < prevsum ) + { + wxSET_ERRNO(ERANGE); + break; + } + } - if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat)); + if ( endptr ) + { + *endptr = (T*)(nptr + (i - wxstr.begin())); + } - return ret; + return sum; } -WXDLLEXPORT unsigned long int wxStrtoul(const wxChar *nptr, wxChar **endptr, int base) +template +static wxULongLong_t wxCRT_DoStrtoull(const T* nptr, T** endptr, int base) { - return (unsigned long int) wxStrtol(nptr, endptr, base); -} + T sign; + wxULongLong_t uval = ::wxCRT_StrtoullBase(nptr, endptr, base, &sign); -#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]; + if ( sign == wxT('-') ) + { + wxSET_ERRNO(ERANGE); + uval = 0; + } - return fopen( wxConvFile.cWX2MB(path), mode_buffer ); + return uval; } -WXDLLEXPORT FILE * wxFreopen(const wxChar *path, const wxChar *mode, FILE *stream) +template +static wxLongLong_t wxCRT_DoStrtoll(const T* nptr, T** endptr, int base) { - 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 ); -} + T sign; + wxULongLong_t uval = ::wxCRT_StrtoullBase(nptr, endptr, base, &sign); + wxLongLong_t val = 0; -WXDLLEXPORT int wxRemove(const wxChar *path) -{ - return remove( wxConvFile.cWX2MB(path) ); -} + if ( sign == wxT('-') ) + { + if (uval <= (wxULongLong_t)wxINT64_MAX + 1) + { + val = -(wxLongLong_t)uval; + } + else + { + wxSET_ERRNO(ERANGE); + } + } + else if ( uval <= wxINT64_MAX ) + { + val = uval; + } + else + { + wxSET_ERRNO(ERANGE); + } -WXDLLEXPORT int wxRename(const wxChar *oldpath, const wxChar *newpath) -{ - return rename( wxConvFile.cWX2MB(oldpath), wxConvFile.cWX2MB(newpath) ); + return val; } -#endif - -#ifndef wxAtof -double WXDLLEXPORT wxAtof(const wxChar *psz) -{ -#ifdef __WXWINCE__ - double d; - wxString str(psz); - if (str.ToDouble(& d)) - return d; - return 0.0; -#else - return atof(wxConvLibc.cWX2MB(psz)); +#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 -#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)); -} - -wxChar * WXDLLEXPORT wxGetenv(const wxChar *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))); - return value.data(); -#else - return getenv(name); +#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 -} - -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) -{ - if ( !maxsize ) - return 0; - - wxCharBuffer buf(maxsize); - - wxCharBuffer bufFmt(wxConvLibc.cWX2MB(fmt)); - if ( !bufFmt ) - return 0; - - size_t ret = strftime(buf.data(), maxsize, bufFmt, tm); - if ( !ret ) - return 0; - - wxWCharBuffer wbuf = wxConvLibc.cMB2WX(buf); - 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; -} -#endif // wxCtime -#endif // wxUSE_WCHAR_T +#endif // wxLongLong_t // ---------------------------------------------------------------------------- // functions which we may need even if !wxUSE_WCHAR_T // ---------------------------------------------------------------------------- -#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) { @@ -1150,15 +1100,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 = (T *)NULL; + return (T *)NULL; } - wxChar *ret = psz; + T *ret = psz; psz = wxStrpbrk(psz, delim); if (!psz) { - *save_ptr = (wxChar*)NULL; + *save_ptr = (T*)NULL; } else { @@ -1169,7 +1119,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 @@ -1197,11 +1154,157 @@ void *calloc( size_t num, size_t size ) #endif // __WXWINCE__ <= 211 -#ifdef __WXWINCE__ +// ============================================================================ +// wxLocaleIsUtf8 +// ============================================================================ -int wxRemove(const wxChar *path) +#if wxUSE_UNICODE_UTF8 + +#if !wxUSE_UTF8_LOCALE_ONLY +bool wxLocaleIsUtf8 = false; // the safer setting if not known +#endif + +static bool wxIsLocaleUtf8() { - return ::DeleteFile(path) == 0; + // NB: we intentionally don't use wxLocale::GetSystemEncodingName(), + // because a) it may be unavailable in some builds and b) has slightly + // different semantics (default locale instead of current) + +#if defined(HAVE_LANGINFO_H) && defined(CODESET) + // GNU libc provides current character set this way (this conforms to + // Unix98) + const char *charset = nl_langinfo(CODESET); + if ( charset ) + { + // "UTF-8" is used by modern glibc versions, but test other variants + // as well, just in case: + if ( strcmp(charset, "UTF-8") == 0 || + strcmp(charset, "utf-8") == 0 || + strcmp(charset, "UTF8") == 0 || + strcmp(charset, "utf8") == 0 ) + { + return true; + } + } +#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) ) + { + return true; + } + + // we don't know what charset libc is using, so assume the worst + // to be safe: + return false; } +void wxUpdateLocaleIsUtf8() +{ +#if wxUSE_UTF8_LOCALE_ONLY + if ( !wxIsLocaleUtf8() ) + { + wxLogFatalError(_T("This program requires UTF-8 locale to run.")); + } +#else // !wxUSE_UTF8_LOCALE_ONLY + wxLocaleIsUtf8 = wxIsLocaleUtf8(); #endif +} + +#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) +{ + CALL_ANSI_OR_UNICODE(return, + wxCRT_PutsA(s.mb_str()), + wxCRT_PutsW(s.wc_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 +// ---------------------------------------------------------------------------- + +#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) + { return wxCRT_VsscanfW(str, format, ap); } +int wxVsscanf(const wxCharBuffer& str, const char *format, va_list ap) + { return wxCRT_VsscanfA(wx_static_cast(const char*, 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(wx_static_cast(const char*, 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(wx_static_cast(const char*, 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