| 1 | #include <sys/stat.h> |
|---|
| 2 | #include <sys/types.h> |
|---|
| 3 | #include <fcntl.h> |
|---|
| 4 | #include <stdio.h> |
|---|
| 5 | #include <stdlib.h> |
|---|
| 6 | #include <string.h> |
|---|
| 7 | |
|---|
| 8 | #include <osg/Geode> |
|---|
| 9 | #include <osg/Group> |
|---|
| 10 | #include <osg/Notify> |
|---|
| 11 | |
|---|
| 12 | #include <osgDB/Registry> |
|---|
| 13 | #include <osgDB/ReadFile> |
|---|
| 14 | #include <osgDB/FileUtils> |
|---|
| 15 | #include <osgDB/FileNameUtils> |
|---|
| 16 | |
|---|
| 17 | #if defined(_WIN32) && !defined(__CYGWIN__) |
|---|
| 18 | #include <direct.h> |
|---|
| 19 | #else |
|---|
| 20 | #include <unistd.h> |
|---|
| 21 | #endif |
|---|
| 22 | |
|---|
| 23 | using namespace osg; |
|---|
| 24 | |
|---|
| 25 | class sgReaderWriterOSGTGZ : public osgDB::ReaderWriter |
|---|
| 26 | { |
|---|
| 27 | public: |
|---|
| 28 | |
|---|
| 29 | sgReaderWriterOSGTGZ() |
|---|
| 30 | { |
|---|
| 31 | supportsExtension("osgtgz","OpenSceneGraph tar gzid'd archive format"); |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | virtual const char* className() const { return "OSGTGZ Database Reader/Writer"; } |
|---|
| 35 | |
|---|
| 36 | virtual ReadResult readNode(const std::string& file, const osgDB::ReaderWriter::Options* options) const |
|---|
| 37 | { |
|---|
| 38 | std::string ext = osgDB::getFileExtension(file); |
|---|
| 39 | if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; |
|---|
| 40 | |
|---|
| 41 | std::string fileName = osgDB::findDataFile( file,options ); |
|---|
| 42 | if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; |
|---|
| 43 | |
|---|
| 44 | OSG_INFO<<"sgReaderWriterOSGTGZ::readNode( "<<fileName.c_str()<<" )\n"; |
|---|
| 45 | |
|---|
| 46 | char dirname[128]; |
|---|
| 47 | char command[1024]; |
|---|
| 48 | |
|---|
| 49 | #if defined(_WIN32) && !defined(__CYGWIN__) |
|---|
| 50 | sprintf( dirname, "C:/Windows/Temp/.osgdb_osgtgz"); |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | sprintf( command, |
|---|
| 54 | "tar xfCz %s %s", |
|---|
| 55 | fileName.c_str(), dirname ); |
|---|
| 56 | mkdir( dirname); |
|---|
| 57 | #endif |
|---|
| 58 | |
|---|
| 59 | #if defined(__linux) || defined(__CYGWIN__) |
|---|
| 60 | sprintf( dirname, "/tmp/.osg%06d", getpid()); |
|---|
| 61 | sprintf( command, |
|---|
| 62 | "tar xfCz %s %s", |
|---|
| 63 | fileName.c_str(), dirname ); |
|---|
| 64 | mkdir( dirname, 0700 ); |
|---|
| 65 | #endif |
|---|
| 66 | |
|---|
| 67 | #ifdef __sgi |
|---|
| 68 | sprintf( dirname, "/tmp/.osg%06d", getpid()); |
|---|
| 69 | sprintf( command, |
|---|
| 70 | "cp %s %s; cd %s;" |
|---|
| 71 | "gzcat %s | tar xf -", |
|---|
| 72 | fileName.c_str(), dirname, dirname, |
|---|
| 73 | fileName.c_str() ); |
|---|
| 74 | mkdir( dirname, 0700 ); |
|---|
| 75 | #endif |
|---|
| 76 | |
|---|
| 77 | if ( system( command ) ) { |
|---|
| 78 | return ReadResult::FILE_NOT_HANDLED; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | osg::ref_ptr<osg::Group> grp = new osg::Group; |
|---|
| 82 | |
|---|
| 83 | osg::ref_ptr<osgDB::ReaderWriter::Options> local_options = options ? static_cast<osgDB::ReaderWriter::Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new osgDB::ReaderWriter::Options; |
|---|
| 84 | local_options->getDatabasePathList().push_front(dirname); |
|---|
| 85 | |
|---|
| 86 | osgDB::DirectoryContents contents = osgDB::getDirectoryContents(dirname); |
|---|
| 87 | for(osgDB::DirectoryContents::iterator itr = contents.begin(); |
|---|
| 88 | itr != contents.end(); |
|---|
| 89 | ++itr) |
|---|
| 90 | { |
|---|
| 91 | std::string file_ext = osgDB::getLowerCaseFileExtension(*itr); |
|---|
| 92 | if (osgDB::equalCaseInsensitive(file_ext,"osg")) |
|---|
| 93 | { |
|---|
| 94 | osg::Node *node = osgDB::readNodeFile( *itr, local_options.get() ); |
|---|
| 95 | grp->addChild( node ); |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | #if defined(_WIN32) && !defined(__CYGWIN__) |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | sprintf( command, "erase %s", dirname ); |
|---|
| 103 | #else |
|---|
| 104 | |
|---|
| 105 | sprintf( command, "rm -rf %s", dirname ); |
|---|
| 106 | #endif |
|---|
| 107 | if ( system( command ) ) { |
|---|
| 108 | return ReadResult::FILE_NOT_HANDLED; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | if( grp->getNumChildren() == 0 ) |
|---|
| 112 | { |
|---|
| 113 | grp->unref(); |
|---|
| 114 | return ReadResult::FILE_NOT_HANDLED; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | return grp.get(); |
|---|
| 118 | |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | }; |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | REGISTER_OSGPLUGIN(osgtgz, sgReaderWriterOSGTGZ) |
|---|