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

@ -85,7 +85,7 @@ class ReaderWriterGZ : public osgDB::ReaderWriter
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;
OSG_INFO<< className() << "::getStreamAndReader:" << baseExt << " ReaderWriter " << rw <<std::endl;
read(fin, outuncompessed);
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;
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;
};
@ -238,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());
}
@ -290,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;
@ -346,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);
@ -359,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;
@ -418,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)