diff options
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/stringutils.cpp | 24 | ||||
-rw-r--r-- | src/utils/stringutils.h | 11 | ||||
-rw-r--r-- | src/utils/strprintf.cpp | 47 | ||||
-rw-r--r-- | src/utils/strprintf.h | 35 |
4 files changed, 35 insertions, 82 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; diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h index 3af0bfa5..89d3d7e9 100644 --- a/src/utils/stringutils.h +++ b/src/utils/stringutils.h @@ -74,6 +74,17 @@ template<typename T> std::string toString(const T &arg) const char *ipToString(int address); /** + * A safe version of sprintf that returns a std::string of the result. + */ +std::string strprintf(char const *, ...) +#ifdef __GNUC__ + /* This attribute is nice: it even works through gettext invokation. For + example, gcc will complain that strprintf(_("%s"), 42) is ill-formed. */ + __attribute__((format(printf, 1, 2))) +#endif +; + +/** * Removes bad characters from a string * * @param str the string to remove the bad chars from diff --git a/src/utils/strprintf.cpp b/src/utils/strprintf.cpp deleted file mode 100644 index d960bc96..00000000 --- a/src/utils/strprintf.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/* - * The Mana World - * Copyright (C) 2007 The Mana World Development Team - * - * This file is part of The Mana World. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#include "utils/strprintf.h" - -#include <cstdarg> -#include <cstdio> - -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; -} diff --git a/src/utils/strprintf.h b/src/utils/strprintf.h deleted file mode 100644 index 78e7a04c..00000000 --- a/src/utils/strprintf.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * The Mana World - * Copyright (C) 2007 The Mana World Development Team - * - * This file is part of The Mana World. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#ifndef UTILS_STRPRINTF_H -#define UTILS_STRPRINTF_H - -#include <string> - -std::string strprintf(char const *, ...) -#ifdef __GNUC__ - /* This attribute is nice: it even works through gettext invokation. For - example, gcc will complain that strprintf(_("%s"), 42) is ill-formed. */ - __attribute__((format(printf, 1, 2))) -#endif -; - -#endif |