Index: experimental/distortionNG/ReaderWriterDist.cpp
===================================================================
--- experimental/distortionNG/ReaderWriterDist.cpp	(revision 366)
+++ experimental/distortionNG/ReaderWriterDist.cpp	(revision 367)
@@ -25,24 +25,43 @@
 	// Get ReaderWriter
 	rw = osgDB::Registry::instance()->getReaderWriterForExtension("osgt");	
-
 }
 
-ReaderWriterDist::ReadResult ReaderWriterDist::readNode( const std::string& file, const osgDB::ReaderWriter::Options* options) const
+ReaderWriterDist::Options* ReaderWriterDist::prepareReading( ReadResult& result, std::string& fileName, std::ios::openmode& mode, const Options* options ) const
 {
-	std::string ext = osgDB::getLowerCaseFileExtension( file );
-	if( !acceptsExtension(ext) )
-		return ReadResult::FILE_NOT_HANDLED;
+	std::string ext = osgDB::getLowerCaseFileExtension( fileName );
+	if ( !acceptsExtension(ext) )
+	{
+		result = ReadResult::FILE_NOT_HANDLED;
+		return 0;
+	}
+	fileName = osgDB::findDataFile( fileName, options );
+	if ( fileName.empty() )
+	{
+		result = ReadResult::FILE_NOT_FOUND;
+		return 0;
+	}
 
-	// check if file exists
-	std::string fileName = osgDB::findDataFile( file, options );
-	if( fileName.empty() )
-		return ReadResult::FILE_NOT_FOUND;
+	osg::ref_ptr<Options> local_opt = options ?
+		static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options;
+	local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName));
 
-	std::ifstream stream( fileName.c_str(), std::ios::in );
-	if (!stream)	return ReadResult::ERROR_IN_READING_FILE;
-	return readNode( stream, options );
+	mode |= std::ios::binary;
+
+	return local_opt.release();
 }
 
-ReaderWriterDist::ReadResult ReaderWriterDist:: readNode( std::istream& fin, const osgDB::ReaderWriter::Options* options ) const
+ReaderWriterDist::ReadResult ReaderWriterDist::readObject( const std::string& file, const Options* options) const
+{
+	ReadResult result = ReadResult::FILE_LOADED;
+	std::string fileName = file;
+	std::ios::openmode mode = std::ios::in;
+	Options* local_opt = prepareReading( result, fileName, mode, options );
+	if ( !result.success() ) return result;
+
+	osgDB::ifstream istream( fileName.c_str(), mode );
+	return readObject( istream, local_opt );
+}
+
+ReaderWriterDist::ReadResult ReaderWriterDist::readObject( std::istream& fin, const Options* options ) const
 {
 	if ( rw )
@@ -56,36 +75,42 @@
 			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;
+	OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to get ReaderWriter for internally used extension" << std::endl;
+	return( ReadResult::NOT_IMPLEMENTED );
 }
 
-ReaderWriterDist::WriteResult writeNode( const osg::Node& node, const std::string& fileName, const Options* options ) const
+ReaderWriterDist::Options* ReaderWriterDist::prepareWriting( WriteResult& result, const std::string& fileName, std::ios::openmode& mode, const Options* options ) const
 {
-	std::string ext = osgDB::getLowerCaseFileExtension( file );
-	if( !acceptsExtension(ext) )
-		return WriteResult::FILE_NOT_HANDLED;
+	std::string ext = osgDB::getFileExtension( fileName );
+	if ( !acceptsExtension(ext) ) result = 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<Options> local_opt = prepareWriting( result, fileName, mode, options );
-    if ( !result.success() ) return result;
+	osg::ref_ptr<Options> local_opt = options ?
+		static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options;
+	local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName));
 
-    osgDB::ofstream fout( fileName.c_str(), mode );
-    if ( !fout ) return WriteResult::ERROR_IN_WRITING_FILE;
+	mode |= std::ios::binary;
 
-    result = writeNode( node, fout, local_opt.get() );
-    fout.close();
-    return result;
+	return local_opt.release();
 }
 
-ReaderWriterDist::WriteResult writeNode( const osg::Node& node, std::ostream& fout, const Options* options ) const
+ReaderWriterDist::WriteResult ReaderWriterDist::writeObject( const osg::Object& object, const std::string& fileName, const osgDB::ReaderWriter::Options* options ) const
+{
+	WriteResult result = WriteResult::FILE_SAVED;
+	std::ios::openmode mode = std::ios::out;
+	osg::ref_ptr<Options> 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 = writeObject( object, fout, local_opt.get() );
+	fout.close();
+	return result;
+}
+
+ReaderWriterDist::WriteResult ReaderWriterDist::writeObject( const osg::Object& object, std::ostream& fout, const osgDB::ReaderWriter::Options* options ) const
 {
 	if ( rw )
 	{
-		osgDB::ReaderWriter::WriteResult wr = rw->writeObject( node, fout, writeOptions );
+		osgDB::ReaderWriter::WriteResult wr = rw->writeObject( object, fout, writeOptions );
 		if (wr.success() )			
 		{
@@ -96,4 +121,5 @@
 	}
 	OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to get ReaderWriter for internally used extension" << std::endl;
+	return( WriteResult::NOT_IMPLEMENTED );
 }
 
