From b1210f9b6933f15f6b54f94be54f8f9e28e84156 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 13 Oct 2002 10:06:58 +0000 Subject: [PATCH] fix VC++ warnings (which could be errors for the other compilers) with returning wx(W)CharBuffer git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_4_BRANCH@17505 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/strconv.cpp | 43 +++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/src/common/strconv.cpp b/src/common/strconv.cpp index 723408f20c..8d8d255d43 100644 --- a/src/common/strconv.cpp +++ b/src/common/strconv.cpp @@ -231,28 +231,41 @@ size_t wxMBConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const const wxWCharBuffer wxMBConv::cMB2WC(const char *psz) const { - if (psz) + if ( psz ) { - size_t nLen = MB2WC((wchar_t *) NULL, psz, 0); // return value excludes /0 - if (nLen == (size_t)-1) - return wxWCharBuffer((wchar_t *) NULL); - wxWCharBuffer buf(nLen); // this allocates nLen1+ - MB2WC((wchar_t *)(const wchar_t *) buf, psz, nLen+1); - return buf; + // calculate the length of the buffer needed first + size_t nLen = MB2WC(NULL, psz, 0); + if ( nLen != (size_t)-1 ) + { + // now do the actual conversion + wxWCharBuffer buf(nLen); + MB2WC(buf.data(), psz, nLen + 1); // with the trailing NUL + + return buf; + } } - else - return wxWCharBuffer((wchar_t *) NULL); + + wxWCharBuffer buf((wchar_t *)NULL); + + return buf; } const wxCharBuffer wxMBConv::cWC2MB(const wchar_t *pwz) const { - // return value excludes NUL - size_t nLen = pwz ? WC2MB((char *) NULL, pwz, 0) : (size_t)-1; - if (nLen == (size_t)-1) - return wxCharBuffer((const char *)NULL); + if ( pwz ) + { + size_t nLen = WC2MB(NULL, pwz, 0); + if ( nLen != (size_t)-1 ) + { + wxCharBuffer buf(nLen); + WC2MB(buf.data(), pwz, nLen + 1); + + return buf; + } + } + + wxCharBuffer buf((char *)NULL); - wxCharBuffer buf(nLen); // this allocates nLen+1 - WC2MB((char *)(const char *) buf, pwz, nLen+1); return buf; }