From 4b5640883c851bc9f771da98190b8d416095ba0d Mon Sep 17 00:00:00 2001 From: Eugene Sandulenko Date: Mon, 18 Oct 2010 19:04:41 +0000 Subject: COMMON: Implement Common::vprintf(). Patch by littleboy svn-id: r53576 --- common/str.cpp | 29 +++++++++++++++++++++++------ common/str.h | 2 ++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/common/str.cpp b/common/str.cpp index c3c19adfe6..b4eadaca60 100644 --- a/common/str.cpp +++ b/common/str.cpp @@ -30,6 +30,18 @@ #include +#ifndef VA_COPY +#if defined(HAVE_VA_COPY) || defined(va_copy) +#define VA_COPY(dest, src) va_copy(dest, src) +#else +#ifdef HAVE___VA_COPY +#define VA_COPY(dest, src) __va_copy(dest, src) +#else +#define VA_COPY(dest, src) (dest) = (src) +#endif +#endif +#endif + namespace Common { MemoryPool *g_refCountPool = 0; // FIXME: This is never freed right now @@ -431,13 +443,22 @@ uint String::hash() const { // static String String::printf(const char *fmt, ...) { + va_list argptr; + + va_start(argptr, fmt); + Common::String output = vprintf(fmt, argptr); + va_end (argptr); + + return output; +} + +String String::vprintf(const char *fmt, va_list argptr) { String output; assert(output.isStorageIntern()); va_list va; - va_start(va, fmt); + VA_COPY(va, argptr); int len = vsnprintf(output._str, _builtinCapacity, fmt, va); - va_end(va); if (len == -1 || len == _builtinCapacity - 1) { // MSVC and IRIX don't return the size the full string would take up. @@ -460,9 +481,7 @@ String String::printf(const char *fmt, ...) { assert(!output.isStorageIntern()); size = output._extern._capacity; - va_start(va, fmt); len = vsnprintf(output._str, size, fmt, va); - va_end(va); } while (len == -1 || len >= size - 1); output._size = len; } else if (len < (int)_builtinCapacity) { @@ -471,9 +490,7 @@ String String::printf(const char *fmt, ...) { } else { // vsnprintf didn't have enough space, so grow buffer output.ensureCapacity(len, false); - va_start(va, fmt); int len2 = vsnprintf(output._str, len+1, fmt, va); - va_end(va); assert(len == len2); output._size = len2; } diff --git a/common/str.h b/common/str.h index e3dec6cdc2..92aa66cb59 100644 --- a/common/str.h +++ b/common/str.h @@ -220,6 +220,8 @@ public: */ static Common::String printf(const char *fmt, ...) GCC_PRINTF(1,2); + static Common::String vprintf(const char *fmt, va_list argptr); + public: typedef char * iterator; typedef const char * const_iterator; -- cgit v1.2.3