diff options
author | Didier Raboud <odyx@debian.org> | 2014-07-02 11:33:47 +0200 |
---|---|---|
committer | Didier Raboud <odyx@debian.org> | 2014-07-02 11:33:47 +0200 |
commit | 2e136acd34c45e5e6d105cabf91c67e5865c38fe (patch) | |
tree | 8561603160cce0b41ba31250539a22d37a2951de /src/common/stringutils.cpp | |
parent | 562be6fe765d7742f7c38fbd82f8cc26369e238b (diff) | |
parent | d9fee8b2adad613cf8c10d153cd5cd7b261b7863 (diff) | |
download | colobot-2e136acd34c45e5e6d105cabf91c67e5865c38fe.tar.gz colobot-2e136acd34c45e5e6d105cabf91c67e5865c38fe.tar.bz2 colobot-2e136acd34c45e5e6d105cabf91c67e5865c38fe.zip |
Merge tag 'colobot-gold-0.1.3-alpha' into debian
Diffstat (limited to 'src/common/stringutils.cpp')
-rw-r--r-- | src/common/stringutils.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/common/stringutils.cpp b/src/common/stringutils.cpp index 953abba..03a0f0b 100644 --- a/src/common/stringutils.cpp +++ b/src/common/stringutils.cpp @@ -17,6 +17,41 @@ #include "common/stringutils.h" +#include <cstdarg> +#include <cstdio> +#include <vector> + + +static std::string VFormat(const char *fmt, va_list ap) +{ + size_t size = 1024; + char stackbuf[1024]; + std::vector<char> dynamicbuf; + char *buf = &stackbuf[0]; + + while (1) + { + int needed = vsnprintf (buf, size, fmt, ap); + + if (needed <= static_cast<int>(size) && needed >= 0) + { + return std::string(buf, static_cast<size_t>(needed)); + } + + size = (needed > 0) ? (needed+1) : (size*2); + dynamicbuf.resize(size); + buf = &dynamicbuf[0]; + } +} + +std::string StrUtils::Format(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + std::string buf = VFormat(fmt, ap); + va_end(ap); + return buf; +} std::string StrUtils::Replace(const std::string &str, const std::string &oldStr, const std::string &newStr) { |