[792] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * |
---|
| 4 | * |
---|
| 5 | * License notice: |
---|
| 6 | * |
---|
| 7 | * This program is free software; you can redistribute it and/or |
---|
| 8 | * modify it under the terms of the GNU General Public License |
---|
| 9 | * as published by the Free Software Foundation; either version 2 |
---|
| 10 | * of the License, or (at your option) any later version. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 20 | * |
---|
| 21 | * Author: |
---|
| 22 | * Fabian 'x3n' Landau |
---|
| 23 | * Co-authors: |
---|
| 24 | * Benjamin Grauer |
---|
| 25 | * |
---|
| 26 | */ |
---|
| 27 | |
---|
[933] | 28 | #ifndef _Util_String_H__ |
---|
| 29 | #define _Util_String_H__ |
---|
[792] | 30 | |
---|
| 31 | #include <string> |
---|
| 32 | #include <sstream> |
---|
| 33 | |
---|
| 34 | #include "UtilPrereqs.h" |
---|
| 35 | |
---|
| 36 | _UtilExport void strip(std::string* str); |
---|
| 37 | _UtilExport std::string getStripped(const std::string& str); |
---|
| 38 | |
---|
[994] | 39 | _UtilExport void stripEnclosingQuotes(std::string* str); |
---|
| 40 | _UtilExport std::string getStrippedEnclosingQuotes(const std::string& str); |
---|
| 41 | |
---|
[792] | 42 | _UtilExport bool isEmpty(const std::string& str); |
---|
| 43 | _UtilExport bool isComment(const std::string& str); |
---|
| 44 | _UtilExport bool isNumeric(const std::string& str); |
---|
| 45 | |
---|
| 46 | _UtilExport void lowercase(std::string* str); |
---|
| 47 | _UtilExport std::string getLowercase(const std::string& str); |
---|
| 48 | |
---|
| 49 | _UtilExport void uppercase(std::string* str); |
---|
| 50 | _UtilExport std::string getUppercase(const std::string& str); |
---|
| 51 | |
---|
| 52 | _UtilExport int nocaseCmp(const std::string& s1, const std::string& s2); |
---|
| 53 | _UtilExport int nocaseCmp(const std::string& s1, const std::string& s2, unsigned int len); |
---|
| 54 | |
---|
| 55 | //! The Convert class has some static member functions to convert strings to values and values to strings. |
---|
| 56 | class _UtilExport Convert |
---|
| 57 | { |
---|
| 58 | public: |
---|
| 59 | /** |
---|
| 60 | @brief Converts a value of any type to a string. |
---|
| 61 | @param output The string to write the result in |
---|
| 62 | @param input The variable to convert |
---|
| 63 | @return True if the conversion succeded |
---|
| 64 | |
---|
| 65 | @example |
---|
| 66 | float f = 3.14; |
---|
| 67 | std::string output; |
---|
[848] | 68 | bool success = Convert::ToString(&output, f); |
---|
[792] | 69 | */ |
---|
| 70 | template <typename T> |
---|
| 71 | static bool ToString(std::string* output, T input) |
---|
| 72 | { |
---|
| 73 | std::ostringstream oss; |
---|
| 74 | if (oss << input) |
---|
| 75 | { |
---|
| 76 | (*output) = oss.str(); |
---|
| 77 | return true; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | return false; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | @brief Converts a value of any type to a string and assigns a defaultvalue if the conversion fails. |
---|
| 85 | @param output The string to write the result in |
---|
| 86 | @param input The variable to convert |
---|
| 87 | @param fallbackString The assigned string if the conversion fails. |
---|
| 88 | @return True if the conversion succeeded |
---|
| 89 | |
---|
| 90 | @example |
---|
| 91 | float f = 3.14; |
---|
| 92 | std::string output; |
---|
[848] | 93 | bool success = Convert::ToString(&output, f, "0.000000"); |
---|
[792] | 94 | */ |
---|
| 95 | template <typename T> |
---|
| 96 | static bool ToString(std::string* output, T input, const std::string& fallbackString) |
---|
| 97 | { |
---|
| 98 | if (Convert::ToString(output, input)) |
---|
| 99 | return true; |
---|
| 100 | |
---|
| 101 | (*output) = fallbackString; |
---|
| 102 | return false; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | /** |
---|
| 106 | @brief Converts a string to a value of any type. |
---|
| 107 | @param output The variable to assign the result to |
---|
| 108 | @param input The string to convert |
---|
| 109 | @return True if the conversion succeeded |
---|
| 110 | |
---|
| 111 | @example |
---|
| 112 | std::string input = "3.14"; |
---|
| 113 | float f; |
---|
[848] | 114 | bool success = string2Number(&f, input); |
---|
[792] | 115 | */ |
---|
| 116 | template <typename T> |
---|
| 117 | static bool FromString(T* output, const std::string& input) |
---|
| 118 | { |
---|
| 119 | std::istringstream iss(input); |
---|
| 120 | if (iss >> (*output)) |
---|
| 121 | return true; |
---|
| 122 | |
---|
| 123 | return false; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | /** |
---|
| 127 | @brief Converts a string to a value of any type. |
---|
| 128 | @param output The variable to assign the result to |
---|
| 129 | @param input The string to convert |
---|
| 130 | @param fallbackValue The assigned value if the conversion fails |
---|
| 131 | @return True if the conversion succeeded |
---|
| 132 | |
---|
| 133 | @example |
---|
| 134 | std::string input = "3.14"; |
---|
| 135 | float f; |
---|
[848] | 136 | bool success = string2Number(&f, input, 0.000000); |
---|
[792] | 137 | */ |
---|
| 138 | template <typename T> |
---|
| 139 | static bool FromString(T* output, const std::string& input, T fallbackValue) |
---|
| 140 | { |
---|
| 141 | if (Convert::FromString(output, input)) |
---|
| 142 | return true; |
---|
| 143 | |
---|
| 144 | (*output) = fallbackValue; |
---|
| 145 | return false; |
---|
| 146 | } |
---|
| 147 | }; |
---|
| 148 | |
---|
[933] | 149 | #endif /* _Util_String_H__ */ |
---|