Changeset 367 for experimental/distortionNG/ReaderWriterDist.cpp
- Timestamp:
- May 31, 2012, 10:11:55 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
experimental/distortionNG/ReaderWriterDist.cpp
r366 r367 25 25 // Get ReaderWriter 26 26 rw = osgDB::Registry::instance()->getReaderWriterForExtension("osgt"); 27 28 27 } 29 28 30 ReaderWriterDist:: ReadResult ReaderWriterDist::readNode( const std::string& file, const osgDB::ReaderWriter::Options* options) const29 ReaderWriterDist::Options* ReaderWriterDist::prepareReading( ReadResult& result, std::string& fileName, std::ios::openmode& mode, const Options* options ) const 31 30 { 32 std::string ext = osgDB::getLowerCaseFileExtension( file ); 33 if( !acceptsExtension(ext) ) 34 return ReadResult::FILE_NOT_HANDLED; 31 std::string ext = osgDB::getLowerCaseFileExtension( fileName ); 32 if ( !acceptsExtension(ext) ) 33 { 34 result = ReadResult::FILE_NOT_HANDLED; 35 return 0; 36 } 37 fileName = osgDB::findDataFile( fileName, options ); 38 if ( fileName.empty() ) 39 { 40 result = ReadResult::FILE_NOT_FOUND; 41 return 0; 42 } 35 43 36 // check if file exists 37 std::string fileName = osgDB::findDataFile( file, options ); 38 if( fileName.empty() ) 39 return ReadResult::FILE_NOT_FOUND; 44 osg::ref_ptr<Options> local_opt = options ? 45 static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options; 46 local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName)); 40 47 41 std::ifstream stream( fileName.c_str(), std::ios::in );42 if (!stream) return ReadResult::ERROR_IN_READING_FILE; 43 return readNode( stream, options);48 mode |= std::ios::binary; 49 50 return local_opt.release(); 44 51 } 45 52 46 ReaderWriterDist::ReadResult ReaderWriterDist:: readNode( std::istream& fin, const osgDB::ReaderWriter::Options* options ) const 53 ReaderWriterDist::ReadResult ReaderWriterDist::readObject( const std::string& file, const Options* options) const 54 { 55 ReadResult result = ReadResult::FILE_LOADED; 56 std::string fileName = file; 57 std::ios::openmode mode = std::ios::in; 58 Options* local_opt = prepareReading( result, fileName, mode, options ); 59 if ( !result.success() ) return result; 60 61 osgDB::ifstream istream( fileName.c_str(), mode ); 62 return readObject( istream, local_opt ); 63 } 64 65 ReaderWriterDist::ReadResult ReaderWriterDist::readObject( std::istream& fin, const Options* options ) const 47 66 { 48 67 if ( rw ) … … 56 75 OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to convert stream to node" << std::endl; 57 76 } 58 else59 OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to get ReaderWriter for internally used extension" << std::endl;77 OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to get ReaderWriter for internally used extension" << std::endl; 78 return( ReadResult::NOT_IMPLEMENTED ); 60 79 } 61 80 62 ReaderWriterDist:: WriteResult writeNode( const osg::Node& node, const std::string& fileName, const Options* options ) const81 ReaderWriterDist::Options* ReaderWriterDist::prepareWriting( WriteResult& result, const std::string& fileName, std::ios::openmode& mode, const Options* options ) const 63 82 { 64 std::string ext = osgDB::getLowerCaseFileExtension( file ); 65 if( !acceptsExtension(ext) ) 66 return WriteResult::FILE_NOT_HANDLED; 83 std::string ext = osgDB::getFileExtension( fileName ); 84 if ( !acceptsExtension(ext) ) result = WriteResult::FILE_NOT_HANDLED; 67 85 68 std::ifstream stream( fileName.c_str(), std::ios::in ); 69 if (!stream) return ReadResult::ERROR_IN_READING_FILE; 70 return readNode( stream, options ); 71 // ------------------ 72 WriteResult result = WriteResult::FILE_SAVED; 73 std::ios::openmode mode = std::ios::out; 74 osg::ref_ptr<Options> local_opt = prepareWriting( result, fileName, mode, options ); 75 if ( !result.success() ) return result; 86 osg::ref_ptr<Options> local_opt = options ? 87 static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options; 88 local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName)); 76 89 77 osgDB::ofstream fout( fileName.c_str(), mode ); 78 if ( !fout ) return WriteResult::ERROR_IN_WRITING_FILE; 90 mode |= std::ios::binary; 79 91 80 result = writeNode( node, fout, local_opt.get() ); 81 fout.close(); 82 return result; 92 return local_opt.release(); 83 93 } 84 94 85 ReaderWriterDist::WriteResult writeNode( const osg::Node& node, std::ostream& fout, const Options* options ) const 95 ReaderWriterDist::WriteResult ReaderWriterDist::writeObject( const osg::Object& object, const std::string& fileName, const osgDB::ReaderWriter::Options* options ) const 96 { 97 WriteResult result = WriteResult::FILE_SAVED; 98 std::ios::openmode mode = std::ios::out; 99 osg::ref_ptr<Options> local_opt = prepareWriting( result, fileName, mode, options ); 100 if ( !result.success() ) return result; 101 102 osgDB::ofstream fout( fileName.c_str(), mode ); 103 if ( !fout ) return WriteResult::ERROR_IN_WRITING_FILE; 104 105 result = writeObject( object, fout, local_opt.get() ); 106 fout.close(); 107 return result; 108 } 109 110 ReaderWriterDist::WriteResult ReaderWriterDist::writeObject( const osg::Object& object, std::ostream& fout, const osgDB::ReaderWriter::Options* options ) const 86 111 { 87 112 if ( rw ) 88 113 { 89 osgDB::ReaderWriter::WriteResult wr = rw->writeObject( node, fout, writeOptions );114 osgDB::ReaderWriter::WriteResult wr = rw->writeObject( object, fout, writeOptions ); 90 115 if (wr.success() ) 91 116 { … … 96 121 } 97 122 OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to get ReaderWriter for internally used extension" << std::endl; 123 return( WriteResult::NOT_IMPLEMENTED ); 98 124 } 99 125
Note: See TracChangeset
for help on using the changeset viewer.