From 90e5586777e453768f3afefc623e032d93c1b77a Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 28 Oct 2004 12:16:47 +0000 Subject: [PATCH] Updates to osgDB::Archive, and IVE plugin to support usage via the Archive. --- include/osgDB/Archive | 8 +- src/osgDB/Archive.cpp | 114 ++++++++++++++++++++++--- src/osgPlugins/ive/ReaderWriterIVE.cpp | 26 ++++++ 3 files changed, 129 insertions(+), 19 deletions(-) diff --git a/include/osgDB/Archive b/include/osgDB/Archive index 18446435f..e549bbedf 100644 --- a/include/osgDB/Archive +++ b/include/osgDB/Archive @@ -44,10 +44,10 @@ class OSGDB_EXPORT Archive : public ReaderWriter }; /** open the archive.*/ - virtual void create(const std::string& filename, unsigned int indexBlockSize=4096); + virtual bool create(const std::string& filename, unsigned int indexBlockSize=4096); /** open the archive.*/ - virtual void open(const std::string& filename, Status status); + virtual bool open(const std::string& filename, Status status); /** close the archive.*/ virtual void close(); @@ -87,7 +87,7 @@ class OSGDB_EXPORT Archive : public ReaderWriter inline pos_type getPositionNextIndexBlock() const { return _filePositionNextIndexBlock; } - void read(std::istream& in); + static IndexBlock* read(std::istream& in); bool getFileReferences(FileNamePositionMap& indexMap); @@ -124,8 +124,6 @@ class OSGDB_EXPORT Archive : public ReaderWriter typedef std::list< osg::ref_ptr > IndexBlockList; - void readIndexBlocks(); - void writeIndexBlocks(); bool addFileReference(pos_type position, const std::string& fileName); diff --git a/src/osgDB/Archive.cpp b/src/osgDB/Archive.cpp index 8b0e45d7e..365066ddf 100644 --- a/src/osgDB/Archive.cpp +++ b/src/osgDB/Archive.cpp @@ -53,22 +53,30 @@ void Archive::IndexBlock::allocateData(unsigned int blockSize) } } -void Archive::IndexBlock::read(std::istream& in) +Archive::IndexBlock* Archive::IndexBlock::read(std::istream& in) { - _filePosition = in.tellg(); - in.read(reinterpret_cast(&_blockSize), sizeof(_blockSize)); - in.read(reinterpret_cast(&_filePositionNextIndexBlock), sizeof(_filePositionNextIndexBlock)); - in.read(reinterpret_cast(&_offsetOfNextAvailableSpace), sizeof(_offsetOfNextAvailableSpace)); + if (!in) return 0; - allocateData(_blockSize); - if (_data) + osg::ref_ptr indexBlock = new IndexBlock; + indexBlock->_filePosition = in.tellg(); + in.read(reinterpret_cast(&indexBlock->_blockSize), sizeof(indexBlock->_blockSize)); + in.read(reinterpret_cast(&indexBlock->_filePositionNextIndexBlock), sizeof(indexBlock->_filePositionNextIndexBlock)); + in.read(reinterpret_cast(&indexBlock->_offsetOfNextAvailableSpace), sizeof(indexBlock-> _offsetOfNextAvailableSpace)); + + indexBlock->allocateData(indexBlock->_blockSize); + if (indexBlock->_data) { - in.read(reinterpret_cast(_data),_blockSize); + in.read(reinterpret_cast(indexBlock->_data),indexBlock->_blockSize); } else { osg::notify(osg::NOTICE)<<"Allocation Problem in Archive::IndexBlock::read(std::istream& in)"<write(_output); _indexBlockList.push_back(indexBlock); } + return true; } -void Archive::open(const std::string& filename, Status status) +bool Archive::open(const std::string& filename, Status status) { if (status==READ) { _status = status; _input.open(filename.c_str()); + + if (_input) + { + osg::notify(osg::NOTICE)<<"trying Archive::open("<(&_version),sizeof(_version)); + + IndexBlock *indexBlock = 0; + + while ( (indexBlock=Archive::IndexBlock::read(_input)) != 0) + { + _indexBlockList.push_back(indexBlock); + if (indexBlock->getPositionNextIndexBlock()==pos_type(0)) break; + + _input.seekg(indexBlock->getPositionNextIndexBlock()); + } + + osg::notify(osg::NOTICE)<<"Archive::open("<getFileReferences(_indexMap); + } + + for(FileNamePositionMap::iterator mitr=_indexMap.begin(); + mitr!=_indexMap.end(); + ++mitr) + { + osg::notify(osg::NOTICE)<<" filename "<<(mitr->first)<<" pos="<<(int)(mitr->second)<write(_output); } } - _output.close(); } } - bool Archive::fileExists(const std::string& filename) const { return (_indexMap.count(filename)!=0); @@ -265,6 +349,8 @@ ReaderWriter::ReadResult Archive::readObject(const std::string& fileName,const O osg::notify(osg::NOTICE)<<"Archive::readObject(obj, "<second); ReaderWriter* rw = osgDB::Registry::instance()->getReaderWriterForExtension(getLowerCaseFileExtension(fileName)); if (!rw) diff --git a/src/osgPlugins/ive/ReaderWriterIVE.cpp b/src/osgPlugins/ive/ReaderWriterIVE.cpp index 9fcef7de6..56d1c9487 100644 --- a/src/osgPlugins/ive/ReaderWriterIVE.cpp +++ b/src/osgPlugins/ive/ReaderWriterIVE.cpp @@ -20,6 +20,11 @@ class IVEReaderWriter : public ReaderWriter return equalCaseInsensitive(extension,"ive"); } + virtual ReadResult readObject(const std::string& file, const Options* options) + { + return readNode(file, options); + } + virtual ReadResult readNode(const std::string& file, const Options* options) { std::string ext = osgDB::getLowerCaseFileExtension(file); @@ -42,6 +47,11 @@ class IVEReaderWriter : public ReaderWriter return readNode(istream,local_opt.get()); } + virtual ReadResult readObject(std::istream& fin, const Options* options) + { + return readNode(fin, options); + } + virtual ReadResult readNode(std::istream& fin, const Options* options) { #define IVE_CATCH_EXCEPTIONS @@ -64,6 +74,15 @@ class IVEReaderWriter : public ReaderWriter #endif } + + + virtual WriteResult writeObject(const Object& object,const std::string& fileName, const osgDB::ReaderWriter::Options* options) + { + const Node* node = dynamic_cast(&object); + if (node) return writeNode( *node, fileName, options ); + return WriteResult::FILE_NOT_HANDLED; + } + virtual WriteResult writeNode(const Node& node,const std::string& fileName, const osgDB::ReaderWriter::Options* options) { std::string ext = getFileExtension(fileName); @@ -75,6 +94,13 @@ class IVEReaderWriter : public ReaderWriter return result; } + virtual WriteResult writeObject(const Object& object,std::ostream& fout, const osgDB::ReaderWriter::Options* options) + { + const Node* node = dynamic_cast(&object); + if (node) return writeNode( *node, fout, options ); + return WriteResult::FILE_NOT_HANDLED; + } + virtual WriteResult writeNode(const Node& node,std::ostream& fout, const osgDB::ReaderWriter::Options* options) { try