]> granicus.if.org Git - taglib/commitdiff
Unified redundant string format functions. (backport from taglib2)
authorTsuda Kageyu <tsuda.kageyu@gmail.com>
Sun, 10 Aug 2014 16:09:07 +0000 (01:09 +0900)
committerTsuda Kageyu <tsuda.kageyu@gmail.com>
Sun, 10 Aug 2014 16:09:07 +0000 (01:09 +0900)
taglib/toolkit/tdebug.cpp
taglib/toolkit/tstring.cpp
taglib/toolkit/tutils.h

index 71350af7c3f251ef4b1e6a7328ef834eca808c65..557baed6ec6a1841c9add6e757097a26e94bdcdb 100644 (file)
 #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.
@@ -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);
index 8287244ba0f5215b2965d6a4650b96a482f55385..47624e3269ec5739e8e558f426b00cbea5598642 100644 (file)
@@ -568,30 +568,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)
index 0874684a10c307d1290efb6815e983adb932817c..92486e23bfa37584ad23b3f9cf0c80daa8fb43ef 100644 (file)
@@ -44,6 +44,9 @@
 # include <sys/endian.h>
 #endif
 
+#include "tstring.h"
+#include <cstdio>
+#include <cstdarg>
 #include <cstring>
 
 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,