Refactored the libtiffOStreamSeekProc function so that it extendeds the stream when the

requested file position is beyond the current end of the stream.  This fix addresses
a bug that occurred when writting to a streamstream.
This commit is contained in:
Robert Osfield 2011-04-29 14:31:45 +00:00
parent 7e7e5405ef
commit fb09d0e553

View File

@ -177,33 +177,55 @@ toff_t libtiffOStreamSeekProc(thandle_t fd, toff_t off, int i)
{ {
std::ostream *fout = (std::ostream*)fd; std::ostream *fout = (std::ostream*)fd;
toff_t ret; toff_t pos_required = 0;
toff_t stream_end = 0;
switch(i) switch(i)
{ {
case SEEK_SET: case SEEK_SET:
fout->seekp(off,std::ios::beg); {
ret = fout->tellp(); pos_required = off;
if(fout->bad())
ret = 0;
break;
fout->seekp(0, std::ios::end);
stream_end = fout->tellp();
break;
}
case SEEK_CUR: case SEEK_CUR:
fout->seekp(off,std::ios::cur); {
ret = fout->tellp(); toff_t stream_curr = fout->tellp();
if(fout->bad()) pos_required = stream_curr + off;
ret = 0;
break;
fout->seekp(0, std::ios::end);
stream_end = fout->tellp();
break;
}
case SEEK_END: case SEEK_END:
fout->seekp(off,std::ios::end); {
ret = fout->tellp(); fout->seekp(0, std::ios::end);
if(fout->bad()) stream_end = fout->tellp();
ret = 0; pos_required = stream_end + off;
break; break;
}
default: default:
ret = 0;
break; break;
} }
if (pos_required>stream_end)
{
// position required past the end of the stream so we need to insert extra characters to
// ensure the stream is big enough to encompass the new the position.
fout->seekp(0, std::ios::end);
for(toff_t i=stream_end; i<pos_required; ++i)
{
fout->put(char(0));
}
}
fout->seekp(pos_required,std::ios::beg);
toff_t ret = fout->tellp();
if (fout->bad())
{
ret = 0;
}
return ret; return ret;
} }