replace string with stringstream (avoid a string copy at read)

This commit is contained in:
Julien Valentin 2017-11-08 15:57:34 +01:00
parent baf6945a7a
commit 79afe82774

View File

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