OpenSceneGraph/src/osgPlugins/tgz/ReaderWriterTGZ.cpp

122 lines
3.8 KiB
C++
Raw Normal View History

2001-01-11 00:32:10 +08:00
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <osg/Geode>
2001-01-11 00:32:10 +08:00
#include <osg/Group>
#include <osg/Notify>
#include <osgDB/ReadFile>
#include <osgDB/FileUtils>
#include <osgDB/FileNameUtils>
#include <osgDB/Registry>
2001-01-11 00:32:10 +08:00
#ifdef _WIN32
#include <direct.h>
2001-01-11 00:32:10 +08:00
#else
#include <unistd.h>
2001-01-11 00:32:10 +08:00
#endif
using namespace osg;
2001-01-11 00:32:10 +08:00
class ReaderWriterTGZ : public osgDB::ReaderWriter
{
public:
virtual const char* className() { return "TGZ Database Reader/Writer"; }
2001-01-11 00:32:10 +08:00
virtual bool acceptsExtension(const std::string& extension)
{
return osgDB::equalCaseInsensitive(extension,"tgz");
}
2001-01-11 00:32:10 +08:00
virtual ReadResult readNode(const std::string& fileName, const osgDB::ReaderWriter::Options*)
2001-01-11 00:32:10 +08:00
{
std::string ext = osgDB::getLowerCaseFileExtension(fileName);
if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
2001-01-11 00:32:10 +08:00
osg::notify(osg::INFO)<< "ReaderWriterTGZ::readNode( "<<fileName.c_str()<<" )\n";
char dirname[128];
char command[1024];
#ifdef _WIN32
strcpy(dirname, "C:/Windows/Temp/.osgdb_tgz");
mkdir( dirname);
// note, the following C option under windows does not seem to work...
// will pursue an better tar.exe later. RO.
sprintf( command,
"tar xfz %s %s",
fileName.c_str(), dirname );
#endif
#ifdef __linux
sprintf( dirname, "/tmp/.tgz%06d", getpid());
mkdir( dirname, 0700 );
sprintf( command,
"tar xfCz %s %s",
fileName.c_str(), dirname );
#endif
#ifdef __sgi
sprintf( dirname, "/tmp/.tgz%06d", getpid());
mkdir( dirname, 0700 );
sprintf( command,
"cp %s %s; cd %s;"
"gzcat %s | tar xf -",
fileName.c_str(), dirname, dirname,
fileName.c_str());
#endif
system( command );
osg::Group *grp = new osg::Group;
osgDB::setFilePath( dirname );
// deactivate the automatic generation of images to geode's.
bool prevCreateNodeFromImage = osgDB::Registry::instance()->getCreateNodeFromImage();
osgDB::Registry::instance()->setCreateNodeFromImage(false);
osgDB::DirectoryContents contents = osgDB::getDirectoryContents(dirname);
for(osgDB::DirectoryContents::iterator itr = contents.begin();
itr != contents.end();
++itr)
{
std::string file_ext = osgDB::getFileExtension(*itr);
if (!acceptsExtension(file_ext) &&
*itr!=std::string(".") &&
*itr!=std::string(".."))
{
osg::Node *node = osgDB::readNodeFile(*itr);
grp->addChild( node );
}
}
// restorre original state of the automatic generation of images to geode's.
osgDB::Registry::instance()->setCreateNodeFromImage(prevCreateNodeFromImage);
#ifdef _WIN32
// note, is this the right command for windows?
// is there any way of overiding the Y/N option? RO.
sprintf( command, "erase %s", dirname );
system( command );
#else
sprintf( command, "rm -rf %s", dirname );
system( command );
#endif
if( grp->getNumChildren() == 0 )
{
grp->unref();
return ReadResult::FILE_NOT_HANDLED;
}
return grp;
2001-01-11 00:32:10 +08:00
}
};
// now register with sgRegistry to instantiate the above
// reader/writer.
osgDB::RegisterReaderWriterProxy<ReaderWriterTGZ> g_readerWriter_TGZ_Proxy;