X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/0c15fcc8144afb956192a4c57d71aae4fb6ff5b0..a696db453c79ff58509195244bcf260dfdf13c81:/src/common/strconv.cpp diff --git a/src/common/strconv.cpp b/src/common/strconv.cpp index e1d49bd9fb..9f327e1eff 100644 --- a/src/common/strconv.cpp +++ b/src/common/strconv.cpp @@ -67,7 +67,7 @@ public: virtual void OnExit() { #if wxUSE_WCHAR_T - wxConvLocal.Clear(); + wxConvLocal.Clear(); #endif } @@ -195,7 +195,6 @@ size_t wxMBConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const { for (size_t i = 0; i < strlen( psz )+1; i++) buf[i] = (wchar_t) psz[i]; - // printf( "libc %s\n", buf ); return strlen( psz ); } else @@ -214,7 +213,6 @@ size_t wxMBConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const { for (size_t i = 0; i < wxStrlen( psz )+1; i++) buf[i] = (char) psz[i]; - // printf( "libc %s\n", buf ); return wxStrlen( psz ); } else @@ -241,20 +239,16 @@ const wxWCharBuffer wxMBConv::cMB2WC(const char *psz) const return wxWCharBuffer((wchar_t *) NULL); } -const wxCharBuffer wxMBConv::cWC2MB(const wchar_t *psz) const +const wxCharBuffer wxMBConv::cWC2MB(const wchar_t *pwz) const { - if (psz) - { - size_t nLen = WC2MB((char *) NULL, psz, 0); // return value excludes /0 - if (nLen == (size_t)-1) - return wxCharBuffer((char *) NULL); - wxCharBuffer buf(nLen); // this allocates nLen+1 - WC2MB((char *)(const char *) buf, psz, nLen+1); - // printf( "str %s\n", (const char*) buf ); - return buf; - } - else - return wxCharBuffer((char *) NULL); + // 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); + + wxCharBuffer buf(nLen); // this allocates nLen+1 + WC2MB((char *)(const char *) buf, pwz, nLen+1); + return buf; } // ---------------------------------------------------------------------------- @@ -506,11 +500,7 @@ public: #define ICONV_FAILED(cres, bufLeft) (cres == (size_t)-1) #endif -#if ICONV_CONST -#define ICONV_CHAR_CAST(x) ((char **)(x)) -#else -#define ICONV_CHAR_CAST(x) ((const char **)(x)) -#endif +#define ICONV_CHAR_CAST(x) ((ICONV_CONST char **)(x)) // ---------------------------------------------------------------------------- // IC_CharSet: encapsulates an iconv character set @@ -665,6 +655,12 @@ size_t IC_CharSet::MB2WC(wchar_t *buf, const char *psz, size_t n) // convert to native endianness WC_BSWAP(buf /* _not_ bufPtr */, res) } + + // NB: iconv was given only strlen(psz) characters on input, and so + // it couldn't convert the trailing zero. Let's do it ourselves + // if there's some room left for it in the output buffer. + if (res < n) + buf[res] = 0; } else { @@ -724,6 +720,12 @@ size_t IC_CharSet::WC2MB(char *buf, const wchar_t *psz, size_t n) cres = iconv( w2m, ICONV_CHAR_CAST(&psz), &inbuf, &buf, &outbuf ); res = n-outbuf; + + // NB: iconv was given only wcslen(psz) characters on input, and so + // it couldn't convert the trailing zero. Let's do it ourselves + // if there's some room left for it in the output buffer. + if (res < n) + buf[0] = 0; } else { @@ -777,7 +779,11 @@ public: size_t MB2WC(wchar_t *buf, const char *psz, size_t n) { size_t len = +#ifdef __WXWINE__ + MultiByteToWideChar(m_CodePage, 0, psz, -1, (WCHAR*) buf, buf ? n : 0); +#else MultiByteToWideChar(m_CodePage, 0, psz, -1, buf, buf ? n : 0); +#endif //VS: returns # of written chars for buf!=NULL and *size* // needed buffer for buf==NULL return len ? (buf ? len : len-1) : (size_t)-1; @@ -785,8 +791,13 @@ public: size_t WC2MB(char *buf, const wchar_t *psz, size_t n) { +#ifdef __WXWINE__ + size_t len = WideCharToMultiByte(m_CodePage, 0, (const WCHAR*) psz, -1, buf, + buf ? n : 0, NULL, NULL); +#else size_t len = WideCharToMultiByte(m_CodePage, 0, psz, -1, buf, buf ? n : 0, NULL, NULL); +#endif //VS: returns # of written chars for buf!=NULL and *size* // needed buffer for buf==NULL return len ? (buf ? len : len-1) : (size_t)-1;