Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/OgreMain/src/OgreString.cpp @ 5

Last change on this file since 5 was 5, checked in by anonymous, 17 years ago

=hoffentlich gehts jetzt

File size: 8.9 KB
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2006 Torus Knot Software Ltd
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23
24You may alternatively use this source under the terms of a specific version of
25the OGRE Unrestricted License provided you have obtained such a license from
26Torus Knot Software Ltd.
27-----------------------------------------------------------------------------
28*/
29#include "OgreStableHeaders.h"
30#include "OgreString.h"
31
32#include "OgreStringVector.h"
33
34namespace Ogre {
35
36        //-----------------------------------------------------------------------
37        const String StringUtil::BLANK;
38        //-----------------------------------------------------------------------
39    void StringUtil::trim(String& str, bool left, bool right)
40    {
41        /*
42        size_t lspaces, rspaces, len = length(), i;
43
44        lspaces = rspaces = 0;
45
46        if( left )
47        {
48            // Find spaces / tabs on the left
49            for( i = 0;
50                i < len && ( at(i) == ' ' || at(i) == '\t' || at(i) == '\r');
51                ++lspaces, ++i );
52        }
53       
54        if( right && lspaces < len )
55        {
56            // Find spaces / tabs on the right
57            for( i = len - 1;
58                i >= 0 && ( at(i) == ' ' || at(i) == '\t' || at(i) == '\r');
59                rspaces++, i-- );
60        }
61
62        *this = substr(lspaces, len-lspaces-rspaces);
63        */
64        static const String delims = " \t\r";
65        if(right)
66            str.erase(str.find_last_not_of(delims)+1); // trim right
67        if(left)
68            str.erase(0, str.find_first_not_of(delims)); // trim left
69    }
70
71    //-----------------------------------------------------------------------
72    std::vector<String> StringUtil::split( const String& str, const String& delims, unsigned int maxSplits)
73    {
74        std::vector<String> ret;
75        // Pre-allocate some space for performance
76        ret.reserve(maxSplits ? maxSplits+1 : 10);    // 10 is guessed capacity for most case
77
78        unsigned int numSplits = 0;
79
80        // Use STL methods
81        size_t start, pos;
82        start = 0;
83        do 
84        {
85            pos = str.find_first_of(delims, start);
86            if (pos == start)
87            {
88                // Do nothing
89                start = pos + 1;
90            }
91            else if (pos == String::npos || (maxSplits && numSplits == maxSplits))
92            {
93                // Copy the rest of the string
94                ret.push_back( str.substr(start) );
95                break;
96            }
97            else
98            {
99                // Copy up to delimiter
100                ret.push_back( str.substr(start, pos - start) );
101                start = pos + 1;
102            }
103            // parse up to next real data
104            start = str.find_first_not_of(delims, start);
105            ++numSplits;
106
107        } while (pos != String::npos);
108
109
110
111        return ret;
112    }
113
114    //-----------------------------------------------------------------------
115    void StringUtil::toLowerCase(String& str)
116    {
117        std::transform(
118            str.begin(),
119            str.end(),
120            str.begin(),
121                        tolower);
122    }
123
124    //-----------------------------------------------------------------------
125    void StringUtil::toUpperCase(String& str) 
126    {
127        std::transform(
128            str.begin(),
129            str.end(),
130            str.begin(),
131                        toupper);
132    }
133    //-----------------------------------------------------------------------
134    bool StringUtil::startsWith(const String& str, const String& pattern, bool lowerCase)
135    {
136        size_t thisLen = str.length();
137        size_t patternLen = pattern.length();
138        if (thisLen < patternLen || patternLen == 0)
139            return false;
140
141        String startOfThis = str.substr(0, patternLen);
142        if (lowerCase)
143            StringUtil::toLowerCase(startOfThis);
144
145        return (startOfThis == pattern);
146    }
147    //-----------------------------------------------------------------------
148    bool StringUtil::endsWith(const String& str, const String& pattern, bool lowerCase)
149    {
150        size_t thisLen = str.length();
151        size_t patternLen = pattern.length();
152        if (thisLen < patternLen || patternLen == 0)
153            return false;
154
155        String endOfThis = str.substr(thisLen - patternLen, patternLen);
156        if (lowerCase)
157            StringUtil::toLowerCase(endOfThis);
158
159        return (endOfThis == pattern);
160    }
161    //-----------------------------------------------------------------------
162    String StringUtil::standardisePath(const String& init)
163    {
164        String path = init;
165
166        std::replace( path.begin(), path.end(), '\\', '/' );
167        if( path[path.length() - 1] != '/' )
168            path += '/';
169
170        return path;
171    }
172    //-----------------------------------------------------------------------
173    void StringUtil::splitFilename(const String& qualifiedName, 
174        String& outBasename, String& outPath)
175    {
176        String path = qualifiedName;
177        // Replace \ with / first
178        std::replace( path.begin(), path.end(), '\\', '/' );
179        // split based on final /
180        size_t i = path.find_last_of('/');
181
182        if (i == String::npos)
183        {
184            outPath.clear();
185                        outBasename = qualifiedName;
186        }
187        else
188        {
189            outBasename = path.substr(i+1, path.size() - i - 1);
190            outPath = path.substr(0, i+1);
191        }
192
193    }
194        //-----------------------------------------------------------------------
195        void StringUtil::splitBaseFilename(const Ogre::String& fullName, 
196                Ogre::String& outBasename, Ogre::String& outExtention)
197        {
198                size_t i = fullName.find_last_of(".");
199                if (i == Ogre::String::npos)
200                {
201                        outExtention.clear();
202                        outBasename = fullName;
203                }
204                else
205                {
206                        outExtention = fullName.substr(i+1);
207                        outBasename = fullName.substr(0, i);
208                }
209        }
210        // ----------------------------------------------------------------------------------------------------------------------------------------------
211        void StringUtil::splitFullFilename(     const Ogre::String& qualifiedName, 
212                Ogre::String& outBasename, Ogre::String& outExtention, Ogre::String& outPath )
213        {
214                Ogre::String fullName;
215                splitFilename( qualifiedName, fullName, outPath );
216                splitBaseFilename( fullName, outBasename, outExtention );
217        }
218    //-----------------------------------------------------------------------
219    bool StringUtil::match(const String& str, const String& pattern, bool caseSensitive)
220    {
221        String tmpStr = str;
222                String tmpPattern = pattern;
223        if (!caseSensitive)
224        {
225            StringUtil::toLowerCase(tmpStr);
226            StringUtil::toLowerCase(tmpPattern);
227        }
228
229        String::const_iterator strIt = tmpStr.begin();
230        String::const_iterator patIt = tmpPattern.begin();
231                String::const_iterator lastWildCardIt = tmpPattern.end();
232        while (strIt != tmpStr.end() && patIt != tmpPattern.end())
233        {
234            if (*patIt == '*')
235            {
236                                lastWildCardIt = patIt;
237                // Skip over looking for next character
238                ++patIt;
239                if (patIt == tmpPattern.end())
240                                {
241                                        // Skip right to the end since * matches the entire rest of the string
242                                        strIt = tmpStr.end();
243                                }
244                                else
245                {
246                                        // scan until we find next pattern character
247                    while(strIt != tmpStr.end() && *strIt != *patIt)
248                        ++strIt;
249                }
250            }
251            else
252            {
253                if (*patIt != *strIt)
254                {
255                                        if (lastWildCardIt != tmpPattern.end())
256                                        {
257                                                // The last wildcard can match this incorrect sequence
258                                                // rewind pattern to wildcard and keep searching
259                                                patIt = lastWildCardIt;
260                                                lastWildCardIt = tmpPattern.end();
261                                        }
262                                        else
263                                        {
264                                                // no wildwards left
265                                                return false;
266                                        }
267                }
268                else
269                {
270                    ++patIt;
271                    ++strIt;
272                }
273            }
274
275        }
276                // If we reached the end of both the pattern and the string, we succeeded
277                if (patIt == tmpPattern.end() && strIt == tmpStr.end())
278                {
279                return true;
280                }
281                else
282                {
283                        return false;
284                }
285
286    }
287
288}
Note: See TracBrowser for help on using the repository browser.