From 39d16996b7616bf8090b140db74fd0b52d50d169 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 22 May 2002 23:14:47 +0000 Subject: [PATCH] restore the stream position in wxImageHandler itself instead of forcing all derived classes to do it themselves git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@15642 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/image.h | 5 ++++- src/common/gifdecod.cpp | 2 +- src/common/imagbmp.cpp | 29 +++++++++++++---------------- src/common/image.cpp | 26 +++++++++++++++++++++++--- src/common/imaggif.cpp | 10 ++-------- src/common/imagiff.cpp | 11 +++-------- src/common/imagjpeg.cpp | 1 - src/common/imagpcx.cpp | 2 -- src/common/imagpng.cpp | 2 -- src/common/imagpnm.cpp | 4 ---- src/common/imagtiff.cpp | 2 -- src/common/xpmdecod.cpp | 2 +- 12 files changed, 47 insertions(+), 49 deletions(-) diff --git a/include/wx/image.h b/include/wx/image.h index 6df67faa3c..c288bb44a3 100644 --- a/include/wx/image.h +++ b/include/wx/image.h @@ -53,7 +53,7 @@ public: virtual int GetImageCount( wxInputStream& stream ); - bool CanRead( wxInputStream& stream ) { return DoCanRead(stream); } + bool CanRead( wxInputStream& stream ) { return CallDoCanRead(stream); } bool CanRead( const wxString& name ); #endif // wxUSE_STREAMS @@ -71,6 +71,9 @@ protected: virtual bool DoCanRead( wxInputStream& stream ) = 0; #endif // wxUSE_STREAMS + // save the stream position, call DoCanRead() and restore the position + bool CallDoCanRead(wxInputStream& stream); + wxString m_name; wxString m_extension; wxString m_mime; diff --git a/src/common/gifdecod.cpp b/src/common/gifdecod.cpp index f7acdfb4e6..784d7bdfab 100644 --- a/src/common/gifdecod.cpp +++ b/src/common/gifdecod.cpp @@ -620,7 +620,7 @@ bool wxGIFDecoder::CanRead() if ( !m_f->Read(buf, WXSIZEOF(buf)) ) return FALSE; - m_f->SeekI(-WXSIZEOF(buf), wxFromCurrent); + m_f->SeekI(-(off_t)WXSIZEOF(buf), wxFromCurrent); return memcmp(buf, "GIF", WXSIZEOF(buf)) == 0; } diff --git a/src/common/imagbmp.cpp b/src/common/imagbmp.cpp index c9b3a3a554..85124d1a75 100644 --- a/src/common/imagbmp.cpp +++ b/src/common/imagbmp.cpp @@ -879,8 +879,6 @@ bool wxBMPHandler::DoCanRead(wxInputStream& stream) if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) return FALSE; - stream.SeekI(-WXSIZEOF(hdr), wxFromCurrent); - // do we have the BMP file signature? return hdr[0] == 'B' && hdr[1] == 'M'; } @@ -1201,12 +1199,11 @@ int wxICOHandler::GetImageCount(wxInputStream& stream) bool wxICOHandler::DoCanRead(wxInputStream& stream) { unsigned char hdr[4]; - off_t iPos = stream.TellI(); - stream.SeekI (0); - stream.Read(hdr, 4); - stream.SeekI(iPos); - //hdr[2] is one for an icon and two for a cursor - return (hdr[0] == '\0' && hdr[1] == '\0' && hdr[2] == '\1' && hdr[3] == '\0'); + if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) + return FALSE; + + // hdr[2] is one for an icon and two for a cursor + return hdr[0] == '\0' && hdr[1] == '\0' && hdr[2] == '\1' && hdr[3] == '\0'; } @@ -1220,12 +1217,11 @@ IMPLEMENT_DYNAMIC_CLASS(wxCURHandler, wxICOHandler) bool wxCURHandler::DoCanRead(wxInputStream& stream) { unsigned char hdr[4]; - off_t iPos = stream.TellI(); - stream.SeekI (0); - stream.Read(hdr, 4); - stream.SeekI(iPos); - //hdr[2] is one for an icon and two for a cursor - return (hdr[0] == '\0' && hdr[1] == '\0' && hdr[2] == '\2' && hdr[3] == '\0'); + if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) + return FALSE; + + // hdr[2] is one for an icon and two for a cursor + return hdr[0] == '\0' && hdr[1] == '\0' && hdr[2] == '\2' && hdr[3] == '\0'; } //----------------------------------------------------------------------------- @@ -1297,8 +1293,9 @@ bool wxANIHandler::DoCanRead(wxInputStream& stream) wxInt32 *list32 = (wxInt32 *) listtxt; wxInt32 *anih32 = (wxInt32 *) anihtxt; - stream.SeekI(0); - stream.Read(&FCC1, 4); + if ( !stream.Read(&FCC1, 4) ) + return FALSE; + if ( FCC1 != *riff32 ) return FALSE; diff --git a/src/common/image.cpp b/src/common/image.cpp index 03f2b4b36e..3241ed03cd 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -1286,12 +1286,32 @@ bool wxImageHandler::CanRead( const wxString& name ) return CanRead(stream); } - else { - wxLogError( _("Can't check image format of file '%s': file does not exist."), name.c_str() ); + wxLogError( _("Can't check image format of file '%s': file does not exist."), name.c_str() ); + return FALSE; +} + +bool wxImageHandler::CallDoCanRead(wxInputStream& stream) +{ + off_t posOld = stream.TellI(); + if ( posOld == wxInvalidOffset ) + { + // can't test unseekable stream + return FALSE; + } + + bool ok = DoCanRead(stream); + + // restore the old position to be able to test other formats and so on + if ( stream.SeekI(posOld) == wxInvalidOffset ) + { + wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!")); + + // reading would fail anyhow as we're not at the right position return FALSE; } -// return FALSE; + + return ok; } #endif // wxUSE_STREAMS diff --git a/src/common/imaggif.cpp b/src/common/imaggif.cpp index b2a1cdf793..1aed9a28cb 100644 --- a/src/common/imaggif.cpp +++ b/src/common/imaggif.cpp @@ -116,14 +116,8 @@ bool wxGIFHandler::SaveFile( wxImage * WXUNUSED(image), bool wxGIFHandler::DoCanRead( wxInputStream& stream ) { - wxGIFDecoder *decod; - bool ok; - - decod = new wxGIFDecoder(&stream); - ok = decod->CanRead(); - delete decod; - - return ok; + wxGIFDecoder decod(&stream); + return decod.CanRead(); } #endif // wxUSE_STREAMS diff --git a/src/common/imagiff.cpp b/src/common/imagiff.cpp index 25d700bc37..e852c4f645 100644 --- a/src/common/imagiff.cpp +++ b/src/common/imagiff.cpp @@ -235,7 +235,7 @@ bool wxIFFDecoder::CanRead() if ( !m_f->Read(buf, WXSIZEOF(buf)) ) return FALSE; - m_f->SeekI(-WXSIZEOF(buf), wxFromCurrent); + m_f->SeekI(-(off_t)WXSIZEOF(buf), wxFromCurrent); return (memcmp(buf, "FORM", 4) == 0) && (memcmp(buf+8, "ILBM", 4) == 0); } @@ -785,14 +785,9 @@ bool wxIFFHandler::SaveFile(wxImage * WXUNUSED(image), bool wxIFFHandler::DoCanRead(wxInputStream& stream) { - wxIFFDecoder *decod; - bool ok; - - decod = new wxIFFDecoder(&stream); - ok = decod->CanRead(); - delete decod; + wxIFFDecoder decod(&stream); - return ok; + return decod.CanRead(); } #endif // wxUSE_STREAMS diff --git a/src/common/imagjpeg.cpp b/src/common/imagjpeg.cpp index a36895eb11..c2525bae43 100644 --- a/src/common/imagjpeg.cpp +++ b/src/common/imagjpeg.cpp @@ -381,7 +381,6 @@ bool wxJPEGHandler::DoCanRead( wxInputStream& stream ) if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) return FALSE; - stream.SeekI(-WXSIZEOF(hdr), wxFromCurrent); return hdr[0] == 0xFF && hdr[1] == 0xD8; } diff --git a/src/common/imagpcx.cpp b/src/common/imagpcx.cpp index 1b9874db40..64a4d246b0 100644 --- a/src/common/imagpcx.cpp +++ b/src/common/imagpcx.cpp @@ -490,8 +490,6 @@ bool wxPCXHandler::DoCanRead( wxInputStream& stream ) if ( !stream ) return FALSE; - stream.SeekI(-1, wxFromCurrent); - // not very safe, but this is all we can get from PCX header :-( return c == 10; } diff --git a/src/common/imagpng.cpp b/src/common/imagpng.cpp index cfdf35d5fb..b1fea3c1fe 100644 --- a/src/common/imagpng.cpp +++ b/src/common/imagpng.cpp @@ -421,8 +421,6 @@ bool wxPNGHandler::DoCanRead( wxInputStream& stream ) if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) return FALSE; - stream.SeekI(-WXSIZEOF(hdr), wxFromCurrent); - return memcmp(hdr, "\211PNG", WXSIZEOF(hdr)) == 0; } diff --git a/src/common/imagpnm.cpp b/src/common/imagpnm.cpp index d5ca1fae4d..a11007de33 100644 --- a/src/common/imagpnm.cpp +++ b/src/common/imagpnm.cpp @@ -137,8 +137,6 @@ bool wxPNMHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool WXUNUS bool wxPNMHandler::DoCanRead( wxInputStream& stream ) { - off_t pos = stream.TellI(); - Skip_Comment(stream); if ( stream.GetC() == 'P' ) @@ -147,12 +145,10 @@ bool wxPNMHandler::DoCanRead( wxInputStream& stream ) { case '3': case '6': - stream.SeekI(pos); return TRUE; } } - stream.SeekI(pos); return FALSE; } diff --git a/src/common/imagtiff.cpp b/src/common/imagtiff.cpp index 48ca43ab98..16c952d297 100644 --- a/src/common/imagtiff.cpp +++ b/src/common/imagtiff.cpp @@ -373,8 +373,6 @@ bool wxTIFFHandler::DoCanRead( wxInputStream& stream ) if ( !stream.Read(&hdr, WXSIZEOF(hdr)) ) return FALSE; - stream.SeekI(-WXSIZEOF(hdr), wxFromCurrent); - return (hdr[0] == 'I' && hdr[1] == 'I') || (hdr[0] == 'M' && hdr[1] == 'M'); } diff --git a/src/common/xpmdecod.cpp b/src/common/xpmdecod.cpp index 3b33f948ac..264f1d1cf3 100644 --- a/src/common/xpmdecod.cpp +++ b/src/common/xpmdecod.cpp @@ -129,7 +129,7 @@ bool wxXPMDecoder::CanRead(wxInputStream& stream) if ( !stream.Read(buf, WXSIZEOF(buf)) ) return FALSE; - stream.SeekI(-WXSIZEOF(buf), wxFromCurrent); + stream.SeekI(-(off_t)WXSIZEOF(buf), wxFromCurrent); return memcmp(buf, "/* XPM */", WXSIZEOF(buf)) == 0; } -- 2.47.2