From baf6945a7a507c7d325476df80fab2729d6c93e0 Mon Sep 17 00:00:00 2001 From: Julien Valentin Date: Wed, 8 Nov 2017 15:40:11 +0100 Subject: [PATCH 1/2] add funcs to read Object from a GZ stream --- src/osgPlugins/gz/ReaderWriterGZ.cpp | 63 +++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/src/osgPlugins/gz/ReaderWriterGZ.cpp b/src/osgPlugins/gz/ReaderWriterGZ.cpp index 8a917742e..477390407 100644 --- a/src/osgPlugins/gz/ReaderWriterGZ.cpp +++ b/src/osgPlugins/gz/ReaderWriterGZ.cpp @@ -45,7 +45,7 @@ class ReaderWriterGZ : public osgDB::ReaderWriter ~ReaderWriterGZ(); - virtual const char* className() const { return "HTTP Protocol Model Reader"; } + virtual const char* className() const { return "GZ Archive Reader/Writer"; } virtual ReadResult openArchive(const std::string& fileName,ArchiveStatus status, unsigned int , const Options* options) const { @@ -77,6 +77,67 @@ class ReaderWriterGZ : public osgDB::ReaderWriter ReadResult readFile(ObjectType objectType, const std::string& fullFileName, const osgDB::ReaderWriter::Options* options) const; + ///return the reader for fullFileName and a outuncompessed stream fetching this file in the archive + osgDB::ReaderWriter *getStreamAndReader(std::stringstream& outuncompessed, std::istream& fin, const std::string& fullFileName) const { + std::string ext = osgDB::getLowerCaseFileExtension(fullFileName); + osgDB::ReaderWriter* rw = 0; + rw = osgDB::Registry::instance()->getReaderWriterForExtension(ext); + std::string baseFileName = osgDB::getNameLessExtension(fullFileName); + std::string baseExt = osgDB::getLowerCaseFileExtension(baseFileName); + rw = osgDB::Registry::instance()->getReaderWriterForExtension(baseExt); + OSG_INFO<getPluginStringData("STREAM_FILENAME"); + std::string ext = osgDB::getLowerCaseFileExtension(fullFileName); + if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; + if (osgDB::containsServerAddress(fullFileName)) return ReadResult::FILE_NOT_HANDLED; + std::stringstream decstream; + osgDB::ReaderWriter* rw = getStreamAndReader(decstream, fin, fullFileName); + return readFile(IMAGE, rw, decstream, local_opt); + } + + virtual ReadResult readHeightField(std::istream& fin,const osgDB::ReaderWriter::Options* local_opt=NULL) const { + std::string fullFileName = local_opt->getPluginStringData("STREAM_FILENAME"); + std::string ext = osgDB::getLowerCaseFileExtension(fullFileName); + if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; + if (osgDB::containsServerAddress(fullFileName)) return ReadResult::FILE_NOT_HANDLED; + std::stringstream decstream; + osgDB::ReaderWriter* rw = getStreamAndReader(decstream, fin, fullFileName); + return readFile(HEIGHTFIELD, rw, decstream, local_opt); + } + + virtual ReadResult readNode(std::istream& fin,const osgDB::ReaderWriter::Options* local_opt=NULL) const { + std::string fullFileName = local_opt->getPluginStringData("STREAM_FILENAME"); + std::string ext = osgDB::getLowerCaseFileExtension(fullFileName); + if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; + if (osgDB::containsServerAddress(fullFileName)) return ReadResult::FILE_NOT_HANDLED; + std::stringstream decstream; + osgDB::ReaderWriter* rw = getStreamAndReader(decstream, fin, fullFileName); + return readFile(NODE, rw, decstream, local_opt); + } + + virtual ReadResult readObject(std::istream& fin,const osgDB::ReaderWriter::Options* local_opt=NULL) const { + std::string fullFileName = local_opt->getPluginStringData("STREAM_FILENAME"); + std::string ext = osgDB::getLowerCaseFileExtension(fullFileName); + if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; + if (osgDB::containsServerAddress(fullFileName)) return ReadResult::FILE_NOT_HANDLED; + std::stringstream decstream; + osgDB::ReaderWriter* rw = getStreamAndReader(decstream, fin, fullFileName); + return readFile(OBJECT, rw, decstream, local_opt); + } + virtual ReadResult readArchive(std::istream& fin,const osgDB::ReaderWriter::Options* local_opt=NULL) const { + std::string fullFileName = local_opt->getPluginStringData("STREAM_FILENAME"); + std::string ext = osgDB::getLowerCaseFileExtension(fullFileName); + if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; + if (osgDB::containsServerAddress(fullFileName)) return ReadResult::FILE_NOT_HANDLED; + std::stringstream decstream; + osgDB::ReaderWriter* rw = getStreamAndReader(decstream, fin, fullFileName); + return readFile(ARCHIVE, rw, decstream, local_opt); + } virtual WriteResult writeObject(const osg::Object& obj, const std::string& fileName, const osgDB::ReaderWriter::Options* options) const From 79afe82774c25f1cce9fd390dc64dfc6141c424f Mon Sep 17 00:00:00 2001 From: Julien Valentin Date: Wed, 8 Nov 2017 15:57:34 +0100 Subject: [PATCH 2/2] replace string with stringstream (avoid a string copy at read) --- src/osgPlugins/gz/ReaderWriterGZ.cpp | 36 ++++++++++++++-------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/osgPlugins/gz/ReaderWriterGZ.cpp b/src/osgPlugins/gz/ReaderWriterGZ.cpp index 477390407..5e8a5bdf3 100644 --- a/src/osgPlugins/gz/ReaderWriterGZ.cpp +++ b/src/osgPlugins/gz/ReaderWriterGZ.cpp @@ -81,11 +81,11 @@ class ReaderWriterGZ : public osgDB::ReaderWriter osgDB::ReaderWriter *getStreamAndReader(std::stringstream& outuncompessed, std::istream& fin, const std::string& fullFileName) const { std::string ext = osgDB::getLowerCaseFileExtension(fullFileName); osgDB::ReaderWriter* rw = 0; - rw = osgDB::Registry::instance()->getReaderWriterForExtension(ext); - std::string baseFileName = osgDB::getNameLessExtension(fullFileName); - std::string baseExt = osgDB::getLowerCaseFileExtension(baseFileName); - rw = osgDB::Registry::instance()->getReaderWriterForExtension(baseExt); - OSG_INFO<getReaderWriterForExtension( ext ); + std::string baseFileName = osgDB::getNameLessExtension( fullFileName ); + std::string baseExt = osgDB::getLowerCaseFileExtension( baseFileName ); + rw = osgDB::Registry::instance()->getReaderWriterForExtension( baseExt ); + OSG_INFO<< className() << "::getStreamAndReader:" << baseExt << " ReaderWriter " << rw <