normalize printf/scanf format strings correctly on all platforms, while accounting for wxArgNormalizer<T> conversions

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@46612 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Václav Slavík
2007-06-22 11:10:36 +00:00
parent 929bd94ee1
commit 50e2789913
5 changed files with 630 additions and 345 deletions

View File

@@ -47,6 +47,9 @@ class WXDLLIMPEXP_BASE wxString;
// * wchar_t* if wxUSE_UNICODE_WCHAR or if wxUSE_UNICODE_UTF8 and the current
// locale is not UTF-8
//
// Note that wxFormatString *must* be used for the format parameter of these
// functions, otherwise the implementation won't work correctly.
//
// Parameters:
// [ there are examples in square brackets showing values of the parameters
// for the wxFprintf() wrapper for fprintf() function with the following
@@ -111,11 +114,12 @@ class WXDLLIMPEXP_BASE wxString;
// wxFormatString
// ----------------------------------------------------------------------------
// This class should be used for format string argument of the functions
// This class must be used for format string argument of the functions
// defined using WX_DEFINE_VARARG_FUNC_* macros. It converts the string to
// char* or wchar_t* for passing to implementation function efficiently (i.e.
// without keeping the converted string in memory for longer than necessary,
// like c_str())
// like c_str()). It also converts format string to the correct form that
// accounts for string changes done by wxArgNormalizer<>
//
// Note that this class can _only_ be used for function arguments!
class WXDLLIMPEXP_BASE wxFormatString
@@ -138,7 +142,13 @@ public:
operator const char*() const
{ return wx_const_cast(wxFormatString*, this)->AsChar(); }
private:
// InputAsChar() returns the value converted passed to ctor, only converted
// to char, while AsChar() takes the the string returned by InputAsChar()
// and does format string conversion on it as well (and similarly for
// ..AsWChar() below)
const char* InputAsChar();
const char* AsChar();
wxCharBuffer m_convertedChar;
#endif // !wxUSE_UNICODE_WCHAR
#if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
@@ -146,7 +156,9 @@ public:
operator const wchar_t*() const
{ return wx_const_cast(wxFormatString*, this)->AsWChar(); }
private:
const wchar_t* InputAsWChar();
const wchar_t* AsWChar();
wxWCharBuffer m_convertedWChar;
#endif // wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
private:
@@ -433,28 +445,38 @@ WX_ARG_NORMALIZER_FORWARD(wxStdWideString, const wxStdWideString&);
#if !wxUSE_UTF8_LOCALE_ONLY
template<>
struct wxArgNormalizerWchar<const wxUniChar&>
: public wxArgNormalizerWchar<wxChar/*FIXME-UTF8: should be wchar_t after ANSI removal*/>
{
wxArgNormalizerWchar(const wxUniChar& s)
: wxArgNormalizerWchar<wxChar>((wxChar)s) {}
wxArgNormalizerWchar(const wxUniChar& s) : m_value(s) {}
// FIXME-UTF8: use wchar_t once ANSI build is removed
wxChar get() const { return m_value; }
wxChar m_value;
};
#endif // !wxUSE_UTF8_LOCALE_ONLY
#if wxUSE_UNICODE_UTF8
template<>
struct wxArgNormalizerUtf8<const wxUniChar&>
: public wxArgNormalizerUtf8<char>
{
wxArgNormalizerUtf8(const wxUniChar& s)
// FIXME-UTF8: this is lossy, we need to convert to string, but that
// requires format string update
: wxArgNormalizerUtf8<char>((const char)s) {}
wxArgNormalizerUtf8(const wxUniChar& s) : m_value(s.AsUTF8()) {}
const wxStringCharType *get() const { return m_value; }
wxUniChar::Utf8CharBuffer m_value;
};
#endif // wxUSE_UNICODE_UTF8
WX_ARG_NORMALIZER_FORWARD(wxUniChar, const wxUniChar&);
WX_ARG_NORMALIZER_FORWARD(const wxUniCharRef&, const wxUniChar&);
WX_ARG_NORMALIZER_FORWARD(wxUniCharRef, const wxUniChar&);
// convert char/wchar_t to wxUniChar to get output in the right encoding:
WX_ARG_NORMALIZER_FORWARD(char, const wxUniChar&);
WX_ARG_NORMALIZER_FORWARD(const char&, const wxUniChar&);
WX_ARG_NORMALIZER_FORWARD(unsigned char, const wxUniChar&);
WX_ARG_NORMALIZER_FORWARD(const unsigned char&, const wxUniChar&);
WX_ARG_NORMALIZER_FORWARD(wchar_t, const wxUniChar&);
WX_ARG_NORMALIZER_FORWARD(const wchar_t&, const wxUniChar&);
#undef WX_ARG_NORMALIZER_FORWARD