diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2009-05-03 12:53:40 +0200 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2009-05-03 12:55:22 +0200 |
commit | 19bf50bbff325024c2ed9ad724aeb6acb39bae39 (patch) | |
tree | 811b25f52c49c501e58604757e5c72d442210377 /src/utils/stringutils.cpp | |
parent | a8ebdf2555ac6b89afa2e0e575ba01c349ef3fe2 (diff) | |
download | mana-19bf50bbff325024c2ed9ad724aeb6acb39bae39.tar.gz mana-19bf50bbff325024c2ed9ad724aeb6acb39bae39.tar.bz2 mana-19bf50bbff325024c2ed9ad724aeb6acb39bae39.tar.xz mana-19bf50bbff325024c2ed9ad724aeb6acb39bae39.zip |
Moved strprintf into stringutils.{h,cpp}
Diffstat (limited to 'src/utils/stringutils.cpp')
-rw-r--r-- | src/utils/stringutils.cpp | 24 |
1 files changed, 24 insertions, 0 deletions
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 <algorithm> +#include <cstdarg> +#include <cstdio> 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; |