Adding support using istream and ostream with the reader writers.

This commit is contained in:
Robert Osfield
2003-05-26 09:27:16 +00:00
parent 063de79eb9
commit 7b657aed3a
3 changed files with 73 additions and 47 deletions

View File

@@ -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);