From Joakim Simonsson, added osgDB::concateFile and osgDB::getRealPath.
Tweaks by Robert Osfield - Moved methods to FileNameUtils, added unix implementation of getRealPath add extern and OSGDB_EXPORT to function declarations.
This commit is contained in:
parent
d63b351137
commit
3ebc5efe05
@ -41,6 +41,13 @@ extern OSGDB_EXPORT bool containsServerAddress(const std::string& filename);
|
||||
extern OSGDB_EXPORT std::string getServerAddress(const std::string& filename);
|
||||
extern OSGDB_EXPORT std::string getServerFileName(const std::string& filename);
|
||||
|
||||
/** Concatenates two paths */
|
||||
extern OSGDB_EXPORT std::string concatPaths(const std::string& left, const std::string& right);
|
||||
|
||||
/** Removes .. and . dirs in a path */
|
||||
extern OSGDB_EXPORT std::string getRealPath(const std::string& path);
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -202,17 +202,43 @@ std::string osgDB::getServerFileName(const std::string& filename)
|
||||
return filename;
|
||||
}
|
||||
|
||||
std::string osgDB::concatPaths(const std::string& left, const std::string& right)
|
||||
{
|
||||
#ifdef WIN32
|
||||
const char delimiterNative = '\\';
|
||||
const char delimiterForeign = '/';
|
||||
#else
|
||||
const char delimiterNative = '/';
|
||||
const char delimiterForeign = '\\';
|
||||
#endif
|
||||
|
||||
// // here a little test I wrote to make sure a couple of the above methods are
|
||||
// // working fine.
|
||||
// void test()
|
||||
// {
|
||||
// std::string test("/here/we/are.exe");
|
||||
// std::string test2("\\there\\you\\go.dll");
|
||||
// std::cout << "getFilePath("<<test<<") "<<osgDB::getFilePath(test)<<std::endl;
|
||||
// std::cout << "getFilePath("<<test2<<") "<<osgDB::getFilePath(test2)<<std::endl;
|
||||
// std::cout << "getSimpleFileName("<<test<<") "<<osgDB::getSimpleFileName(test)<<std::endl;
|
||||
// std::cout << "getSimpleFileName("<<test2<<") "<<osgDB::getSimpleFileName(test2)<<std::endl;
|
||||
// std::cout << "getStrippedName("<<test<<") "<<osgDB::getStrippedName(test)<<std::endl;
|
||||
// std::cout << "getStrippedName("<<test2<<") "<<osgDB::getStrippedName(test2)<<std::endl;
|
||||
// }
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
std::string osgDB::getRealPath(const std::string& path)
|
||||
{
|
||||
#ifdef WIN32
|
||||
TCHAR retbuf[MAX_PATH + sizeof(TCHAR)];
|
||||
GetFullPathName(path.c_str(), sizeof(retbuf), retbuf, 0);
|
||||
return std::string(retbuf);
|
||||
#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
|
||||
}
|
||||
|
@ -203,7 +203,10 @@ std::string osgDB::findFileInPath(const std::string& filename, const FilePathLis
|
||||
++itr)
|
||||
{
|
||||
osg::notify(osg::DEBUG_INFO) << "itr='" <<*itr<< "'\n";
|
||||
std::string path = itr->empty() ? filename : *itr + '/'+ filename;
|
||||
std::string path = itr->empty() ? filename : concatPaths(*itr, filename);
|
||||
|
||||
path = getRealPath(path);
|
||||
|
||||
osg::notify(osg::DEBUG_INFO) << "FindFileInPath() : trying " << path << " ...\n";
|
||||
if(fileExists(path))
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user