From Jean-Sebastien Guay and Robert Osfield, cleaned up the way that unix/windows file separators are managed.

This commit is contained in:
Robert Osfield 2010-11-01 10:52:20 +00:00
parent 862a0c68e3
commit cd336a7d73
2 changed files with 27 additions and 11 deletions

View File

@ -44,7 +44,14 @@ extern OSGDB_EXPORT std::string convertFileNameToWindowsStyle(const std::string&
extern OSGDB_EXPORT std::string convertFileNameToUnixStyle(const std::string& fileName);
extern OSGDB_EXPORT std::string convertToLowerCase(const std::string& fileName);
const char UNIX_PATH_SEPARATOR = '/';
const char WINDOWS_PATH_SEPARATOR = '\\';
/** Get the path separator for the current platform. */
extern OSGDB_EXPORT char getNativePathSeparator();
/** Check if the path contains only the current platform's path separators. */
extern OSGDB_EXPORT bool isFileNameNativeStyle(const std::string& fileName);
/** Convert the path to contain only the current platform's path separators. */
extern OSGDB_EXPORT std::string convertFileNameToNativeStyle(const std::string& fileName);
extern OSGDB_EXPORT bool equalCaseInsensitive(const std::string& lhs,const std::string& rhs);

View File

@ -68,9 +68,9 @@ 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)
while( (slash=new_fileName.find_first_of(UNIX_PATH_SEPARATOR,slash)) != std::string::npos)
{
new_fileName[slash]='\\';
new_fileName[slash]=WINDOWS_PATH_SEPARATOR;
}
return new_fileName;
}
@ -80,20 +80,29 @@ 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)
while( (slash=new_fileName.find_first_of(WINDOWS_PATH_SEPARATOR,slash)) != std::string::npos)
{
new_fileName[slash]='/';
new_fileName[slash]=UNIX_PATH_SEPARATOR;
}
return new_fileName;
}
char osgDB::getNativePathSeparator()
{
#if defined(WIN32) && !defined(__CYGWIN__)
return WINDOWS_PATH_SEPARATOR;
#else
return UNIX_PATH_SEPARATOR;
#endif
}
bool osgDB::isFileNameNativeStyle(const std::string& fileName)
{
#if defined(WIN32) && !defined(__CYGWIN__)
return fileName.find('/') == std::string::npos; // return true if no unix style slash exist
return fileName.find(UNIX_PATH_SEPARATOR) == 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
return fileName.find(WINDOWS_PATH_SEPARATOR) == std::string::npos; // return true if no windows style backslash exist
#endif
}
@ -245,11 +254,11 @@ std::string osgDB::getServerFileName(const std::string& filename)
std::string osgDB::concatPaths(const std::string& left, const std::string& right)
{
#if defined(WIN32) && !defined(__CYGWIN__)
const char delimiterNative = '\\';
const char delimiterForeign = '/';
const char delimiterNative = WINDOWS_PATH_SEPARATOR;
const char delimiterForeign = UNIX_PATH_SEPARATOR;
#else
const char delimiterNative = '/';
const char delimiterForeign = '\\';
const char delimiterNative = UNIX_PATH_SEPARATOR;
const char delimiterForeign = WINDOWS_PATH_SEPARATOR;
#endif
if(left.empty())
@ -299,7 +308,7 @@ std::string osgDB::getRealPath(const std::string& path)
if (0 == GetLongPathName(tempbuf1, tempbuf2, sizeof(tempbuf2)))
return std::string(retbuf);
FilePath = std::string(tempbuf2);
FilePath.append("\\");
FilePath += WINDOWS_PATH_SEPARATOR;
FilePath.append(getSimpleFileName(std::string(retbuf)));
return FilePath;
}