]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Added macro to allow simpler logic in functions with the need to vsnprintf
authorDaniel Vassdal <shutter@canternet.org>
Sat, 18 May 2013 18:31:25 +0000 (11:31 -0700)
committerDaniel Vassdal <shutter@canternet.org>
Sat, 18 May 2013 21:01:21 +0000 (14:01 -0700)
include/inspircd.h
include/inspstring.h
src/helperfuncs.cpp

index 9a9583499ab150995dfcf01dce8279d9cac4ad64..de574b6f41e2c0b7557e38a077c75d41adc099a0 100644 (file)
@@ -543,7 +543,8 @@ class CoreExport InspIRCd
        * @param ...
        * @return The formatted string
        */
-       static const char* Format(const char* formatString, ...);
+       static const char* Format(const char* formatString, ...) CUSTOM_PRINTF(1, 2);
+       static const char* Format(const va_list &vaList, const char* formatString) CUSTOM_PRINTF(2, 0);
 
        static void QuickExit(int status);
 
index eb7b7218f7798a833fbcf7aee1aa370fb2ac64be..ccc77da663156644b8723c34d6b998408ab07987 100644 (file)
 #include "config.h"
 #include <cstring>
 
+/** Sets ret to the formated string. last is the last parameter before ..., and format is the format in printf-style */
+#define VAFORMAT(ret, last, format) \
+       do { \
+       va_list _vaList; \
+       va_start(_vaList, last); \
+       ret = InspIRCd::Format(_vaList, format); \
+       va_end(_vaList); \
+       } while (false);
+
 /** Compose a hex string from raw data.
  * @param raw The raw data to compose hex from (can be NULL if rawsize is 0)
  * @param rawsize The size of the raw data buffer
index d11a13aa0f57e78133aa09b70e47a00eb9f9b774..c097b00330693885304e3b76b02efaffb17db2d3 100644 (file)
@@ -424,19 +424,22 @@ unsigned long InspIRCd::Duration(const std::string &str)
        return total + subtotal;
 }
 
-const char* InspIRCd::Format(const char* formatString, ...)
+const char* InspIRCd::Format(const va_list &vaList, const char* formatString)
 {
        static std::vector<char> formatBuffer(1024);
        int vsnret = 0;
-
-       va_list vaList;
-       va_start(vaList, formatString);
        while ((vsnret = vsnprintf(&formatBuffer[0], formatBuffer.size(), formatString, vaList)) < 0 || static_cast<unsigned int>(vsnret) >= formatBuffer.size())
                formatBuffer.resize(formatBuffer.size() * 2);
-       va_end(vaList);
        return &formatBuffer[0];
 }
 
+const char* InspIRCd::Format(const char* formatString, ...)
+{
+       const char* ret;
+       VAFORMAT(ret, formatString, formatString);
+       return ret;
+}
+
 bool InspIRCd::ULine(const std::string& sserver)
 {
        if (sserver.empty())