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 | #include <cctype> |
---|
29 | |
---|
30 | #include "String.h" |
---|
31 | |
---|
32 | /** |
---|
33 | @brief Removes all whitespaces from a string. |
---|
34 | @param str The string to strip |
---|
35 | */ |
---|
36 | void strip(std::string* str) |
---|
37 | { |
---|
38 | unsigned int pos; |
---|
39 | while ((pos = (*str).find(" ")) < (*str).length()) |
---|
40 | (*str).erase(pos, 1); |
---|
41 | while ((pos = (*str).find("\t")) < (*str).length()) |
---|
42 | (*str).erase(pos, 1); |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | @brief Returns a copy of a string without whitespaces. |
---|
47 | @param str The string to strip |
---|
48 | @return The stripped line |
---|
49 | */ |
---|
50 | std::string getStripped(const std::string& str) |
---|
51 | { |
---|
52 | std::string output = std::string(str); |
---|
53 | strip(&output); |
---|
54 | return output; |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | @brief Removes enclosing quotes if available. |
---|
59 | @brief str The string to strip |
---|
60 | */ |
---|
61 | void stripEnclosingQuotes(std::string* str) |
---|
62 | { |
---|
63 | unsigned int start = std::string::npos; |
---|
64 | unsigned int end = 0; |
---|
65 | |
---|
66 | for (unsigned int pos = 0; (pos < (*str).size()) && (pos < std::string::npos); pos++) |
---|
67 | { |
---|
68 | if ((*str)[pos] == '"') |
---|
69 | { |
---|
70 | start = pos; |
---|
71 | break; |
---|
72 | } |
---|
73 | |
---|
74 | if (((*str)[pos] != ' ') && ((*str)[pos] != '\t') && ((*str)[pos] != '\n')) |
---|
75 | return; |
---|
76 | } |
---|
77 | |
---|
78 | for (unsigned int pos = (*str).size() - 1; pos < std::string::npos; pos--) |
---|
79 | { |
---|
80 | if ((*str)[pos] == '"') |
---|
81 | { |
---|
82 | end = pos; |
---|
83 | break; |
---|
84 | } |
---|
85 | |
---|
86 | if (((*str)[pos] != ' ') && ((*str)[pos] != '\t') && ((*str)[pos] != '\n')) |
---|
87 | return; |
---|
88 | } |
---|
89 | |
---|
90 | if ((start != std::string::npos) && (end != 0)) |
---|
91 | (*str) = (*str).substr(start + 1, end - start - 1); |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | @brief Returns a copy of the string with removed enclosing quotes (if available). |
---|
96 | @brief str The string to strip |
---|
97 | @return The striped copy of the string |
---|
98 | */ |
---|
99 | std::string getStrippedEnclosingQuotes(const std::string& str) |
---|
100 | { |
---|
101 | std::string output = std::string(str); |
---|
102 | stripEnclosingQuotes(&output); |
---|
103 | return output; |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | @brief Determines if a string in is a comment. |
---|
108 | @param str The string to check |
---|
109 | @return True = it's a comment |
---|
110 | |
---|
111 | A comment is defined by a leading '#', '%', ';' or '//'. |
---|
112 | */ |
---|
113 | bool isComment(const std::string& str) |
---|
114 | { |
---|
115 | // Strip the line, whitespaces are disturbing |
---|
116 | std::string teststring = getStripped(str); |
---|
117 | |
---|
118 | // There are four possible comment-symbols: |
---|
119 | // 1) #comment in script-language style |
---|
120 | // 2) %comment in matlab style |
---|
121 | // 3) ;comment in unreal tournament config-file style |
---|
122 | // 4) //comment in code style |
---|
123 | if (teststring.size() >= 2) |
---|
124 | { |
---|
125 | if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';' || (teststring[0] == '/' && teststring[1] == '/')) |
---|
126 | return true; |
---|
127 | } |
---|
128 | else if (teststring.size() == 1) |
---|
129 | { |
---|
130 | if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';') |
---|
131 | return true; |
---|
132 | } |
---|
133 | |
---|
134 | return false; |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | @brief Determines if a string is empty (contains only whitespaces). |
---|
139 | @param str The string to check |
---|
140 | @return True = it's empty |
---|
141 | */ |
---|
142 | bool isEmpty(const std::string& str) |
---|
143 | { |
---|
144 | std::string temp = getStripped(str); |
---|
145 | return ((temp == "") || (temp.size() == 0)); |
---|
146 | } |
---|
147 | |
---|
148 | /** |
---|
149 | @brief Determines if a string contains only numbers and maximal one '.'. |
---|
150 | @param str The string to check |
---|
151 | @return True = it's a number |
---|
152 | */ |
---|
153 | bool isNumeric(const std::string& str) |
---|
154 | { |
---|
155 | bool foundPoint = false; |
---|
156 | |
---|
157 | for (std::string::const_iterator it = str.begin(); it != str.end(); ++it) |
---|
158 | { |
---|
159 | if (((*it) < '0' || (*it) > '9')) |
---|
160 | { |
---|
161 | if ((*it) != '.' && !foundPoint) |
---|
162 | foundPoint = true; |
---|
163 | else |
---|
164 | return false; |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | return true; |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | @brief Replaces each char between A and Z with its lowercase equivalent. |
---|
173 | @param str The string to convert |
---|
174 | */ |
---|
175 | void lowercase(std::string* str) |
---|
176 | { |
---|
177 | for (unsigned int i = 0; i < str->size(); ++i) |
---|
178 | { |
---|
179 | (*str)[i] = tolower((*str)[i]); |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | /** |
---|
184 | @brief Returns a copy of the given string without uppercase chars. |
---|
185 | @param str The string |
---|
186 | @return The copy |
---|
187 | */ |
---|
188 | std::string getLowercase(const std::string& str) |
---|
189 | { |
---|
190 | std::string output = std::string(str); |
---|
191 | lowercase(&output); |
---|
192 | return output; |
---|
193 | } |
---|
194 | |
---|
195 | /** |
---|
196 | @brief Replaces each char between a and z with its uppercase equivalent. |
---|
197 | @param str The string to convert |
---|
198 | */ |
---|
199 | void uppercase(std::string* str) |
---|
200 | { |
---|
201 | for (unsigned int i = 0; i < str->size(); ++i) |
---|
202 | { |
---|
203 | (*str)[i] = toupper((*str)[i]); |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | /** |
---|
208 | @brief Returns a copy of the given string without lowercase chars. |
---|
209 | @param str The string |
---|
210 | @return The copy |
---|
211 | */ |
---|
212 | std::string getUppercase(const std::string& str) |
---|
213 | { |
---|
214 | std::string output = std::string(str); |
---|
215 | uppercase(&output); |
---|
216 | return output; |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | * @brief compares two strings without ignoring the case |
---|
221 | * @param s1 first string |
---|
222 | * @param s2 second string |
---|
223 | */ |
---|
224 | int nocaseCmp(const std::string& s1, const std::string& s2) |
---|
225 | { |
---|
226 | std::string::const_iterator it1=s1.begin(); |
---|
227 | std::string::const_iterator it2=s2.begin(); |
---|
228 | |
---|
229 | //stop when either string's end has been reached |
---|
230 | while ( (it1!=s1.end()) && (it2!=s2.end()) ) |
---|
231 | { |
---|
232 | if(::toupper(*it1) != ::toupper(*it2)) //letters differ? |
---|
233 | // return -1 to indicate smaller than, 1 otherwise |
---|
234 | return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1; |
---|
235 | //proceed to the next character in each string |
---|
236 | ++it1; |
---|
237 | ++it2; |
---|
238 | } |
---|
239 | size_t size1=s1.size(), size2=s2.size();// cache lengths |
---|
240 | //return -1,0 or 1 according to strings' lengths |
---|
241 | if (size1==size2) |
---|
242 | return 0; |
---|
243 | return (size1<size2) ? -1 : 1; |
---|
244 | } |
---|
245 | |
---|
246 | |
---|
247 | /** |
---|
248 | * @brief compares two strings without ignoring the case |
---|
249 | * @param s1 first string |
---|
250 | * @param s2 second string |
---|
251 | * @param len how far from the beginning to start. |
---|
252 | */ |
---|
253 | int nocaseCmp(const std::string& s1, const std::string& s2, unsigned int len) |
---|
254 | { |
---|
255 | if (len == 0) |
---|
256 | return 0; |
---|
257 | std::string::const_iterator it1=s1.begin(); |
---|
258 | std::string::const_iterator it2=s2.begin(); |
---|
259 | |
---|
260 | //stop when either string's end has been reached |
---|
261 | while ( (it1!=s1.end()) && (it2!=s2.end()) && len-- > 0) |
---|
262 | { |
---|
263 | if(::toupper(*it1) != ::toupper(*it2)) //letters differ? |
---|
264 | // return -1 to indicate smaller than, 1 otherwise |
---|
265 | return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1; |
---|
266 | //proceed to the next character in each string |
---|
267 | ++it1; |
---|
268 | ++it2; |
---|
269 | } |
---|
270 | return 0; |
---|
271 | } |
---|