2006-07-18 23:21:48 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2003-01-22 00:45:36 +08:00
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2007-07-24 04:10:13 +08:00
|
|
|
#include <stdlib.h>
|
2007-12-24 23:19:52 +08:00
|
|
|
#include <string.h>
|
2007-07-24 04:10:13 +08:00
|
|
|
#include <limits.h>
|
|
|
|
|
2001-10-22 05:27:40 +08:00
|
|
|
#include <osgDB/FileNameUtils>
|
2007-12-11 23:55:02 +08:00
|
|
|
#include <osgDB/FileUtils>
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2007-01-18 17:03:08 +08:00
|
|
|
#ifdef WIN32
|
2008-03-04 23:10:22 +08:00
|
|
|
#define _WIN32_WINNT 0x0500
|
2007-01-18 17:03:08 +08:00
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2002-01-21 03:30:16 +08:00
|
|
|
#if defined(__sgi)
|
2002-02-08 17:30:02 +08:00
|
|
|
#include <ctype.h>
|
2002-12-06 17:19:35 +08:00
|
|
|
#elif defined(__GNUC__) || !defined(WIN32) || defined(__MWERKS__)
|
2002-01-21 00:24:54 +08:00
|
|
|
#include <cctype>
|
|
|
|
using std::tolower;
|
2001-09-20 05:08:56 +08:00
|
|
|
#endif
|
|
|
|
|
2002-02-23 01:12:10 +08:00
|
|
|
using namespace std;
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
std::string osgDB::getFilePath(const std::string& fileName)
|
|
|
|
{
|
2010-03-10 20:04:14 +08:00
|
|
|
std::string::size_type slash = fileName.find_last_of("/\\");
|
|
|
|
if (slash==std::string::npos) return std::string();
|
|
|
|
else return std::string(fileName, 0, slash);
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string osgDB::getSimpleFileName(const std::string& fileName)
|
|
|
|
{
|
2010-03-10 20:04:14 +08:00
|
|
|
std::string::size_type slash = fileName.find_last_of("/\\");
|
|
|
|
if (slash==std::string::npos) return fileName;
|
|
|
|
else return std::string(fileName.begin()+slash+1,fileName.end());
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string osgDB::getFileExtension(const std::string& fileName)
|
|
|
|
{
|
|
|
|
std::string::size_type dot = fileName.find_last_of('.');
|
|
|
|
if (dot==std::string::npos) return std::string("");
|
|
|
|
return std::string(fileName.begin()+dot+1,fileName.end());
|
|
|
|
}
|
|
|
|
|
2008-09-22 22:46:54 +08:00
|
|
|
std::string osgDB::getFileExtensionIncludingDot(const std::string& fileName)
|
|
|
|
{
|
|
|
|
std::string::size_type dot = fileName.find_last_of('.');
|
|
|
|
if (dot==std::string::npos) return std::string("");
|
|
|
|
return std::string(fileName.begin()+dot,fileName.end());
|
|
|
|
}
|
|
|
|
|
2005-02-11 17:58:30 +08:00
|
|
|
std::string osgDB::convertFileNameToWindowsStyle(const std::string& fileName)
|
|
|
|
{
|
|
|
|
std::string new_fileName(fileName);
|
|
|
|
|
|
|
|
std::string::size_type slash = 0;
|
|
|
|
while( (slash=new_fileName.find_first_of('/',slash)) != std::string::npos)
|
|
|
|
{
|
|
|
|
new_fileName[slash]='\\';
|
|
|
|
}
|
|
|
|
return new_fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string osgDB::convertFileNameToUnixStyle(const std::string& fileName)
|
|
|
|
{
|
|
|
|
std::string new_fileName(fileName);
|
|
|
|
|
|
|
|
std::string::size_type slash = 0;
|
|
|
|
while( (slash=new_fileName.find_first_of('\\',slash)) != std::string::npos)
|
|
|
|
{
|
|
|
|
new_fileName[slash]='/';
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool osgDB::isFileNameNativeStyle(const std::string& fileName)
|
|
|
|
{
|
2007-02-09 06:31:02 +08:00
|
|
|
#if defined(WIN32) && !defined(__CYGWIN__)
|
2005-02-11 17:58:30 +08:00
|
|
|
return fileName.find('/') == std::string::npos; // return true if no unix style slash exist
|
|
|
|
#else
|
|
|
|
return fileName.find('\\') == std::string::npos; // return true if no windows style slash exist
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string osgDB::convertFileNameToNativeStyle(const std::string& fileName)
|
|
|
|
{
|
2007-02-09 06:31:02 +08:00
|
|
|
#if defined(WIN32) && !defined(__CYGWIN__)
|
2005-02-11 17:58:30 +08:00
|
|
|
return convertFileNameToWindowsStyle(fileName);
|
|
|
|
#else
|
|
|
|
return convertFileNameToUnixStyle(fileName);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
std::string osgDB::getLowerCaseFileExtension(const std::string& filename)
|
|
|
|
{
|
2008-07-13 23:24:45 +08:00
|
|
|
return convertToLowerCase(osgDB::getFileExtension(filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string osgDB::convertToLowerCase(const std::string& str)
|
|
|
|
{
|
|
|
|
std::string lowcase_str(str);
|
|
|
|
for(std::string::iterator itr=lowcase_str.begin();
|
|
|
|
itr!=lowcase_str.end();
|
2001-09-20 05:08:56 +08:00
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
*itr = tolower(*itr);
|
|
|
|
}
|
2008-07-13 23:24:45 +08:00
|
|
|
return lowcase_str;
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
|
2004-05-07 23:18:59 +08:00
|
|
|
// strip one level of extension from the filename.
|
|
|
|
std::string osgDB::getNameLessExtension(const std::string& fileName)
|
|
|
|
{
|
2010-03-10 19:40:17 +08:00
|
|
|
std::string::size_type dot = fileName.find_last_of('.');
|
|
|
|
std::string::size_type slash = fileName.find_last_of("/\\"); // Finds forward slash *or* back slash
|
|
|
|
if (dot==std::string::npos || (slash!=std::string::npos && dot<slash)) return fileName;
|
2004-05-07 23:18:59 +08:00
|
|
|
return std::string(fileName.begin(),fileName.begin()+dot);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-10 19:40:17 +08:00
|
|
|
// strip all extensions from the filename.
|
|
|
|
std::string osgDB::getNameLessAllExtensions(const std::string& fileName)
|
|
|
|
{
|
|
|
|
// Finds start serach position: from last slash, or the begining of the string if none found
|
|
|
|
std::string::size_type startPos = fileName.find_last_of("/\\"); // Finds forward slash *or* back slash
|
|
|
|
if (startPos == std::string::npos) startPos = 0;
|
|
|
|
std::string::size_type dot = fileName.find_first_of('.', startPos); // Finds *FIRST* dot from start pos
|
|
|
|
if (dot==std::string::npos) return fileName;
|
|
|
|
return std::string(fileName.begin(),fileName.begin()+dot);
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
std::string osgDB::getStrippedName(const std::string& fileName)
|
|
|
|
{
|
2002-06-12 22:46:44 +08:00
|
|
|
std::string simpleName = getSimpleFileName(fileName);
|
2004-05-07 23:18:59 +08:00
|
|
|
return getNameLessExtension( simpleName );
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool osgDB::equalCaseInsensitive(const std::string& lhs,const std::string& rhs)
|
|
|
|
{
|
|
|
|
if (lhs.size()!=rhs.size()) return false;
|
|
|
|
std::string::const_iterator litr = lhs.begin();
|
|
|
|
std::string::const_iterator ritr = rhs.begin();
|
|
|
|
while (litr!=lhs.end())
|
|
|
|
{
|
|
|
|
if (tolower(*litr)!=tolower(*ritr)) return false;
|
|
|
|
++litr;
|
|
|
|
++ritr;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool osgDB::equalCaseInsensitive(const std::string& lhs,const char* rhs)
|
|
|
|
{
|
|
|
|
if (rhs==NULL || lhs.size()!=strlen(rhs)) return false;
|
|
|
|
std::string::const_iterator litr = lhs.begin();
|
|
|
|
const char* cptr = rhs;
|
|
|
|
while (litr!=lhs.end())
|
|
|
|
{
|
|
|
|
if (tolower(*litr)!=tolower(*cptr)) return false;
|
|
|
|
++litr;
|
|
|
|
++cptr;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2002-06-12 22:46:44 +08:00
|
|
|
|
2009-03-10 20:21:13 +08:00
|
|
|
|
|
|
|
|
2004-10-06 21:11:04 +08:00
|
|
|
bool osgDB::containsServerAddress(const std::string& filename)
|
|
|
|
{
|
2009-03-10 20:21:13 +08:00
|
|
|
// need to check for ://
|
2010-03-05 23:17:26 +08:00
|
|
|
std::string::size_type pos(filename.find("://"));
|
2009-03-10 20:21:13 +08:00
|
|
|
if (pos == std::string::npos)
|
|
|
|
return false;
|
|
|
|
std::string proto(filename.substr(0, pos));
|
|
|
|
|
|
|
|
return Registry::instance()->isProtocolRegistered(proto);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string osgDB::getServerProtocol(const std::string& filename)
|
|
|
|
{
|
2010-03-05 23:17:26 +08:00
|
|
|
std::string::size_type pos(filename.find("://"));
|
2009-03-10 20:21:13 +08:00
|
|
|
if (pos != std::string::npos)
|
|
|
|
return filename.substr(0,pos);
|
|
|
|
|
|
|
|
return "";
|
2004-10-06 21:11:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string osgDB::getServerAddress(const std::string& filename)
|
|
|
|
{
|
2010-03-05 23:17:26 +08:00
|
|
|
std::string::size_type pos(filename.find("://"));
|
2009-03-10 20:21:13 +08:00
|
|
|
|
|
|
|
if (pos != std::string::npos)
|
2004-10-06 21:11:04 +08:00
|
|
|
{
|
2009-03-10 20:21:13 +08:00
|
|
|
std::string::size_type pos_slash = filename.find_first_of('/',pos+3);
|
2004-10-06 21:11:04 +08:00
|
|
|
if (pos_slash!=std::string::npos)
|
|
|
|
{
|
2009-03-10 20:21:13 +08:00
|
|
|
return filename.substr(pos+3,pos_slash-pos-3);
|
2004-10-06 21:11:04 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-03-10 20:21:13 +08:00
|
|
|
return filename.substr(pos+3,std::string::npos);
|
2004-10-06 21:11:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string osgDB::getServerFileName(const std::string& filename)
|
|
|
|
{
|
2010-03-05 23:36:32 +08:00
|
|
|
std::string::size_type pos(filename.find("://"));
|
2009-03-10 20:21:13 +08:00
|
|
|
|
|
|
|
if (pos != std::string::npos)
|
2004-10-06 21:11:04 +08:00
|
|
|
{
|
2009-03-10 20:21:13 +08:00
|
|
|
std::string::size_type pos_slash = filename.find_first_of('/',pos+3);
|
2004-10-06 21:11:04 +08:00
|
|
|
if (pos_slash!=std::string::npos)
|
|
|
|
{
|
|
|
|
return filename.substr(pos_slash+1,std::string::npos);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2007-01-17 23:59:29 +08:00
|
|
|
std::string osgDB::concatPaths(const std::string& left, const std::string& right)
|
|
|
|
{
|
2007-02-09 06:31:02 +08:00
|
|
|
#if defined(WIN32) && !defined(__CYGWIN__)
|
2007-01-17 23:59:29 +08:00
|
|
|
const char delimiterNative = '\\';
|
|
|
|
const char delimiterForeign = '/';
|
|
|
|
#else
|
|
|
|
const char delimiterNative = '/';
|
|
|
|
const char delimiterForeign = '\\';
|
|
|
|
#endif
|
|
|
|
|
2009-11-23 18:19:37 +08:00
|
|
|
if(left.empty())
|
|
|
|
{
|
|
|
|
return(right);
|
|
|
|
}
|
2007-01-17 23:59:29 +08:00
|
|
|
char lastChar = left[left.size() - 1];
|
|
|
|
|
|
|
|
if(lastChar == delimiterNative)
|
|
|
|
{
|
|
|
|
return left + right;
|
|
|
|
}
|
|
|
|
else if(lastChar == delimiterForeign)
|
|
|
|
{
|
|
|
|
return left.substr(0, left.size() - 1) + delimiterNative + right;
|
|
|
|
}
|
|
|
|
else // lastChar != a delimiter
|
|
|
|
{
|
|
|
|
return left + delimiterNative + right;
|
|
|
|
}
|
|
|
|
}
|
2004-10-06 21:11:04 +08:00
|
|
|
|
2007-01-17 23:59:29 +08:00
|
|
|
std::string osgDB::getRealPath(const std::string& path)
|
|
|
|
{
|
2007-02-09 06:31:02 +08:00
|
|
|
#if defined(WIN32) && !defined(__CYGWIN__)
|
2007-12-11 23:55:02 +08:00
|
|
|
// Not unicode compatible should give an error if UNICODE defined
|
|
|
|
char retbuf[MAX_PATH + 1];
|
|
|
|
char tempbuf1[MAX_PATH + 1];
|
|
|
|
GetFullPathName(path.c_str(), sizeof(retbuf), retbuf, NULL);
|
|
|
|
// Force drive letter to upper case
|
|
|
|
if ((retbuf[1] == ':') && islower(retbuf[0]))
|
|
|
|
retbuf[0] = _toupper(retbuf[0]);
|
|
|
|
if (fileExists(std::string(retbuf)))
|
|
|
|
{
|
|
|
|
// Canonicalise the full path
|
|
|
|
GetShortPathName(retbuf, tempbuf1, sizeof(tempbuf1));
|
|
|
|
GetLongPathName(tempbuf1, retbuf, sizeof(retbuf));
|
|
|
|
return std::string(retbuf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Canonicalise the directories
|
|
|
|
std::string FilePath = getFilePath(retbuf);
|
|
|
|
char tempbuf2[MAX_PATH + 1];
|
|
|
|
if (0 == GetShortPathName(FilePath.c_str(), tempbuf1, sizeof(tempbuf1)))
|
|
|
|
return std::string(retbuf);
|
|
|
|
if (0 == GetLongPathName(tempbuf1, tempbuf2, sizeof(tempbuf2)))
|
|
|
|
return std::string(retbuf);
|
|
|
|
FilePath = std::string(tempbuf2);
|
|
|
|
FilePath.append("\\");
|
|
|
|
FilePath.append(getSimpleFileName(std::string(retbuf)));
|
|
|
|
return FilePath;
|
|
|
|
}
|
2007-01-17 23:59:29 +08:00
|
|
|
#else
|
|
|
|
char resolved_path[PATH_MAX];
|
|
|
|
char* result = realpath(path.c_str(), resolved_path);
|
|
|
|
|
|
|
|
if (result) return std::string(resolved_path);
|
|
|
|
else return path;
|
|
|
|
#endif
|
|
|
|
}
|