+ else // we already have ms_wcCharsetName
+ {
+ m2w = iconv_open(ms_wcCharsetName, cname);
+ }
+
+ // NB: don't ever pass NULL to iconv_open(), it may crash!
+ if ( ms_wcCharsetName )
+ {
+ w2m = iconv_open( cname, ms_wcCharsetName);
+ }
+ else
+ {
+ w2m = (iconv_t)-1;
+ }
+}
+
+IC_CharSet::~IC_CharSet()
+{
+ if ( m2w != (iconv_t)-1 )
+ iconv_close(m2w);
+ if ( w2m != (iconv_t)-1 )
+ iconv_close(w2m);
+}
+
+size_t IC_CharSet::MB2WC(wchar_t *buf, const char *psz, size_t n)
+{
+ size_t inbuf = strlen(psz);
+ size_t outbuf = n * SIZEOF_WCHAR_T;
+ size_t res, cres;
+ // VS: Use these instead of psz, buf because iconv() modifies its arguments:
+ wchar_t *bufPtr = buf;
+ const char *pszPtr = psz;
+
+ if (buf)
+ {
+ // have destination buffer, convert there
+ cres = iconv(m2w,
+ ICONV_CHAR_CAST(&pszPtr), &inbuf,
+ (char**)&bufPtr, &outbuf);
+ res = n - (outbuf / SIZEOF_WCHAR_T);
+
+ if (ms_wcNeedsSwap)
+ {
+ // 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
+ {
+ // no destination buffer... convert using temp buffer
+ // to calculate destination buffer requirement
+ wchar_t tbuf[8];
+ res = 0;
+ do {
+ bufPtr = tbuf;
+ outbuf = 8*SIZEOF_WCHAR_T;
+
+ cres = iconv(m2w,
+ ICONV_CHAR_CAST(&pszPtr), &inbuf,
+ (char**)&bufPtr, &outbuf );
+
+ res += 8-(outbuf/SIZEOF_WCHAR_T);
+ } while ((cres==(size_t)-1) && (errno==E2BIG));
+ }
+
+ if (ICONV_FAILED(cres, inbuf))
+ {
+ //VS: it is ok if iconv fails, hence trace only
+ wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
+ return (size_t)-1;
+ }
+
+ return res;
+}
+
+size_t IC_CharSet::WC2MB(char *buf, const wchar_t *psz, size_t n)
+{
+ size_t inbuf = wxWcslen(psz) * SIZEOF_WCHAR_T;
+ size_t outbuf = n;
+ size_t res, cres;
+
+ wchar_t *tmpbuf = 0;
+
+ if (ms_wcNeedsSwap)
+ {
+ // need to copy to temp buffer to switch endianness
+ // this absolutely doesn't rock!
+ // (no, doing WC_BSWAP twice on the original buffer won't help, as it
+ // could be in read-only memory, or be accessed in some other thread)
+ tmpbuf=(wchar_t*)malloc((inbuf+1)*SIZEOF_WCHAR_T);
+ memcpy(tmpbuf,psz,(inbuf+1)*SIZEOF_WCHAR_T);
+ WC_BSWAP(tmpbuf, inbuf)
+ psz=tmpbuf;
+ }
+
+ if (buf)
+ {
+ // have destination buffer, convert there
+ 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
+ {
+ // no destination buffer... convert using temp buffer
+ // to calculate destination buffer requirement
+ char tbuf[16];
+ res = 0;
+ do {
+ buf = tbuf; outbuf = 16;
+
+ cres = iconv( w2m, ICONV_CHAR_CAST(&psz), &inbuf, &buf, &outbuf );
+
+ res += 16 - outbuf;
+ } while ((cres==(size_t)-1) && (errno==E2BIG));
+ }
+
+ if (ms_wcNeedsSwap)
+ {
+ free(tmpbuf);
+ }
+
+ if (ICONV_FAILED(cres, inbuf))
+ {
+ //VS: it is ok if iconv fails, hence trace only
+ wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
+ return (size_t)-1;
+ }
+
+ return res;