+ size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
+ {
+ size_t len =
+ MultiByteToWideChar(CodePage, 0, psz, -1, buf, buf ? n : 0);
+ //VS: returns # of written chars for buf!=NULL and *size*
+ // 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)
+ {
+ size_t len = WideCharToMultiByte(CodePage, 0, psz, -1, buf,
+ buf ? n : 0, NULL, NULL);
+ //VS: returns # of written chars for buf!=NULL and *size*
+ // needed buffer for buf==NULL
+ return len ? (buf ? len : len-1) : (size_t)-1;
+ }
+
+ bool usable()
+ { return CodePage != -1; }
+
+public:
+ long CodePage;
+};
+#endif // __WIN32__
+
+#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 = wxTheFontMapper->CharsetToEncoding(name, FALSE);
+ m2w.Init(enc, wxFONTENCODING_UNICODE);
+ w2m.Init(wxFONTENCODING_UNICODE, enc);
+ }
+
+ size_t MB2WC(wchar_t *buf, const char *psz, size_t 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 n)
+ {
+#if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
+ size_t inbuf = std::wcslen(psz);
+#else
+ size_t inbuf = ::wcslen(psz);
+#endif
+ if (buf)
+ w2m.Convert(psz,buf);
+
+ return inbuf;
+ }
+
+ bool usable()
+ { return (enc!=wxFONTENCODING_SYSTEM) && (enc!=wxFONTENCODING_DEFAULT); }
+
+public:
+ wxFontEncoding enc;
+ wxEncodingConverter m2w, w2m;
+};
+
+#endif // wxUSE_FONTMAP
+
+static wxCharacterSet *wxGetCharacterSet(const wxChar *name)
+{
+ wxCharacterSet *cset = NULL;
+ if (name)
+ {
+ if (wxStricmp(name, wxT("UTF8")) == 0 || wxStricmp(name, wxT("UTF-8")) == 0)
+ {
+ cset = new ID_CharSet(name, &wxConvUTF8);
+ }
+ else
+ {
+#ifdef HAVE_ICONV_H
+ cset = new IC_CharSet(name); // may not take NULL
+#endif
+ }
+ }
+
+ if (cset && cset->usable())
+ return cset;
+
+ if (cset)
+ {
+ delete cset;
+ cset = NULL;
+ }
+
+#if defined(__WIN32__) && !defined(__WXMICROWIN__)
+ cset = new CP_CharSet(name); // may take NULL
+ if (cset->usable())
+ return cset;
+
+ delete cset;
+#endif // __WIN32__
+
+#if wxUSE_FONTMAP
+ cset = new EC_CharSet(name);
+ if (cset->usable())
+ return cset;
+#endif // wxUSE_FONTMAP
+
+ delete cset;
+ wxLogError(_("Unknown encoding '%s'!"), name);
+ return NULL;
+}