Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/util/String.cc @ 1064

Last change on this file since 1064 was 1064, checked in by rgrieder, 16 years ago
  • replaced all String2Number with ConvertValue
  • replaced all tokenize with SubString
  • dealt with warnings under msvc
  • removed some warnings by placing casts
  • bugfix in audio: local variable pushed into member variable std::vector
  • updated StableHeaders.h
File size: 13.7 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
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
29#include "String.h"
30
31#include <cctype>
32#include <iostream>
33
34/**
35    @brief Removes all whitespaces from a string.
36    @param str The string to strip
37*/
38void 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*/
52std::string getStripped(const std::string& str)
53{
54    std::string output = std::string(str);
55    strip(&output);
56    return output;
57}
58
59/**
60    @brief Returns a copy of a string without trailing whitespaces.
61    @param str The string
62    @return The modified copy
63*/
64std::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*/
79unsigned 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*/
104bool 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*/
127bool 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*/
139std::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*/
154std::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/**
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*/
196bool 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
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    }
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*/
225bool isEmpty(const std::string& str)
226{
227    std::string temp = getStripped(str);
228    return ((temp == "") || (temp.size() == 0));
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*/
236bool 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
254std::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
272std::string removeSlashes(const std::string& str)
273{
274    if (str.size() == 0)
275        return str;
276
277    std::string output = "";
278    for (unsigned int pos = 0; pos < str.size() - 1; )
279    {
280        if (str[pos] == '\\')
281        {
282            if (str[pos + 1] == '\\') { output += '\\'; pos += 2; continue; }
283            else if (str[pos + 1] == 'n') { output += '\n'; pos += 2; continue; }
284            else if (str[pos + 1] == 't') { output += '\t'; pos += 2; continue; }
285            else if (str[pos + 1] == 'v') { output += '\v'; pos += 2; continue; }
286            else if (str[pos + 1] == 'b') { output += '\b'; pos += 2; continue; }
287            else if (str[pos + 1] == 'r') { output += '\r'; pos += 2; continue; }
288            else if (str[pos + 1] == 'f') { output += '\f'; pos += 2; continue; }
289            else if (str[pos + 1] == 'a') { output += '\a'; pos += 2; continue; }
290            else if (str[pos + 1] == '"') { output += '"'; pos += 2; continue; }
291            else if (str[pos + 1] == '0') { output += '\0'; pos += 2; continue; }
292        }
293        output += str[pos];
294        pos++;
295        if (pos == str.size() - 1)
296            output += str[pos];
297    }
298
299    return output;
300}
301
302/**
303    @brief Replaces each char between A and Z with its lowercase equivalent.
304    @param str The string to convert
305*/
306void lowercase(std::string* str)
307{
308    for (unsigned int i = 0; i < str->size(); ++i)
309    {
310        (*str)[i] = (char)tolower((*str)[i]);
311    }
312}
313
314/**
315    @brief Returns a copy of the given string without uppercase chars.
316    @param str The string
317    @return The copy
318*/
319std::string getLowercase(const std::string& str)
320{
321    std::string output = std::string(str);
322    lowercase(&output);
323    return output;
324}
325
326/**
327    @brief Replaces each char between a and z with its uppercase equivalent.
328    @param str The string to convert
329*/
330void uppercase(std::string* str)
331{
332    for (unsigned int i = 0; i < str->size(); ++i)
333    {
334        (*str)[i] = (char)toupper((*str)[i]);
335    }
336}
337
338/**
339    @brief Returns a copy of the given string without lowercase chars.
340    @param str The string
341    @return The copy
342*/
343std::string getUppercase(const std::string& str)
344{
345    std::string output = std::string(str);
346    uppercase(&output);
347    return output;
348}
349
350/**
351    @brief compares two strings without ignoring the case
352    @param s1 first string
353    @param s2 second string
354*/
355int nocaseCmp(const std::string& s1, const std::string& s2)
356{
357    std::string::const_iterator it1=s1.begin();
358    std::string::const_iterator it2=s2.begin();
359
360    //stop when either string's end has been reached
361    while ( (it1!=s1.end()) && (it2!=s2.end()) )
362    {
363        if(::toupper(*it1) != ::toupper(*it2)) //letters differ?
364            // return -1 to indicate smaller than, 1 otherwise
365            return (::toupper(*it1)  < ::toupper(*it2)) ? -1 : 1;
366        //proceed to the next character in each string
367        ++it1;
368        ++it2;
369    }
370    size_t size1=s1.size(), size2=s2.size();// cache lengths
371    //return -1,0 or 1 according to strings' lengths
372    if (size1==size2)
373        return 0;
374    return (size1<size2) ? -1 : 1;
375}
376
377
378/**
379    @brief compares two strings without ignoring the case
380    @param s1 first string
381    @param s2 second string
382    @param len how far from the beginning to start.
383*/
384int nocaseCmp(const std::string& s1, const std::string& s2, unsigned int len)
385{
386    if (len == 0)
387        return 0;
388    std::string::const_iterator it1=s1.begin();
389    std::string::const_iterator it2=s2.begin();
390
391    //stop when either string's end has been reached
392    while ( (it1!=s1.end()) && (it2!=s2.end()) && len-- > 0)
393    {
394        if(::toupper(*it1) != ::toupper(*it2)) //letters differ?
395            // return -1 to indicate smaller than, 1 otherwise
396            return (::toupper(*it1)  < ::toupper(*it2)) ? -1 : 1;
397        //proceed to the next character in each string
398        ++it1;
399        ++it2;
400    }
401    return 0;
402}
403
404/**
405    @brief Returns true if the string contains a comment, introduced by #, %, ; or //.
406    @param str The string
407    @return True if the string contains a comment
408*/
409bool hasComment(const std::string& str)
410{
411    return (getCommentPosition(str) != std::string::npos);
412}
413
414/**
415    @brief If the string contains a comment, the comment gets returned (including the comment symbol), an empty string otherwise.
416    @param str The string
417    @return The comment
418*/
419std::string getComment(const std::string& str)
420{
421    return str.substr(getCommentPosition(str));
422}
423
424/**
425    @brief If the string contains a comment, the position of the comment-symbol gets returned, std::string::npos otherwise.
426    @param str The string
427    @return The position
428*/
429unsigned int getCommentPosition(const std::string& str)
430{
431    return getNextCommentPosition(str, 0);
432}
433
434/**
435    @brief Returns the position of the next comment-symbol, starting with start.
436    @param str The string
437    @param start The startposition
438    @return The position
439*/
440unsigned int getNextCommentPosition(const std::string& str, unsigned int start)
441{
442    for (unsigned int i = start; i < str.size(); i++)
443        if (isComment(str.substr(i)))
444            return i;
445
446    return std::string::npos;
447}
Note: See TracBrowser for help on using the repository browser.