+
+ size_t WC2MB(char *buf, const wchar_t *psz, size_t n)
+ {
+#if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
+ size_t inbuf = std::wcslen(psz) * SIZEOF_WCHAR_T;
+#else
+ size_t inbuf = ::wcslen(psz) * SIZEOF_WCHAR_T;
+#endif
+ size_t outbuf = n;
+ size_t res, cres;
+
+#ifdef WC_NEED_BSWAP
+ // 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)
+ wchar_t *tmpbuf=(wchar_t*)malloc((inbuf+1)*SIZEOF_WCHAR_T);
+ memcpy(tmpbuf,psz,(inbuf+1)*SIZEOF_WCHAR_T);
+ WC_BSWAP(tmpbuf, inbuf)
+ psz=tmpbuf;
+#endif
+ if (buf)
+ {
+ // have destination buffer, convert there
+#ifdef WX_ICONV_TAKES_CHAR
+ cres = iconv( w2m, (char**)&psz, &inbuf, &buf, &outbuf );
+#else
+ cres = iconv( w2m, (const char**)&psz, &inbuf, &buf, &outbuf );
+#endif
+ res = n-outbuf;
+ }
+ else
+ {
+ // no destination buffer... convert using temp buffer
+ // to calculate destination buffer requirement
+ char tbuf[16];
+ res = 0;
+ do {
+ buf = tbuf; outbuf = 16;
+#ifdef WX_ICONV_TAKES_CHAR
+ cres = iconv( w2m, (char**)&psz, &inbuf, &buf, &outbuf );
+#else
+ cres = iconv( w2m, (const char**)&psz, &inbuf, &buf, &outbuf );
+#endif
+ res += 16 - outbuf;
+ } while ((cres==(size_t)-1) && (errno==E2BIG));
+ }