Ignore:
Timestamp:
May 31, 2012, 10:11:55 PM (12 years ago)
Author:
Torben Dannhauer
Message:

Dist-Plugin compiles, but not yet tested

File:
1 edited

Legend:

Unmodified
Added
Removed
  • experimental/distortionNG/ReaderWriterDist.cpp

    r366 r367  
    2525        // Get ReaderWriter
    2626        rw = osgDB::Registry::instance()->getReaderWriterForExtension("osgt"); 
    27 
    2827}
    2928
    30 ReaderWriterDist::ReadResult ReaderWriterDist::readNode( const std::string& file, const osgDB::ReaderWriter::Options* options) const
     29ReaderWriterDist::Options* ReaderWriterDist::prepareReading( ReadResult& result, std::string& fileName, std::ios::openmode& mode, const Options* options ) const
    3130{
    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        }
    3543
    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));
    4047
    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();
    4451}
    4552
    46 ReaderWriterDist::ReadResult ReaderWriterDist:: readNode( std::istream& fin, const osgDB::ReaderWriter::Options* options ) const
     53ReaderWriterDist::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
     65ReaderWriterDist::ReadResult ReaderWriterDist::readObject( std::istream& fin, const Options* options ) const
    4766{
    4867        if ( rw )
     
    5675                        OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to convert stream to node" << std::endl;
    5776        }
    58         else
    59                 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 );
    6079}
    6180
    62 ReaderWriterDist::WriteResult writeNode( const osg::Node& node, const std::string& fileName, const Options* options ) const
     81ReaderWriterDist::Options* ReaderWriterDist::prepareWriting( WriteResult& result, const std::string& fileName, std::ios::openmode& mode, const Options* options ) const
    6382{
    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;
    6785
    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));
    7689
    77     osgDB::ofstream fout( fileName.c_str(), mode );
    78     if ( !fout ) return WriteResult::ERROR_IN_WRITING_FILE;
     90        mode |= std::ios::binary;
    7991
    80     result = writeNode( node, fout, local_opt.get() );
    81     fout.close();
    82     return result;
     92        return local_opt.release();
    8393}
    8494
    85 ReaderWriterDist::WriteResult writeNode( const osg::Node& node, std::ostream& fout, const Options* options ) const
     95ReaderWriterDist::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
     110ReaderWriterDist::WriteResult ReaderWriterDist::writeObject( const osg::Object& object, std::ostream& fout, const osgDB::ReaderWriter::Options* options ) const
    86111{
    87112        if ( rw )
    88113        {
    89                 osgDB::ReaderWriter::WriteResult wr = rw->writeObject( node, fout, writeOptions );
     114                osgDB::ReaderWriter::WriteResult wr = rw->writeObject( object, fout, writeOptions );
    90115                if (wr.success() )                     
    91116                {
     
    96121        }
    97122        OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to get ReaderWriter for internally used extension" << std::endl;
     123        return( WriteResult::NOT_IMPLEMENTED );
    98124}
    99125
Note: See TracChangeset for help on using the changeset viewer.