]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/string.cpp
Uses wxUSE_WCHAR_T for compilers that don't support wchar_t.
[wxWidgets.git] / src / common / string.cpp
index 7759ed8766ed8c91d33f326ee257dfff63106e3d..2d371b9fa4893a67ab64d2d8f63d177a4dff5c16 100644 (file)
@@ -256,8 +256,7 @@ wxString::wxString(const void *pStart, const void *pEnd)
 wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength)
 {
   // first get necessary size
 wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength)
 {
   // first get necessary size
-
-  size_t nLen = conv.MB2WC((wchar_t *) NULL, psz, 0);
+  size_t nLen = psz ? conv.MB2WC((wchar_t *) NULL, psz, 0) : 0;
 
   // nLength is number of *Unicode* characters here!
   if (nLen > nLength)
 
   // nLength is number of *Unicode* characters here!
   if (nLen > nLength)
@@ -279,8 +278,7 @@ wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength)
 wxString::wxString(const wchar_t *pwz)
 {
   // first get necessary size
 wxString::wxString(const wchar_t *pwz)
 {
   // first get necessary size
-
-  size_t nLen = wxWC2MB((char *) NULL, pwz, 0);
+  size_t nLen = pwz ? wxWC2MB((char *) NULL, pwz, 0) : 0;
 
   // empty?
   if ( nLen != 0 ) {
 
   // empty?
   if ( nLen != 0 ) {
@@ -1187,8 +1185,8 @@ int wxString::PrintfV(const wxChar* pszFormat, va_list argptr)
            if (s.Len() < min_width)
              s.Pad(min_width - s.Len(), _T(' '), adj_left);
            *this += s;
            if (s.Len() < min_width)
              s.Pad(min_width - s.Len(), _T(' '), adj_left);
            *this += s;
-           done = TRUE;
          }
          }
+         done = TRUE;
          break;
        case _T('n'):
          if (ilen == 0) {
          break;
        case _T('n'):
          if (ilen == 0) {
@@ -2021,6 +2019,7 @@ static void wxLoadCharacterSets(void)
 
 static wxCharacterSet *wxFindCharacterSet(const wxChar *charset)
 {
 
 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)
   wxLoadCharacterSets();
   for (size_t n=0; n<wxCharsets.GetCount(); n++)
     if (wxCharsets[n].names.Index(charset) != wxNOT_FOUND)
@@ -2032,15 +2031,19 @@ WXDLLEXPORT_DATA(wxCSConv) wxConv_local((const wxChar *)NULL);
 
 wxCSConv::wxCSConv(const wxChar *charset)
 {
 
 wxCSConv::wxCSConv(const wxChar *charset)
 {
-  if (!charset) {
-#ifdef __UNIX__
-    wxChar *lang = wxGetenv(_T("LANG"));
-    wxChar *dot = lang ? wxStrchr(lang, _T('.')) : (wxChar *)NULL;
-    if (dot) charset = dot+1;
-#endif
-  }
+  m_name = (wxChar *) NULL;
   m_cset = (wxCharacterSet *) NULL;
   m_cset = (wxCharacterSet *) NULL;
-  m_deferred = FALSE;
+  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
   if (charset) {
 #ifdef __UNIX__
     // first, convert the character set name to standard form
@@ -2065,15 +2068,17 @@ wxCSConv::wxCSConv(const wxChar *charset)
   }
 }
 
   }
 }
 
-wxCSConv::~wxCSConv()
-{
-  free(m_name);
-}
-
 void wxCSConv::LoadNow()
 {
 //  wxPrintf(_T("Conversion request\n"));
   if (m_deferred) {
 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;
   }
     m_cset = wxFindCharacterSet(m_name);
     m_deferred = FALSE;
   }
@@ -2084,15 +2089,16 @@ size_t wxCSConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const
   ((wxCSConv *)this)->LoadNow(); // discard constness
   if (buf) {
     if (m_cset) {
   ((wxCSConv *)this)->LoadNow(); // discard constness
   if (buf) {
     if (m_cset) {
-      for (size_t c=0; c<=n; c++)
+      for (size_t c=0; c<n; c++)
        buf[c] = m_cset->data[psz[c]];
     } else {
       // latin-1 (direct)
        buf[c] = m_cset->data[psz[c]];
     } else {
       // latin-1 (direct)
-      for (size_t c=0; c<=n; c++)
+      for (size_t c=0; c<n; c++)
        buf[c] = psz[c];
     }
        buf[c] = psz[c];
     }
+    return n;
   }
   }
-  return n;
+  return strlen(psz);
 }
 
 size_t wxCSConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
 }
 
 size_t wxCSConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
@@ -2100,17 +2106,17 @@ size_t wxCSConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
   ((wxCSConv *)this)->LoadNow(); // discard constness
   if (buf) {
     if (m_cset) {
   ((wxCSConv *)this)->LoadNow(); // discard constness
   if (buf) {
     if (m_cset) {
-      for (size_t c=0; c<=n; c++) {
+      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)
        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++)
+      for (size_t c=0; c<n; c++)
        buf[c] = (psz[c]>0xff) ? '?' : psz[c];
     }
        buf[c] = (psz[c]>0xff) ? '?' : psz[c];
     }
+    return n;
   }
   }
-  return n;
+  return wcslen(psz);
 }
 }
-