]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/strconv.cpp
1. derive wxGTK wxRadioBox from wxRadioBoxBase now, as in all other ports
[wxWidgets.git] / src / common / strconv.cpp
index 0b2da84fd31401a28cba64ea37867244c0a1bf6c..c55469c0a2be23b417715049b71c1d4b76b5844a 100644 (file)
@@ -1,5 +1,5 @@
 /////////////////////////////////////////////////////////////////////////////
 /////////////////////////////////////////////////////////////////////////////
-// Name:        strconv.cpp
+// Name:        src/common/strconv.cpp
 // Purpose:     Unicode conversion classes
 // Author:      Ove Kaaven, Robert Roebling, Vadim Zeitlin, Vaclav Slavik,
 //              Ryan Norton, Fredrik Roubert (UTF7)
 // Purpose:     Unicode conversion classes
 // Author:      Ove Kaaven, Robert Roebling, Vadim Zeitlin, Vaclav Slavik,
 //              Ryan Norton, Fredrik Roubert (UTF7)
 // headers
 // ----------------------------------------------------------------------------
 
 // headers
 // ----------------------------------------------------------------------------
 
-#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
-  #pragma implementation "strconv.h"
-#endif
-
 // For compilers that support precompilation, includes "wx.h".
 #include "wx/wxprec.h"
 
 // For compilers that support precompilation, includes "wx.h".
 #include "wx/wxprec.h"
 
 
 #define TRACE_STRCONV _T("strconv")
 
 
 #define TRACE_STRCONV _T("strconv")
 
+#if SIZEOF_WCHAR_T == 2
+    #define WC_UTF16
+#endif
+
 // ============================================================================
 // implementation
 // ============================================================================
 // ============================================================================
 // implementation
 // ============================================================================
@@ -657,7 +657,7 @@ size_t wxMBConvUTF8::MB2WC(wchar_t *buf, const char *psz, size_t n) const
                     }
 #else // !WC_UTF16
                     if (buf)
                     }
 #else // !WC_UTF16
                     if (buf)
-                        *buf++ = res;
+                        *buf++ = (wchar_t)res;
                     len++;
 #endif // WC_UTF16/!WC_UTF16
                 }
                     len++;
 #endif // WC_UTF16/!WC_UTF16
                 }
@@ -678,7 +678,7 @@ size_t wxMBConvUTF8::MB2WC(wchar_t *buf, const char *psz, size_t n) const
                         len += pa;
 #else
                         if (buf)
                         len += pa;
 #else
                         if (buf)
-                            *buf++ = wxUnicodePUA + (unsigned char)*opsz;
+                            *buf++ = (wchar_t)(wxUnicodePUA + (unsigned char)*opsz);
                         opsz++;
                         len++;
 #endif
                         opsz++;
                         len++;
 #endif
@@ -690,11 +690,11 @@ size_t wxMBConvUTF8::MB2WC(wchar_t *buf, const char *psz, size_t n) const
                     {
                         if ( buf && len + 3 < n )
                         {
                     {
                         if ( buf && len + 3 < n )
                         {
-                            unsigned char n = *opsz;
+                            unsigned char on = *opsz;
                             *buf++ = L'\\';
                             *buf++ = L'\\';
-                            *buf++ = (wchar_t)( L'0' + n / 0100 );
-                            *buf++ = (wchar_t)( L'0' + (n % 0100) / 010 );
-                            *buf++ = (wchar_t)( L'0' + n % 010 );
+                            *buf++ = (wchar_t)( L'0' + on / 0100 );
+                            *buf++ = (wchar_t)( L'0' + (on % 0100) / 010 );
+                            *buf++ = (wchar_t)( L'0' + on % 010 );
                         }
                         opsz++;
                         len += 4;
                         }
                         opsz++;
                         len += 4;
@@ -906,7 +906,7 @@ size_t wxMBConvUTF16straight::MB2WC(wchar_t *buf, const char *psz, size_t n) con
             return pa;
 
         if (buf)
             return pa;
 
         if (buf)
-            *buf++ = cc;
+            *buf++ = (wchar_t)cc;
         len++;
         psz += pa * sizeof(wxUint16);
     }
         len++;
         psz += pa * sizeof(wxUint16);
     }
@@ -966,7 +966,7 @@ size_t wxMBConvUTF16swap::MB2WC(wchar_t *buf, const char *psz, size_t n) const
             return pa;
 
         if (buf)
             return pa;
 
         if (buf)
-            *buf++ = cc;
+            *buf++ = (wchar_t)cc;
 
         len++;
         psz += pa * sizeof(wxUint16);
 
         len++;
         psz += pa * sizeof(wxUint16);
@@ -1167,7 +1167,7 @@ size_t wxMBConvUTF32straight::MB2WC(wchar_t *buf, const char *psz, size_t n) con
     while (*(wxUint32*)psz && (!buf || len < n))
     {
         if (buf)
     while (*(wxUint32*)psz && (!buf || len < n))
     {
         if (buf)
-            *buf++ = *(wxUint32*)psz;
+            *buf++ = (wchar_t)(*(wxUint32*)psz);
         len++;
         psz += sizeof(wxUint32);
     }
         len++;
         psz += sizeof(wxUint32);
     }
@@ -1353,6 +1353,8 @@ wxMBConv_iconv::wxMBConv_iconv(const wxChar *name)
     // check for charset that represents wchar_t:
     if ( ms_wcCharsetName.empty() )
     {
     // check for charset that represents wchar_t:
     if ( ms_wcCharsetName.empty() )
     {
+        wxLogTrace(TRACE_STRCONV, _T("Looking for wide char codeset:"));
+
 #if wxUSE_FONTMAP
         const wxChar **names = wxFontMapperBase::GetAllEncodingNames(WC_ENC);
 #else // !wxUSE_FONTMAP
 #if wxUSE_FONTMAP
         const wxChar **names = wxFontMapperBase::GetAllEncodingNames(WC_ENC);
 #else // !wxUSE_FONTMAP
@@ -1367,23 +1369,28 @@ wxMBConv_iconv::wxMBConv_iconv(const wxChar *name)
         };
 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
 
         };
 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
 
-        for ( ; *names; ++names )
+        for ( ; *names && ms_wcCharsetName.empty(); ++names )
         {
         {
-            const wxString name(*names);
+            const wxString nameCS(*names);
 
             // first try charset with explicit bytesex info (e.g. "UCS-4LE"):
 
             // first try charset with explicit bytesex info (e.g. "UCS-4LE"):
-            wxString nameXE(name);
+            wxString nameXE(nameCS);
             #ifdef WORDS_BIGENDIAN
                 nameXE += _T("BE");
             #else // little endian
                 nameXE += _T("LE");
             #endif
 
             #ifdef WORDS_BIGENDIAN
                 nameXE += _T("BE");
             #else // little endian
                 nameXE += _T("LE");
             #endif
 
+            wxLogTrace(TRACE_STRCONV, _T("  trying charset \"%s\""),
+                       nameXE.c_str());
+
             m2w = iconv_open(nameXE.ToAscii(), cname);
             if ( m2w == ICONV_T_INVALID )
             {
                 // try charset w/o bytesex info (e.g. "UCS4")
             m2w = iconv_open(nameXE.ToAscii(), cname);
             if ( m2w == ICONV_T_INVALID )
             {
                 // try charset w/o bytesex info (e.g. "UCS4")
-                m2w = iconv_open(name.ToAscii(), cname);
+                wxLogTrace(TRACE_STRCONV, _T("  trying charset \"%s\""),
+                           nameCS.c_str());
+                m2w = iconv_open(nameCS.ToAscii(), cname);
 
                 // and check for bytesex ourselves:
                 if ( m2w != ICONV_T_INVALID )
 
                 // and check for bytesex ourselves:
                 if ( m2w != ICONV_T_INVALID )
@@ -1408,11 +1415,11 @@ wxMBConv_iconv::wxMBConv_iconv(const wxChar *name)
                     {
                         wxLogLastError(wxT("iconv"));
                         wxLogError(_("Conversion to charset '%s' doesn't work."),
                     {
                         wxLogLastError(wxT("iconv"));
                         wxLogError(_("Conversion to charset '%s' doesn't work."),
-                                   name.c_str());
+                                   nameCS.c_str());
                     }
                     else // ok, can convert to this encoding, remember it
                     {
                     }
                     else // ok, can convert to this encoding, remember it
                     {
-                        ms_wcCharsetName = name
+                        ms_wcCharsetName = nameCS;
                         ms_wcNeedsSwap = wbuf[0] != (wchar_t)buf[0];
                     }
                 }
                         ms_wcNeedsSwap = wbuf[0] != (wchar_t)buf[0];
                     }
                 }
@@ -1489,8 +1496,8 @@ size_t wxMBConv_iconv::MB2WC(wchar_t *buf, const char *psz, size_t n) const
         if (ms_wcNeedsSwap)
         {
             // convert to native endianness
         if (ms_wcNeedsSwap)
         {
             // convert to native endianness
-            for ( unsigned n = 0; n < res; n++ )
-                buf[n] = WC_BSWAP(buf[n]);
+            for ( unsigned i = 0; i < res; i++ )
+                buf[n] = WC_BSWAP(buf[i]);
         }
 
         // NB: iconv was given only strlen(psz) characters on input, and so
         }
 
         // NB: iconv was given only strlen(psz) characters on input, and so
@@ -1534,7 +1541,8 @@ size_t wxMBConv_iconv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
     wxMutexLocker lock(wxConstCast(this, wxMBConv_iconv)->m_iconvMutex);
 #endif
 
     wxMutexLocker lock(wxConstCast(this, wxMBConv_iconv)->m_iconvMutex);
 #endif
 
-    size_t inbuf = wxWcslen(psz) * SIZEOF_WCHAR_T;
+    size_t inlen = wxWcslen(psz);
+    size_t inbuf = inlen * SIZEOF_WCHAR_T;
     size_t outbuf = n;
     size_t res, cres;
 
     size_t outbuf = n;
     size_t res, cres;
 
@@ -1546,9 +1554,9 @@ size_t wxMBConv_iconv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
         // (doing WC_BSWAP twice on the original buffer won't help, as it
         //  could be in read-only memory, or be accessed in some other thread)
         tmpbuf = (wchar_t *)malloc(inbuf + SIZEOF_WCHAR_T);
         // (doing WC_BSWAP twice on the original buffer won't help, as it
         //  could be in read-only memory, or be accessed in some other thread)
         tmpbuf = (wchar_t *)malloc(inbuf + SIZEOF_WCHAR_T);
-        for ( size_t n = 0; n < inbuf; n++ )
-            tmpbuf[n] = WC_BSWAP(psz[n]);
-        tmpbuf[inbuf] = L'\0';
+        for ( size_t i = 0; i < inlen; i++ )
+            tmpbuf[n] = WC_BSWAP(psz[i]);
+        tmpbuf[inlen] = L'\0';
         psz = tmpbuf;
     }
 
         psz = tmpbuf;
     }
 
@@ -1587,7 +1595,6 @@ size_t wxMBConv_iconv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
 
     if (ICONV_FAILED(cres, inbuf))
     {
 
     if (ICONV_FAILED(cres, inbuf))
     {
-        //VS: it is ok if iconv fails, hence trace only
         wxLogTrace(TRACE_STRCONV, wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
         return (size_t)-1;
     }
         wxLogTrace(TRACE_STRCONV, wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
         return (size_t)-1;
     }
@@ -2842,5 +2849,3 @@ WXDLLIMPEXP_DATA_BASE(wxMBConv) wxConvLibc,
                                 wxConvUTF8;
 
 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
                                 wxConvUTF8;
 
 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
-
-