]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/strconv.cpp
text attributes corrected
[wxWidgets.git] / src / common / strconv.cpp
index 6a4ff39c4faa1f5e3c1b68ad6524979de5fa5fe0..50571277e74f536f153cef1b21b928c5e0516b72 100644 (file)
@@ -217,14 +217,18 @@ const wxCharBuffer wxMBConv::cWC2MB(const wchar_t *pwz) const
     return buf;
 }
 
-size_t wxMBConv::MB2WC(wchar_t* szBuffer, const char* szString, 
-                       size_t outsize, size_t nStringLen) const
+const wxWCharBuffer wxMBConv::cMB2WC(const char *szString, size_t nStringLen, size_t* pOutSize) const
 {
+    wxASSERT(pOutSize != NULL);
+
     const char* szEnd = szString + nStringLen + 1;
     const char* szPos = szString;
     const char* szStart = szPos;
 
     size_t nActualLength = 0;
+    size_t nCurrentSize = nStringLen; //try normal size first (should never resize?)
+
+    wxWCharBuffer theBuffer(nCurrentSize);
 
     //Convert the string until the length() is reached, continuing the
     //loop every time a null character is reached
@@ -237,18 +241,31 @@ size_t wxMBConv::MB2WC(wchar_t* szBuffer, const char* szString,
 
         //Invalid conversion?
         if( nLen == (size_t)-1 )
-            return nLen;
+        {
+            *pOutSize = 0;
+            theBuffer.data()[0u] = wxT('\0');
+            return theBuffer;
+        }
+
 
         //Increase the actual length (+1 for current null character)
         nActualLength += nLen + 1;
 
-        //Only copy data in if buffer size is big enough
-        if (szBuffer != NULL &&
-            nActualLength <= outsize)
+        //if buffer too big, realloc the buffer
+        if (nActualLength > (nCurrentSize+1))
         {
-            //Convert the current (sub)string
-            if ( MB2WC(&szBuffer[szPos - szStart], szPos, nLen + 1) == (size_t)-1 )
-                return (size_t)-1;
+            wxWCharBuffer theNewBuffer(nCurrentSize << 1);
+            memcpy(theNewBuffer.data(), theBuffer.data(), nCurrentSize * sizeof(wchar_t));
+            theBuffer = theNewBuffer;
+            nCurrentSize <<= 1;
+        }
+
+        //Convert the current (sub)string
+        if ( MB2WC(&theBuffer.data()[szPos - szStart], szPos, nLen + 1) == (size_t)-1 )
+        {
+            *pOutSize = 0;
+            theBuffer.data()[0u] = wxT('\0');
+            return theBuffer;
         }
 
         //Increment to next (sub)string
@@ -258,17 +275,23 @@ size_t wxMBConv::MB2WC(wchar_t* szBuffer, const char* szString,
         szPos += strlen(szPos) + 1;
     }
 
-    return nActualLength - 1; //success - return actual length
+    //success - return actual length and the buffer
+    *pOutSize = nActualLength;
+    return theBuffer;  
 }
 
-size_t wxMBConv::WC2MB(char* szBuffer, const wchar_t* szString, 
-                       size_t outsize, size_t nStringLen) const
+const wxCharBuffer wxMBConv::cWC2MB(const wchar_t *szString, size_t nStringLen, size_t* pOutSize) const
 {
+    wxASSERT(pOutSize != NULL);
+
     const wchar_t* szEnd = szString + nStringLen + 1;
     const wchar_t* szPos = szString;
     const wchar_t* szStart = szPos;
 
     size_t nActualLength = 0;
+    size_t nCurrentSize = nStringLen << 2; //try * 4 first
+
+    wxCharBuffer theBuffer(nCurrentSize);
 
     //Convert the string until the length() is reached, continuing the
     //loop every time a null character is reached
@@ -281,18 +304,30 @@ size_t wxMBConv::WC2MB(char* szBuffer, const wchar_t* szString,
 
         //Invalid conversion?
         if( nLen == (size_t)-1 )
-            return nLen;
+        {
+            *pOutSize = 0;
+            theBuffer.data()[0u] = wxT('\0');
+            return theBuffer;
+        }
 
         //Increase the actual length (+1 for current null character)
         nActualLength += nLen + 1;
         
-        //Only copy data in if buffer size is big enough
-        if (szBuffer != NULL &&
-            nActualLength <= outsize)
+        //if buffer too big, realloc the buffer
+        if (nActualLength > (nCurrentSize+1))
         {
-            //Convert the current (sub)string
-            if(WC2MB(&szBuffer[szPos - szStart], szPos, nLen + 1) == (size_t)-1 )
-                return (size_t)-1;
+            wxCharBuffer theNewBuffer(nCurrentSize << 1);
+            memcpy(theNewBuffer.data(), theBuffer.data(), nCurrentSize);
+            theBuffer = theNewBuffer;
+            nCurrentSize <<= 1;
+        }
+
+        //Convert the current (sub)string
+        if(WC2MB(&theBuffer.data()[szPos - szStart], szPos, nLen + 1) == (size_t)-1 )
+        {
+            *pOutSize = 0;
+            theBuffer.data()[0u] = wxT('\0');
+            return theBuffer;
         }
 
         //Increment to next (sub)string
@@ -302,7 +337,9 @@ size_t wxMBConv::WC2MB(char* szBuffer, const wchar_t* szString,
         szPos += wxWcslen(szPos) + 1;
     }
 
-    return nActualLength - 1;  //success - return actual length
+    //success - return actual length and the buffer
+    *pOutSize = nActualLength;
+    return theBuffer;  
 }
 
 // ----------------------------------------------------------------------------
@@ -399,7 +436,7 @@ size_t wxMBConvUTF7::MB2WC(wchar_t *buf, const char *psz, size_t n) const
                 d += cc;
                 for (l += 6; l >= 8; lsb = !lsb)
                 {
-                    c = (d >> (l -= 8)) % 256;
+                    c = (unsigned char)((d >> (l -= 8)) % 256);
                     if (lsb)
                     {
                         if (buf)
@@ -408,7 +445,7 @@ size_t wxMBConvUTF7::MB2WC(wchar_t *buf, const char *psz, size_t n) const
                     }
                     else
                         if (buf)
-                            *buf = c << 8;
+                            *buf = (wchar_t)(c << 8);
                 }
             }
             if (*psz == '-')
@@ -473,12 +510,8 @@ size_t wxMBConvUTF7::WC2MB(char *buf, const wchar_t
             len++;
         }
 #ifndef WC_UTF16
-#ifdef __VMS
-       else if (cc > 0xffff)
-#else
-       else if (cc > ((const wchar_t)0xffff))
-#endif
-        {
+        else if (((wxUint32)cc) > 0xffff)
+           {
             // no surrogate pair generation (yet?)
             return (size_t)-1;
         }
@@ -2024,7 +2057,7 @@ public:
         if (buf == NULL)
         {
             //apple specs say at least 32
-            n = max( 32 , byteInLen ) ;
+            n = wxMax( 32 , byteInLen ) ;
             tbuf = (wchar_t*) malloc( n * SIZEOF_WCHAR_T) ;
         }
         ByteCount byteBufferLen = n * sizeof( UniChar ) ;
@@ -2065,7 +2098,7 @@ public:
         if (buf == NULL)
         {
             //apple specs say at least 32
-            n = max( 32 , ((byteInLen / SIZEOF_WCHAR_T) * 8) + SIZEOF_WCHAR_T );
+            n = wxMax( 32 , ((byteInLen / SIZEOF_WCHAR_T) * 8) + SIZEOF_WCHAR_T );
             tbuf = (char*) malloc( n ) ;
         }