diff --git a/taglib/toolkit/tdebug.cpp b/taglib/toolkit/tdebug.cpp index 71350af7..557baed6 100644 --- a/taglib/toolkit/tdebug.cpp +++ b/taglib/toolkit/tdebug.cpp @@ -30,43 +30,12 @@ #include "tdebug.h" #include "tstring.h" #include "tdebuglistener.h" +#include "tutils.h" #include #include #include -using namespace TagLib; - -namespace -{ - String format(const char *fmt, ...) - { - va_list args; - va_start(args, fmt); - - char buf[256]; - -#if defined(HAVE_SNPRINTF) - - vsnprintf(buf, sizeof(buf), fmt, args); - -#elif defined(HAVE_SPRINTF_S) - - vsprintf_s(buf, fmt, args); - -#else - - // Be careful. May cause a buffer overflow. - vsprintf(buf, fmt, args); - -#endif - - va_end(args); - - return String(buf); - } -} - namespace TagLib { // The instance is defined in tdebuglistener.cpp. @@ -75,7 +44,7 @@ namespace TagLib void debug(const String &s) { #if !defined(NDEBUG) || defined(TRACE_IN_RELEASE) - + debugListener->printMessage("TagLib: " + s + "\n"); #endif @@ -85,10 +54,11 @@ namespace TagLib { #if !defined(NDEBUG) || defined(TRACE_IN_RELEASE) - for(size_t i = 0; i < v.size(); ++i) + for(size_t i = 0; i < v.size(); ++i) { std::string bits = std::bitset<8>(v[i]).to_string(); - String msg = format("*** [%d] - char '%c' - int %d, 0x%02x, 0b%s\n", + String msg = Utils::formatString( + "*** [%d] - char '%c' - int %d, 0x%02x, 0b%s\n", i, v[i], v[i], v[i], bits.c_str()); debugListener->printMessage(msg); diff --git a/taglib/toolkit/tstring.cpp b/taglib/toolkit/tstring.cpp index be61fdc5..0dbbd910 100644 --- a/taglib/toolkit/tstring.cpp +++ b/taglib/toolkit/tstring.cpp @@ -543,30 +543,7 @@ bool String::isAscii() const String String::number(int n) // static { - static const size_t BufferSize = 11; // Sufficient to store "-214748364". - static const char *Format = "%d"; - - char buffer[BufferSize]; - int length; - -#if defined(HAVE_SNPRINTF) - - length = snprintf(buffer, BufferSize, Format, n); - -#elif defined(HAVE_SPRINTF_S) - - length = sprintf_s(buffer, Format, n); - -#else - - length = sprintf(buffer, Format, n); - -#endif - - if(length > 0) - return String(buffer); - else - return String::null; + return Utils::formatString("%d", n); } TagLib::wchar &String::operator[](int i) diff --git a/taglib/toolkit/tutils.h b/taglib/toolkit/tutils.h index 0874684a..92486e23 100644 --- a/taglib/toolkit/tutils.h +++ b/taglib/toolkit/tutils.h @@ -44,6 +44,9 @@ # include #endif +#include "tstring.h" +#include +#include #include namespace TagLib @@ -147,6 +150,47 @@ namespace TagLib #endif } + inline String formatString(const char *format, ...) + { + // Sufficient buffer size for the current internal uses. + // Consider changing this value when you use this function. + + static const size_t BufferSize = 128; + + va_list args; + va_start(args, format); + + char buf[BufferSize]; + int length; + +#if defined(HAVE_SNPRINTF) + + length = vsnprintf(buf, BufferSize, format, args); + +#elif defined(HAVE_SPRINTF_S) + + length = vsprintf_s(buf, format, args); + +#else + + // The last resort. May cause a buffer overflow. + + length = vsprintf(buf, format, args); + if(length >= BufferSize) { + debug("Utils::formatString() - Buffer overflow! Returning an empty string."); + length = -1; + } + +#endif + + va_end(args); + + if(length != -1) + return String(buf); + else + return String::null; + } + enum ByteOrder { LittleEndian,