X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/e98d32057de9051bdad3b7442357c42226b16be8..f24b783af3785e748b775409d926361e87e8eb13:/src/common/wxchar.cpp diff --git a/src/common/wxchar.cpp b/src/common/wxchar.cpp index a41ac22e71..52fc61910b 100644 --- a/src/common/wxchar.cpp +++ b/src/common/wxchar.cpp @@ -167,6 +167,10 @@ bool WXDLLEXPORT wxOKlibc() #if !defined(wxVsnprintf_) +#if !wxUSE_WXVSNPRINTF + #error wxUSE_WXVSNPRINTF must be 1 if our wxVsnprintf_ is used +#endif + // wxUSE_STRUTILS says our wxVsnprintf_ implementation to use or not to // use wxStrlen and wxStrncpy functions over one-char processing loops. // @@ -213,7 +217,7 @@ enum wxPrintfArgType { wxPAT_INT, // %d, %i, %o, %u, %x, %X wxPAT_LONGINT, // %ld, etc -#if SIZEOF_LONG_LONG +#ifdef wxLongLong_t wxPAT_LONGLONGINT, // %Ld, etc #endif wxPAT_SIZET, // %Zd, etc @@ -238,8 +242,8 @@ enum wxPrintfArgType { typedef union { int pad_int; // %d, %i, %o, %u, %x, %X long int pad_longint; // %ld, etc -#if SIZEOF_LONG_LONG - long long int pad_longlongint; // %Ld, etc +#ifdef wxLongLong_t + wxLongLong_t pad_longlongint; // %Ld, etc #endif size_t pad_sizet; // %Zd, etc @@ -347,7 +351,8 @@ bool wxPrintfConvSpec::Parse(const wxChar *format) // temporary parse data size_t flagofs = 1; - bool in_prec, prec_dot; + bool in_prec, // true if we found the dot in some previous iteration + prec_dot; // true if the dot has been already added to m_szFlags int ilen = 0; m_bAlignLeft = in_prec = prec_dot = false; @@ -417,6 +422,25 @@ bool wxPrintfConvSpec::Parse(const wxChar *format) CHECK_PREC m_szFlags[flagofs++] = char(ch); break; +#ifdef __WXMSW__ + // under Windows we support the special '%I64' notation as longlong + // integer conversion specifier for MSVC compatibility + // (it behaves exactly as '%lli' or '%Li' or '%qi') + case wxT('I'): + if (*(m_pArgEnd+1) != wxT('6') || + *(m_pArgEnd+2) != wxT('4')) + return false; // bad format + + m_pArgEnd++; + m_pArgEnd++; + + ilen = 2; + CHECK_PREC + m_szFlags[flagofs++] = char(ch); + m_szFlags[flagofs++] = '6'; + m_szFlags[flagofs++] = '4'; + break; +#endif // __WXMSW__ case wxT('Z'): ilen = 3; @@ -508,11 +532,11 @@ bool wxPrintfConvSpec::Parse(const wxChar *format) else if (ilen == 1) m_type = wxPAT_LONGINT; else if (ilen == 2) -#if SIZEOF_LONG_LONG +#ifdef wxLongLong_t m_type = wxPAT_LONGLONGINT; -#else // !long long +#else // !wxLongLong_t m_type = wxPAT_LONGINT; -#endif // long long/!long long +#endif // wxLongLong_t/!wxLongLong_t else if (ilen == 3) m_type = wxPAT_SIZET; done = true; @@ -624,7 +648,7 @@ void wxPrintfConvSpec::ReplaceAsteriskWith(int width) // find the first * in our flag buffer char *pwidth = strchr(m_szFlags, '*'); - wxASSERT(pwidth); + wxCHECK_RET(pwidth, _T("field width must be specified")); // save what follows the * (the +1 is to skip the asterisk itself!) strcpy(temp, pwidth+1); @@ -678,11 +702,11 @@ bool wxPrintfConvSpec::LoadArg(wxPrintfArg *p, va_list &argptr) case wxPAT_LONGINT: p->pad_longint = va_arg(argptr, long int); break; -#if SIZEOF_LONG_LONG +#ifdef wxLongLong_t case wxPAT_LONGLONGINT: - p->pad_longlongint = va_arg(argptr, long long int); + p->pad_longlongint = va_arg(argptr, wxLongLong_t); break; -#endif +#endif // wxLongLong_t case wxPAT_SIZET: p->pad_sizet = va_arg(argptr, size_t); break; @@ -762,7 +786,7 @@ int wxPrintfConvSpec::Process(wxChar *buf, size_t lenMax, wxPrintfArg *p, size_t lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longint); break; -#if SIZEOF_LONG_LONG +#ifdef wxLongLong_t case wxPAT_LONGLONGINT: lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longlongint); break; @@ -928,7 +952,7 @@ int wxPrintfConvSpec::Process(wxChar *buf, size_t lenMax, wxPrintfArg *p, size_t { case wxPAT_INT: case wxPAT_LONGINT: -#if SIZEOF_LONG_LONG +#ifdef wxLongLong_t case wxPAT_LONGLONGINT: #endif case wxPAT_SIZET: @@ -993,19 +1017,23 @@ int wxPrintfConvSpec::Process(wxChar *buf, size_t lenMax, wxPrintfArg *p, size_t return lenCur; } -// differences from standard strncpy: -// 1) copies everything from 'source' except for '%%' sequence which is copied as '%' -// 2) returns the number of written characters in 'dest' as it could differ from given 'n' -// 3) much less optimized, unfortunately... -static int wxCopyStrWithPercents(wxChar *dest, const wxChar *source, size_t n) +// Copy chars from source to dest converting '%%' to '%'. Takes at most maxIn +// chars from source and write at most outMax chars to dest, returns the +// number of chars actually written. Does not treat null specially. +// +static int wxCopyStrWithPercents( + size_t maxOut, + wxChar *dest, + size_t maxIn, + const wxChar *source) { size_t written = 0; - if (n == 0) + if (maxIn == 0) return 0; size_t i; - for ( i = 0; i < n-1; source++, i++) + for ( i = 0; i < maxIn-1 && written < maxOut; source++, i++) { dest[written++] = *source; if (*(source+1) == wxT('%')) @@ -1016,7 +1044,7 @@ static int wxCopyStrWithPercents(wxChar *dest, const wxChar *source, size_t n) } } - if (i < n) + if (i < maxIn && written < maxOut) // copy last character inconditionally dest[written++] = *source; @@ -1097,8 +1125,10 @@ int WXDLLEXPORT wxVsnprintf_(wxChar *buf, size_t lenMax, } if (posarg_present && nonposarg_present) + { + buf[0] = 0; return -1; // format strings with both positional and - // non-positional conversion specifier are unsupported !! + } // non-positional conversion specifier are unsupported !! // on platforms where va_list is an array type, it is necessary to make a // copy to be able to pass it to LoadArg as a reference. @@ -1118,8 +1148,12 @@ int WXDLLEXPORT wxVsnprintf_(wxChar *buf, size_t lenMax, va_end(ap); // something failed while loading arguments from the variable list... + // (e.g. the user repeated twice the same positional argument) if (!ok) + { + buf[0] = 0; return -1; + } // finally, process each conversion specifier with its own argument toparse = format; @@ -1128,23 +1162,21 @@ int WXDLLEXPORT wxVsnprintf_(wxChar *buf, size_t lenMax, // copy in the output buffer the portion of the format string between // last specifier and the current one size_t tocopy = ( arg[i].m_pArgPos - toparse ); - if (lenCur+tocopy >= lenMax) + + lenCur += wxCopyStrWithPercents(lenMax - lenCur, buf + lenCur, + tocopy, toparse); + if (lenCur == lenMax) { - // not enough space in the output buffer ! - // copy until the end of remaining space and then stop - wxCopyStrWithPercents(buf+lenCur, toparse, lenMax - lenCur - 1); - buf[lenMax-1] = wxT('\0'); - return -1; + buf[lenMax - 1] = 0; + return lenMax+1; // not enough space in the output buffer ! } - lenCur += wxCopyStrWithPercents(buf+lenCur, toparse, tocopy); - // process this specifier directly in the output buffer - int n = arg[i].Process(buf+lenCur, lenMax - lenCur, &argdata[arg[i].m_pos]); + int n = arg[i].Process(buf+lenCur, lenMax - lenCur, &argdata[arg[i].m_pos], lenCur); if (n == -1) { buf[lenMax-1] = wxT('\0'); // be sure to always NUL-terminate the string - return -1; // not enough space in the output buffer ! + return lenMax+1; // not enough space in the output buffer ! } lenCur += n; @@ -1158,11 +1190,14 @@ int WXDLLEXPORT wxVsnprintf_(wxChar *buf, size_t lenMax, // conversion specifier // NOTE2: the +1 is because we want to copy also the '\0' size_t tocopy = wxStrlen(format) + 1 - ( toparse - format ) ; - if (lenCur+tocopy >= lenMax) - return -1; // not enough space in the output buffer ! - // the -1 is because of the '\0' - lenCur += wxCopyStrWithPercents(buf+lenCur, toparse, tocopy) - 1; + lenCur += wxCopyStrWithPercents(lenMax - lenCur, buf + lenCur, + tocopy, toparse) - 1; + if (buf[lenCur]) + { + buf[lenCur] = 0; + return lenMax+1; // not enough space in the output buffer ! + } wxASSERT(lenCur == wxStrlen(buf)); return lenCur; @@ -1172,7 +1207,13 @@ int WXDLLEXPORT wxVsnprintf_(wxChar *buf, size_t lenMax, #undef APPEND_STR #undef CHECK_PREC -#endif // !wxVsnprintfA +#else // wxVsnprintf_ is defined + +#if wxUSE_WXVSNPRINTF + #error wxUSE_WXVSNPRINTF must be 0 if our wxVsnprintf_ is not used +#endif + +#endif // !wxVsnprintf_ #if !defined(wxSnprintf_) int WXDLLEXPORT wxSnprintf_(wxChar *buf, size_t len, const wxChar *format, ...) @@ -1212,9 +1253,13 @@ int WXDLLEXPORT wxSnprintf_(wxChar *buf, size_t len, const wxChar *format, ...) #ifdef wxNEED_FPUTS int wxFputs(const wchar_t *ws, FILE *stream) { + wxCharBuffer buf(wxConvLibc.cWC2MB(ws)); + if ( !buf ) + return -1; + // counting the number of wide characters written isn't worth the trouble, // simply distinguish between ok and error - return fputs(wxConvLibc.cWC2MB(ws), stream) == -1 ? -1 : 0; + return fputs(buf, stream) == -1 ? -1 : 0; } #endif // wxNEED_FPUTS