[792] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
[1056] | 3 | * > www.orxonox.net < |
---|
[792] | 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * Benjamin Grauer |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[1062] | 29 | #include "String.h" |
---|
| 30 | |
---|
[1052] | 31 | #include <cctype> |
---|
| 32 | #include <iostream> |
---|
[792] | 33 | |
---|
| 34 | /** |
---|
| 35 | @brief Removes all whitespaces from a string. |
---|
| 36 | @param str The string to strip |
---|
| 37 | */ |
---|
| 38 | void strip(std::string* str) |
---|
| 39 | { |
---|
| 40 | unsigned int pos; |
---|
| 41 | while ((pos = (*str).find(" ")) < (*str).length()) |
---|
| 42 | (*str).erase(pos, 1); |
---|
| 43 | while ((pos = (*str).find("\t")) < (*str).length()) |
---|
| 44 | (*str).erase(pos, 1); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | @brief Returns a copy of a string without whitespaces. |
---|
| 49 | @param str The string to strip |
---|
| 50 | @return The stripped line |
---|
| 51 | */ |
---|
| 52 | std::string getStripped(const std::string& str) |
---|
| 53 | { |
---|
| 54 | std::string output = std::string(str); |
---|
| 55 | strip(&output); |
---|
| 56 | return output; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | /** |
---|
[1052] | 60 | @brief Returns a copy of a string without trailing whitespaces. |
---|
| 61 | @param str The string |
---|
| 62 | @return The modified copy |
---|
| 63 | */ |
---|
| 64 | std::string removeTrailingWhitespaces(const std::string& str) |
---|
| 65 | { |
---|
| 66 | unsigned int pos1 = 0; |
---|
| 67 | unsigned int pos2 = str.size() - 1; |
---|
| 68 | for (; pos1 < str.size() && (str[pos1] == ' ' || str[pos1] == '\t' || str[pos1] == '\n'); pos1++); |
---|
| 69 | for (; pos2 >= 0 && (str[pos2] == ' ' || str[pos2] == '\t' || str[pos2] == '\n'); pos2--); |
---|
| 70 | return str.substr(pos1, pos2 - pos1 + 1); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | @brief Returns the position of the next quote in the string, starting with start. |
---|
| 75 | @param str The string |
---|
| 76 | @param start The startposition |
---|
| 77 | @return The position of the next quote (std::string::npos if there is no next quote) |
---|
| 78 | */ |
---|
| 79 | unsigned int getNextQuote(const std::string& str, unsigned int start) |
---|
| 80 | { |
---|
| 81 | unsigned int quote = start - 1; |
---|
| 82 | |
---|
| 83 | while ((quote = str.find('\"', quote + 1)) != std::string::npos) |
---|
| 84 | { |
---|
| 85 | unsigned int backslash = quote; |
---|
| 86 | unsigned int numbackslashes = 0; |
---|
| 87 | for (; backslash > 0; backslash--, numbackslashes++) |
---|
| 88 | if (str[backslash - 1] != '\\') |
---|
| 89 | break; |
---|
| 90 | |
---|
| 91 | if (numbackslashes % 2 == 0) |
---|
| 92 | break; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | return quote; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /** |
---|
| 99 | @brief Returns true if pos is between two quotes. |
---|
| 100 | @param str The string |
---|
| 101 | @param pos The position to check |
---|
| 102 | @return True if pos is between two quotes |
---|
| 103 | */ |
---|
| 104 | bool isBetweenQuotes(const std::string& str, unsigned int pos) |
---|
| 105 | { |
---|
| 106 | if (pos == std::string::npos) |
---|
| 107 | return false; |
---|
| 108 | |
---|
| 109 | unsigned int quotecount = 0; |
---|
| 110 | unsigned int quote = 0; |
---|
| 111 | while ((quote = getNextQuote(str, quote)) < pos) |
---|
| 112 | { |
---|
| 113 | quotecount++; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | if (quote == std::string::npos) |
---|
| 117 | return false; |
---|
| 118 | |
---|
| 119 | return ((quotecount % 2) == 1); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | /** |
---|
| 123 | @brief Returns true if the string contains something like '..."between quotes"...' |
---|
| 124 | @param The string |
---|
| 125 | @return True if there is something between quotes |
---|
| 126 | */ |
---|
| 127 | bool hasStringBetweenQuotes(const std::string& str) |
---|
| 128 | { |
---|
| 129 | unsigned int pos1 = getNextQuote(str, 0); |
---|
| 130 | unsigned int pos2 = getNextQuote(str, pos1 + 1); |
---|
| 131 | return (pos1 != std::string::npos && pos2 != std::string::npos && pos2 > pos1 + 1); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | /** |
---|
| 135 | @brief If the string contains something like '..."between quotes"...' then 'between quotes' gets returned (without quotes). |
---|
| 136 | @param The string |
---|
| 137 | @param The string between the quotes |
---|
| 138 | */ |
---|
| 139 | std::string getStringBetweenQuotes(const std::string& str) |
---|
| 140 | { |
---|
| 141 | unsigned int pos1 = getNextQuote(str, 0); |
---|
| 142 | unsigned int pos2 = getNextQuote(str, pos1 + 1); |
---|
| 143 | if (pos1 != std::string::npos && pos2 != std::string::npos) |
---|
| 144 | return str.substr(pos1, pos2 - pos1 + 1); |
---|
| 145 | else |
---|
| 146 | return ""; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | /** |
---|
| 150 | @brief Removes enclosing quotes if available. |
---|
| 151 | @brief str The string to strip |
---|
| 152 | @return The string with removed quotes |
---|
| 153 | */ |
---|
| 154 | std::string stripEnclosingQuotes(const std::string& str) |
---|
| 155 | { |
---|
| 156 | unsigned int start = std::string::npos; |
---|
| 157 | unsigned int end = 0; |
---|
| 158 | |
---|
| 159 | for (unsigned int pos = 0; (pos < str.size()) && (pos < std::string::npos); pos++) |
---|
| 160 | { |
---|
| 161 | if (str[pos] == '"') |
---|
| 162 | { |
---|
| 163 | start = pos; |
---|
| 164 | break; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | if ((str[pos] != ' ') && (str[pos] != '\t') && (str[pos] != '\n')) |
---|
| 168 | return str; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | for (unsigned int pos = str.size() - 1; pos < std::string::npos; pos--) |
---|
| 172 | { |
---|
| 173 | if (str[pos] == '"') |
---|
| 174 | { |
---|
| 175 | end = pos; |
---|
| 176 | break; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | if ((str[pos] != ' ') && (str[pos] != '\t') && (str[pos] != '\n')) |
---|
| 180 | return str; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | if ((start != std::string::npos) && (end != 0)) |
---|
| 184 | return str.substr(start + 1, end - start - 1); |
---|
| 185 | else |
---|
| 186 | return str; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | /** |
---|
[792] | 190 | @brief Determines if a string in is a comment. |
---|
| 191 | @param str The string to check |
---|
| 192 | @return True = it's a comment |
---|
| 193 | |
---|
| 194 | A comment is defined by a leading '#', '%', ';' or '//'. |
---|
| 195 | */ |
---|
| 196 | bool isComment(const std::string& str) |
---|
| 197 | { |
---|
| 198 | // Strip the line, whitespaces are disturbing |
---|
| 199 | std::string teststring = getStripped(str); |
---|
| 200 | |
---|
| 201 | // There are four possible comment-symbols: |
---|
| 202 | // 1) #comment in script-language style |
---|
| 203 | // 2) %comment in matlab style |
---|
| 204 | // 3) ;comment in unreal tournament config-file style |
---|
| 205 | // 4) //comment in code style |
---|
[1052] | 206 | if (teststring.size() >= 2) |
---|
| 207 | { |
---|
| 208 | if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';' || (teststring[0] == '/' && teststring[1] == '/')) |
---|
| 209 | return true; |
---|
| 210 | } |
---|
| 211 | else if (teststring.size() == 1) |
---|
| 212 | { |
---|
| 213 | if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';') |
---|
| 214 | return true; |
---|
| 215 | } |
---|
[792] | 216 | |
---|
| 217 | return false; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | /** |
---|
| 221 | @brief Determines if a string is empty (contains only whitespaces). |
---|
| 222 | @param str The string to check |
---|
| 223 | @return True = it's empty |
---|
| 224 | */ |
---|
| 225 | bool isEmpty(const std::string& str) |
---|
| 226 | { |
---|
[1052] | 227 | std::string temp = getStripped(str); |
---|
| 228 | return ((temp == "") || (temp.size() == 0)); |
---|
[792] | 229 | } |
---|
| 230 | |
---|
| 231 | /** |
---|
| 232 | @brief Determines if a string contains only numbers and maximal one '.'. |
---|
| 233 | @param str The string to check |
---|
| 234 | @return True = it's a number |
---|
| 235 | */ |
---|
| 236 | bool isNumeric(const std::string& str) |
---|
| 237 | { |
---|
| 238 | bool foundPoint = false; |
---|
| 239 | |
---|
| 240 | for (std::string::const_iterator it = str.begin(); it != str.end(); ++it) |
---|
| 241 | { |
---|
| 242 | if (((*it) < '0' || (*it) > '9')) |
---|
| 243 | { |
---|
| 244 | if ((*it) != '.' && !foundPoint) |
---|
| 245 | foundPoint = true; |
---|
| 246 | else |
---|
| 247 | return false; |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | return true; |
---|
| 252 | } |
---|
| 253 | |
---|
[1052] | 254 | std::string addSlashes(const std::string& str) |
---|
| 255 | { |
---|
| 256 | std::string output = str; |
---|
| 257 | |
---|
| 258 | for (unsigned int pos = 0; (pos = output.find('\\', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\\\"); } |
---|
| 259 | for (unsigned int pos = 0; (pos = output.find('\n', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\n"); } |
---|
| 260 | for (unsigned int pos = 0; (pos = output.find('\t', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\t"); } |
---|
| 261 | for (unsigned int pos = 0; (pos = output.find('\v', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\v"); } |
---|
| 262 | for (unsigned int pos = 0; (pos = output.find('\b', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\b"); } |
---|
| 263 | for (unsigned int pos = 0; (pos = output.find('\r', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\r"); } |
---|
| 264 | for (unsigned int pos = 0; (pos = output.find('\f', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\f"); } |
---|
| 265 | for (unsigned int pos = 0; (pos = output.find('\a', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\a"); } |
---|
| 266 | for (unsigned int pos = 0; (pos = output.find('"', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\\""); } |
---|
| 267 | for (unsigned int pos = 0; (pos = output.find('\0', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\0"); } |
---|
| 268 | |
---|
| 269 | return output; |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | std::string removeSlashes(const std::string& str) |
---|
| 273 | { |
---|
| 274 | if (str.size() == 0) |
---|
| 275 | return str; |
---|
[1111] | 276 | else if (str.size() == 1) |
---|
| 277 | { |
---|
| 278 | //TODO: decide whether we need the commented code |
---|
| 279 | /*if (str[0] != '\\') |
---|
| 280 | return ""; |
---|
| 281 | else*/ |
---|
| 282 | return str; |
---|
| 283 | } |
---|
[1052] | 284 | |
---|
| 285 | std::string output = ""; |
---|
| 286 | for (unsigned int pos = 0; pos < str.size() - 1; ) |
---|
| 287 | { |
---|
| 288 | if (str[pos] == '\\') |
---|
| 289 | { |
---|
| 290 | if (str[pos + 1] == '\\') { output += '\\'; pos += 2; continue; } |
---|
| 291 | else if (str[pos + 1] == 'n') { output += '\n'; pos += 2; continue; } |
---|
| 292 | else if (str[pos + 1] == 't') { output += '\t'; pos += 2; continue; } |
---|
| 293 | else if (str[pos + 1] == 'v') { output += '\v'; pos += 2; continue; } |
---|
| 294 | else if (str[pos + 1] == 'b') { output += '\b'; pos += 2; continue; } |
---|
| 295 | else if (str[pos + 1] == 'r') { output += '\r'; pos += 2; continue; } |
---|
| 296 | else if (str[pos + 1] == 'f') { output += '\f'; pos += 2; continue; } |
---|
| 297 | else if (str[pos + 1] == 'a') { output += '\a'; pos += 2; continue; } |
---|
| 298 | else if (str[pos + 1] == '"') { output += '"'; pos += 2; continue; } |
---|
| 299 | else if (str[pos + 1] == '0') { output += '\0'; pos += 2; continue; } |
---|
| 300 | } |
---|
| 301 | output += str[pos]; |
---|
| 302 | pos++; |
---|
| 303 | if (pos == str.size() - 1) |
---|
| 304 | output += str[pos]; |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | return output; |
---|
| 308 | } |
---|
| 309 | |
---|
[792] | 310 | /** |
---|
| 311 | @brief Replaces each char between A and Z with its lowercase equivalent. |
---|
| 312 | @param str The string to convert |
---|
| 313 | */ |
---|
| 314 | void lowercase(std::string* str) |
---|
| 315 | { |
---|
[1052] | 316 | for (unsigned int i = 0; i < str->size(); ++i) |
---|
| 317 | { |
---|
[1064] | 318 | (*str)[i] = (char)tolower((*str)[i]); |
---|
[1052] | 319 | } |
---|
[792] | 320 | } |
---|
| 321 | |
---|
| 322 | /** |
---|
| 323 | @brief Returns a copy of the given string without uppercase chars. |
---|
| 324 | @param str The string |
---|
| 325 | @return The copy |
---|
| 326 | */ |
---|
| 327 | std::string getLowercase(const std::string& str) |
---|
| 328 | { |
---|
| 329 | std::string output = std::string(str); |
---|
| 330 | lowercase(&output); |
---|
| 331 | return output; |
---|
| 332 | } |
---|
| 333 | |
---|
| 334 | /** |
---|
| 335 | @brief Replaces each char between a and z with its uppercase equivalent. |
---|
| 336 | @param str The string to convert |
---|
| 337 | */ |
---|
| 338 | void uppercase(std::string* str) |
---|
| 339 | { |
---|
[1052] | 340 | for (unsigned int i = 0; i < str->size(); ++i) |
---|
| 341 | { |
---|
[1064] | 342 | (*str)[i] = (char)toupper((*str)[i]); |
---|
[1052] | 343 | } |
---|
[792] | 344 | } |
---|
| 345 | |
---|
| 346 | /** |
---|
| 347 | @brief Returns a copy of the given string without lowercase chars. |
---|
| 348 | @param str The string |
---|
| 349 | @return The copy |
---|
| 350 | */ |
---|
| 351 | std::string getUppercase(const std::string& str) |
---|
| 352 | { |
---|
| 353 | std::string output = std::string(str); |
---|
| 354 | uppercase(&output); |
---|
| 355 | return output; |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | /** |
---|
[1052] | 359 | @brief compares two strings without ignoring the case |
---|
| 360 | @param s1 first string |
---|
| 361 | @param s2 second string |
---|
| 362 | */ |
---|
[792] | 363 | int nocaseCmp(const std::string& s1, const std::string& s2) |
---|
| 364 | { |
---|
| 365 | std::string::const_iterator it1=s1.begin(); |
---|
| 366 | std::string::const_iterator it2=s2.begin(); |
---|
| 367 | |
---|
| 368 | //stop when either string's end has been reached |
---|
| 369 | while ( (it1!=s1.end()) && (it2!=s2.end()) ) |
---|
| 370 | { |
---|
| 371 | if(::toupper(*it1) != ::toupper(*it2)) //letters differ? |
---|
| 372 | // return -1 to indicate smaller than, 1 otherwise |
---|
| 373 | return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1; |
---|
| 374 | //proceed to the next character in each string |
---|
| 375 | ++it1; |
---|
| 376 | ++it2; |
---|
| 377 | } |
---|
| 378 | size_t size1=s1.size(), size2=s2.size();// cache lengths |
---|
| 379 | //return -1,0 or 1 according to strings' lengths |
---|
| 380 | if (size1==size2) |
---|
| 381 | return 0; |
---|
| 382 | return (size1<size2) ? -1 : 1; |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | |
---|
| 386 | /** |
---|
[1052] | 387 | @brief compares two strings without ignoring the case |
---|
| 388 | @param s1 first string |
---|
| 389 | @param s2 second string |
---|
| 390 | @param len how far from the beginning to start. |
---|
| 391 | */ |
---|
[792] | 392 | int nocaseCmp(const std::string& s1, const std::string& s2, unsigned int len) |
---|
| 393 | { |
---|
| 394 | if (len == 0) |
---|
| 395 | return 0; |
---|
| 396 | std::string::const_iterator it1=s1.begin(); |
---|
| 397 | std::string::const_iterator it2=s2.begin(); |
---|
| 398 | |
---|
| 399 | //stop when either string's end has been reached |
---|
| 400 | while ( (it1!=s1.end()) && (it2!=s2.end()) && len-- > 0) |
---|
| 401 | { |
---|
| 402 | if(::toupper(*it1) != ::toupper(*it2)) //letters differ? |
---|
| 403 | // return -1 to indicate smaller than, 1 otherwise |
---|
| 404 | return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1; |
---|
| 405 | //proceed to the next character in each string |
---|
| 406 | ++it1; |
---|
| 407 | ++it2; |
---|
| 408 | } |
---|
| 409 | return 0; |
---|
| 410 | } |
---|
[1052] | 411 | |
---|
| 412 | /** |
---|
| 413 | @brief Returns true if the string contains a comment, introduced by #, %, ; or //. |
---|
| 414 | @param str The string |
---|
| 415 | @return True if the string contains a comment |
---|
| 416 | */ |
---|
| 417 | bool hasComment(const std::string& str) |
---|
| 418 | { |
---|
| 419 | return (getCommentPosition(str) != std::string::npos); |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | /** |
---|
| 423 | @brief If the string contains a comment, the comment gets returned (including the comment symbol), an empty string otherwise. |
---|
| 424 | @param str The string |
---|
| 425 | @return The comment |
---|
| 426 | */ |
---|
| 427 | std::string getComment(const std::string& str) |
---|
| 428 | { |
---|
| 429 | return str.substr(getCommentPosition(str)); |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | /** |
---|
| 433 | @brief If the string contains a comment, the position of the comment-symbol gets returned, std::string::npos otherwise. |
---|
| 434 | @param str The string |
---|
| 435 | @return The position |
---|
| 436 | */ |
---|
| 437 | unsigned int getCommentPosition(const std::string& str) |
---|
| 438 | { |
---|
| 439 | return getNextCommentPosition(str, 0); |
---|
| 440 | } |
---|
| 441 | |
---|
| 442 | /** |
---|
| 443 | @brief Returns the position of the next comment-symbol, starting with start. |
---|
| 444 | @param str The string |
---|
| 445 | @param start The startposition |
---|
| 446 | @return The position |
---|
| 447 | */ |
---|
| 448 | unsigned int getNextCommentPosition(const std::string& str, unsigned int start) |
---|
| 449 | { |
---|
| 450 | for (unsigned int i = start; i < str.size(); i++) |
---|
| 451 | if (isComment(str.substr(i))) |
---|
| 452 | return i; |
---|
| 453 | |
---|
| 454 | return std::string::npos; |
---|
| 455 | } |
---|