Factor out 3 copies of identical code into wxInputStreamPeeker

Resolve the long standing "NOTE" comments about having the same code in
wxAnimationDecoder::CanRead(), wxImage::CanRead() and GetImageCount() by
extracting the common logic into a helper wxInputStreamPeeker class and
using it from all places.

This loses the possibility to log a debug message if rewinding the
stream fails, but this is probably not very valuable and the actual
error should be already logged by SeekI() itself when it fails on a
seekable stream.

No real changes.
This commit is contained in:
Vadim Zeitlin
2021-12-15 16:31:16 +01:00
parent b9a1931394
commit 28c3605f6b
3 changed files with 53 additions and 56 deletions

View File

@@ -3445,25 +3445,8 @@ wxIMPLEMENT_ABSTRACT_CLASS(wxImageHandler, wxObject);
#if wxUSE_STREAMS
int wxImageHandler::GetImageCount( wxInputStream& stream )
{
// NOTE: this code is the same of wxAnimationDecoder::CanRead and
// wxImageHandler::CallDoCanRead
if ( !stream.IsSeekable() )
return false; // can't test unseekable stream
wxFileOffset posOld = stream.TellI();
int n = DoGetImageCount(stream);
// restore the old position to be able to test other formats and so on
if ( stream.SeekI(posOld) == wxInvalidOffset )
{
wxLogDebug(wxT("Failed to rewind the stream in wxImageHandler!"));
// reading would fail anyhow as we're not at the right position
return false;
}
return n;
return wxInputStreamPeeker(stream).
CallIfCanSeek(&wxImageHandler::DoGetImageCount, this);
}
bool wxImageHandler::CanRead( const wxString& name )
@@ -3481,25 +3464,8 @@ bool wxImageHandler::CanRead( const wxString& name )
bool wxImageHandler::CallDoCanRead(wxInputStream& stream)
{
// NOTE: this code is the same of wxAnimationDecoder::CanRead and
// wxImageHandler::GetImageCount
if ( !stream.IsSeekable() )
return false; // can't test unseekable stream
wxFileOffset posOld = stream.TellI();
bool ok = DoCanRead(stream);
// restore the old position to be able to test other formats and so on
if ( stream.SeekI(posOld) == wxInvalidOffset )
{
wxLogDebug(wxT("Failed to rewind the stream in wxImageHandler!"));
// reading would fail anyhow as we're not at the right position
return false;
}
return ok;
return wxInputStreamPeeker(stream)
.CallIfCanSeek(&wxImageHandler::DoCanRead, this);
}
#endif // wxUSE_STREAMS