]> git.saurik.com Git - wxWidgets.git/commitdiff
tried to clean strdup() mess: we now have wxStrdup[AW] which should be always available
authorVadim Zeitlin <vadim@wxwidgets.org>
Tue, 10 Dec 2002 00:43:28 +0000 (00:43 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Tue, 10 Dec 2002 00:43:28 +0000 (00:43 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@18172 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/buffer.h
include/wx/wxchar.h
src/common/wxchar.cpp

index 17fd98fff29cd93738e50bf19e3abdb6485903dd..ad956432ee7ac83cdfd03c723a02824f509fcb4e 100644 (file)
@@ -16,8 +16,6 @@
 
 #include "wx/wxchar.h"
 
-#include <string.h> // strdup
-
 // ----------------------------------------------------------------------------
 // Special classes for (wide) character strings: they use malloc/free instead
 // of new/delete
@@ -95,32 +93,11 @@ private:                                                                    \
     chartype *m_str;                                                        \
 }
 
-#ifndef strdup
-inline char *strdup(const char *cs)
-{
-    size_t len = 0;
-    while (cs[len] != 0)
-        len++;
-    const size_t siz = (len + 1)*sizeof(char);
-    char *csCopy = (char *)malloc(siz);
-    memcpy(csCopy, cs, siz);
-    return csCopy;
-}
-#endif
-
-DEFINE_BUFFER(wxCharBuffer, char, strdup);
+DEFINE_BUFFER(wxCharBuffer, char, wxStrdupA);
 
 #if wxUSE_WCHAR_T
 
-inline wchar_t *wxWcsdupReplacement(const wchar_t *wcs)
-{
-    const size_t siz = (wxWcslen(wcs) + 1)*sizeof(wchar_t);
-    wchar_t *wcsCopy = (wchar_t *)malloc(siz);
-    memcpy(wcsCopy, wcs, siz);
-    return wcsCopy;
-}
-
-DEFINE_BUFFER(wxWCharBuffer, wchar_t, wxWcsdupReplacement);
+DEFINE_BUFFER(wxWCharBuffer, wchar_t, wxStrdupW);
 
 #endif // wxUSE_WCHAR_T
 
index 71e38836d6b759df1a164012caa44462a1bdf9ae..03a406d0ffc491cf4e67bd73fcbb95d818d0370b 100644 (file)
 // Required for wxPrintf() etc
 #include <stdarg.h>
 
+// Almost all compiler have strdup(), but not quite all: CodeWarrior under Mac
+// and VC++ for Windows CE don't provide it
+#if !(defined(__MWERKS__) && defined(__WXMAC__)) && !defined(__WXWINCE__)
+    // use #define, not inline wrapper, as it is tested with #ifndef below
+    #define wxStrdupA strdup
+#endif
+
 // non Unix compilers which do have wchar.h (but not tchar.h which is included
 // below and which includes wchar.h anyhow).
 // Actually MinGW has tchar.h, but it does not include wchar.h
     #define  wxStrcoll   _tcscoll
     #define  wxStrcpy    _tcscpy
     #define  wxStrcspn   _tcscspn
-    #define  wxStrdup    _tcsdup
+    #define  wxStrdupW   _wcsdup        // notice the 'W'!
     #define  wxStrftime  _tcsftime
     #define  wxStricmp   _tcsicmp
     #define  wxStrnicmp  _tcsnicmp
         #define  wxStrcoll   strcoll
         #define  wxStrcpy    strcpy
         #define  wxStrcspn   strcspn
-        #if !defined(__MWERKS__) || !defined(__WXMAC__)
-            #define  wxStrdup    strdup
-        #endif
+
         // wxStricmp and wxStrnicmp are defined below
         #define  wxStrlen_   strlen // used in wxStrlen inline function
         #define  wxStrncat   strncat
@@ -572,6 +577,17 @@ inline bool wxIsEmpty(const wxChar *p) { return !p || !*p; }
 // safe version of strlen() (returns 0 if passed NULL pointer)
 inline size_t wxStrlen(const wxChar *psz) { return psz ? wxStrlen_(psz) : 0; }
 
+// each of strdup() and wcsdup() may or may not be available but we need both
+// of them anyhow for wx/buffer.h so we define the missing one(s) in
+// wxchar.cpp and so we should always have both wxStrdupA and wxStrdupW
+// defined -- if this is somehow not the case in some situations, please
+// correct that and not the lines here
+#if wxUSE_UNICODE
+    #define wxStrdup wxStrdupW
+#else
+    #define wxStrdup wxStrdupA
+#endif
+
 WXDLLEXPORT bool wxOKlibc(); // for internal use
 
 // ----------------------------------------------------------------------------
@@ -736,8 +752,12 @@ WXDLLEXPORT bool wxOKlibc(); // for internal use
     WXDLLEXPORT size_t   wxStrxfrm(wxChar *dest, const wxChar *src, size_t n);
 #endif // wxNEED_WX_STRING_H
 
-#ifndef wxStrdup
-WXDLLEXPORT wxChar * wxStrdup(const wxChar *psz);
+#ifndef wxStrdupA
+WXDLLEXPORT char *wxStrdupA(const char *psz);
+#endif
+
+#ifndef wxStrdupW
+WXDLLEXPORT wchar_t *wxStrdupW(const wchar_t *pwz);
 #endif
 
 #ifndef wxStricmp
index cffcebd01b37a545ca1da812755ddcbd322591b6..ea280f723102e1a02a6e4bc4fdcdd2143fc1ae33 100644 (file)
@@ -964,15 +964,26 @@ WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)CharLower((LPTSTR)(ch)); }
 WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)CharUpper((LPTSTR)(ch)); }
 #endif
 
-#ifndef wxStrdup
-WXDLLEXPORT wxChar * wxStrdup(const wxChar *psz)
+#ifndef wxStrdupA
+
+WXDLLEXPORT char *wxStrdupA(const char *s)
+{
+    return strcpy((char *)malloc(strlen(s) + 1), s);
+}
+
+#endif // wxStrdupA
+
+#ifndef wxStrdupW
+
+WXDLLEXPORT wchar_t * wxStrdupW(const wchar_t *pwz)
 {
-  size_t size = (wxStrlen(psz) + 1) * sizeof(wxChar);
-  wxChar *ret = (wxChar *) malloc(size);
-  memcpy(ret, psz, size);
+  size_t size = (wxWcslen(pwz) + 1) * sizeof(wchar_t);
+  wchar_t *ret = (wchar_t *) malloc(size);
+  memcpy(ret, pwz, size);
   return ret;
 }
-#endif
+
+#endif // wxStrdupW
 
 #ifndef wxStricmp
 int WXDLLEXPORT wxStricmp(const wxChar *psz1, const wxChar *psz2)