final cleanup of string conversion with null characters (hopefully :))
authorRyan Norton <wxprojects@comcast.net>
Sat, 6 Nov 2004 08:23:50 +0000 (08:23 +0000)
committerRyan Norton <wxprojects@comcast.net>
Sat, 6 Nov 2004 08:23:50 +0000 (08:23 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@30320 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/strconv.h
src/common/strconv.cpp
src/common/string.cpp

index a28fe448eb554fff9686cc3128a4b414c1232172..5d93e872ce33748a9478deb2cb5654976d33a262 100644 (file)
@@ -46,17 +46,17 @@ public:
     virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const = 0;
     virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const = 0;
 
-    // actual conversion for strings with embedded null characters
-    //
-    // outputSize is the size of the output buffer
-    // pszLen is the length of the input string (including all but last null character)
-    size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize, size_t pszLen) const;
-    size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize, size_t pszLen) const;
-
     // MB <-> WC
     const wxWCharBuffer cMB2WC(const char *psz) const;
     const wxCharBuffer cWC2MB(const wchar_t *psz) const;
 
+    // MB <-> WC for strings with embedded null characters
+    //
+    // pszLen length of the input string
+    // pOutSize gets the final size of the converted string
+    const wxWCharBuffer cMB2WC(const char *psz, size_t pszLen, size_t* pOutSize) const;
+    const wxCharBuffer cWC2MB(const wchar_t *psz, size_t pszLen, size_t* pOutSize) const;
+
     // convenience functions for converting MB or WC to/from wxWin default
 #if wxUSE_UNICODE
     const wxWCharBuffer cMB2WX(const char *psz) const { return cMB2WC(psz); }
index 88c3c6ca79a48a21f0e4261e11139539a187c5ff..56e2894bd3dce00743b4931c3cf3450e32d2321e 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;  
 }
 
 // ----------------------------------------------------------------------------
index 0eae6299c973bd7dae141f2e55919ca3363a942e..1c8fadcb69b5bd84bbd555260491b5a1781db20e 100644 (file)
@@ -1028,45 +1028,21 @@ wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength)
     // anything to do?
     if ( (nLen != 0) && (nLen != (size_t)-1) )
     {
-        //Get length of converted string
-        size_t dwConvLen = conv.MB2WC(NULL, psz, 0, nLen);
+        //Convert string
+        size_t nRealSize;
+        wxWCharBuffer theBuffer = conv.cMB2WC(psz, nLen, &nRealSize);
 
-        //if valid, do the conversion
-        if (dwConvLen != (size_t) -1)
-        {
-            //Get internal buffer
-            wxStringBufferLength internalBuffer(*this, dwConvLen + 1);
-
-            //Do the actual conversion & Set the length of the buffer
-            internalBuffer.SetLength(
-                   conv.MB2WC(internalBuffer, psz, dwConvLen + 1, nLen)
-                                    );
-        }
+        //Copy 
+        if (nRealSize)
+            assign( theBuffer.data() , nRealSize - 1 );
     }
 }
 
 //Convert wxString in Unicode mode to a multi-byte string
 const wxCharBuffer wxString::mb_str(wxMBConv& conv) const
 {
-    //Get length of converted string
-    size_t dwConvLen = conv.WC2MB(NULL, (*this).c_str(), 0, length());
-
-    //if valid, do the conversion
-    if (dwConvLen != (size_t) -1)
-    {
-        //Create good buffer
-        wxCharBuffer buffer(dwConvLen + 1);
-
-        //Do the actual conversion
-        conv.WC2MB(buffer.data(), (*this).c_str(), dwConvLen + 1, length());
-
-        return buffer;
-    }
-
-    //create bogus buffer
-    wxCharBuffer buffer(1);
-    buffer.data()[0u] = 0;
-    return buffer;
+    size_t dwOutSize;
+    return conv.cWC2MB(c_str(), length(), &dwOutSize);
 }
 
 #else // ANSI
@@ -1108,20 +1084,13 @@ wxString::wxString(const wchar_t *pwz, wxMBConv& conv, size_t nLength)
     // anything to do?
     if ( (nLen != 0) && (nLen != (size_t)-1) )
     {
-        //Get length of converted string
-        size_t dwConvLen = conv.WC2MB(NULL, pwz, 0, nLen);
+        //Convert string
+        size_t nRealSize;
+        wxCharBuffer theBuffer = conv.cWC2MB(pwz, nLen, &nRealSize);
 
-        //if valid, do the conversion
-        if (dwConvLen != (size_t) -1)
-        {
-            //Get internal buffer
-            wxStringBufferLength internalBuffer(*this, dwConvLen + 1);
-
-            //Do the actual conversion & Set the length of the buffer
-            internalBuffer.SetLength(
-                   conv.WC2MB(internalBuffer, pwz, dwConvLen + 1, nLen)
-                                    );
-        }
+        //Copy 
+        if (nRealSize)
+            assign( theBuffer.data() , nRealSize - 1 );
     }
 }
 
@@ -1129,25 +1098,8 @@ wxString::wxString(const wchar_t *pwz, wxMBConv& conv, size_t nLength)
 //mode is not enabled and wxUSE_WCHAR_T is enabled
 const wxWCharBuffer wxString::wc_str(wxMBConv& conv) const
 {
-    //Get length of converted string
-    size_t dwConvLen = conv.MB2WC(NULL, (*this).c_str(), 0, length());
-
-    //if valid, do the conversion
-    if (dwConvLen != (size_t) -1)
-    {
-        //Create good buffer
-        wxWCharBuffer buffer(dwConvLen + 1);
-
-        //Do the actual conversion
-        conv.MB2WC(buffer.data(), (*this).c_str(), dwConvLen + 1, length());
-
-        return buffer;
-    }
-
-    //create bogus buffer
-    wxWCharBuffer buffer(1);
-    buffer.data()[0u] = 0;
-    return buffer;
+    size_t dwOutSize;
+    return conv.cMB2WC(c_str(), length(), &dwOutSize);
 }
 
 #endif // wxUSE_WCHAR_T