[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 | |
---|
[947] | 28 | #include <cctype> |
---|
| 29 | |
---|
[792] | 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 | /** |
---|
[1006] | 58 | @brief Returns a copy of a string without trailing whitespaces. |
---|
| 59 | @param str The string |
---|
| 60 | @return The modified copy |
---|
| 61 | */ |
---|
| 62 | std::string removeTrailingWhitespaces(const std::string& str) |
---|
| 63 | { |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | /** |
---|
| 67 | @brief Returns true if the string contains something like '..."between quotes"...' |
---|
| 68 | @param The string |
---|
| 69 | @return True if there is something between quotes |
---|
| 70 | */ |
---|
| 71 | bool hasStringBetweenQuotes(const std::string& str) |
---|
| 72 | { |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | @brief If the string contains something like '..."between quotes"...' then 'between quotes' gets returned (without quotes). |
---|
| 77 | @param The string |
---|
| 78 | @param The string between the quotes |
---|
| 79 | */ |
---|
| 80 | std::string getStringBetweenQuotes(const std::string& str) |
---|
| 81 | { |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | /** |
---|
[994] | 85 | @brief Removes enclosing quotes if available. |
---|
| 86 | @brief str The string to strip |
---|
| 87 | */ |
---|
| 88 | void stripEnclosingQuotes(std::string* str) |
---|
| 89 | { |
---|
| 90 | unsigned int start = std::string::npos; |
---|
| 91 | unsigned int end = 0; |
---|
| 92 | |
---|
| 93 | for (unsigned int pos = 0; (pos < (*str).size()) && (pos < std::string::npos); pos++) |
---|
| 94 | { |
---|
| 95 | if ((*str)[pos] == '"') |
---|
| 96 | { |
---|
| 97 | start = pos; |
---|
| 98 | break; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | if (((*str)[pos] != ' ') && ((*str)[pos] != '\t') && ((*str)[pos] != '\n')) |
---|
| 102 | return; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | for (unsigned int pos = (*str).size() - 1; pos < std::string::npos; pos--) |
---|
| 106 | { |
---|
| 107 | if ((*str)[pos] == '"') |
---|
| 108 | { |
---|
| 109 | end = pos; |
---|
| 110 | break; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | if (((*str)[pos] != ' ') && ((*str)[pos] != '\t') && ((*str)[pos] != '\n')) |
---|
| 114 | return; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | if ((start != std::string::npos) && (end != 0)) |
---|
| 118 | (*str) = (*str).substr(start + 1, end - start - 1); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | /** |
---|
| 122 | @brief Returns a copy of the string with removed enclosing quotes (if available). |
---|
| 123 | @brief str The string to strip |
---|
| 124 | @return The striped copy of the string |
---|
| 125 | */ |
---|
| 126 | std::string getStrippedEnclosingQuotes(const std::string& str) |
---|
| 127 | { |
---|
| 128 | std::string output = std::string(str); |
---|
| 129 | stripEnclosingQuotes(&output); |
---|
| 130 | return output; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | /** |
---|
[792] | 134 | @brief Determines if a string in is a comment. |
---|
| 135 | @param str The string to check |
---|
| 136 | @return True = it's a comment |
---|
| 137 | |
---|
| 138 | A comment is defined by a leading '#', '%', ';' or '//'. |
---|
| 139 | */ |
---|
| 140 | bool isComment(const std::string& str) |
---|
| 141 | { |
---|
| 142 | // Strip the line, whitespaces are disturbing |
---|
| 143 | std::string teststring = getStripped(str); |
---|
| 144 | |
---|
| 145 | // There are four possible comment-symbols: |
---|
| 146 | // 1) #comment in script-language style |
---|
| 147 | // 2) %comment in matlab style |
---|
| 148 | // 3) ;comment in unreal tournament config-file style |
---|
| 149 | // 4) //comment in code style |
---|
[957] | 150 | if (teststring.size() >= 2) |
---|
| 151 | { |
---|
| 152 | if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';' || (teststring[0] == '/' && teststring[1] == '/')) |
---|
| 153 | return true; |
---|
| 154 | } |
---|
| 155 | else if (teststring.size() == 1) |
---|
| 156 | { |
---|
| 157 | if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';') |
---|
| 158 | return true; |
---|
| 159 | } |
---|
[792] | 160 | |
---|
| 161 | return false; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | /** |
---|
| 165 | @brief Determines if a string is empty (contains only whitespaces). |
---|
| 166 | @param str The string to check |
---|
| 167 | @return True = it's empty |
---|
| 168 | */ |
---|
| 169 | bool isEmpty(const std::string& str) |
---|
| 170 | { |
---|
[931] | 171 | std::string temp = getStripped(str); |
---|
| 172 | return ((temp == "") || (temp.size() == 0)); |
---|
[792] | 173 | } |
---|
| 174 | |
---|
| 175 | /** |
---|
| 176 | @brief Determines if a string contains only numbers and maximal one '.'. |
---|
| 177 | @param str The string to check |
---|
| 178 | @return True = it's a number |
---|
| 179 | */ |
---|
| 180 | bool isNumeric(const std::string& str) |
---|
| 181 | { |
---|
| 182 | bool foundPoint = false; |
---|
| 183 | |
---|
| 184 | for (std::string::const_iterator it = str.begin(); it != str.end(); ++it) |
---|
| 185 | { |
---|
| 186 | if (((*it) < '0' || (*it) > '9')) |
---|
| 187 | { |
---|
| 188 | if ((*it) != '.' && !foundPoint) |
---|
| 189 | foundPoint = true; |
---|
| 190 | else |
---|
| 191 | return false; |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | return true; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | /** |
---|
| 199 | @brief Replaces each char between A and Z with its lowercase equivalent. |
---|
| 200 | @param str The string to convert |
---|
| 201 | */ |
---|
| 202 | void lowercase(std::string* str) |
---|
| 203 | { |
---|
[947] | 204 | for (unsigned int i = 0; i < str->size(); ++i) |
---|
| 205 | { |
---|
| 206 | (*str)[i] = tolower((*str)[i]); |
---|
| 207 | } |
---|
[792] | 208 | } |
---|
| 209 | |
---|
| 210 | /** |
---|
| 211 | @brief Returns a copy of the given string without uppercase chars. |
---|
| 212 | @param str The string |
---|
| 213 | @return The copy |
---|
| 214 | */ |
---|
| 215 | std::string getLowercase(const std::string& str) |
---|
| 216 | { |
---|
| 217 | std::string output = std::string(str); |
---|
| 218 | lowercase(&output); |
---|
| 219 | return output; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | /** |
---|
| 223 | @brief Replaces each char between a and z with its uppercase equivalent. |
---|
| 224 | @param str The string to convert |
---|
| 225 | */ |
---|
| 226 | void uppercase(std::string* str) |
---|
| 227 | { |
---|
[947] | 228 | for (unsigned int i = 0; i < str->size(); ++i) |
---|
| 229 | { |
---|
| 230 | (*str)[i] = toupper((*str)[i]); |
---|
| 231 | } |
---|
[792] | 232 | } |
---|
| 233 | |
---|
| 234 | /** |
---|
| 235 | @brief Returns a copy of the given string without lowercase chars. |
---|
| 236 | @param str The string |
---|
| 237 | @return The copy |
---|
| 238 | */ |
---|
| 239 | std::string getUppercase(const std::string& str) |
---|
| 240 | { |
---|
| 241 | std::string output = std::string(str); |
---|
| 242 | uppercase(&output); |
---|
| 243 | return output; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | /** |
---|
[1006] | 247 | @brief compares two strings without ignoring the case |
---|
| 248 | @param s1 first string |
---|
| 249 | @param s2 second string |
---|
| 250 | */ |
---|
[792] | 251 | int nocaseCmp(const std::string& s1, const std::string& s2) |
---|
| 252 | { |
---|
| 253 | std::string::const_iterator it1=s1.begin(); |
---|
| 254 | std::string::const_iterator it2=s2.begin(); |
---|
| 255 | |
---|
| 256 | //stop when either string's end has been reached |
---|
| 257 | while ( (it1!=s1.end()) && (it2!=s2.end()) ) |
---|
| 258 | { |
---|
| 259 | if(::toupper(*it1) != ::toupper(*it2)) //letters differ? |
---|
| 260 | // return -1 to indicate smaller than, 1 otherwise |
---|
| 261 | return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1; |
---|
| 262 | //proceed to the next character in each string |
---|
| 263 | ++it1; |
---|
| 264 | ++it2; |
---|
| 265 | } |
---|
| 266 | size_t size1=s1.size(), size2=s2.size();// cache lengths |
---|
| 267 | //return -1,0 or 1 according to strings' lengths |
---|
| 268 | if (size1==size2) |
---|
| 269 | return 0; |
---|
| 270 | return (size1<size2) ? -1 : 1; |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | |
---|
| 274 | /** |
---|
[1006] | 275 | @brief compares two strings without ignoring the case |
---|
| 276 | @param s1 first string |
---|
| 277 | @param s2 second string |
---|
| 278 | @param len how far from the beginning to start. |
---|
| 279 | */ |
---|
[792] | 280 | int nocaseCmp(const std::string& s1, const std::string& s2, unsigned int len) |
---|
| 281 | { |
---|
| 282 | if (len == 0) |
---|
| 283 | return 0; |
---|
| 284 | std::string::const_iterator it1=s1.begin(); |
---|
| 285 | std::string::const_iterator it2=s2.begin(); |
---|
| 286 | |
---|
| 287 | //stop when either string's end has been reached |
---|
| 288 | while ( (it1!=s1.end()) && (it2!=s2.end()) && len-- > 0) |
---|
| 289 | { |
---|
| 290 | if(::toupper(*it1) != ::toupper(*it2)) //letters differ? |
---|
| 291 | // return -1 to indicate smaller than, 1 otherwise |
---|
| 292 | return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1; |
---|
| 293 | //proceed to the next character in each string |
---|
| 294 | ++it1; |
---|
| 295 | ++it2; |
---|
| 296 | } |
---|
| 297 | return 0; |
---|
| 298 | } |
---|
[1006] | 299 | |
---|
| 300 | /** |
---|
| 301 | @brief Returns true if the string contains a comment, introduced by #, %, ; or //. |
---|
| 302 | @param str The string |
---|
| 303 | @return True if the string contains a comment |
---|
| 304 | */ |
---|
| 305 | bool hasComment(const std::string& str) |
---|
| 306 | { |
---|
| 307 | return (getCommentPosition(str) != std::string::npos); |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | /** |
---|
| 311 | @brief If the string contains a comment, the comment gets returned (including the comment symbol), an empty string otherwise. |
---|
| 312 | @param str The string |
---|
| 313 | @return The comment |
---|
| 314 | */ |
---|
| 315 | std::string getComment(const std::string& str) |
---|
| 316 | { |
---|
| 317 | return str.substr(getCommentPosition(str)); |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | /** |
---|
| 321 | @brief If the string contains a comment, the position of the comment-symbol gets returned, std::string::npos otherwise. |
---|
| 322 | @param str The string |
---|
| 323 | @return The position |
---|
| 324 | */ |
---|
| 325 | unsigned int getCommentPosition(const std::string& str) |
---|
| 326 | { |
---|
| 327 | for (unsigned int i = 0; i < str.size(); i++) |
---|
| 328 | if (isComment(str.substr(i))) |
---|
| 329 | return i; |
---|
| 330 | |
---|
| 331 | return std::string::npos; |
---|
| 332 | } |
---|