Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core2/src/util/String.cc @ 949

Last change on this file since 949 was 947, checked in by landauf, 17 years ago
  • added CommandExecutor
  • added ConsoleCommand macros
  • added getTypename to all MultiTypes
  • added 2 static maps to Identifier that contain all existing Identifiers with their names and lowercase names respectively.
  • added 2 maps to each Identifier that contain all console commands of the Identifier with their names and lowercase names respectively
  • using tolower(.) and toupper(.) instead of selfmade hacks in String.h
  • added AccessLevel enum
  • added some test-console-commands to OutputHandler, Ambient and SpaceShip
File size: 5.9 KB
Line 
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*/
36void 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*/
50std::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*/
64bool 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[0] == '#' || teststring[0] == '%' || teststring[0] == ';' || (teststring[0] == '/' && teststring[0] == '/'))
75        return true;
76
77    return false;
78}
79
80/**
81    @brief Determines if a string is empty (contains only whitespaces).
82    @param str The string to check
83    @return True = it's empty
84*/
85bool isEmpty(const std::string& str)
86{
87    std::string temp = getStripped(str);
88    return ((temp == "") || (temp.size() == 0));
89}
90
91/**
92    @brief Determines if a string contains only numbers and maximal one '.'.
93    @param str The string to check
94    @return True = it's a number
95*/
96bool isNumeric(const std::string& str)
97{
98    bool foundPoint = false;
99
100    for (std::string::const_iterator it = str.begin(); it != str.end(); ++it)
101    {
102        if (((*it) < '0' || (*it) > '9'))
103        {
104            if ((*it) != '.' && !foundPoint)
105                foundPoint = true;
106            else
107                return false;
108        }
109    }
110
111    return true;
112}
113
114/**
115    @brief Replaces each char between A and Z with its lowercase equivalent.
116    @param str The string to convert
117*/
118void lowercase(std::string* str)
119{
120    for (unsigned int i = 0; i < str->size(); ++i)
121    {
122        (*str)[i] = tolower((*str)[i]);
123    }
124}
125
126/**
127    @brief Returns a copy of the given string without uppercase chars.
128    @param str The string
129    @return The copy
130*/
131std::string getLowercase(const std::string& str)
132{
133    std::string output = std::string(str);
134    lowercase(&output);
135    return output;
136}
137
138/**
139    @brief Replaces each char between a and z with its uppercase equivalent.
140    @param str The string to convert
141*/
142void uppercase(std::string* str)
143{
144    for (unsigned int i = 0; i < str->size(); ++i)
145    {
146        (*str)[i] = toupper((*str)[i]);
147    }
148}
149
150/**
151    @brief Returns a copy of the given string without lowercase chars.
152    @param str The string
153    @return The copy
154*/
155std::string getUppercase(const std::string& str)
156{
157    std::string output = std::string(str);
158    uppercase(&output);
159    return output;
160}
161
162/**
163 * @brief compares two strings without ignoring the case
164 * @param s1 first string
165 * @param s2 second string
166 */
167int nocaseCmp(const std::string& s1, const std::string& s2)
168{
169    std::string::const_iterator it1=s1.begin();
170    std::string::const_iterator it2=s2.begin();
171
172    //stop when either string's end has been reached
173    while ( (it1!=s1.end()) && (it2!=s2.end()) )
174    {
175        if(::toupper(*it1) != ::toupper(*it2)) //letters differ?
176            // return -1 to indicate smaller than, 1 otherwise
177            return (::toupper(*it1)  < ::toupper(*it2)) ? -1 : 1;
178        //proceed to the next character in each string
179        ++it1;
180        ++it2;
181    }
182    size_t size1=s1.size(), size2=s2.size();// cache lengths
183    //return -1,0 or 1 according to strings' lengths
184    if (size1==size2)
185        return 0;
186    return (size1<size2) ? -1 : 1;
187}
188
189
190/**
191 * @brief compares two strings without ignoring the case
192 * @param s1 first string
193 * @param s2 second string
194 * @param len how far from the beginning to start.
195 */
196int nocaseCmp(const std::string& s1, const std::string& s2, unsigned int len)
197{
198    if (len == 0)
199        return 0;
200    std::string::const_iterator it1=s1.begin();
201    std::string::const_iterator it2=s2.begin();
202
203    //stop when either string's end has been reached
204    while ( (it1!=s1.end()) && (it2!=s2.end()) && len-- > 0)
205    {
206        if(::toupper(*it1) != ::toupper(*it2)) //letters differ?
207            // return -1 to indicate smaller than, 1 otherwise
208            return (::toupper(*it1)  < ::toupper(*it2)) ? -1 : 1;
209        //proceed to the next character in each string
210        ++it1;
211        ++it2;
212    }
213    return 0;
214}
Note: See TracBrowser for help on using the repository browser.