#include "ReaderWriterDist.h" #include ReaderWriterDist::ReaderWriterDist() { supportsExtension( "dist", "distortion set loader"); extensionToAdd = ".dist"; // Configure Compression and instantiate read/write-options bool asAscii = true; bool compressionEnabled = false; std::string readOptionString = ""; std::string writeOptionString = ""; if(asAscii) { readOptionString = "Ascii"; writeOptionString = "Ascii"; } if (compressionEnabled) writeOptionString+=" Compressor=zlib"; readOptions = new osgDB::Options( readOptionString.c_str() ); writeOptions = new osgDB::Options( writeOptionString.c_str() ); // Get ReaderWriter rw = osgDB::Registry::instance()->getReaderWriterForExtension("osgt"); } ReaderWriterDist::ReadResult ReaderWriterDist::readNode( const std::string& file, const osgDB::ReaderWriter::Options* options) const { std::string ext = osgDB::getLowerCaseFileExtension( file ); if( !acceptsExtension(ext) ) return ReadResult::FILE_NOT_HANDLED; // check if file exists std::string fileName = osgDB::findDataFile( file, options ); if( fileName.empty() ) return ReadResult::FILE_NOT_FOUND; std::ifstream stream( fileName.c_str(), std::ios::in ); if (!stream) return ReadResult::ERROR_IN_READING_FILE; return readNode( stream, options ); } ReaderWriterDist::ReadResult ReaderWriterDist:: readNode( std::istream& fin, const osgDB::ReaderWriter::Options* options ) const { if ( rw ) { osgDB::ReaderWriter::ReadResult rr = rw->readObject( fin, readOptions ); if (rr.success()) { return( rr.takeObject() ); } else OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to convert stream to node" << std::endl; } else OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to get ReaderWriter for internally used extension" << std::endl; } ReaderWriterDist::WriteResult writeNode( const osg::Node& node, const std::string& fileName, const Options* options ) const { std::string ext = osgDB::getLowerCaseFileExtension( file ); if( !acceptsExtension(ext) ) return WriteResult::FILE_NOT_HANDLED; std::ifstream stream( fileName.c_str(), std::ios::in ); if (!stream) return ReadResult::ERROR_IN_READING_FILE; return readNode( stream, options ); // ------------------ WriteResult result = WriteResult::FILE_SAVED; std::ios::openmode mode = std::ios::out; osg::ref_ptr local_opt = prepareWriting( result, fileName, mode, options ); if ( !result.success() ) return result; osgDB::ofstream fout( fileName.c_str(), mode ); if ( !fout ) return WriteResult::ERROR_IN_WRITING_FILE; result = writeNode( node, fout, local_opt.get() ); fout.close(); return result; } ReaderWriterDist::WriteResult writeNode( const osg::Node& node, std::ostream& fout, const Options* options ) const { if ( rw ) { osgDB::ReaderWriter::WriteResult wr = rw->writeObject( node, fout, writeOptions ); if (wr.success() ) { OSG_NOTIFY( osg::WARN ) << "schreiben geschafft!"<< std::endl; } else OSG_NOTIFY( osg::WARN ) << "ERROR: Save failed: " << wr.message() << std::endl; return(wr); } OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to get ReaderWriter for internally used extension" << std::endl; } // Add ourself to the Registry to instantiate the reader/writer. REGISTER_OSGPLUGIN(dist, ReaderWriterDist)