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