| 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/ReadFile> |
|---|
| 13 | #include <osgDB/FileUtils> |
|---|
| 14 | #include <osgDB/FileNameUtils> |
|---|
| 15 | #include <osgDB/Registry> |
|---|
| 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 ReaderWriterTGZ : public osgDB::ReaderWriter |
|---|
| 26 | { |
|---|
| 27 | public: |
|---|
| 28 | virtual const char* className() const { return "TGZ Database Reader/Writer"; } |
|---|
| 29 | |
|---|
| 30 | ReaderWriterTGZ() |
|---|
| 31 | { |
|---|
| 32 | supportsExtension("tgz","Tar gzip'd archive format"); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | virtual ReadResult readNode(const std::string& file, const osgDB::ReaderWriter::Options* options) const |
|---|
| 36 | { |
|---|
| 37 | std::string ext = osgDB::getLowerCaseFileExtension(file); |
|---|
| 38 | if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; |
|---|
| 39 | |
|---|
| 40 | OSG_NOTICE<<"file="<<file<<std::endl; |
|---|
| 41 | |
|---|
| 42 | std::string fileName = osgDB::findDataFile( file, options ); |
|---|
| 43 | |
|---|
| 44 | OSG_NOTICE<<"fileName="<<fileName<<std::endl; |
|---|
| 45 | |
|---|
| 46 | if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; |
|---|
| 47 | |
|---|
| 48 | OSG_INFO<< "ReaderWriterTGZ::readNode( "<<fileName.c_str()<<" )\n"; |
|---|
| 49 | |
|---|
| 50 | char dirname[128]; |
|---|
| 51 | char command[1024]; |
|---|
| 52 | |
|---|
| 53 | #if defined(_WIN32) && !defined(__CYGWIN__) |
|---|
| 54 | if ( getenv("TEMP") != NULL ){ |
|---|
| 55 | strcpy(dirname, getenv("TEMP")); |
|---|
| 56 | }else{ |
|---|
| 57 | |
|---|
| 58 | strcpy(dirname, "./"); |
|---|
| 59 | } |
|---|
| 60 | strcat(dirname, ".osgdb_tgz"); |
|---|
| 61 | mkdir( dirname); |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | sprintf( command, |
|---|
| 65 | "tar xfCz \"%s\" \"%s\"", |
|---|
| 66 | fileName.c_str(), dirname ); |
|---|
| 67 | #endif |
|---|
| 68 | |
|---|
| 69 | #if defined(__linux) || defined(__CYGWIN__) |
|---|
| 70 | sprintf( dirname, "/tmp/.tgz%06d", getpid()); |
|---|
| 71 | mkdir( dirname, 0700 ); |
|---|
| 72 | sprintf( command, |
|---|
| 73 | "tar xfCz %s %s", |
|---|
| 74 | fileName.c_str(), dirname ); |
|---|
| 75 | #endif |
|---|
| 76 | #ifdef __sgi |
|---|
| 77 | sprintf( dirname, "/tmp/.tgz%06d", getpid()); |
|---|
| 78 | mkdir( dirname, 0700 ); |
|---|
| 79 | sprintf( command, |
|---|
| 80 | "cp %s %s; cd %s;" |
|---|
| 81 | "gzcat %s | tar xf -", |
|---|
| 82 | fileName.c_str(), dirname, dirname, |
|---|
| 83 | fileName.c_str()); |
|---|
| 84 | #endif |
|---|
| 85 | |
|---|
| 86 | OSG_NOTICE<<"Running command '"<<command<<"'"<<std::endl; |
|---|
| 87 | |
|---|
| 88 | int result = system( command ); |
|---|
| 89 | if (result!=0) return ReadResult::ERROR_IN_READING_FILE; |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | osg::ref_ptr<osg::Group> grp = new osg::Group; |
|---|
| 93 | |
|---|
| 94 | OSG_NOTICE<<"Done"<<std::endl; |
|---|
| 95 | |
|---|
| 96 | osg::ref_ptr<osgDB::ReaderWriter::Options> local_options = options ? static_cast<osgDB::ReaderWriter::Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new osgDB::ReaderWriter::Options; |
|---|
| 97 | local_options->getDatabasePathList().push_front(dirname); |
|---|
| 98 | |
|---|
| 99 | OSG_NOTICE<<"local_options->getDatabasePathList().="<<local_options->getDatabasePathList().front()<<std::endl; |
|---|
| 100 | OSG_NOTICE<<"dirname="<<dirname<<std::endl; |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | bool prevCreateNodeFromImage = osgDB::Registry::instance()->getCreateNodeFromImage(); |
|---|
| 104 | osgDB::Registry::instance()->setCreateNodeFromImage(false); |
|---|
| 105 | |
|---|
| 106 | osgDB::DirectoryContents contents = osgDB::getDirectoryContents(dirname); |
|---|
| 107 | for(osgDB::DirectoryContents::iterator itr = contents.begin(); |
|---|
| 108 | itr != contents.end(); |
|---|
| 109 | ++itr) |
|---|
| 110 | { |
|---|
| 111 | std::string file_ext = osgDB::getFileExtension(*itr); |
|---|
| 112 | if (!acceptsExtension(file_ext) && |
|---|
| 113 | *itr!=std::string(".") && |
|---|
| 114 | *itr!=std::string("..")) |
|---|
| 115 | { |
|---|
| 116 | osg::Node *node = osgDB::readNodeFile(*itr, local_options.get()); |
|---|
| 117 | grp->addChild( node ); |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | osgDB::Registry::instance()->setCreateNodeFromImage(prevCreateNodeFromImage); |
|---|
| 123 | |
|---|
| 124 | #if defined(_WIN32) && !defined(__CYGWIN__) |
|---|
| 125 | sprintf( command, "erase /F /Q /S \"%s\"", dirname ); |
|---|
| 126 | #else |
|---|
| 127 | sprintf( command, "rm -rf %s", dirname ); |
|---|
| 128 | #endif |
|---|
| 129 | OSG_NOTICE<<"Running command '"<<command<<"'"<<std::endl; |
|---|
| 130 | |
|---|
| 131 | result = system( command ); |
|---|
| 132 | if (result!=0) return ReadResult::ERROR_IN_READING_FILE; |
|---|
| 133 | |
|---|
| 134 | if( grp->getNumChildren() == 0 ) |
|---|
| 135 | { |
|---|
| 136 | return ReadResult::FILE_NOT_HANDLED; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | return grp.get(); |
|---|
| 140 | |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | }; |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | REGISTER_OSGPLUGIN(tgz, ReaderWriterTGZ) |
|---|