mirror of
https://github.com/taglib/taglib.git
synced 2025-05-27 21:20:26 -04:00
Merge pull request #424 from TsudaKageyu/format
Unified redundant string format functions. (backport from taglib2)
This commit is contained in:
commit
1bc5acd7a7
@ -30,43 +30,12 @@
|
||||
#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);
|
||||
|
@ -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)
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user