Added Don's new osgDB::makeDirectory() code FileUtils.

This commit is contained in:
Robert Osfield 2004-09-01 14:49:18 +00:00
parent 543f4493f6
commit ac6d25a840
2 changed files with 85 additions and 0 deletions

View File

@ -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);

View File

@ -24,20 +24,99 @@
#include <Winbase.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <direct.h> // 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 <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#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 <osg/Notify>
#include <osgDB/FileUtils>
#include <osgDB/FileNameUtils>
#include <osgDB/Registry>
#include <errno.h>
#include <stack>
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<std::string> 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)
{