#include <clib.h>
#endif
-#ifdef HAVE_ICONV_H
+#ifdef HAVE_ICONV
#include <iconv.h>
#endif
class wxCharacterSet
{
public:
- wxCharacterSet(const wxChar*name)
- : cname(name) {}
- virtual ~wxCharacterSet()
- {}
- virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
- { return (size_t)-1; }
- virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n)
- { return (size_t)-1; }
- virtual bool usable()
- { return FALSE; }
+ wxCharacterSet(const wxChar*name) : cname(name) {}
+ virtual ~wxCharacterSet() {}
+ virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) = 0;
+ virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) = 0;
+ virtual bool usable() const = 0;
public:
const wxChar*cname;
};
size_t WC2MB(char *buf, const wchar_t *psz, size_t n)
{ return work ? work->WC2MB(buf,psz,n) : (size_t)-1; }
- bool usable()
+ bool usable() const
{ return work!=NULL; }
public:
wxMBConv*work;
// The classes doing conversion using the iconv_xxx() functions
// ============================================================================
-#ifdef HAVE_ICONV_H
+#ifdef HAVE_ICONV
// VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with E2BIG
// if output buffer is _exactly_ as big as needed. Such case is (unless there's
#define ICONV_FAILED(cres, bufLeft) (cres == (size_t)-1)
#endif
-#ifdef WX_ICONV_TAKES_CHAR
- #define ICONV_CHAR_CAST(x) (char **)x
-#else
- #define ICONV_CHAR_CAST(x) (const char **)x
-#endif
+#define ICONV_CHAR_CAST(x) ((ICONV_CONST char **)(x))
// ----------------------------------------------------------------------------
// IC_CharSet: encapsulates an iconv character set
return res;
}
-#endif // HAVE_ICONV_H
+#endif // HAVE_ICONV
// ============================================================================
// Win32 conversion classes
return len ? (buf ? len : len-1) : (size_t)-1;
}
- bool usable()
+ bool usable() const
{ return m_CodePage != -1; }
public:
{
if (name)
enc = wxTheFontMapper->CharsetToEncoding(name, FALSE);
- m2w.Init(enc, wxFONTENCODING_UNICODE);
- w2m.Init(wxFONTENCODING_UNICODE, enc);
+
+ m_ok = m2w.Init(enc, wxFONTENCODING_UNICODE) &&
+ w2m.Init(wxFONTENCODING_UNICODE, enc);
}
size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
return inbuf;
}
- bool usable()
- { return (enc!=wxFONTENCODING_SYSTEM) && (enc!=wxFONTENCODING_DEFAULT); }
+ bool usable() const { return m_ok; }
public:
wxFontEncoding enc;
wxEncodingConverter m2w, w2m;
+
+ // were we initialized successfully?
+ bool m_ok;
};
#endif // wxUSE_FONTMAP
static wxCharacterSet *wxGetCharacterSet(const wxChar *name)
{
- wxCharacterSet *cset = NULL;
- if (name)
+ // check for the special case of ASCII charset
+#if wxUSE_FONTMAP
+ if ( wxTheFontMapper->CharsetToEncoding(name) == wxFONTENCODING_DEFAULT )
+#else // wxUSE_FONTMAP
+ if ( !name )
+#endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
{
- 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
- }
+ // don't convert at all
+ return NULL;
}
- if (cset && cset->usable())
- return cset;
+ // the test above must have taken care of this case
+ wxCHECK_MSG( name, NULL, _T("NULL name must be wxFONTENCODING_DEFAULT") );
+
+ wxCharacterSet *cset;
- if (cset)
+ if ( wxStricmp(name, wxT("UTF8")) == 0 || wxStricmp(name, wxT("UTF-8")) == 0)
{
- delete cset;
+ 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
}
+ if ( cset->usable() )
+ return cset;
+
+ delete cset;
+ cset = NULL;
+
#if defined(__WIN32__) && !defined(__WXMICROWIN__)
- cset = new CP_CharSet(name); // may take NULL
- if (cset->usable())
+ cset = new CP_CharSet(name);
+ if ( cset->usable() )
return cset;
delete cset;
+ cset = NULL;
#endif // __WIN32__
#if wxUSE_FONTMAP
cset = new EC_CharSet(name);
- if (cset->usable())
+ if ( cset->usable() )
return cset;
-#endif // wxUSE_FONTMAP
delete cset;
- wxLogError(_("Unknown encoding '%s'!"), name);
+ cset = NULL;
+#endif // wxUSE_FONTMAP
+
+ wxLogError(_("Cannot convert from encoding '%s'!"), name);
+
return NULL;
}