From 19bf50bbff325024c2ed9ad724aeb6acb39bae39 Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Sun, 3 May 2009 12:53:40 +0200 Subject: Moved strprintf into stringutils.{h,cpp} --- src/utils/stringutils.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/utils/stringutils.cpp') diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp index 57a0131e..c9428974 100644 --- a/src/utils/stringutils.cpp +++ b/src/utils/stringutils.cpp @@ -22,6 +22,8 @@ #include "utils/stringutils.h" #include +#include +#include std::string &trim(std::string &str) { @@ -69,6 +71,28 @@ const char *ipToString(int address) return asciiIP; } +std::string strprintf(char const *format, ...) +{ + char buf[256]; + va_list(args); + va_start(args, format); + int nb = vsnprintf(buf, 256, format, args); + va_end(args); + if (nb < 256) + { + return buf; + } + // The static size was not big enough, try again with a dynamic allocation. + ++nb; + char *buf2 = new char[nb]; + va_start(args, format); + vsnprintf(buf2, nb, format, args); + va_end(args); + std::string res(buf2); + delete [] buf2; + return res; +} + std::string &removeBadChars(std::string &str) { std::string::size_type pos; -- cgit v1.2.3-70-g09d2