| 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 Determines if a string in is a comment. |
|---|
| 59 | @param str The string to check |
|---|
| 60 | @return True = it's a comment |
|---|
| 61 | |
|---|
| 62 | A comment is defined by a leading '#', '%', ';' or '//'. |
|---|
| 63 | */ |
|---|
| 64 | bool isComment(const std::string& str) |
|---|
| 65 | { |
|---|
| 66 | // Strip the line, whitespaces are disturbing |
|---|
| 67 | std::string teststring = getStripped(str); |
|---|
| 68 | |
|---|
| 69 | // There are four possible comment-symbols: |
|---|
| 70 | // 1) #comment in script-language style |
|---|
| 71 | // 2) %comment in matlab style |
|---|
| 72 | // 3) ;comment in unreal tournament config-file style |
|---|
| 73 | // 4) //comment in code style |
|---|
| 74 | if (teststring.size() >= 2) |
|---|
| 75 | { |
|---|
| 76 | if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';' || (teststring[0] == '/' && teststring[1] == '/')) |
|---|
| 77 | return true; |
|---|
| 78 | } |
|---|
| 79 | else if (teststring.size() == 1) |
|---|
| 80 | { |
|---|
| 81 | if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';') |
|---|
| 82 | return true; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | return false; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | /** |
|---|
| 89 | @brief Determines if a string is empty (contains only whitespaces). |
|---|
| 90 | @param str The string to check |
|---|
| 91 | @return True = it's empty |
|---|
| 92 | */ |
|---|
| 93 | bool isEmpty(const std::string& str) |
|---|
| 94 | { |
|---|
| 95 | std::string temp = getStripped(str); |
|---|
| 96 | return ((temp == "") || (temp.size() == 0)); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | /** |
|---|
| 100 | @brief Determines if a string contains only numbers and maximal one '.'. |
|---|
| 101 | @param str The string to check |
|---|
| 102 | @return True = it's a number |
|---|
| 103 | */ |
|---|
| 104 | bool isNumeric(const std::string& str) |
|---|
| 105 | { |
|---|
| 106 | bool foundPoint = false; |
|---|
| 107 | |
|---|
| 108 | for (std::string::const_iterator it = str.begin(); it != str.end(); ++it) |
|---|
| 109 | { |
|---|
| 110 | if (((*it) < '0' || (*it) > '9')) |
|---|
| 111 | { |
|---|
| 112 | if ((*it) != '.' && !foundPoint) |
|---|
| 113 | foundPoint = true; |
|---|
| 114 | else |
|---|
| 115 | return false; |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | return true; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | /** |
|---|
| 123 | @brief Replaces each char between A and Z with its lowercase equivalent. |
|---|
| 124 | @param str The string to convert |
|---|
| 125 | */ |
|---|
| 126 | void lowercase(std::string* str) |
|---|
| 127 | { |
|---|
| 128 | for (unsigned int i = 0; i < str->size(); ++i) |
|---|
| 129 | { |
|---|
| 130 | (*str)[i] = tolower((*str)[i]); |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | /** |
|---|
| 135 | @brief Returns a copy of the given string without uppercase chars. |
|---|
| 136 | @param str The string |
|---|
| 137 | @return The copy |
|---|
| 138 | */ |
|---|
| 139 | std::string getLowercase(const std::string& str) |
|---|
| 140 | { |
|---|
| 141 | std::string output = std::string(str); |
|---|
| 142 | lowercase(&output); |
|---|
| 143 | return output; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | /** |
|---|
| 147 | @brief Replaces each char between a and z with its uppercase equivalent. |
|---|
| 148 | @param str The string to convert |
|---|
| 149 | */ |
|---|
| 150 | void uppercase(std::string* str) |
|---|
| 151 | { |
|---|
| 152 | for (unsigned int i = 0; i < str->size(); ++i) |
|---|
| 153 | { |
|---|
| 154 | (*str)[i] = toupper((*str)[i]); |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | /** |
|---|
| 159 | @brief Returns a copy of the given string without lowercase chars. |
|---|
| 160 | @param str The string |
|---|
| 161 | @return The copy |
|---|
| 162 | */ |
|---|
| 163 | std::string getUppercase(const std::string& str) |
|---|
| 164 | { |
|---|
| 165 | std::string output = std::string(str); |
|---|
| 166 | uppercase(&output); |
|---|
| 167 | return output; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | /** |
|---|
| 171 | * @brief compares two strings without ignoring the case |
|---|
| 172 | * @param s1 first string |
|---|
| 173 | * @param s2 second string |
|---|
| 174 | */ |
|---|
| 175 | int nocaseCmp(const std::string& s1, const std::string& s2) |
|---|
| 176 | { |
|---|
| 177 | std::string::const_iterator it1=s1.begin(); |
|---|
| 178 | std::string::const_iterator it2=s2.begin(); |
|---|
| 179 | |
|---|
| 180 | //stop when either string's end has been reached |
|---|
| 181 | while ( (it1!=s1.end()) && (it2!=s2.end()) ) |
|---|
| 182 | { |
|---|
| 183 | if(::toupper(*it1) != ::toupper(*it2)) //letters differ? |
|---|
| 184 | // return -1 to indicate smaller than, 1 otherwise |
|---|
| 185 | return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1; |
|---|
| 186 | //proceed to the next character in each string |
|---|
| 187 | ++it1; |
|---|
| 188 | ++it2; |
|---|
| 189 | } |
|---|
| 190 | size_t size1=s1.size(), size2=s2.size();// cache lengths |
|---|
| 191 | //return -1,0 or 1 according to strings' lengths |
|---|
| 192 | if (size1==size2) |
|---|
| 193 | return 0; |
|---|
| 194 | return (size1<size2) ? -1 : 1; |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | /** |
|---|
| 199 | * @brief compares two strings without ignoring the case |
|---|
| 200 | * @param s1 first string |
|---|
| 201 | * @param s2 second string |
|---|
| 202 | * @param len how far from the beginning to start. |
|---|
| 203 | */ |
|---|
| 204 | int nocaseCmp(const std::string& s1, const std::string& s2, unsigned int len) |
|---|
| 205 | { |
|---|
| 206 | if (len == 0) |
|---|
| 207 | return 0; |
|---|
| 208 | std::string::const_iterator it1=s1.begin(); |
|---|
| 209 | std::string::const_iterator it2=s2.begin(); |
|---|
| 210 | |
|---|
| 211 | //stop when either string's end has been reached |
|---|
| 212 | while ( (it1!=s1.end()) && (it2!=s2.end()) && len-- > 0) |
|---|
| 213 | { |
|---|
| 214 | if(::toupper(*it1) != ::toupper(*it2)) //letters differ? |
|---|
| 215 | // return -1 to indicate smaller than, 1 otherwise |
|---|
| 216 | return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1; |
|---|
| 217 | //proceed to the next character in each string |
|---|
| 218 | ++it1; |
|---|
| 219 | ++it2; |
|---|
| 220 | } |
|---|
| 221 | return 0; |
|---|
| 222 | } |
|---|