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
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;
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;
}
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';
}
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';
}
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';
}
//-----------------------------------------------------------------------------
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;
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
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
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);
}
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
if ( !stream.Read(hdr, WXSIZEOF(hdr)) )
return FALSE;
- stream.SeekI(-WXSIZEOF(hdr), wxFromCurrent);
return hdr[0] == 0xFF && hdr[1] == 0xD8;
}
if ( !stream )
return FALSE;
- stream.SeekI(-1, wxFromCurrent);
-
// not very safe, but this is all we can get from PCX header :-(
return c == 10;
}
if ( !stream.Read(hdr, WXSIZEOF(hdr)) )
return FALSE;
- stream.SeekI(-WXSIZEOF(hdr), wxFromCurrent);
-
return memcmp(hdr, "\211PNG", WXSIZEOF(hdr)) == 0;
}
bool wxPNMHandler::DoCanRead( wxInputStream& stream )
{
- off_t pos = stream.TellI();
-
Skip_Comment(stream);
if ( stream.GetC() == 'P' )
{
case '3':
case '6':
- stream.SeekI(pos);
return TRUE;
}
}
- stream.SeekI(pos);
return FALSE;
}
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');
}
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;
}