Adding support using istream and ostream with the reader writers.
This commit is contained in:
@@ -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); }
|
||||
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "MatrixTransform.h"
|
||||
#include "Group.h"
|
||||
|
||||
#include <osg/Notify>
|
||||
|
||||
#include <osgDB/FileNameUtils>
|
||||
#include <osgDB/Registry>
|
||||
|
||||
@@ -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<const osg::MatrixTransform*>(&node))
|
||||
const_cast<ive::MatrixTransform*>(static_cast<const ive::MatrixTransform*>(&node))->write(out);
|
||||
@@ -67,13 +81,12 @@ class IVEReaderWriter : public ReaderWriter
|
||||
const_cast<ive::Group*>(static_cast<const ive::Group*>(&node))->write(out);
|
||||
else
|
||||
std::cout<<"File must start with a MatrixTransform or Group "<<std::endl;
|
||||
ostream.flush();
|
||||
ostream.close();
|
||||
fout.flush();
|
||||
return WriteResult::FILE_SAVED;
|
||||
}
|
||||
catch(ive::Exception e)
|
||||
{
|
||||
std::cout<<"Error parsing OSG file: "<< e.getError() << std::endl;
|
||||
osg::notify(osg::WARN)<<"Error parsing OSG file: "<< e.getError() << std::endl;
|
||||
}
|
||||
return WriteResult::FILE_NOT_HANDLED;
|
||||
|
||||
|
||||
@@ -21,54 +21,58 @@ class OSGReaderWriter : public ReaderWriter
|
||||
|
||||
virtual ReadResult readObject(const std::string& fileName, const Options* opt) { return readNode(fileName,opt); }
|
||||
|
||||
virtual ReadResult readNode(const std::string& fileName, const Options*)
|
||||
virtual ReadResult readNode(const std::string& fileName, const Options* opt)
|
||||
{
|
||||
std::string ext = getFileExtension(fileName);
|
||||
if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
|
||||
|
||||
std::ifstream fin(fileName.c_str());
|
||||
std::ifstream fin(fileName.c_str());
|
||||
if (fin)
|
||||
{
|
||||
Input fr;
|
||||
fr.attach(&fin);
|
||||
return readNode(fin, opt);
|
||||
}
|
||||
return 0L;
|
||||
|
||||
}
|
||||
|
||||
virtual ReadResult readNode(std::istream& fin, const Options*)
|
||||
{
|
||||
|
||||
typedef std::vector<osg::Node*> 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<osg::Node*> 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);
|
||||
|
||||
Reference in New Issue
Block a user