Merge pull request #380 from mp3butcher/MDI7

add funcs to read GZ Objects from a stream
This commit is contained in:
OpenSceneGraph git repository 2017-11-10 13:36:52 +00:00 committed by GitHub
commit 860df3e10e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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<< className() << "::getStreamAndReader:" << baseExt << " ReaderWriter " << rw <<std::endl;
read(fin, outuncompessed);
return rw;
}
virtual ReadResult readImage(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(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
@ -104,8 +165,8 @@ class ReaderWriterGZ : public osgDB::ReaderWriter
WriteResult writeFile(ObjectType objectType, const osg::Object* object, const std::string& fullFileName, const osgDB::ReaderWriter::Options* options) const;
bool read(std::istream& fin, std::string& destination) const;
bool write(std::ostream& fout, const std::string& source) const;
bool read(std::istream& fin, std::stringstream& destination) const;
bool write(std::ostream& fout, const std::stringstream& source) const;
};
@ -177,10 +238,8 @@ osgDB::ReaderWriter::ReadResult ReaderWriterGZ::readFile(ObjectType objectType,
if (!fin) return ReadResult::ERROR_IN_READING_FILE;
std::string dest;
read(fin, dest);
std::stringstream strstream(dest);
std::stringstream strstream;
read(fin, strstream);
return readFile(objectType, rw, strstream, local_opt.get());
}
@ -229,14 +288,14 @@ osgDB::ReaderWriter::WriteResult ReaderWriterGZ::writeFile(ObjectType objectType
osgDB::ofstream fout(fullFileName.c_str(), std::ios::binary|std::ios::out);
write(fout,strstream.str());
write(fout,strstream);
return writeResult;
}
#define CHUNK 16384
bool ReaderWriterGZ::read(std::istream& fin, std::string& destination) const
bool ReaderWriterGZ::read(std::istream& fin, std::stringstream& destination) const
{
int ret;
unsigned have;
@ -252,7 +311,7 @@ bool ReaderWriterGZ::read(std::istream& fin, std::string& destination) const
strm.next_in = Z_NULL;
ret = inflateInit2(&strm,
15 + 32 // autodetected zlib or gzip header
);
);
if (ret != Z_OK)
return false;
@ -285,8 +344,7 @@ bool ReaderWriterGZ::read(std::istream& fin, std::string& destination) const
return false;
}
have = CHUNK - strm.avail_out;
destination.append((char*)out, have);
destination.write((char*)out,have);
} while (strm.avail_out == 0);
@ -298,8 +356,9 @@ bool ReaderWriterGZ::read(std::istream& fin, std::string& destination) const
return ret == Z_STREAM_END ? true : false;
}
bool ReaderWriterGZ::write(std::ostream& fout, const std::string& source) const
bool ReaderWriterGZ::write(std::ostream& fout, const std::stringstream& sourcestream) const
{
std::string source = sourcestream.str();
int ret, flush = Z_FINISH;
unsigned have;
z_stream strm;
@ -322,7 +381,7 @@ bool ReaderWriterGZ::write(std::ostream& fout, const std::string& source) const
8, // default
stategy);
if (ret != Z_OK)
return false;
return false;
strm.avail_in = source.size();
strm.next_in = (Bytef*)(&(*source.begin()));
@ -357,6 +416,8 @@ bool ReaderWriterGZ::write(std::ostream& fout, const std::string& source) const
}
// now register with Registry to instantiate the above
// reader/writer.
REGISTER_OSGPLUGIN(GZ, ReaderWriterGZ)