]> git.saurik.com Git - wxWidgets.git/commitdiff
Allow passing the error value to wxStreamBase::Reset().
authorVadim Zeitlin <vadim@wxwidgets.org>
Fri, 22 Jul 2011 16:16:00 +0000 (16:16 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Fri, 22 Jul 2011 16:16:00 +0000 (16:16 +0000)
It can be useful to induce an error on the stream explicitly, e.g. because an
incorrect value was read from it and we want to indicate it to the caller by
setting stream error to wxSTREAM_READ_ERROR.

Allow to do this by passing an optional error value to wxStreamBase::Reset().

Add an example of using the new functionality to the docview sample which
needs it to be able to signal errors while reading the files.

Also document this method that previously wasn't documented at all.

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

include/wx/stream.h
interface/wx/stream.h
samples/docview/doc.cpp

index 9119658421f46abac4b57c6de3087b989ef94eaf..ed07ce61a86f4503539641d6b550f9cabc6f8e43 100644 (file)
@@ -64,7 +64,7 @@ public:
     bool operator!() const { return !IsOk(); }
 
     // reset the stream state
-    void Reset() { m_lasterror = wxSTREAM_NO_ERROR; }
+    void Reset(wxStreamError error = wxSTREAM_NO_ERROR) { m_lasterror = error; }
 
     // this doesn't make sense for all streams, always test its return value
     virtual size_t GetSize() const;
index 36617cc0912c72a533ec58bd60bd75accae919a0..4f3bb18a4566122b9eeb692a5fe8aa321c1abfa2 100644 (file)
@@ -80,6 +80,18 @@ public:
     */
     virtual bool IsSeekable() const;
 
+    /**
+        Resets the stream state.
+
+        By default, resets the stream to good state, i.e. clears any errors.
+        Since wxWidgets 2.9.3 can be also used to explicitly set the state to
+        the specified error (the @a error argument didn't exist in the previous
+        versions).
+
+        @see GetLastError()
+     */
+    void Reset(wxStreamError error = wxSTREAM_NO_ERROR);
+
     /**
         Returns the opposite of IsOk().
         You can use this function to test the validity of the stream as if
index 24f3984b165033c74cc83840cbe4ea1c921c47f7..e093d82ae3c578ec913eb3657fe025ba20498099 100644 (file)
@@ -79,6 +79,16 @@ DocumentIstream& DrawingDocument::LoadObject(DocumentIstream& istream)
 
     wxInt32 count = 0;
     stream >> count;
+    if ( count < 0 )
+    {
+        wxLogWarning("Drawing document corrupted: invalid segments count.");
+#if wxUSE_STD_IOSTREAM
+        istream.clear(std::ios::badbit);
+#else
+        istream.Reset(wxSTREAM_READ_ERROR);
+#endif
+        return istream;
+    }
 
     for ( int n = 0; n < count; n++ )
     {