From 07243717e26bda497d5a8b325c2e6acf4ee033cf Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 10 Dec 2002 00:43:28 +0000 Subject: [PATCH] tried to clean strdup() mess: we now have wxStrdup[AW] which should be always available git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@18172 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/buffer.h | 27 ++------------------------- include/wx/wxchar.h | 32 ++++++++++++++++++++++++++------ src/common/wxchar.cpp | 23 +++++++++++++++++------ 3 files changed, 45 insertions(+), 37 deletions(-) diff --git a/include/wx/buffer.h b/include/wx/buffer.h index 17fd98fff2..ad956432ee 100644 --- a/include/wx/buffer.h +++ b/include/wx/buffer.h @@ -16,8 +16,6 @@ #include "wx/wxchar.h" -#include // 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 diff --git a/include/wx/wxchar.h b/include/wx/wxchar.h index 71e38836d6..03a406d0ff 100644 --- a/include/wx/wxchar.h +++ b/include/wx/wxchar.h @@ -80,6 +80,13 @@ // Required for wxPrintf() etc #include +// 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 @@ -269,7 +276,7 @@ #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 @@ -459,9 +466,7 @@ #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 diff --git a/src/common/wxchar.cpp b/src/common/wxchar.cpp index cffcebd01b..ea280f7231 100644 --- a/src/common/wxchar.cpp +++ b/src/common/wxchar.cpp @@ -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) -- 2.45.2