#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
// true if the wide char encoding we use (i.e. ms_wcCharsetName) has
// different endian-ness than the native one
- static bool ms_wcNeedsSwap = FALSE;
+ static bool ms_wcNeedsSwap;
};
const char *IC_CharSet::ms_wcCharsetName = NULL;
+bool IC_CharSet::ms_wcNeedsSwap = FALSE;
IC_CharSet::IC_CharSet(const wxChar *name)
: wxCharacterSet(name)
else
{
ms_wcCharsetName = NULL;
- wxLogError(_("Impossible to convert to/from charset '%s'."), name);
+
+ // VS: we must not output an error here, since wxWindows will safely
+ // fall back to using wxEncodingConverter.
+ wxLogTrace(wxT("strconv"), wxT("Impossible to convert to/from charset '%s' with iconv, falling back to wxEncodingConverter."), name);
+ //wxLogError(
}
}
wxLogTrace(wxT("strconv"), wxT("wchar_t charset is '%s', needs swap: %i"), ms_wcCharsetName, ms_wcNeedsSwap);
{
w2m = iconv_open(wxConvLibc.cWX2MB(name), ms_wcCharsetName);
}
+ else
+ {
+ w2m = (iconv_t)-1;
+ }
}
IC_CharSet::~IC_CharSet()
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") );
- if (cset)
+ wxCharacterSet *cset;
+
+ if ( wxStricmp(name, wxT("UTF8")) == 0 || wxStricmp(name, wxT("UTF-8")) == 0)
+ {
+ cset = new ID_CharSet(name, &wxConvUTF8);
+ }
+ else
{
- delete cset;
+#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;
}
return len;
}
-// VZ: are the classes below used at all??
-#if 0
-
-#ifdef HAVE_ICONV_H
-
-class IC_CharSetConverter
-{
-public:
- IC_CharSetConverter(IC_CharSet *from, IC_CharSet *to)
- {
- cnv = iconv_open(wxConvLibc.cWX2MB(to->cname),
- wxConvLibc.cWX2MB(from->cname));
- }
-
- ~IC_CharSetConverter()
- {
- if (cnv != (iconv_t)-1)
- iconv_close(cnv);
- }
-
- size_t Convert(char *buf, const char *psz, size_t n)
- {
- size_t inbuf = strlen(psz);
- size_t outbuf = n;
- size_t res = iconv( cnv, ICONV_CHAR_CAST(&psz), &inbuf, &buf, &outbuf );
- if (res == (size_t)-1)
- return (size_t)-1;
- return (n - outbuf);
- }
-
-public:
- iconv_t cnv;
-};
-
-#endif // HAVE_ICONV_H
-
-class EC_CharSetConverter
-{
-public:
- EC_CharSetConverter(EC_CharSet* from,EC_CharSet* to)
- { cnv.Init(from->enc,to->enc); }
-
- size_t Convert(char* buf, const char* psz, size_t n)
- {
- size_t inbuf = strlen(psz);
- if (buf) cnv.Convert(psz,buf);
- return inbuf;
- }
-
-public:
- wxEncodingConverter cnv;
-};
-
-#endif // 0
-
#else // !wxUSE_WCHAR_T
// ----------------------------------------------------------------------------