+// ----------------------------------------------------------------------------
+// UTF-8
+// ----------------------------------------------------------------------------
+
+WXDLLEXPORT_DATA(wxMBConvUTF8) wxConvUTF8;
+
+static unsigned long utf8_max[]={0x7f,0x7ff,0xffff,0x1fffff,0x3ffffff,0x7fffffff,0xffffffff};
+
+size_t wxMBConvUTF8::MB2WC(wchar_t *buf, const char *psz, size_t n) const
+{
+ size_t len = 0;
+
+ while (*psz && ((!buf) || (len<n))) {
+ unsigned char cc=*psz++, fc=cc;
+ unsigned cnt;
+ for (cnt=0; fc&0x80; cnt++) fc<<=1;
+ if (!cnt) {
+ // plain ASCII char
+ if (buf) *buf++=cc;
+ len++;
+ } else {
+ cnt--;
+ if (!cnt) {
+ // invalid UTF-8 sequence
+ return (size_t)-1;
+ } else {
+ unsigned ocnt=cnt-1;
+ unsigned long res=cc&(0x3f>>cnt);
+ while (cnt--) {
+ cc = *psz++;
+ if ((cc&0xC0)!=0x80) {
+ // invalid UTF-8 sequence
+ return (size_t)-1;
+ }
+ res=(res<<6)|(cc&0x3f);
+ }
+ if (res<=utf8_max[ocnt]) {
+ // illegal UTF-8 encoding
+ return (size_t)-1;
+ }
+ if (buf) *buf++=res;
+ len++;
+ }
+ }
+ }
+ if (buf && (len<n)) *buf = 0;
+ return len;
+}
+
+size_t wxMBConvUTF8::WC2MB(char *buf, const wchar_t *psz, size_t n) const
+{
+ size_t len = 0;
+
+ while (*psz && ((!buf) || (len<n))) {
+ unsigned long cc=(*psz++)&0x7fffffff;
+ unsigned cnt;
+ for (cnt=0; cc>utf8_max[cnt]; cnt++);
+ if (!cnt) {
+ // plain ASCII char
+ if (buf) *buf++=cc;
+ len++;
+ } else {
+ len+=cnt+1;
+ if (buf) {
+ *buf++=(-128>>cnt)|((cc>>(cnt*6))&(0x3f>>cnt));
+ while (cnt--)
+ *buf++=0x80|((cc>>(cnt*6))&0x3f);
+ }
+ }
+ }
+ if (buf && (len<n)) *buf = 0;
+ return len;
+}
+
+// ----------------------------------------------------------------------------
+// specified character set
+// ----------------------------------------------------------------------------
+
+class wxCharacterSet
+{
+public:
+ wxArrayString names;
+ wchar_t *data;
+};
+
+#ifndef WX_PRECOMP
+ #include "wx/dynarray.h"
+ #include "wx/filefn.h"
+ #include "wx/textfile.h"
+ #include "wx/tokenzr.h"
+ #include "wx/utils.h"
+#endif
+
+WX_DECLARE_OBJARRAY(wxCharacterSet, wxCSArray);
+#include "wx/arrimpl.cpp"
+WX_DEFINE_OBJARRAY(wxCSArray);
+
+static wxCSArray wxCharsets;
+
+static void wxLoadCharacterSets(void)
+{
+ static bool already_loaded = FALSE;
+
+ if (already_loaded) return;
+
+ already_loaded = TRUE;
+#if defined(__UNIX__) && wxUSE_TEXTFILE
+ // search through files in /usr/share/i18n/charmaps
+ wxString fname;
+ for (fname = ::wxFindFirstFile(_T("/usr/share/i18n/charmaps/*"));
+ !fname.IsEmpty();
+ fname = ::wxFindNextFile()) {
+ wxTextFile cmap(fname);
+ if (cmap.Open()) {
+ wxCharacterSet *cset = new wxCharacterSet;
+ wxString comchar,escchar;
+ bool in_charset = FALSE;
+
+ // wxFprintf(stderr,_T("Loaded: %s\n"),fname.c_str());
+
+ wxString line;
+ for (line = cmap.GetFirstLine();
+ !cmap.Eof();
+ line = cmap.GetNextLine()) {
+ // wxFprintf(stderr,_T("line contents: %s\n"),line.c_str());
+ wxStringTokenizer token(line);
+ wxString cmd = token.GetNextToken();
+ if (cmd == comchar) {
+ if (token.GetNextToken() == _T("alias"))
+ cset->names.Add(token.GetNextToken());
+ }
+ else if (cmd == _T("<code_set_name>"))
+ cset->names.Add(token.GetNextToken());
+ else if (cmd == _T("<comment_char>"))
+ comchar = token.GetNextToken();
+ else if (cmd == _T("<escape_char>"))
+ escchar = token.GetNextToken();
+ else if (cmd == _T("<mb_cur_min>")) {
+ delete cset;
+ cset = (wxCharacterSet *) NULL;
+ break; // we don't support multibyte charsets ourselves (yet)
+ }
+ else if (cmd == _T("CHARMAP")) {
+ cset->data = (wchar_t *)calloc(256, sizeof(wchar_t));
+ in_charset = TRUE;
+ }
+ else if (cmd == _T("END")) {
+ if (token.GetNextToken() == _T("CHARMAP"))
+ in_charset = FALSE;
+ }
+ else if (in_charset) {
+ // format: <NUL> /x00 <U0000> NULL (NUL)
+ // <A> /x41 <U0041> LATIN CAPITAL LETTER A
+ wxString hex = token.GetNextToken();
+ // skip whitespace (why doesn't wxStringTokenizer do this?)
+ while (wxIsEmpty(hex) && token.HasMoreTokens()) hex = token.GetNextToken();
+ wxString uni = token.GetNextToken();
+ // skip whitespace again
+ while (wxIsEmpty(uni) && token.HasMoreTokens()) uni = token.GetNextToken();
+ if ((hex.Len() > 2) && (wxString(hex.GetChar(0)) == escchar) && (hex.GetChar(1) == _T('x')) &&
+ (uni.Left(2) == _T("<U"))) {
+ hex.MakeUpper(); uni.MakeUpper();
+ int pos = ::wxHexToDec(hex.Mid(2,2));
+ if (pos>=0) {
+ unsigned long uni1 = ::wxHexToDec(uni.Mid(2,2));
+ unsigned long uni2 = ::wxHexToDec(uni.Mid(4,2));
+ cset->data[pos] = (uni1 << 16) | uni2;
+ // wxFprintf(stderr,_T("char %02x mapped to %04x (%c)\n"),pos,cset->data[pos],cset->data[pos]);
+ }
+ }
+ }
+ }
+ if (cset) {
+ cset->names.Shrink();
+ wxCharsets.Add(cset);
+ }
+ }
+ }
+#endif
+ wxCharsets.Shrink();
+}
+
+static wxCharacterSet *wxFindCharacterSet(const wxChar *charset)
+{
+ if (!charset) return (wxCharacterSet *)NULL;
+ wxLoadCharacterSets();
+ for (size_t n=0; n<wxCharsets.GetCount(); n++)
+ if (wxCharsets[n].names.Index(charset) != wxNOT_FOUND)
+ return &(wxCharsets[n]);
+ return (wxCharacterSet *)NULL;
+}
+
+WXDLLEXPORT_DATA(wxCSConv) wxConvLocal((const wxChar *)NULL);
+
+wxCSConv::wxCSConv(const wxChar *charset)
+{
+ m_name = (wxChar *) NULL;
+ m_cset = (wxCharacterSet *) NULL;
+ m_deferred = TRUE;
+ SetName(charset);
+}
+
+wxCSConv::~wxCSConv()
+{
+ if (m_name) free(m_name);
+}
+
+void wxCSConv::SetName(const wxChar *charset)
+{
+ if (charset) {
+#ifdef __UNIX__
+ // first, convert the character set name to standard form
+ wxString codeset;
+ if (wxString(charset,3).CmpNoCase(_T("ISO")) == 0) {
+ // make sure it's represented in the standard form: ISO_8859-1
+ codeset = _T("ISO_");
+ charset += 3;
+ if ((*charset == _T('-')) || (*charset == _T('_'))) charset++;
+ if (wxStrlen(charset)>4) {
+ if (wxString(charset,4) == _T("8859")) {
+ codeset << _T("8859-");
+ if (*charset == _T('-')) charset++;
+ }
+ }
+ }
+ codeset << charset;
+ codeset.MakeUpper();
+ m_name = wxStrdup(codeset.c_str());
+ m_deferred = TRUE;
+#endif
+ }
+}
+
+void wxCSConv::LoadNow()
+{
+// wxPrintf(_T("Conversion request\n"));
+ if (m_deferred) {
+ if (!m_name) {
+#ifdef __UNIX__
+ wxChar *lang = wxGetenv(_T("LANG"));
+ wxChar *dot = lang ? wxStrchr(lang, _T('.')) : (wxChar *)NULL;
+ if (dot) SetName(dot+1);
+#endif
+ }
+ m_cset = wxFindCharacterSet(m_name);
+ m_deferred = FALSE;
+ }
+}
+
+size_t wxCSConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const
+{
+ ((wxCSConv *)this)->LoadNow(); // discard constness
+ if (buf) {
+ if (m_cset) {
+ for (size_t c=0; c<n; c++)
+ buf[c] = m_cset->data[(unsigned char)(psz[c])];
+ } else {
+ // latin-1 (direct)
+ for (size_t c=0; c<n; c++)
+ buf[c] = (unsigned char)(psz[c]);
+ }
+ return n;
+ }
+ return strlen(psz);
+}
+
+size_t wxCSConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const