[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 | |
---|
[1006] | 36 | _UtilExport void strip(std::string* str); |
---|
| 37 | _UtilExport std::string getStripped(const std::string& str); |
---|
[792] | 38 | |
---|
[1006] | 39 | _UtilExport std::string removeTrailingWhitespaces(const std::string& str); |
---|
[994] | 40 | |
---|
[1020] | 41 | _UtilExport unsigned int getNextQuote(const std::string& str, unsigned int start); |
---|
[1049] | 42 | _UtilExport bool isBetweenQuotes(const std::string& str, unsigned int pos); |
---|
[1020] | 43 | |
---|
[1006] | 44 | _UtilExport bool hasStringBetweenQuotes(const std::string& str); |
---|
| 45 | _UtilExport std::string getStringBetweenQuotes(const std::string& str); |
---|
[792] | 46 | |
---|
[1049] | 47 | _UtilExport std::string stripEnclosingQuotes(const std::string& str); |
---|
[792] | 48 | |
---|
[1006] | 49 | _UtilExport bool isEmpty(const std::string& str); |
---|
| 50 | _UtilExport bool isComment(const std::string& str); |
---|
| 51 | _UtilExport bool isNumeric(const std::string& str); |
---|
[792] | 52 | |
---|
[1049] | 53 | _UtilExport std::string addSlashes(const std::string& str); |
---|
| 54 | _UtilExport std::string removeSlashes(const std::string& str); |
---|
| 55 | |
---|
[1006] | 56 | _UtilExport void lowercase(std::string* str); |
---|
| 57 | _UtilExport std::string getLowercase(const std::string& str); |
---|
[792] | 58 | |
---|
[1006] | 59 | _UtilExport void uppercase(std::string* str); |
---|
| 60 | _UtilExport std::string getUppercase(const std::string& str); |
---|
| 61 | |
---|
| 62 | _UtilExport int nocaseCmp(const std::string& s1, const std::string& s2); |
---|
| 63 | _UtilExport int nocaseCmp(const std::string& s1, const std::string& s2, unsigned int len); |
---|
| 64 | |
---|
| 65 | _UtilExport bool hasComment(const std::string& str); |
---|
| 66 | _UtilExport std::string getComment(const std::string& str); |
---|
| 67 | _UtilExport unsigned int getCommentPosition(const std::string& str); |
---|
[1049] | 68 | _UtilExport unsigned int getNextCommentPosition(const std::string& str, unsigned int start = 0); |
---|
[1006] | 69 | |
---|
[792] | 70 | //! The Convert class has some static member functions to convert strings to values and values to strings. |
---|
| 71 | class _UtilExport Convert |
---|
| 72 | { |
---|
| 73 | public: |
---|
| 74 | /** |
---|
| 75 | @brief Converts a value of any type to a string. |
---|
| 76 | @param output The string to write the result in |
---|
| 77 | @param input The variable to convert |
---|
| 78 | @return True if the conversion succeded |
---|
| 79 | |
---|
| 80 | @example |
---|
| 81 | float f = 3.14; |
---|
| 82 | std::string output; |
---|
[848] | 83 | bool success = Convert::ToString(&output, f); |
---|
[792] | 84 | */ |
---|
| 85 | template <typename T> |
---|
| 86 | static bool ToString(std::string* output, T input) |
---|
| 87 | { |
---|
| 88 | std::ostringstream oss; |
---|
| 89 | if (oss << input) |
---|
| 90 | { |
---|
| 91 | (*output) = oss.str(); |
---|
| 92 | return true; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | return false; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /** |
---|
| 99 | @brief Converts a value of any type to a string and assigns a defaultvalue if the conversion fails. |
---|
| 100 | @param output The string to write the result in |
---|
| 101 | @param input The variable to convert |
---|
| 102 | @param fallbackString The assigned string if the conversion fails. |
---|
| 103 | @return True if the conversion succeeded |
---|
| 104 | |
---|
| 105 | @example |
---|
| 106 | float f = 3.14; |
---|
| 107 | std::string output; |
---|
[848] | 108 | bool success = Convert::ToString(&output, f, "0.000000"); |
---|
[792] | 109 | */ |
---|
| 110 | template <typename T> |
---|
| 111 | static bool ToString(std::string* output, T input, const std::string& fallbackString) |
---|
| 112 | { |
---|
| 113 | if (Convert::ToString(output, input)) |
---|
| 114 | return true; |
---|
| 115 | |
---|
| 116 | (*output) = fallbackString; |
---|
| 117 | return false; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /** |
---|
| 121 | @brief Converts a string to a value of any type. |
---|
| 122 | @param output The variable to assign the result to |
---|
| 123 | @param input The string to convert |
---|
| 124 | @return True if the conversion succeeded |
---|
| 125 | |
---|
| 126 | @example |
---|
| 127 | std::string input = "3.14"; |
---|
| 128 | float f; |
---|
[848] | 129 | bool success = string2Number(&f, input); |
---|
[792] | 130 | */ |
---|
| 131 | template <typename T> |
---|
| 132 | static bool FromString(T* output, const std::string& input) |
---|
| 133 | { |
---|
| 134 | std::istringstream iss(input); |
---|
| 135 | if (iss >> (*output)) |
---|
| 136 | return true; |
---|
| 137 | |
---|
| 138 | return false; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | /** |
---|
| 142 | @brief Converts a string to a value of any type. |
---|
| 143 | @param output The variable to assign the result to |
---|
| 144 | @param input The string to convert |
---|
| 145 | @param fallbackValue The assigned value if the conversion fails |
---|
| 146 | @return True if the conversion succeeded |
---|
| 147 | |
---|
| 148 | @example |
---|
| 149 | std::string input = "3.14"; |
---|
| 150 | float f; |
---|
[848] | 151 | bool success = string2Number(&f, input, 0.000000); |
---|
[792] | 152 | */ |
---|
| 153 | template <typename T> |
---|
| 154 | static bool FromString(T* output, const std::string& input, T fallbackValue) |
---|
| 155 | { |
---|
| 156 | if (Convert::FromString(output, input)) |
---|
| 157 | return true; |
---|
| 158 | |
---|
| 159 | (*output) = fallbackValue; |
---|
| 160 | return false; |
---|
| 161 | } |
---|
| 162 | }; |
---|
| 163 | |
---|
[933] | 164 | #endif /* _Util_String_H__ */ |
---|