From: <man...@us...> - 2014-09-28 09:23:10
|
Revision: 4319 http://sourceforge.net/p/modplug/code/4319 Author: manxorist Date: 2014-09-28 09:22:57 +0000 (Sun, 28 Sep 2014) Log Message: ----------- [Ref] Move mpt::String::Format to test.cpp as the test cases are the only place left where sprintf-like functionality is required (in order to validate our own formatting functions) outside of mptrack/ (which currently still uses MFC sprintf variants all over the place). 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 2014-09-28 09:11:17 UTC (rev 4318) +++ trunk/OpenMPT/common/mptString.cpp 2014-09-28 09:22:57 UTC (rev 4319) @@ -62,38 +62,7 @@ namespace mpt { namespace String { -#ifdef MODPLUG_TRACKER -std::string Format(const char *format, ...) -{ - #if MPT_COMPILER_MSVC - va_list argList; - va_start(argList, format); - - // Count the needed array size. - const size_t nCount = _vscprintf(format, argList); // null character not included. - std::vector<char> buf(nCount + 1); // + 1 is for null terminator. - vsprintf_s(&(buf[0]), buf.size(), format, argList); - - va_end(argList); - return &(buf[0]); - #else - va_list argList; - va_start(argList, format); - int size = vsnprintf(NULL, 0, format, argList); // get required size, requires c99 compliant vsnprintf which msvc does not have - va_end(argList); - std::vector<char> temp(size + 1); - va_start(argList, format); - vsnprintf(&(temp[0]), size + 1, format, argList); - va_end(argList); - return &(temp[0]); - #endif -} - -#endif - - - /* default 1:1 mapping static const uint32 CharsetTableISO8859_1[256] = { Modified: trunk/OpenMPT/common/mptString.h =================================================================== --- trunk/OpenMPT/common/mptString.h 2014-09-28 09:11:17 UTC (rev 4318) +++ trunk/OpenMPT/common/mptString.h 2014-09-28 09:22:57 UTC (rev 4319) @@ -30,17 +30,6 @@ { -#ifdef MODPLUG_TRACKER -// There are 4 reasons why this is not available for library code: -// 1. printf-like functionality is not type-safe. -// 2. There are portability problems with char/wchar_t and the semantics of %s/%ls/%S . -// 3. There are portability problems with specifying format for 64bit integers. -// 4. Formatting of floating point values depends on the currently set C locale. -// A library is not allowed to mock with that and thus cannot influence the behavior in this case. -std::string MPT_PRINTF_FUNC(1,2) Format(const char * format, ...); -#endif - - // Remove whitespace at start of string static inline std::string LTrim(std::string str, const std::string &whitespace = " \n\r\t") //----------------------------------------------------------------------------------------- Modified: trunk/OpenMPT/test/test.cpp =================================================================== --- trunk/OpenMPT/test/test.cpp 2014-09-28 09:11:17 UTC (rev 4318) +++ trunk/OpenMPT/test/test.cpp 2014-09-28 09:22:57 UTC (rev 4319) @@ -244,10 +244,52 @@ } + +#ifdef MODPLUG_TRACKER + +// In tracker debug builds, the sprintf-like function is retained in order to be able to validate our own formatting against sprintf. + +// There are 4 reasons why this is not available for library code: +// 1. printf-like functionality is not type-safe. +// 2. There are portability problems with char/wchar_t and the semantics of %s/%ls/%S . +// 3. There are portability problems with specifying format for 64bit integers. +// 4. Formatting of floating point values depends on the currently set C locale. +// A library is not allowed to mock with that and thus cannot influence the behavior in this case. + +static std::string MPT_PRINTF_FUNC(1,2) StringFormat(const char * format, ...); + +static std::string StringFormat(const char *format, ...) +{ + #if MPT_COMPILER_MSVC + va_list argList; + va_start(argList, format); + + // Count the needed array size. + const size_t nCount = _vscprintf(format, argList); // null character not included. + std::vector<char> buf(nCount + 1); // + 1 is for null terminator. + vsprintf_s(&(buf[0]), buf.size(), format, argList); + + va_end(argList); + return &(buf[0]); + #else + va_list argList; + va_start(argList, format); + int size = vsnprintf(NULL, 0, format, argList); // get required size, requires c99 compliant vsnprintf which msvc does not have + va_end(argList); + std::vector<char> temp(size + 1); + va_start(argList, format); + vsnprintf(&(temp[0]), size + 1, format, argList); + va_end(argList); + return &(temp[0]); + #endif +} + +#endif + static void TestFloatFormat(double x, const char * format, mpt::FormatFlags f, std::size_t width = 0, int precision = -1) { #ifdef MODPLUG_TRACKER - std::string str_sprintf = mpt::String::Format(format, x); + std::string str_sprintf = StringFormat(format, x); #endif std::string str_iostreams = mpt::Format().SetFlags(f).SetWidth(width).SetPrecision(precision).ToString(x); std::string str_parsed = mpt::Format().ParsePrintf(format).ToString(x); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |