root/OpenSceneGraph/trunk/src/osgDB/FileNameUtils.cpp @ 8912

Revision 8912, 8.4 kB (checked in by robert, 6 years ago)

Added getExtensionIncludingDot to provide more efficient handling of extensions

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under 
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13#include <stdlib.h>
14#include <string.h>
15#include <limits.h>
16
17#include <osgDB/FileNameUtils>
18#include <osgDB/FileUtils>
19
20#ifdef WIN32
21    #define _WIN32_WINNT 0x0500
22    #include <windows.h>
23#endif
24
25#if defined(__sgi)
26    #include <ctype.h>
27#elif defined(__GNUC__) || !defined(WIN32) || defined(__MWERKS__)
28    #include <cctype>
29    using std::tolower;
30#endif
31
32using namespace std;
33
34std::string osgDB::getFilePath(const std::string& fileName)
35{
36    std::string::size_type slash1 = fileName.find_last_of('/');
37    std::string::size_type slash2 = fileName.find_last_of('\\');
38    if (slash1==std::string::npos)
39    {
40        if (slash2==std::string::npos) return std::string();
41        return std::string(fileName,0,slash2);
42    }
43    if (slash2==std::string::npos) return std::string(fileName,0,slash1);
44    return std::string(fileName, 0, slash1>slash2 ?  slash1 : slash2);
45}
46
47
48std::string osgDB::getSimpleFileName(const std::string& fileName)
49{
50    std::string::size_type slash1 = fileName.find_last_of('/');
51    std::string::size_type slash2 = fileName.find_last_of('\\');
52    if (slash1==std::string::npos)
53    {
54        if (slash2==std::string::npos) return fileName;
55        return std::string(fileName.begin()+slash2+1,fileName.end());
56    }
57    if (slash2==std::string::npos) return std::string(fileName.begin()+slash1+1,fileName.end());
58    return std::string(fileName.begin()+(slash1>slash2?slash1:slash2)+1,fileName.end());
59}
60
61
62std::string osgDB::getFileExtension(const std::string& fileName)
63{
64    std::string::size_type dot = fileName.find_last_of('.');
65    if (dot==std::string::npos) return std::string("");
66    return std::string(fileName.begin()+dot+1,fileName.end());
67}
68
69std::string osgDB::getFileExtensionIncludingDot(const std::string& fileName)
70{
71    std::string::size_type dot = fileName.find_last_of('.');
72    if (dot==std::string::npos) return std::string("");
73    return std::string(fileName.begin()+dot,fileName.end());
74}
75
76std::string osgDB::convertFileNameToWindowsStyle(const std::string& fileName)
77{
78    std::string new_fileName(fileName);
79   
80    std::string::size_type slash = 0;
81    while( (slash=new_fileName.find_first_of('/',slash)) != std::string::npos)
82    {
83        new_fileName[slash]='\\';
84    }
85    return new_fileName;
86}
87
88std::string osgDB::convertFileNameToUnixStyle(const std::string& fileName)
89{
90    std::string new_fileName(fileName);
91   
92    std::string::size_type slash = 0;
93    while( (slash=new_fileName.find_first_of('\\',slash)) != std::string::npos)
94    {
95        new_fileName[slash]='/';
96    }
97
98    return new_fileName;
99}
100
101bool osgDB::isFileNameNativeStyle(const std::string& fileName)
102{
103#if defined(WIN32) && !defined(__CYGWIN__)
104    return fileName.find('/') == std::string::npos; // return true if no unix style slash exist
105#else
106    return fileName.find('\\') == std::string::npos; // return true if no windows style slash exist
107#endif
108}
109
110std::string osgDB::convertFileNameToNativeStyle(const std::string& fileName)
111{
112#if defined(WIN32) && !defined(__CYGWIN__)
113    return convertFileNameToWindowsStyle(fileName);
114#else
115    return convertFileNameToUnixStyle(fileName);
116#endif
117}
118
119
120
121std::string osgDB::getLowerCaseFileExtension(const std::string& filename)
122{
123    return convertToLowerCase(osgDB::getFileExtension(filename));
124}
125
126std::string osgDB::convertToLowerCase(const std::string& str)
127{
128    std::string lowcase_str(str);
129    for(std::string::iterator itr=lowcase_str.begin();
130        itr!=lowcase_str.end();
131        ++itr)
132    {
133        *itr = tolower(*itr);
134    }
135    return lowcase_str;
136}
137
138// strip one level of extension from the filename.
139std::string osgDB::getNameLessExtension(const std::string& fileName)
140{
141    std::string::size_type dot = fileName.find_last_of('.');
142    if (dot==std::string::npos) return fileName;
143    return std::string(fileName.begin(),fileName.begin()+dot);
144}
145
146
147std::string osgDB::getStrippedName(const std::string& fileName)
148{
149    std::string simpleName = getSimpleFileName(fileName);
150    return getNameLessExtension( simpleName );
151}
152
153
154bool osgDB::equalCaseInsensitive(const std::string& lhs,const std::string& rhs)
155{
156    if (lhs.size()!=rhs.size()) return false;
157    std::string::const_iterator litr = lhs.begin();
158    std::string::const_iterator ritr = rhs.begin();
159    while (litr!=lhs.end())
160    {
161        if (tolower(*litr)!=tolower(*ritr)) return false;
162        ++litr;
163        ++ritr;
164    }
165    return true;
166}
167
168bool osgDB::equalCaseInsensitive(const std::string& lhs,const char* rhs)
169{
170    if (rhs==NULL || lhs.size()!=strlen(rhs)) return false;
171    std::string::const_iterator litr = lhs.begin();
172    const char* cptr = rhs;
173    while (litr!=lhs.end())
174    {
175        if (tolower(*litr)!=tolower(*cptr)) return false;
176        ++litr;
177        ++cptr;
178    }
179    return true;
180}
181
182bool osgDB::containsServerAddress(const std::string& filename)
183{
184    // need to check for http://
185    if (filename.size()<7) return false;
186    if (filename.compare(0,7,"http://")==0) return true;
187    return false;
188}
189
190std::string osgDB::getServerAddress(const std::string& filename)
191{
192    if (filename.size()>=7 && filename.compare(0,7,"http://")==0)
193    {
194        std::string::size_type pos_slash = filename.find_first_of('/',7);
195        if (pos_slash!=std::string::npos)
196        {
197            return filename.substr(7,pos_slash-7);
198        }
199        else
200        {
201            return filename.substr(7,std::string::npos);
202        }
203    }
204    return "";
205}
206
207std::string osgDB::getServerFileName(const std::string& filename)
208{
209    if (filename.size()>=7 && filename.compare(0,7,"http://")==0)
210    {
211        std::string::size_type pos_slash = filename.find_first_of('/',7);
212        if (pos_slash!=std::string::npos)
213        {
214            return filename.substr(pos_slash+1,std::string::npos);
215        }
216        else
217        {
218            return "";
219        }
220   
221    }
222    return filename;
223}
224
225std::string osgDB::concatPaths(const std::string& left, const std::string& right)
226{
227#if defined(WIN32) && !defined(__CYGWIN__)
228    const char delimiterNative  = '\\';
229    const char delimiterForeign = '/';
230#else
231    const char delimiterNative  = '/';
232    const char delimiterForeign = '\\';
233#endif
234
235    char lastChar = left[left.size() - 1];
236
237    if(lastChar == delimiterNative)
238    {
239        return left + right;
240    }
241    else if(lastChar == delimiterForeign)
242    {
243        return left.substr(0, left.size() - 1) + delimiterNative + right;
244    }
245    else // lastChar != a delimiter
246    {
247        return left + delimiterNative + right;
248    }
249}
250
251std::string osgDB::getRealPath(const std::string& path)
252{
253#if defined(WIN32)  && !defined(__CYGWIN__)
254    // Not unicode compatible should give an error if UNICODE defined
255    char retbuf[MAX_PATH + 1];
256    char tempbuf1[MAX_PATH + 1];
257    GetFullPathName(path.c_str(), sizeof(retbuf), retbuf, NULL);
258    // Force drive letter to upper case
259    if ((retbuf[1] == ':') && islower(retbuf[0]))
260        retbuf[0] = _toupper(retbuf[0]);
261    if (fileExists(std::string(retbuf)))
262    {
263        // Canonicalise the full path
264        GetShortPathName(retbuf, tempbuf1, sizeof(tempbuf1));
265        GetLongPathName(tempbuf1, retbuf, sizeof(retbuf));
266        return std::string(retbuf);
267    }
268    else
269    {
270        // Canonicalise the directories
271        std::string FilePath = getFilePath(retbuf);
272        char tempbuf2[MAX_PATH + 1];
273        if (0 == GetShortPathName(FilePath.c_str(), tempbuf1, sizeof(tempbuf1)))
274            return std::string(retbuf);
275        if (0 == GetLongPathName(tempbuf1, tempbuf2, sizeof(tempbuf2)))
276            return std::string(retbuf);
277        FilePath = std::string(tempbuf2);
278        FilePath.append("\\");
279        FilePath.append(getSimpleFileName(std::string(retbuf)));
280        return FilePath;
281    }
282#else
283    char resolved_path[PATH_MAX];
284    char* result = realpath(path.c_str(), resolved_path);
285   
286    if (result) return std::string(resolved_path);
287    else return path;
288#endif 
289}
Note: See TracBrowser for help on using the browser.