#include "tdebug.h"
#include "tstring.h"
#include "tdebuglistener.h"
+#include "tutils.h"
#include <bitset>
#include <cstdio>
#include <cstdarg>
-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.
void debug(const String &s)
{
#if !defined(NDEBUG) || defined(TRACE_IN_RELEASE)
-
+
debugListener->printMessage("TagLib: " + s + "\n");
#endif
{
#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);
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)
# include <sys/endian.h>
#endif
+#include "tstring.h"
+#include <cstdio>
+#include <cstdarg>
#include <cstring>
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,