X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/317246dfcbc87cc6c70eac2f7f33bf2d61a02ae5..0ae0bb792b13e17c62610245111c3ce7bf254464:/src/common/strconv.cpp diff --git a/src/common/strconv.cpp b/src/common/strconv.cpp index a868a4a2ad..cdbaabf73e 100644 --- a/src/common/strconv.cpp +++ b/src/common/strconv.cpp @@ -6,7 +6,7 @@ // Created: 29/01/98 // RCS-ID: $Id$ // Copyright: (c) 1999 Ove Kaaven, Robert Roebling, Vadim Zeitlin, Vaclav Slavik -// Licence: wxWindows license +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// // ============================================================================ @@ -17,7 +17,7 @@ // headers // ---------------------------------------------------------------------------- -#ifdef __GNUG__ +#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) #pragma implementation "strconv.h" #endif @@ -37,7 +37,10 @@ #include "wx/msw/private.h" #endif +#ifndef __WXWINCE__ #include +#endif + #include #include #include @@ -50,18 +53,19 @@ // ---------------------------------------------------------------------------- #if wxUSE_WCHAR_T - WXDLLEXPORT_DATA(wxMBConv) wxConvLibc; - WXDLLEXPORT_DATA(wxCSConv) wxConvLocal((const wxChar *)NULL); - WXDLLEXPORT_DATA(wxCSConv) wxConvISO8859_1(_T("iso-8859-1")); + WXDLLIMPEXP_DATA_BASE(wxMBConv) wxConvLibc; + WXDLLIMPEXP_DATA_BASE(wxCSConv) wxConvLocal((const wxChar *)NULL); + WXDLLIMPEXP_DATA_BASE(wxCSConv) wxConvISO8859_1(_T("iso-8859-1")); #else // stand-ins in absence of wchar_t - WXDLLEXPORT_DATA(wxMBConv) wxConvLibc, - wxConvFile, - wxConvISO8859_1, - wxConvLocal; + WXDLLIMPEXP_DATA_BASE(wxMBConv) wxConvLibc, + wxConvFile, + wxConvISO8859_1, + wxConvLocal, + wxConvUTF8; #endif // wxUSE_WCHAR_T -WXDLLEXPORT_DATA(wxMBConv *) wxConvCurrent = &wxConvLibc; +WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvCurrent = &wxConvLibc; class wxStrConvModule: public wxModule { @@ -274,53 +278,11 @@ const wxCharBuffer wxMBConv::cWC2MB(const wchar_t *pwz) const return buf; } -// ---------------------------------------------------------------------------- -// standard gdk conversion -// ---------------------------------------------------------------------------- - -#ifdef __WXGTK12__ - -WXDLLEXPORT_DATA(wxMBConvGdk) wxConvGdk; - -#include - -size_t wxMBConvGdk::MB2WC(wchar_t *buf, const char *psz, size_t n) const -{ - if (buf) - { - return gdk_mbstowcs((GdkWChar *)buf, psz, n); - } - else - { - GdkWChar *nbuf = new GdkWChar[n=strlen(psz)]; - size_t len = gdk_mbstowcs(nbuf, psz, n); - delete[] nbuf; - return len; - } -} - -size_t wxMBConvGdk::WC2MB(char *buf, const wchar_t *psz, size_t n) const -{ - char *mbstr = gdk_wcstombs((GdkWChar *)psz); - size_t len = mbstr ? strlen(mbstr) : 0; - if (buf) - { - if (len > n) - len = n; - memcpy(buf, psz, len); - if (len < n) - buf[len] = 0; - } - return len; -} - -#endif // GTK > 1.0 - // ---------------------------------------------------------------------------- // UTF-7 // ---------------------------------------------------------------------------- -WXDLLEXPORT_DATA(wxMBConvUTF7) wxConvUTF7; +WXDLLIMPEXP_DATA_BASE(wxMBConvUTF7) wxConvUTF7; #if 0 static char utf7_setD[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ" @@ -351,7 +313,7 @@ size_t wxMBConvUTF7::WC2MB(char * WXUNUSED(buf), // UTF-8 // ---------------------------------------------------------------------------- -WXDLLEXPORT_DATA(wxMBConvUTF8) wxConvUTF8; +WXDLLIMPEXP_DATA_BASE(wxMBConvUTF8) wxConvUTF8; static wxUint32 utf8_max[]= { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff, 0xffffffff }; @@ -471,13 +433,12 @@ size_t wxMBConvUTF8::WC2MB(char *buf, const wchar_t *psz, size_t n) const class wxCharacterSet { public: - wxCharacterSet(const wxChar*name) : cname(name) {} + wxCharacterSet() { } 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; }; // ---------------------------------------------------------------------------- @@ -487,8 +448,7 @@ public: class ID_CharSet : public wxCharacterSet { public: - ID_CharSet(const wxChar *name, wxMBConv *cnv) - : wxCharacterSet(name), work(cnv) {} + ID_CharSet(wxMBConv *cnv) : work(cnv) {} size_t MB2WC(wchar_t *buf, const char *psz, size_t n) { return work ? work->MB2WC(buf,psz,n) : (size_t)-1; } @@ -561,7 +521,6 @@ const char *IC_CharSet::ms_wcCharsetName = NULL; bool IC_CharSet::ms_wcNeedsSwap = FALSE; IC_CharSet::IC_CharSet(const wxChar *name) - : wxCharacterSet(name) { // Do it the hard way char cname[100]; @@ -784,23 +743,23 @@ size_t IC_CharSet::WC2MB(char *buf, const wchar_t *psz, size_t n) #if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__) -#ifdef __WXWINE__ - #define WINE_CAST (WCHAR *) -#else - #define WINE_CAST -#endif - -extern long wxCharsetToCodepage(const wxChar *charset); // from utils.cpp +// from utils.cpp +extern WXDLLIMPEXP_BASE long wxCharsetToCodepage(const wxChar *charset); +extern WXDLLIMPEXP_BASE long wxEncodingToCodepage(wxFontEncoding encoding); class CP_CharSet : public wxCharacterSet { public: CP_CharSet(const wxChar* name) - : wxCharacterSet(name) { m_CodePage = wxCharsetToCodepage(name); } + CP_CharSet(wxFontEncoding encoding) + { + m_CodePage = wxEncodingToCodepage(encoding); + } + size_t MB2WC(wchar_t *buf, const char *psz, size_t n) { const size_t len = ::MultiByteToWideChar @@ -809,7 +768,7 @@ public: 0, // flags (none) psz, // input string -1, // its length (NUL-terminated) - WINE_CAST buf, // output string + buf, // output string buf ? n : 0 // size of output buffer ); @@ -824,7 +783,7 @@ public: ( m_CodePage, // code page 0, // flags (none) - WINE_CAST psz, // input string + psz, // input string -1, // it is (wide) NUL-terminated buf, // output buffer buf ? n : 0, // and its size @@ -852,17 +811,31 @@ public: class EC_CharSet : public wxCharacterSet { +private: + void Init() + { + m_ok = m2w.Init(m_enc, wxFONTENCODING_UNICODE) && + w2m.Init(wxFONTENCODING_UNICODE, m_enc); + } + public: // temporarily just use wxEncodingConverter stuff, // so that it works while a better implementation is built - EC_CharSet(const wxChar* name) : wxCharacterSet(name), - enc(wxFONTENCODING_SYSTEM) + EC_CharSet(const wxChar* name) { if (name) - enc = wxFontMapper::Get()->CharsetToEncoding(name, FALSE); + m_enc = wxFontMapper::Get()->CharsetToEncoding(name, FALSE); + else + m_enc = wxFONTENCODING_SYSTEM; - m_ok = m2w.Init(enc, wxFONTENCODING_UNICODE) && - w2m.Init(wxFONTENCODING_UNICODE, enc); + Init(); + } + + EC_CharSet(wxFontEncoding enc) + { + m_enc = enc; + + Init(); } size_t MB2WC(wchar_t *buf, const char *psz, size_t WXUNUSED(n)) @@ -885,11 +858,13 @@ public: bool usable() const { return m_ok; } public: - wxFontEncoding enc; + wxFontEncoding m_enc; wxEncodingConverter m2w, w2m; // were we initialized successfully? bool m_ok; + + DECLARE_NO_COPY_CLASS(EC_CharSet) }; #endif // wxUSE_FONTMAP @@ -897,37 +872,46 @@ public: // ---------------------------------------------------------------------------- // the function creating the wxCharacterSet for the specified charset on the // current system, trying all possibilities +// +// it uses the name if it is given or encoding if name == NULL // ---------------------------------------------------------------------------- -static wxCharacterSet *wxGetCharacterSet(const wxChar *name) +static wxCharacterSet * +wxGetCharacterSet(const wxChar *name, wxFontEncoding encoding) { // check for the special case of ASCII charset + if ( (!name && encoding == wxFONTENCODING_DEFAULT) #if wxUSE_FONTMAP - if ( wxFontMapper::Get()->CharsetToEncoding(name) == wxFONTENCODING_DEFAULT ) -#else // wxUSE_FONTMAP - if ( !name ) -#endif // wxUSE_FONTMAP/!wxUSE_FONTMAP + || (name && wxFontMapper::Get()-> + CharsetToEncoding(name) == wxFONTENCODING_DEFAULT) +#endif // wxUSE_FONTMAP + ) { // don't convert at all return NULL; } - // the test above must have taken care of this case - wxCHECK_MSG( name, NULL, _T("NULL name must be wxFONTENCODING_DEFAULT") ); - wxCharacterSet *cset; - if ( wxStricmp(name, wxT("UTF8")) == 0 || wxStricmp(name, wxT("UTF-8")) == 0) + if ( (name && + (wxStricmp(name, wxT("UTF8")) == 0 || + wxStricmp(name, wxT("UTF-8")) == 0)) || + encoding == wxFONTENCODING_UTF8 ) { - cset = new ID_CharSet(name, &wxConvUTF8); + cset = new ID_CharSet(&wxConvUTF8); } - else + else // !UTF-8 { #ifdef HAVE_ICONV - cset = new IC_CharSet(name); -#else // !HAVE_ICONV - cset = NULL; -#endif // HAVE_ICONV/!HAVE_ICONV + if ( name ) + { + cset = new IC_CharSet(name); + } + else +#endif // HAVE_ICONV + { + cset = NULL; + } } // it can only be NULL in this case @@ -943,7 +927,7 @@ static wxCharacterSet *wxGetCharacterSet(const wxChar *name) } #if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__) - cset = new CP_CharSet(name); + cset = name ? new CP_CharSet(name) : new CP_CharSet(encoding); if ( cset->usable() ) return cset; @@ -952,7 +936,7 @@ static wxCharacterSet *wxGetCharacterSet(const wxChar *name) #endif // defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__) #if wxUSE_FONTMAP - cset = new EC_CharSet(name); + cset = name ? new EC_CharSet(name) : new EC_CharSet(encoding); if ( cset->usable() ) return cset; @@ -960,7 +944,15 @@ static wxCharacterSet *wxGetCharacterSet(const wxChar *name) cset = NULL; #endif // wxUSE_FONTMAP - wxLogError(_("Cannot convert from encoding '%s'!"), name); + wxLogError(_("Cannot convert from encoding '%s'!"), + name ? name + : +#if wxUSE_FONTMAP + wxFontMapper::GetEncodingDescription(encoding).c_str() +#else // !wxUSE_FONTMAP + wxString::Format(_T("%s"), encoding).c_str() +#endif // wxUSE_FONTMAP/!wxUSE_FONTMAP + ); return NULL; } @@ -969,40 +961,57 @@ static wxCharacterSet *wxGetCharacterSet(const wxChar *name) // wxCSConv implementation // ============================================================================ -wxCSConv::wxCSConv(const wxChar *charset) +void wxCSConv::Init() { m_name = (wxChar *)NULL; m_cset = (wxCharacterSet *) NULL; m_deferred = TRUE; +} + +wxCSConv::wxCSConv(const wxChar *charset) +{ + Init(); + m_encoding = wxFONTENCODING_DEFAULT; SetName(charset); } +wxCSConv::wxCSConv(wxFontEncoding encoding) +{ + Init(); + + m_encoding = encoding; +} + wxCSConv::~wxCSConv() { Clear(); } wxCSConv::wxCSConv(const wxCSConv& conv) - : wxMBConv() + : wxMBConv() { - Clear(); + Init(); + SetName(conv.m_name); + m_encoding = conv.m_encoding; } wxCSConv& wxCSConv::operator=(const wxCSConv& conv) { Clear(); + SetName(conv.m_name); + m_encoding = conv.m_encoding; + return *this; } void wxCSConv::Clear() { - if (m_name) - free(m_name); - if (m_cset) - delete m_cset; + free(m_name); + delete m_cset; + m_name = NULL; m_cset = NULL; } @@ -1023,7 +1032,7 @@ void wxCSConv::LoadNow() // it would probably be better to make GetSystemEncodingName() always // available (i.e. even when wxUSE_INTL == 0)? #if wxUSE_INTL - if ( !m_name ) + if ( !m_name && m_encoding == wxFONTENCODING_DEFAULT ) { wxString name = wxLocale::GetSystemEncodingName(); if ( !name.empty() ) @@ -1034,7 +1043,7 @@ void wxCSConv::LoadNow() #endif // wxUSE_INTL // wxGetCharacterSet() complains about NULL name - m_cset = m_name ? wxGetCharacterSet(m_name) : NULL; + m_cset = wxGetCharacterSet(m_name, m_encoding); m_deferred = FALSE; } }