]> git.saurik.com Git - wxWidgets.git/commitdiff
Add convenient wxMBConv::cMB2WC/WC2MB overloads taking buffers.
authorVadim Zeitlin <vadim@wxwidgets.org>
Sat, 12 Sep 2009 22:40:25 +0000 (22:40 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sat, 12 Sep 2009 22:40:25 +0000 (22:40 +0000)
These overloads allow not to worry about buffer lengths and just convert
between wxCharBuffer and wxWCharBuffer directly in a convenient way.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@61896 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

docs/changes.txt
include/wx/strconv.h
interface/wx/strconv.h
src/common/strconv.cpp

index 8043a1d7b732b57fd240a3ee9916910924bc3167..805c8b5bccea565e557aa75217283f55edaac5d9 100644 (file)
@@ -380,6 +380,7 @@ All:
 - Added bilinear image resizing algorithm to wxImage (bishop).
 - Fix bug with position argument in wxImage::Size() (Byron Sorgdrager).
 - Fix bug with parsing concatenated switches in wxCmdLineParser (Mike Funduc).
+- Added wxMBConv::cMB2WC(wxCharBuffer) and cWC2MB(wxWCharBuffer) overloads.
 
 All (GUI):
 
index 0061aa770c524c6a0a70689ba21d3d0686bd0eed..87911259a36e0b3f1506e90a8a80770edf21e888 100644 (file)
@@ -108,6 +108,12 @@ public:
     const wxCharBuffer
         cWC2MB(const wchar_t *in, size_t inLen, size_t *outLen) const;
 
+    // And yet more convenience functions for converting the entire buffers:
+    // these are the simplest and least error-prone as you never need to bother
+    // with lengths/sizes directly.
+    const wxWCharBuffer cMB2WC(const wxScopedCharBuffer& in) const;
+    const wxCharBuffer cWC2MB(const wxScopedWCharBuffer& in) 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 174133388e839ded8d50d8349873a27b1ec01544..2f1f55eda869a1ebef5b11153845e4ec9cc50bb1 100644 (file)
@@ -177,6 +177,23 @@ public:
                                size_t inLen,
                                size_t *outLen) const;
 
+    /**
+        Converts a char buffer to wide char one.
+
+        This is the most convenient and safest conversion function as you
+        don't have to deal with the buffer lengths directly. Use it if the
+        input buffer is known not to be empty or if you are sure that the
+        conversion is going to succeed -- otherwise, use the overload above to
+        be able to distinguish between empty input and conversion failure.
+
+        @return
+            The buffer containing the converted text, empty if the input was
+            empty or if the conversion failed.
+
+        @since 2.9.1
+     */
+    const wxWCharBuffer cMB2WC(const wxCharBuffer& buf) const;
+
     //@{
     /**
         Converts from multibyte encoding to the current wxChar type (which
@@ -204,6 +221,23 @@ public:
                               size_t inLen,
                               size_t *outLen) const;
 
+    /**
+        Converts a wide char buffer to char one.
+
+        This is the most convenient and safest conversion function as you
+        don't have to deal with the buffer lengths directly. Use it if the
+        input buffer is known not to be empty or if you are sure that the
+        conversion is going to succeed -- otherwise, use the overload above to
+        be able to distinguish between empty input and conversion failure.
+
+        @return
+            The buffer containing the converted text, empty if the input was
+            empty or if the conversion failed.
+
+        @since 2.9.1
+     */
+    const wxCharBuffer cWC2MB(const wxWCharBuffer& buf) const;
+
     //@{
     /**
         Converts from Unicode to the current wxChar type.
index bf207f880bb7757321e3e2ce19cd96f68817317f..c0d9daf06b2e59b8d78a43f0a16012e1f4b51df4 100644 (file)
@@ -490,6 +490,42 @@ wxMBConv::cWC2MB(const wchar_t *inBuff, size_t inLen, size_t *outLen) const
     return wxCharBuffer();
 }
 
+const wxWCharBuffer wxMBConv::cMB2WC(const wxScopedCharBuffer& buf) const
+{
+    const size_t srcLen = buf.length();
+    if ( srcLen )
+    {
+        const size_t dstLen = ToWChar(NULL, 0, buf, srcLen);
+        if ( dstLen != wxCONV_FAILED )
+        {
+            wxWCharBuffer wbuf(dstLen);
+            wbuf.data()[dstLen] = L'\0';
+            if ( ToWChar(wbuf.data(), dstLen, buf, srcLen) != wxCONV_FAILED )
+                return wbuf;
+        }
+    }
+
+    return wxWCharBuffer();
+}
+
+const wxCharBuffer wxMBConv::cWC2MB(const wxScopedWCharBuffer& wbuf) const
+{
+    const size_t srcLen = wbuf.length();
+    if ( srcLen )
+    {
+        const size_t dstLen = FromWChar(NULL, 0, wbuf, srcLen);
+        if ( dstLen != wxCONV_FAILED )
+        {
+            wxCharBuffer buf(dstLen);
+            buf.data()[dstLen] = '\0';
+            if ( FromWChar(buf.data(), dstLen, wbuf, srcLen) != wxCONV_FAILED )
+                return buf;
+        }
+    }
+
+    return wxCharBuffer();
+}
+
 // ----------------------------------------------------------------------------
 // wxMBConvLibc
 // ----------------------------------------------------------------------------