From Chris Hanson, " Add support for requesting and setting the current directory (a la getcwd/chdir) via a

C++-friendly string-class API.

 Prevent osgDB::concatPaths from faulting if the supplied "left" path string is empty."
This commit is contained in:
Robert Osfield 2009-11-23 10:19:37 +00:00
parent 87452fe1b9
commit d09323f93e
3 changed files with 56 additions and 1 deletions

View File

@ -40,6 +40,12 @@ extern OSGDB_EXPORT bool makeDirectory( const std::string &directoryPath );
// Make a new directory for a given file.
extern OSGDB_EXPORT bool makeDirectoryForFile( const std::string &filePath );
// Get current working directory.
extern OSGDB_EXPORT std::string getCurrentWorkingDirectory( void );
// Set current working directory.
extern OSGDB_EXPORT bool setCurrentWorkingDirectory( const std::string &newCurrentWorkingDirectory );
/** return true if a file exists. */
extern OSGDB_EXPORT bool fileExists(const std::string& filename);
@ -90,7 +96,7 @@ inline FilePathList& getLibraryFilePathList() { return osgDB::Registry::instance
extern OSGDB_EXPORT std::string findLibraryFile(const std::string& filename,CaseSensitivity caseSensitivity=CASE_SENSITIVE);
/** convert a string containing a list of paths delimited either with ';' (Windows) or ':' (All other platforms) into FilePath representation.*/
/** convert a string containing a list of paths delimited either with ';' (Windows) or ':' (All other platforms) into FilePath representation.*/
extern OSGDB_EXPORT void convertStringPathIntoFilePathList(const std::string& paths,FilePathList& filepath);
extern OSGDB_EXPORT void appendPlatformSpecificLibraryFilePaths(FilePathList& filepath);

View File

@ -250,6 +250,10 @@ std::string osgDB::concatPaths(const std::string& left, const std::string& right
const char delimiterForeign = '\\';
#endif
if(left.empty())
{
return(right);
}
char lastChar = left[left.size() - 1];
if(lastChar == delimiterNative)

View File

@ -11,6 +11,15 @@
* OpenSceneGraph Public License for more details.
*/
// handle TCHAR type on various platforms
// #ifndef is inspired by https://svn.apache.org/repos/asf/logging/log4cxx/tags/v0_9_4/include/log4cxx/helpers/tchar.h
// defining type as plain char is from unzip.h, line 64
#ifndef TCHAR
typedef char TCHAR;
#endif
// currently this impl is for _all_ platforms, except as defined.
// the mac version will change soon to reflect the path scheme under osx, but
// for now, the above include is commented out, and the below code takes precedence.
@ -194,6 +203,42 @@ bool osgDB::makeDirectoryForFile( const std::string &path )
return makeDirectory( getFilePath( path ));
}
std::string osgDB::getCurrentWorkingDirectory( void )
{
// MAX_PATH/cwd inspired by unzip.cpp
#ifndef MAX_PATH
#define MAX_PATH 1024
#endif
TCHAR rootdir[MAX_PATH];
if(getcwd(rootdir,MAX_PATH-1))
{
return(rootdir);
}
return("");
}// osgDB::getCurrentWorkingDirectory
bool osgDB::setCurrentWorkingDirectory( const std::string &newCurrentWorkingDirectory )
{
if (newCurrentWorkingDirectory.empty())
{
osg::notify(osg::DEBUG_INFO) << "osgDB::setCurrentWorkingDirectory(): called with empty string." << std::endl;
return false;
}
#ifdef OSG_USE_UTF8_FILENAME
return _wchdir( OSGDB_STRING_TO_FILENAME(newCurrentWorkingDirectory).c_str()) == 0;
#else
return chdir( newCurrentWorkingDirectory.c_str()) == 0;
#endif
return true;
} // osgDB::setCurrentWorkingDirectory
void osgDB::convertStringPathIntoFilePathList(const std::string& paths,FilePathList& filepath)
{
#if defined(WIN32) && !defined(__CYGWIN__)