X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/297eb8e6d23db4e24843640ead6609577ccee2c2..db71eb0640221199170ac9ea83371c2d467fa456:/src/common/wxchar.cpp diff --git a/src/common/wxchar.cpp b/src/common/wxchar.cpp index eaab6699ac..10bcd27cb4 100644 --- a/src/common/wxchar.cpp +++ b/src/common/wxchar.cpp @@ -20,6 +20,8 @@ #pragma hdrstop #endif +#include "wx/wxchar.h" + #define _ISOC9X_SOURCE 1 // to get vsscanf() #define _BSD_SOURCE 1 // to still get strdup() @@ -35,11 +37,11 @@ #endif #ifndef WX_PRECOMP - #include "wx/wxchar.h" #include "wx/string.h" #include "wx/hash.h" + #include "wx/utils.h" // for wxMin and wxMax + #include "wx/log.h" #endif - #include "wx/utils.h" // for wxMin and wxMax #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H) #include @@ -194,22 +196,34 @@ bool WXDLLEXPORT wxOKlibc() // implemented later in this file using wxVsnprintf() and that would // result in an endless recursion and thus in a stack overflow #if wxUSE_UNICODE - - #if defined(__WXWINCE__) || ( defined(__VISUALC__) && __VISUALC__ <= 1200 ) - #define HAVE_BROKEN_SWPRINTF_DECL + #if defined(__WINDOWS__) && !defined(HAVE_SWPRINTF) + // all compilers under Windows should have swprintf() + #define HAVE_SWPRINTF #endif + // NB: MSVC 6 has only non-standard swprintf() declaration and while MSVC 7 + // and 7.1 do have the standard one, it's completely broken unless + // /Zc:wchar_t is used while the other one works so use it instead, and + // only VC8 has a working standard-compliant swprintf() + #if defined(__WXWINCE__) || \ + (defined(__VISUALC__) && __VISUALC__ < 1400) || \ + defined(__GNUWIN32__) || \ + defined(__BORLANDC__) + #ifndef HAVE_BROKEN_SWPRINTF_DECL + #define HAVE_BROKEN_SWPRINTF_DECL + #endif + #endif - // problem: on some systems swprintf takes the 'max' argument while on others - // it doesn't + // problem: on some systems swprintf takes the 'max' argument while on + // others it doesn't #if defined(HAVE_BROKEN_SWPRINTF_DECL) - - // like when using sprintf(), since 'max' is not used, wxVsnprintf() should - // always ensure that 'buff' is big enough for all common needs + // like when using sprintf(), since 'max' is not used, wxVsnprintf() + // should always ensure that 'buff' is big enough for all common needs #define system_sprintf(buff, max, flags, data) \ ::swprintf(buff, flags, data) - #else + #define SYSTEM_SPRINTF_IS_UNSAFE + #else #if !defined(HAVE_SWPRINTF) #error wxVsnprintf() needs a system swprintf() implementation! #endif @@ -217,24 +231,24 @@ bool WXDLLEXPORT wxOKlibc() #define system_sprintf(buff, max, flags, data) \ ::swprintf(buff, max, flags, data) #endif - -#else - - #if defined(HAVE_SNPRINTF) - +#else // !wxUSE_UNICODE + #if defined(__VISUALC__) || \ + (defined(__BORLANDC__) && __BORLANDC__ >= 0x540) + #define system_sprintf(buff, max, flags, data) \ + ::_snprintf(buff, max, flags, data) + #elif defined(HAVE_SNPRINTF) #define system_sprintf(buff, max, flags, data) \ ::snprintf(buff, max, flags, data) - - #else // NB: at least sprintf() should *always* be available - - // since 'max' is not used in this case, wxVsnprintf() should always ensure - // that 'buff' is big enough for all common needs + #else // NB: at least sprintf() should always be available + // since 'max' is not used in this case, wxVsnprintf() should always + // ensure that 'buff' is big enough for all common needs // (see wxMAX_SVNPRINTF_FLAGBUFFER_LEN and wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN) #define system_sprintf(buff, max, flags, data) \ ::sprintf(buff, flags, data) + #define SYSTEM_SPRINTF_IS_UNSAFE #endif -#endif +#endif // wxUSE_UNICODE/!wxUSE_UNICODE @@ -388,7 +402,7 @@ bool wxPrintfConvSpec::Parse(const wxChar *format) #define CHECK_PREC \ if (in_prec && !prec_dot) \ { \ - m_szFlags[flagofs++] = '.'; \ + m_szFlags[flagofs++] = wxT('.'); \ prec_dot = true; \ } @@ -528,7 +542,7 @@ bool wxPrintfConvSpec::Parse(const wxChar *format) case wxT('X'): CHECK_PREC m_szFlags[flagofs++] = ch; - m_szFlags[flagofs] = '\0'; + m_szFlags[flagofs] = wxT('\0'); if (ilen == 0) m_type = wxPAT_INT; else if (ilen == -1) @@ -556,7 +570,7 @@ bool wxPrintfConvSpec::Parse(const wxChar *format) case wxT('G'): CHECK_PREC m_szFlags[flagofs++] = ch; - m_szFlags[flagofs] = '\0'; + m_szFlags[flagofs] = wxT('\0'); if (ilen == 2) m_type = wxPAT_LONGDOUBLE; else @@ -566,6 +580,8 @@ bool wxPrintfConvSpec::Parse(const wxChar *format) case wxT('p'): m_type = wxPAT_POINTER; + m_szFlags[flagofs++] = ch; + m_szFlags[flagofs] = '\0'; done = true; break; @@ -664,7 +680,10 @@ void wxPrintfConvSpec::ReplaceAsteriskWith(int width) } // replace * with the actual integer given as width - int maxlen = (m_szFlags + wxMAX_SVNPRINTF_FLAGBUFFER_LEN - pwidth) / sizeof(wxChar); +#ifndef SYSTEM_SPRINTF_IS_UNSAFE + int maxlen = (m_szFlags + wxMAX_SVNPRINTF_FLAGBUFFER_LEN - pwidth) / + sizeof(wxChar); +#endif int offset = system_sprintf(pwidth, maxlen, wxT("%d"), abs(width)); // restore after the expanded * what was following it @@ -723,7 +742,7 @@ bool wxPrintfConvSpec::LoadArg(wxPrintfArg *p, va_list &argptr) break; case wxPAT_CHAR: - p->pad_char = va_arg(argptr, int); // char is promoted to int when passed through '...' + p->pad_char = (char)va_arg(argptr, int); // char is promoted to int when passed through '...' break; case wxPAT_WCHAR: p->pad_wchar = (wchar_t)va_arg(argptr, int); // char is promoted to int when passed through '...' @@ -948,6 +967,10 @@ int wxPrintfConvSpec::Process(wxChar *buf, size_t lenMax, wxPrintfArg *p) return -1; } +#ifdef HAVE_BROKEN_SWPRINTF_DECL + wxUnusedVar(lenScratch); // avoid dummy warnings +#endif + // if we used system's sprintf() then we now need to append the s_szScratch // buffer to the given one... switch (m_type) @@ -963,7 +986,7 @@ int wxPrintfConvSpec::Process(wxChar *buf, size_t lenMax, wxPrintfArg *p) case wxPAT_POINTER: #if wxUSE_STRUTILS { - wxASSERT(lenScratch >= 0 && lenScratch < wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN); + wxASSERT( /* lenScratch >= 0 && */ lenScratch < wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN); if (lenMax < lenScratch) { // fill output buffer and then return -1