From ac6d25a8402dfaa340a77cbfa3b651b0e0b3a077 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 1 Sep 2004 14:49:18 +0000 Subject: [PATCH] Added Don's new osgDB::makeDirectory() code FileUtils. --- include/osgDB/FileUtils | 6 ++++ src/osgDB/FileUtils.cpp | 79 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/include/osgDB/FileUtils b/include/osgDB/FileUtils index 64ed55b85..a6202593a 100644 --- a/include/osgDB/FileUtils +++ b/include/osgDB/FileUtils @@ -36,6 +36,12 @@ enum FileType DIRECTORY, }; +// Make a new directory. Returns true if directory exists or was created. +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 ); + /** return true if a file exisits. */ extern OSGDB_EXPORT bool fileExists(const std::string& filename); diff --git a/src/osgDB/FileUtils.cpp b/src/osgDB/FileUtils.cpp index 1cc220d0f..a711bff65 100644 --- a/src/osgDB/FileUtils.cpp +++ b/src/osgDB/FileUtils.cpp @@ -24,20 +24,99 @@ #include #include #include + #include // for _mkdir + #define mkdir(x,y) _mkdir((x)) + // set up for windows so acts just like unix access(). #define F_OK 4 + + #else // unix #include #include #include #endif + // set up _S_ISDIR() +#if !defined(S_ISDIR) +# if defined( _S_IFDIR) && !defined( __S_IFDIR) +# define __S_IFDIR _S_IFDIR +# endif +# define S_ISDIR(mode) (mode&__S_IFDIR) +#endif + #include #include #include #include +#include +#include + +bool osgDB::makeDirectory( const std::string &path ) +{ + if (path.empty()) + { + osg::notify(osg::DEBUG_INFO) << "osgDB::makeDirectory(): cannot create an empty directory" << std::endl; + return false; + } + + struct stat stbuf; + if( stat( path.c_str(), &stbuf ) == 0 ) + { + if( S_ISDIR(stbuf.st_mode)) + return true; + else + { + osg::notify(osg::DEBUG_INFO) << "osgDB::makeDirectory(): " << + path << " already exists and is not a directory!" << std::endl; + return false; + } + } + + std::string dir = path; + std::stack paths; + while( true ) + { + if( dir.empty() ) + break; + + if( stat( dir.c_str(), &stbuf ) < 0 ) + { + switch( errno ) + { + case ENOENT: + case ENOTDIR: + paths.push( dir ); + break; + + default: + osg::notify(osg::DEBUG_INFO) << "osgDB::makeDirectory(): " << sys_errlist[errno] << std::endl; + return false; + } + } + dir = getFilePath(std::string(dir)); + } + + while( !paths.empty() ) + { + std::string dir = paths.top(); + + if( mkdir( dir.c_str(), 0755 )< 0 ) + { + osg::notify(osg::DEBUG_INFO) << "osgDB::makeDirectory(): " << sys_errlist[errno] << std::endl; + return false; + } + paths.pop(); + } + return true; +} + +bool osgDB::makeDirectoryForFile( const std::string &path ) +{ + return makeDirectory( getFilePath( path )); +} void osgDB::convertStringPathIntoFilePathList(const std::string& paths,FilePathList& filepath) {