From: <man...@us...> - 2015-05-25 14:39:16
|
Revision: 5171 http://sourceforge.net/p/modplug/code/5171 Author: manxorist Date: 2015-05-25 14:39:10 +0000 (Mon, 25 May 2015) Log Message: ----------- [Ref] Make our string formatting functions work with MFC class CString. This avoids the need for the mpt::tstring type introduced some revisions ago; remove it again. Modified Paths: -------------- trunk/OpenMPT/common/mptString.cpp trunk/OpenMPT/common/mptString.h trunk/OpenMPT/test/test.cpp Modified: trunk/OpenMPT/common/mptString.cpp =================================================================== --- trunk/OpenMPT/common/mptString.cpp 2015-05-25 12:52:07 UTC (rev 5170) +++ trunk/OpenMPT/common/mptString.cpp 2015-05-25 14:39:10 UTC (rev 5171) @@ -1602,6 +1602,54 @@ return result; } +#if defined(_MFC_VER) +template<> +CString PrintImplTemplate<CString>(const CString & format + , const CString & x1 + , const CString & x2 + , const CString & x3 + , const CString & x4 + , const CString & x5 + , const CString & x6 + , const CString & x7 + , const CString & x8 + ) +{ + CString result; + const std::size_t len = format.GetLength(); + for(std::size_t pos = 0; pos != len; ++pos) + { + CString::XCHAR c = format[pos]; + if(pos + 1 != len && c == _T('%')) + { + pos++; + c = format[pos]; + if(_T('1') <= c && c <= _T('9')) + { + const std::size_t n = c - _T('0'); + switch(n) + { + case 1: result += x1; break; + case 2: result += x2; break; + case 3: result += x3; break; + case 4: result += x4; break; + case 5: result += x5; break; + case 6: result += x6; break; + case 7: result += x7; break; + case 8: result += x8; break; + } + continue; + } else if(c != _T('%')) + { + result += CString(_T('%')); + } + } + result += CString(c); + } + return result; +} +#endif + std::string PrintImpl(const std::string & format , const std::string & x1 , const std::string & x2 @@ -1648,6 +1696,22 @@ } #endif +#if defined(_MFC_VER) +CString PrintImpl(const CString & format + , const CString & x1 + , const CString & x2 + , const CString & x3 + , const CString & x4 + , const CString & x5 + , const CString & x6 + , const CString & x7 + , const CString & x8 + ) +{ + return PrintImplTemplate<CString>(format, x1,x2,x3,x4,x5,x6,x7,x8); +} +#endif + } // namespace detail Modified: trunk/OpenMPT/common/mptString.h =================================================================== --- trunk/OpenMPT/common/mptString.h 2015-05-25 12:52:07 UTC (rev 5170) +++ trunk/OpenMPT/common/mptString.h 2015-05-25 14:39:10 UTC (rev 5171) @@ -392,22 +392,6 @@ #if defined(MPT_OS_WINDOWS) -#ifdef UNICODE - -typedef std::wstring tstring; - -template <typename Tsrc> inline mpt::tstring ToWinAPI(const Tsrc &src) { return mpt::ToWide(src); } -template <typename Tsrc> inline mpt::tstring ToWinAPI(Charset from, const Tsrc &src) { return mpt::ToWide(from, src); } - -#else - -typedef std::string tstring; - -template <typename Tsrc> inline mpt::tstring ToWinAPI(const Tsrc &src) { return mpt::ToLocale(src); } -template <typename Tsrc> inline mpt::tstring ToWinAPI(Charset from, const Tsrc &src) { return mpt::ToLocale(from, src); } - -#endif - namespace String { namespace detail { @@ -752,6 +736,13 @@ #if MPT_USTRING_MODE_UTF8 template <> struct ToStringTFunctor<mpt::ustring> { template <typename T> inline mpt::ustring operator() (const T & x) { return ToUString(x); } }; #endif +#if defined(_MFC_VER) +#ifdef UNICODE +template <> struct ToStringTFunctor<CString> { template <typename T> inline CString operator() (const T & x) { return mpt::ToCString(ToUString(x)); } }; +#else +template <> struct ToStringTFunctor<CString> { template <typename T> inline CString operator() (const T & x) { return mpt::ToCString(mpt::CharsetLocale, ToString(x)); } }; +#endif +#endif template<typename Tstring, typename T> inline Tstring ToStringT(const T & x) { return ToStringTFunctor<Tstring>()(x); } @@ -823,6 +814,13 @@ #if MPT_USTRING_MODE_UTF8 template <> struct FormatValTFunctor<mpt::ustring> { template <typename T> inline mpt::ustring operator() (const T & x, const Format & f) { return mpt::ToUnicode(mpt::CharsetUTF8, FormatVal(x, f)); } }; #endif +#if defined(_MFC_VER) +#ifdef UNICODE +template <> struct FormatValTFunctor<CString> { template <typename T> inline CString operator() (const T & x, const Format & f) { return mpt::ToCString(FormatValW(x, f)); } }; +#else +template <> struct FormatValTFunctor<CString> { template <typename T> inline CString operator() (const T & x, const Format & f) { return mpt::ToCString(mpt::CharsetLocale, FormatVal(x, f)); } }; +#endif +#endif class Format @@ -1031,6 +1029,9 @@ #else typedef fmtT<mpt::ustring> ufmt; #endif +#if defined(_MFC_VER) +typedef fmtT<CString> tfmt; +#endif } // namespace mpt @@ -1053,6 +1054,9 @@ #if MPT_USTRING_MODE_UTF8 template <> struct to_string_type<mpt::ustring > { typedef mpt::ustring type; }; #endif +#if defined(_MFC_VER) +template <> struct to_string_type<CString > { typedef CString type; }; +#endif template <typename T, std::size_t N> struct to_string_type<T [N]> { typedef typename to_string_type<T>::type type; }; std::string PrintImpl(const std::string & format @@ -1092,6 +1096,19 @@ ); #endif +#if defined(_MFC_VER) +CString PrintImpl(const CString & format + , const CString & x1 = CString() + , const CString & x2 = CString() + , const CString & x3 = CString() + , const CString & x4 = CString() + , const CString & x5 = CString() + , const CString & x6 = CString() + , const CString & x7 = CString() + , const CString & x8 = CString() + ); +#endif + } // namespace detail template<typename Tformat Modified: trunk/OpenMPT/test/test.cpp =================================================================== --- trunk/OpenMPT/test/test.cpp 2015-05-25 12:52:07 UTC (rev 5170) +++ trunk/OpenMPT/test/test.cpp 2015-05-25 14:39:10 UTC (rev 5171) @@ -497,6 +497,11 @@ VERIFY_EQUAL(mpt::String::Print("%%%1", "a"), "%a"); VERIFY_EQUAL(mpt::String::Print("%b", "a"), "%b"); +#if defined(_MFC_VER) + VERIFY_EQUAL(mpt::String::Print(CString(_T("%1%2%3")),1,2,3), _T("123")); + VERIFY_EQUAL(mpt::String::Print(CString(_T("%1%2%3")),1,mpt::tfmt::dec0<3>(2),3), _T("10023")); +#endif + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |