X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/44d12026b8577bc687cb00802bbb9edf4d5204d3..5f7348ce627157e21bec507623ebd31c1e9dc762:/src/common/strconv.cpp diff --git a/src/common/strconv.cpp b/src/common/strconv.cpp index e3ef168ba3..40bc93d871 100644 --- a/src/common/strconv.cpp +++ b/src/common/strconv.cpp @@ -40,11 +40,8 @@ #if wxUSE_WCHAR_T -#ifdef __WXMSW__ - #include "wx/msw/private.h" -#endif - #ifdef __WINDOWS__ + #include "wx/msw/private.h" #include "wx/msw/missing.h" #endif @@ -55,9 +52,6 @@ #include #include #include -#ifdef HAVE_LANGINFO_H - #include -#endif #if defined(__WIN32__) && !defined(__WXMICROWIN__) #define wxHAVE_WIN32_MB2WC @@ -155,7 +149,7 @@ static size_t decode_utf16(const wxUint16* input, wxUint32& output) output = *input; return 1; } - else if ((input[1]<0xdc00) || (input[1]>=0xdfff)) + else if ((input[1]<0xdc00) || (input[1]>0xdfff)) { output = *input; return (size_t)-1; @@ -273,9 +267,9 @@ const wxWCharBuffer wxMBConv::cMB2WC(const char *szString, size_t nStringLen, si } //Increment to next (sub)string - //Note that we have to use strlen here instead of nLen - //here because XX2XX gives us the size of the output buffer, - //not neccessarly the length of the string + //Note that we have to use strlen instead of nLen here + //because XX2XX gives us the size of the output buffer, + //which is not necessarily the length of the string szPos += strlen(szPos) + 1; } @@ -335,9 +329,9 @@ const wxCharBuffer wxMBConv::cWC2MB(const wchar_t *szString, size_t nStringLen, } //Increment to next (sub)string - //Note that we have to use wxWcslen here instead of nLen - //here because XX2XX gives us the size of the output buffer, - //not neccessarly the length of the string + //Note that we have to use wxWcslen instead of nLen here + //because XX2XX gives us the size of the output buffer, + //which is not necessarily the length of the string szPos += wxWcslen(szPos) + 1; } @@ -360,45 +354,39 @@ size_t wxMBConvLibc::WC2MB(char *buf, const wchar_t *psz, size_t n) const return wxWC2MB(buf, psz, n); } +#ifdef __UNIX__ + // ---------------------------------------------------------------------------- -// wxConvBrokenFileNames is made for GTK2 in Unicode mode when -// files are accidentally written in an encoding which is not -// the system encoding. Typically, the system encoding will be -// UTF8 but there might be files stored in ISO8859-1 on disk. +// wxConvBrokenFileNames // ---------------------------------------------------------------------------- -class wxConvBrokenFileNames: public wxMBConvLibc -{ -public: - wxConvBrokenFileNames() : m_utf8conv(wxMBConvUTF8::MAP_INVALID_UTF8_TO_OCTAL) { } - virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const; - virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const; - inline bool UseUTF8() const; -private: - wxMBConvUTF8 m_utf8conv; -}; - -bool wxConvBrokenFileNames::UseUTF8() const +wxConvBrokenFileNames::wxConvBrokenFileNames(const wxChar *charset) { - return wxLocale::GetSystemEncoding() == wxFONTENCODING_UTF8; + if ( !charset || wxStricmp(charset, _T("UTF-8")) == 0 + || wxStricmp(charset, _T("UTF8")) == 0 ) + m_conv = new wxMBConvUTF8(wxMBConvUTF8::MAP_INVALID_UTF8_TO_OCTAL); + else + m_conv = new wxCSConv(charset); } -size_t wxConvBrokenFileNames::MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const +size_t +wxConvBrokenFileNames::MB2WC(wchar_t *outputBuf, + const char *psz, + size_t outputSize) const { - if (UseUTF8()) - return m_utf8conv.MB2WC( outputBuf, psz, outputSize ); - else - return wxMBConvLibc::MB2WC( outputBuf, psz, outputSize ); + return m_conv->MB2WC( outputBuf, psz, outputSize ); } -size_t wxConvBrokenFileNames::WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const +size_t +wxConvBrokenFileNames::WC2MB(char *outputBuf, + const wchar_t *psz, + size_t outputSize) const { - if (UseUTF8()) - return m_utf8conv.WC2MB( outputBuf, psz, outputSize ); - else - return wxMBConvLibc::WC2MB( outputBuf, psz, outputSize ); + return m_conv->WC2MB( outputBuf, psz, outputSize ); } +#endif + // ---------------------------------------------------------------------------- // UTF-7 // ---------------------------------------------------------------------------- @@ -553,7 +541,7 @@ size_t wxMBConvUTF7::WC2MB(char *buf, const wchar_t *psz, size_t n) const } #ifndef WC_UTF16 else if (((wxUint32)cc) > 0xffff) - { + { // no surrogate pair generation (yet?) return (size_t)-1; } @@ -633,6 +621,15 @@ size_t wxMBConvUTF8::MB2WC(wchar_t *buf, const char *psz, size_t n) const if (buf) *buf++ = cc; len++; + + // escape the escape character for octal escapes + if ((m_options & MAP_INVALID_UTF8_TO_OCTAL) + && cc == '\\' && (!buf || len < n)) + { + if (buf) + *buf++ = cc; + len++; + } } else { @@ -723,9 +720,9 @@ size_t wxMBConvUTF8::MB2WC(wchar_t *buf, const char *psz, size_t n) const { unsigned char n = *opsz; *buf++ = L'\\'; - *buf++ = L'0' + n / 0100; - *buf++ = L'0' + (n % 0100) / 010; - *buf++ = L'0' + n % 010; + *buf++ = (wchar_t)( L'0' + n / 0100 ); + *buf++ = (wchar_t)( L'0' + (n % 0100) / 010 ); + *buf++ = (wchar_t)( L'0' + n % 010 ); } opsz++; len += 4; @@ -770,15 +767,23 @@ size_t wxMBConvUTF8::WC2MB(char *buf, const wchar_t *psz, size_t n) const *buf++ = (char)(cc - wxUnicodePUA); len++; } + else if ( (m_options & MAP_INVALID_UTF8_TO_OCTAL) + && cc == L'\\' && psz[0] == L'\\' ) + { + if (buf) + *buf++ = (char)cc; + psz++; + len++; + } else if ( (m_options & MAP_INVALID_UTF8_TO_OCTAL) && cc == L'\\' && isoctal(psz[0]) && isoctal(psz[1]) && isoctal(psz[2]) ) { if (buf) { - *buf++ = (char) (psz[0] - L'0')*0100 + - (psz[1] - L'0')*010 + - (psz[2] - L'0'); + *buf++ = (char) ((psz[0] - L'0')*0100 + + (psz[1] - L'0')*010 + + (psz[2] - L'0')); } psz += 3; @@ -2108,7 +2113,7 @@ public: UniChar* szUniBuffer = (UniChar*) szUnConv; #if SIZEOF_WCHAR_T == 4 - wxMBConvUTF16BE converter ; + wxMBConvUTF16 converter ; nBufSize = converter.WC2MB( NULL , szUnConv , 0 ); szUniBuffer = new UniChar[ (nBufSize / sizeof(UniChar)) + 1] ; converter.WC2MB( (char*) szUniBuffer , szUnConv, nBufSize + sizeof(UniChar)) ; @@ -2244,7 +2249,7 @@ public: // we have to terminate here, because n might be larger for the trailing zero, and if UniChar // is not properly terminated we get random characters at the end ubuf[byteOutLen / sizeof( UniChar ) ] = 0 ; - wxMBConvUTF16BE converter ; + wxMBConvUTF16 converter ; res = converter.MB2WC( (buf ? buf : tbuf) , (const char*)ubuf , n ) ; free( ubuf ) ; #else @@ -2277,7 +2282,7 @@ public: ByteCount byteBufferLen = n ; UniChar* ubuf = NULL ; #if SIZEOF_WCHAR_T == 4 - wxMBConvUTF16BE converter ; + wxMBConvUTF16 converter ; size_t unicharlen = converter.WC2MB( NULL , psz , 0 ) ; byteInLen = unicharlen ; ubuf = (UniChar*) malloc( byteInLen + 2 ) ; @@ -2508,7 +2513,7 @@ wxMBConv *wxCSConv::DoCreate() const #if wxUSE_FONTMAP if ( name.empty() ) - name = wxFontMapperBase::Get()->GetEncodingName(m_encoding); + name = wxFontMapperBase::GetEncodingName(m_encoding); #endif // wxUSE_FONTMAP wxMBConv_iconv *conv = new wxMBConv_iconv(name); @@ -2731,7 +2736,6 @@ static wxCSConv wxConvLocalObj(wxFONTENCODING_SYSTEM); static wxCSConv wxConvISO8859_1Obj(wxFONTENCODING_ISO8859_1); static wxMBConvUTF7 wxConvUTF7Obj; static wxMBConvUTF8 wxConvUTF8Obj; -static wxConvBrokenFileNames wxConvBrokenFileNamesObj; WXDLLIMPEXP_DATA_BASE(wxMBConv&) wxConvLibc = wxConvLibcObj; WXDLLIMPEXP_DATA_BASE(wxCSConv&) wxConvLocal = wxConvLocalObj; @@ -2742,8 +2746,6 @@ WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvCurrent = &wxConvLibcObj; WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvFileName = & #ifdef __WXOSX__ wxConvUTF8Obj; -#elif __WXGTK20__ - wxConvBrokenFileNamesObj; #else wxConvLibcObj; #endif