X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/405d8f465740a78d2d19a2b108ac8fd65ff1a5e2..9b69526274b023fa1460b29a92bea8bf82e4703f:/src/common/strconv.cpp diff --git a/src/common/strconv.cpp b/src/common/strconv.cpp index 1f736fe8cd..0631cb5b68 100644 --- a/src/common/strconv.cpp +++ b/src/common/strconv.cpp @@ -60,7 +60,7 @@ WXDLLEXPORT_DATA(wxMBConv *) wxConvCurrent = &wxConvLibc; #include #endif -#ifdef HAVE_ICONV_H +#ifdef HAVE_ICONV #include #endif @@ -508,16 +508,11 @@ extern long wxCharsetToCodepage(const wxChar *name) class wxCharacterSet { public: - wxCharacterSet(const wxChar*name) - : cname(name) {} - virtual ~wxCharacterSet() - {} - virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) - { return (size_t)-1; } - virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) - { return (size_t)-1; } - virtual bool usable() - { return FALSE; } + wxCharacterSet(const wxChar*name) : cname(name) {} + virtual ~wxCharacterSet() {} + virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) = 0; + virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) = 0; + virtual bool usable() const = 0; public: const wxChar*cname; }; @@ -538,7 +533,7 @@ public: size_t WC2MB(char *buf, const wchar_t *psz, size_t n) { return work ? work->WC2MB(buf,psz,n) : (size_t)-1; } - bool usable() + bool usable() const { return work!=NULL; } public: wxMBConv*work; @@ -549,7 +544,7 @@ public: // The classes doing conversion using the iconv_xxx() functions // ============================================================================ -#ifdef HAVE_ICONV_H +#ifdef HAVE_ICONV // VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with E2BIG // if output buffer is _exactly_ as big as needed. Such case is (unless there's @@ -565,11 +560,7 @@ public: #define ICONV_FAILED(cres, bufLeft) (cres == (size_t)-1) #endif -#ifdef WX_ICONV_TAKES_CHAR - #define ICONV_CHAR_CAST(x) (char **)x -#else - #define ICONV_CHAR_CAST(x) (const char **)x -#endif +#define ICONV_CHAR_CAST(x) ((ICONV_CONST char **)(x)) // ---------------------------------------------------------------------------- // IC_CharSet: encapsulates an iconv character set @@ -664,7 +655,11 @@ IC_CharSet::IC_CharSet(const wxChar *name) else { ms_wcCharsetName = NULL; - wxLogError(_("Impossible to convert to/from charset '%s'."), name); + + // VS: we must not output an error here, since wxWindows will safely + // fall back to using wxEncodingConverter. + wxLogTrace(wxT("strconv"), wxT("Impossible to convert to/from charset '%s' with iconv, falling back to wxEncodingConverter."), name); + //wxLogError( } } wxLogTrace(wxT("strconv"), wxT("wchar_t charset is '%s', needs swap: %i"), ms_wcCharsetName, ms_wcNeedsSwap); @@ -805,7 +800,7 @@ size_t IC_CharSet::WC2MB(char *buf, const wchar_t *psz, size_t n) return res; } -#endif // HAVE_ICONV_H +#endif // HAVE_ICONV // ============================================================================ // Win32 conversion classes @@ -839,7 +834,7 @@ public: return len ? (buf ? len : len-1) : (size_t)-1; } - bool usable() + bool usable() const { return m_CodePage != -1; } public: @@ -863,8 +858,9 @@ public: { if (name) enc = wxTheFontMapper->CharsetToEncoding(name, FALSE); - m2w.Init(enc, wxFONTENCODING_UNICODE); - w2m.Init(wxFONTENCODING_UNICODE, enc); + + m_ok = m2w.Init(enc, wxFONTENCODING_UNICODE) && + w2m.Init(wxFONTENCODING_UNICODE, enc); } size_t MB2WC(wchar_t *buf, const char *psz, size_t n) @@ -889,12 +885,14 @@ public: return inbuf; } - bool usable() - { return (enc!=wxFONTENCODING_SYSTEM) && (enc!=wxFONTENCODING_DEFAULT); } + bool usable() const { return m_ok; } public: wxFontEncoding enc; wxEncodingConverter m2w, w2m; + + // were we initialized successfully? + bool m_ok; }; #endif // wxUSE_FONTMAP @@ -906,46 +904,61 @@ public: static wxCharacterSet *wxGetCharacterSet(const wxChar *name) { - wxCharacterSet *cset = NULL; - if (name) + // check for the special case of ASCII charset +#if wxUSE_FONTMAP + if ( wxTheFontMapper->CharsetToEncoding(name) == wxFONTENCODING_DEFAULT ) +#else // wxUSE_FONTMAP + if ( !name ) +#endif // wxUSE_FONTMAP/!wxUSE_FONTMAP { - if (wxStricmp(name, wxT("UTF8")) == 0 || wxStricmp(name, wxT("UTF-8")) == 0) - { - cset = new ID_CharSet(name, &wxConvUTF8); - } - else - { -#ifdef HAVE_ICONV_H - cset = new IC_CharSet(name); // may not take NULL -#endif - } + // don't convert at all + return NULL; } - if (cset && cset->usable()) - return cset; + // the test above must have taken care of this case + wxCHECK_MSG( name, NULL, _T("NULL name must be wxFONTENCODING_DEFAULT") ); - if (cset) + wxCharacterSet *cset; + + if ( wxStricmp(name, wxT("UTF8")) == 0 || wxStricmp(name, wxT("UTF-8")) == 0) + { + cset = new ID_CharSet(name, &wxConvUTF8); + } + else { - delete cset; +#ifdef HAVE_ICONV + cset = new IC_CharSet(name); +#else // !HAVE_ICONV cset = NULL; +#endif // HAVE_ICONV/!HAVE_ICONV } + if ( cset->usable() ) + return cset; + + delete cset; + cset = NULL; + #if defined(__WIN32__) && !defined(__WXMICROWIN__) - cset = new CP_CharSet(name); // may take NULL - if (cset->usable()) + cset = new CP_CharSet(name); + if ( cset->usable() ) return cset; delete cset; + cset = NULL; #endif // __WIN32__ #if wxUSE_FONTMAP cset = new EC_CharSet(name); - if (cset->usable()) + if ( cset->usable() ) return cset; -#endif // wxUSE_FONTMAP delete cset; - wxLogError(_("Unknown encoding '%s'!"), name); + cset = NULL; +#endif // wxUSE_FONTMAP + + wxLogError(_("Cannot convert from encoding '%s'!"), name); + return NULL; } @@ -1036,61 +1049,6 @@ size_t wxCSConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const return len; } -// VZ: are the classes below used at all?? -#if 0 - -#ifdef HAVE_ICONV_H - -class IC_CharSetConverter -{ -public: - IC_CharSetConverter(IC_CharSet *from, IC_CharSet *to) - { - cnv = iconv_open(wxConvLibc.cWX2MB(to->cname), - wxConvLibc.cWX2MB(from->cname)); - } - - ~IC_CharSetConverter() - { - if (cnv != (iconv_t)-1) - iconv_close(cnv); - } - - size_t Convert(char *buf, const char *psz, size_t n) - { - size_t inbuf = strlen(psz); - size_t outbuf = n; - size_t res = iconv( cnv, ICONV_CHAR_CAST(&psz), &inbuf, &buf, &outbuf ); - if (res == (size_t)-1) - return (size_t)-1; - return (n - outbuf); - } - -public: - iconv_t cnv; -}; - -#endif // HAVE_ICONV_H - -class EC_CharSetConverter -{ -public: - EC_CharSetConverter(EC_CharSet* from,EC_CharSet* to) - { cnv.Init(from->enc,to->enc); } - - size_t Convert(char* buf, const char* psz, size_t n) - { - size_t inbuf = strlen(psz); - if (buf) cnv.Convert(psz,buf); - return inbuf; - } - -public: - wxEncodingConverter cnv; -}; - -#endif // 0 - #else // !wxUSE_WCHAR_T // ----------------------------------------------------------------------------