]> git.saurik.com Git - wxWidgets.git/commitdiff
restore the stream position in wxImageHandler itself instead of forcing all
authorVadim Zeitlin <vadim@wxwidgets.org>
Wed, 22 May 2002 23:14:47 +0000 (23:14 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Wed, 22 May 2002 23:14:47 +0000 (23:14 +0000)
derived classes to do it themselves

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@15642 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

12 files changed:
include/wx/image.h
src/common/gifdecod.cpp
src/common/imagbmp.cpp
src/common/image.cpp
src/common/imaggif.cpp
src/common/imagiff.cpp
src/common/imagjpeg.cpp
src/common/imagpcx.cpp
src/common/imagpng.cpp
src/common/imagpnm.cpp
src/common/imagtiff.cpp
src/common/xpmdecod.cpp

index 6df67faa3c9e11ebdc649dfce081b66525fabc62..c288bb44a3a3286e7b3fc59b1d5b4f6a8af9508a 100644 (file)
@@ -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;
index f7acdfb4e67017a8a8d6cbf39632ab1138589bb0..784d7bdfab11dfbd6eeff229564443e21a5f6af5 100644 (file)
@@ -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;
 }
index c9b3a3a55490eb256ddf5f4754a66000944700de..85124d1a7548879f883d82e5de44160de60f70a8 100644 (file)
@@ -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;
 
index 03f2b4b36e5a8dafdca33dedd06cd52ea7ff1467..3241ed03cd28b49cc16423fc9d1888c2f66e5241 100644 (file)
@@ -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
index b2a1cdf79330f3333bdd42159c07d4c3999e6692..1aed9a28cbb4cb9ea4e2857d48f096eeecf1891c 100644 (file)
@@ -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
index 25d700bc377dadfc3e7968959fb1741a9815975a..e852c4f645c3f1788995a6c3d9cd428fab297f4b 100644 (file)
@@ -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
index a36895eb111201b359d8e162e4c40ba4950c913c..c2525bae43fcf288e561e5f091b17f3e87aea94c 100644 (file)
@@ -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;
 }
 
index 1b9874db40fefc3e6db88ce116eea19a51baeba7..64a4d246b0efa5058f7aec2cfe7ed9524243ad88 100644 (file)
@@ -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;
 }
index cfdf35d5fb5f269ab9bbf80c455459a6b3a1e190..b1fea3c1fef766158cb0cdeb0d153db1d0dbb2d8 100644 (file)
@@ -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;
 }
 
index d5ca1fae4ddfa7a2e6075188680dd9fb297a9a7e..a11007de33c94d4f177031c3705b2107d3c3c36e 100644 (file)
@@ -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;
 }
 
index 48ca43ab9856c737e2980d08925d94970610ff79..16c952d297020d7d399ab2730a928017e081e345 100644 (file)
@@ -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');
 }
index 3b33f948acb91f734f01538aa29ffaec4acfa9d5..264f1d1cf3d89e781521e667f71b72e9a38a5135 100644 (file)
@@ -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;
 }