From 7b657aed3aa867c8dee28b7fc1dd50a7fe1408eb Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 26 May 2003 09:27:16 +0000 Subject: [PATCH] Adding support using istream and ostream with the reader writers. --- include/osgDB/ReaderWriter | 10 ++++ src/osgPlugins/ive/ReaderWriterIVE.cpp | 33 +++++++---- src/osgPlugins/osg/ReaderWriterOSG.cpp | 77 +++++++++++++------------- 3 files changed, 73 insertions(+), 47 deletions(-) diff --git a/include/osgDB/ReaderWriter b/include/osgDB/ReaderWriter index ed6d719a9..c3a70726a 100644 --- a/include/osgDB/ReaderWriter +++ b/include/osgDB/ReaderWriter @@ -134,6 +134,16 @@ class OSGDB_EXPORT ReaderWriter : public osg::Referenced virtual WriteResult writeObject(const osg::Object& /*obj*/,const std::string& /*fileName*/,const Options* =NULL) {return WriteResult(WriteResult::FILE_NOT_HANDLED); } virtual WriteResult writeImage(const osg::Image& /*image*/,const std::string& /*fileName*/,const Options* =NULL) {return WriteResult(WriteResult::FILE_NOT_HANDLED); } virtual WriteResult writeNode(const osg::Node& /*node*/,const std::string& /*fileName*/,const Options* =NULL) { return WriteResult(WriteResult::FILE_NOT_HANDLED); } + + virtual ReadResult readObject(std::istream& /*fin*/,const Options* =NULL) { return ReadResult(ReadResult::FILE_NOT_HANDLED); } + virtual ReadResult readImage(std::istream& /*fin*/,const Options* =NULL) { return ReadResult(ReadResult::FILE_NOT_HANDLED); } + virtual ReadResult readNode(std::istream& /*fin*/,const Options* =NULL) { return ReadResult(ReadResult::FILE_NOT_HANDLED); } + + virtual WriteResult writeObject(const osg::Object& /*obj*/,std::ostream& /*fout*/,const Options* =NULL) {return WriteResult(WriteResult::FILE_NOT_HANDLED); } + virtual WriteResult writeImage(const osg::Image& /*image*/,std::ostream& /*fout*/,const Options* =NULL) {return WriteResult(WriteResult::FILE_NOT_HANDLED); } + virtual WriteResult writeNode(const osg::Node& /*node*/,std::ostream& /*fout*/,const Options* =NULL) { return WriteResult(WriteResult::FILE_NOT_HANDLED); } + + }; } diff --git a/src/osgPlugins/ive/ReaderWriterIVE.cpp b/src/osgPlugins/ive/ReaderWriterIVE.cpp index 49f123c19..4cc66aff7 100644 --- a/src/osgPlugins/ive/ReaderWriterIVE.cpp +++ b/src/osgPlugins/ive/ReaderWriterIVE.cpp @@ -1,6 +1,8 @@ #include "MatrixTransform.h" #include "Group.h" +#include + #include #include @@ -17,15 +19,20 @@ class IVEReaderWriter : public ReaderWriter return equalCaseInsensitive(extension,"ive"); } - virtual ReadResult readNode(const std::string& fileName, const Options*) + virtual ReadResult readNode(const std::string& fileName, const Options* options) { std::string ext = getFileExtension(fileName); if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; - + + std::ifstream istream(fileName.c_str(), std::ios::in | std::ios::binary); + return readNode(istream,options); + } + + virtual ReadResult readNode(std::istream& fin, const Options*) + { try{ // Create datainputstream. - std::ifstream istream(fileName.c_str(), std::ios::in | std::ios::binary); - ive::DataInputStream* in = new ive::DataInputStream(&istream); + ive::DataInputStream* in = new ive::DataInputStream(&fin); // Which object is written first in the stream. int id = in->peekInt(); @@ -54,12 +61,19 @@ class IVEReaderWriter : public ReaderWriter return 0; } - virtual WriteResult writeNode(const Node& node,const std::string& fileName, const osgDB::ReaderWriter::Options*) + virtual WriteResult writeNode(const Node& node,const std::string& fileName, const osgDB::ReaderWriter::Options* options) + { + std::ofstream fout(fileName.c_str(), std::ios::out | std::ios::binary); + WriteResult result = writeNode(node, fout, options); + fout.close(); + return result; + } + + virtual WriteResult writeNode(const Node& node,std::ostream& fout, const osgDB::ReaderWriter::Options*) { try { - std::ofstream ostream(fileName.c_str(), std::ios::out | std::ios::binary); - ive::DataOutputStream* out = new ive::DataOutputStream(&ostream); + ive::DataOutputStream* out = new ive::DataOutputStream(&fout); // write ive file. if(dynamic_cast(&node)) const_cast(static_cast(&node))->write(out); @@ -67,13 +81,12 @@ class IVEReaderWriter : public ReaderWriter const_cast(static_cast(&node))->write(out); else std::cout<<"File must start with a MatrixTransform or Group "< NodeList; - NodeList nodeList; + Input fr; + fr.attach(&fin); - // load all nodes in file, placing them in a group. - while(!fr.eof()) - { - Node *node = fr.readNode(); - if (node) nodeList.push_back(node); - else fr.advanceOverCurrentFieldOrBlock(); - } + typedef std::vector NodeList; + NodeList nodeList; - if (nodeList.empty()) - { - return ReadResult("No data loaded from "+fileName); - } - else if (nodeList.size()==1) - { - return nodeList.front(); - } - else - { - Group* group = new Group; - group->setName("import group"); - for(NodeList::iterator itr=nodeList.begin(); - itr!=nodeList.end(); - ++itr) - { - group->addChild(*itr); - } - return group; - } + // load all nodes in file, placing them in a group. + while(!fr.eof()) + { + Node *node = fr.readNode(); + if (node) nodeList.push_back(node); + else fr.advanceOverCurrentFieldOrBlock(); + } + if (nodeList.empty()) + { + return ReadResult("No data loaded"); + } + else if (nodeList.size()==1) + { + return nodeList.front(); } else { - return 0L; + Group* group = new Group; + group->setName("import group"); + for(NodeList::iterator itr=nodeList.begin(); + itr!=nodeList.end(); + ++itr) + { + group->addChild(*itr); + } + return group; } + } virtual WriteResult writeObject(const Object& obj,const std::string& fileName, const osgDB::ReaderWriter::Options*) @@ -84,13 +88,12 @@ class OSGReaderWriter : public ReaderWriter return WriteResult("Unable to open file for output"); } - virtual WriteResult writeNode(const Node& node,const std::string& fileName, const osgDB::ReaderWriter::Options*) + virtual WriteResult writeNode(const Node& node,const std::string& fileName, const osgDB::ReaderWriter::Options* options) { std::string ext = getFileExtension(fileName); if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED; - Output fout; - fout.open(fileName.c_str()); + Output fout(fileName.c_str()); if (fout) { fout.writeObject(node);