+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;
+}
+
+#endif // HAVE_ICONV
+
+// ============================================================================
+// Win32 conversion classes
+// ============================================================================
+
+#if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
+
+extern long wxCharsetToCodepage(const wxChar *charset); // from utils.cpp
+
+class CP_CharSet : public wxCharacterSet
+{
+public:
+ CP_CharSet(const wxChar* name)
+ : wxCharacterSet(name)
+ {
+ m_CodePage = wxCharsetToCodepage(name);
+ }
+
+ size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
+ {
+ const size_t len = ::MultiByteToWideChar
+ (
+ m_CodePage, // code page
+ 0, // flags (none)
+ psz, // input string
+ -1, // its length (NUL-terminated)
+ buf, // output string
+ buf ? n : 0 // size of output buffer
+ );
+
+ // note that it returns # of written chars for buf != NULL and *size*
+ // of the needed buffer for buf == NULL
+ return len ? (buf ? len : len - 1) : (size_t)-1;
+ }
+
+ size_t WC2MB(char *buf, const wchar_t *psz, size_t n)
+ {
+ const size_t len = ::WideCharToMultiByte
+ (
+ m_CodePage, // code page
+ 0, // flags (none)
+ psz, // input string
+ -1, // it is (wide) NUL-terminated
+ buf, // output buffer
+ buf ? n : 0, // and its size
+ NULL, // default "replacement" char
+ NULL // [out] was it used?
+ );
+
+ // see the comment above!
+ return len ? (buf ? len : len - 1) : (size_t)-1;
+ }
+
+ bool usable() const
+ { return m_CodePage != -1; }
+
+public:
+ long m_CodePage;
+};
+#endif // defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
+
+// ============================================================================
+// wxEncodingConverter based conversion classes
+// ============================================================================
+
+#if wxUSE_FONTMAP
+
+class EC_CharSet : public wxCharacterSet
+{
+public:
+ // temporarily just use wxEncodingConverter stuff,
+ // so that it works while a better implementation is built
+ EC_CharSet(const wxChar* name) : wxCharacterSet(name),
+ enc(wxFONTENCODING_SYSTEM)
+ {
+ if (name)
+ enc = wxFontMapper::Get()->CharsetToEncoding(name, FALSE);
+
+ m_ok = m2w.Init(enc, wxFONTENCODING_UNICODE) &&
+ w2m.Init(wxFONTENCODING_UNICODE, enc);
+ }
+
+ size_t MB2WC(wchar_t *buf, const char *psz, size_t WXUNUSED(n))
+ {
+ size_t inbuf = strlen(psz);
+ if (buf)
+ m2w.Convert(psz,buf);
+ return inbuf;
+ }
+
+ size_t WC2MB(char *buf, const wchar_t *psz, size_t WXUNUSED(n))
+ {
+ const size_t inbuf = wxWcslen(psz);
+ if (buf)
+ w2m.Convert(psz,buf);
+
+ return inbuf;
+ }
+
+ bool usable() const { return m_ok; }
+
+public:
+ wxFontEncoding enc;
+ wxEncodingConverter m2w, w2m;
+
+ // were we initialized successfully?
+ bool m_ok;
+};
+
+#endif // wxUSE_FONTMAP
+
+// ----------------------------------------------------------------------------
+// the function creating the wxCharacterSet for the specified charset on the
+// current system, trying all possibilities
+// ----------------------------------------------------------------------------
+
+static wxCharacterSet *wxGetCharacterSet(const wxChar *name)
+{
+ // check for the special case of ASCII charset
+#if wxUSE_FONTMAP
+ if ( wxFontMapper::Get()->CharsetToEncoding(name) == wxFONTENCODING_DEFAULT )
+#else // wxUSE_FONTMAP
+ if ( !name )
+#endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
+ {
+ // don't convert at all
+ return NULL;
+ }
+
+ // the test above must have taken care of this case
+ wxCHECK_MSG( name, NULL, _T("NULL name must be wxFONTENCODING_DEFAULT") );
+
+ wxCharacterSet *cset;
+
+ if ( wxStricmp(name, wxT("UTF8")) == 0 || wxStricmp(name, wxT("UTF-8")) == 0)
+ {
+ cset = new ID_CharSet(name, &wxConvUTF8);
+ }
+ else
+ {
+#ifdef HAVE_ICONV
+ cset = new IC_CharSet(name);
+#else // !HAVE_ICONV
+ cset = NULL;
+#endif // HAVE_ICONV/!HAVE_ICONV
+ }
+
+ // it can only be NULL in this case
+#ifndef HAVE_ICONV
+ if ( cset )
+#endif // !HAVE_ICONV
+ {
+ if ( cset->usable() )
+ return cset;
+
+ delete cset;
+ cset = NULL;
+ }
+
+#if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
+ cset = new CP_CharSet(name);
+ if ( cset->usable() )
+ return cset;
+
+ delete cset;
+ cset = NULL;
+#endif // defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
+
+#if wxUSE_FONTMAP
+ cset = new EC_CharSet(name);
+ if ( cset->usable() )
+ return cset;
+
+ delete cset;
+ cset = NULL;
+#endif // wxUSE_FONTMAP
+
+ wxLogError(_("Cannot convert from encoding '%s'!"), name);
+
+ return NULL;
+}
+
+// ============================================================================
+// wxCSConv implementation
+// ============================================================================