#include "wx/defs.h"
#include "wx/string.h"
#include "wx/intl.h"
+#if wxUSE_THREADS
+ #include <wx/thread.h>
+#endif
#endif
#include <ctype.h>
#if wxUSE_UNICODE
// from multibyte string
-wxString::wxString(const char *psz, wxMBConvv& conv, size_t nLength)
+wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength)
{
// first get necessary size
#endif
-// ---------------------------------------------------------------------------
-// multibyte<->Unicode conversion
-// ---------------------------------------------------------------------------
-
-#if wxUSE_UNICODE
-const wxCharBuffer& mb_str(wxMBConv& conv) const
-{
- size_t nLen = conv.WC2MB((char *) NULL, m_pchData, 0);
- wxCharBuffer buf(nLen);
- conv.WC2MB(buf, m_pchData, nLen);
-}
-#else
-const wxWCharBuffer& wc_str(wxMBConv& conv) const
-{
- size_t nLen = conv.MB2WC((wchar_t *) NULL, m_pchData, 0);
- wxCharBuffer buf(nLen);
- conv.MB2WC(buf, m_pchData, nLen);
-}
-#endif
-
// ---------------------------------------------------------------------------
// memory allocation
// ---------------------------------------------------------------------------
char *val = va_arg(argptr, char *);
#if wxUSE_UNICODE
// ASCII->Unicode constructor handles max_width right
- wxString s(val, max_width);
+ wxString s(val, wxConv_libc, max_width);
#else
size_t len = wxSTRING_MAXLEN;
if (val) {
m_nCount = 0;
}
+// minimizes the memory usage by freeing unused memory
+void wxArrayString::Shrink()
+{
+ // only do it if we have some memory to free
+ if( m_nCount < m_nSize ) {
+ // allocates exactly as much memory as we need
+ wxChar **pNew = new wxChar *[m_nCount];
+
+ // copy data to new location
+ memcpy(pNew, m_pItems, m_nCount*sizeof(wxChar *));
+ delete [] m_pItems;
+ m_pItems = pNew;
+ }
+}
+
// searches the array for an item (forward or backwards)
int wxArrayString::Index(const wxChar *sz, bool bCase, bool bFromEnd) const
{
{
wxASSERT( str.GetStringData()->IsValid() );
- wxCHECK_RET( nIndex <= m_nCount, ("bad index in wxArrayString::Insert") );
+ wxCHECK_RET( nIndex <= m_nCount, _("bad index in wxArrayString::Insert") );
Grow();
// we can only sort one array at a time with the quick-sort based
// implementation
#if wxUSE_THREADS
- #include <wx/thread.h>
-
// need a critical section to protect access to gs_compareFunction and
// gs_sortAscending variables
static wxCriticalSection *gs_critsectStringSort = NULL;
// MBConv
// ============================================================================
-WXDLLEXPORT_DATA(wxMBConv) wxConv_libc;
+WXDLLEXPORT_DATA(wxMBConv *) wxConv_current = &wxConv_libc;
// ----------------------------------------------------------------------------
// standard libc conversion
// ----------------------------------------------------------------------------
-size_t wxMBConv::MB2WC(wchar_t *buf, const char *psz, size_t n)
+WXDLLEXPORT_DATA(wxMBConv) wxConv_libc;
+
+size_t wxMBConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const
+{
+ return wxMB2WC(buf, psz, n);
+}
+
+size_t wxMBConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
+{
+ return wxWC2MB(buf, psz, n);
+}
+
+// ----------------------------------------------------------------------------
+// standard file conversion
+// ----------------------------------------------------------------------------
+
+WXDLLEXPORT_DATA(wxMBConv_file) wxConv_file;
+
+// just use the libc conversion for now
+size_t wxMBConv_file::MB2WC(wchar_t *buf, const char *psz, size_t n) const
{
return wxMB2WC(buf, psz, n);
}
-size_t wxMBConv::WC2MB(char *buf, const wchar_t *psz, size_t n)
+size_t wxMBConv_file::WC2MB(char *buf, const wchar_t *psz, size_t n) const
{
return wxWC2MB(buf, psz, n);
}
// ----------------------------------------------------------------------------
-// UTF-7
+// standard gdk conversion
// ----------------------------------------------------------------------------
-class wxMBConv_UTF7
+#if defined(__WXGTK__) && (GTK_MINOR_VERSION > 0)
+WXDLLEXPORT_DATA(wxMBConv_gdk) wxConv_gdk;
+
+#include <gdk/gdk.h>
+
+size_t wxMBConv_gdk::MB2WC(wchar_t *buf, const char *psz, size_t n) const
+{
+ if (buf) {
+ return gdk_mbstowcs((GdkWChar *)buf, psz, n);
+ } else {
+ GdkWChar *nbuf = new GdkWChar[n=strlen(psz)];
+ size_t len = gdk_mbstowcs(nbuf, psz, n);
+ delete [] nbuf;
+ return len;
+ }
+}
+
+size_t wxMBConv_gdk::WC2MB(char *buf, const wchar_t *psz, size_t n) const
{
- virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n);
- virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n);
+ char *mbstr = gdk_wcstombs((GdkWChar *)psz);
+ size_t len = mbstr ? strlen(mbstr) : 0;
+ if (buf) {
+ if (len > n) len = n;
+ memcpy(buf, psz, len);
+ if (len < n) buf[len] = 0;
+ }
+ return len;
}
+#endif // GTK > 1.0
+
+// ----------------------------------------------------------------------------
+// UTF-7
+// ----------------------------------------------------------------------------
WXDLLEXPORT_DATA(wxMBConv_UTF7) wxConv_UTF7;
// TODO: write actual implementations of UTF-7 here
-size_t wxMBConv_UTF7::MB2WC(wchar_t *buf, const char *psz, size_t n)
+size_t wxMBConv_UTF7::MB2WC(wchar_t *buf, const char *psz, size_t n) const
{
return 0;
}
-size_t wxMBConv_UTF7::WC2MB(char *buf, const wchar_t *psz, size_t n)
+size_t wxMBConv_UTF7::WC2MB(char *buf, const wchar_t *psz, size_t n) const
{
return 0;
}
// UTF-8
// ----------------------------------------------------------------------------
-class wxMBConv_UTF8
-{
- virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n);
- virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n);
-}
-
WXDLLEXPORT_DATA(wxMBConv_UTF8) wxConv_UTF8;
// TODO: write actual implementations of UTF-8 here
-size_t wxMBConv_UTF8::MB2WC(wchar_t *buf, const char *psz, size_t n)
+size_t wxMBConv_UTF8::MB2WC(wchar_t *buf, const char *psz, size_t n) const
{
return wxMB2WC(buf, psz, n);
}
-size_t wxMBConv_UTF8::WC2MB(char *buf, const wchar_t *psz, size_t n)
+size_t wxMBConv_UTF8::WC2MB(char *buf, const wchar_t *psz, size_t n) const
{
return wxWC2MB(buf, psz, n);
}
// specified character set
// ----------------------------------------------------------------------------
-// TODO: write actual implementation of character set conversion here
+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;
+
+#if defined(__UNIX__)
+ // 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")) {
+ wxStringTokenizer names(token.GetNextToken(),_T("/"));
+ wxString name;
+ while (!(name = names.GetNextToken()).IsEmpty())
+ cset->names.Add(name);
+ }
+ }
+ 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.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();
+ already_loaded = TRUE;
+}
+
+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) wxConv_local((const wxChar *)NULL);
+
wxCSConv::wxCSConv(const wxChar *charset)
{
- data = (wxChar *) NULL;
+ m_name = (wxChar *) NULL;
+ m_cset = (wxCharacterSet *) NULL;
+ m_deferred = TRUE;
+ SetName(charset);
+}
+
+wxCSConv::~wxCSConv()
+{
+ if (m_name) free(m_name);
}
-size_t wxCSConv::MB2WC(wchar_t *buf, const char *psz, size_t n)
+void wxCSConv::SetName(const wxChar *charset)
{
- if (buf && !data) {
- // latin-1 (direct)
- for (size_t c=0; c<=n; c++)
- buf[c] = psz[c];
+ 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[psz[c]];
+ } else {
+ // latin-1 (direct)
+ for (size_t c=0; c<=n; c++)
+ buf[c] = psz[c];
+ }
}
return n;
}
-size_t wxCSConv::WC2MB(char *buf, const wchar_t *psz, size_t n)
+size_t wxCSConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
{
- if (buf && !data) {
- // latin-1 (direct)
- for (size_t c=0; c<=n; c++)
- buf[c] = (psz[c]>0xff) ? '?' : psz[c];
+ ((wxCSConv *)this)->LoadNow(); // discard constness
+ if (buf) {
+ if (m_cset) {
+ for (size_t c=0; c<=n; c++) {
+ size_t n;
+ for (n=0; (n<256) && (m_cset->data[n] != psz[c]); n++);
+ buf[c] = (n>0xff) ? '?' : n;
+ }
+ } else {
+ // latin-1 (direct)
+ for (size_t c=0; c<=n; c++)
+ buf[c] = (psz[c]>0xff) ? '?' : psz[c];
+ }
}
return n;
}