]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/stream.cpp
got rid of wxSocketBase::m_error which could get out of sync with wxSocketImpl::m_err...
[wxWidgets.git] / src / common / stream.cpp
index c612f4835b6b5fa3f3b1e34745802a295aaf5575..657e6675d4e7c22358aefd0bb9a792b9925242d0 100644 (file)
@@ -75,24 +75,24 @@ void wxStreamBuffer::Init()
     m_fixed = true;
 }
 
-wxStreamBuffer::wxStreamBuffer(BufMode mode)
+void wxStreamBuffer::InitWithStream(wxStreamBase& stream, BufMode mode)
 {
     Init();
 
-    m_stream = NULL;
+    m_stream = &stream;
     m_mode = mode;
 
-    m_flushable = false;
+    m_flushable = true;
 }
 
-wxStreamBuffer::wxStreamBuffer(wxStreamBase& stream, BufMode mode)
+wxStreamBuffer::wxStreamBuffer(BufMode mode)
 {
     Init();
 
-    m_stream = &stream;
+    m_stream = NULL;
     m_mode = mode;
 
-    m_flushable = true;
+    m_flushable = false;
 }
 
 wxStreamBuffer::wxStreamBuffer(const wxStreamBuffer& buffer)
@@ -1204,21 +1204,33 @@ void wxFilterClassFactory::Remove()
 // wxBufferedInputStream
 // ----------------------------------------------------------------------------
 
-wxBufferedInputStream::wxBufferedInputStream(wxInputStream& s,
+namespace
+{
+
+// helper function used for initializing the buffer used by
+// wxBufferedInput/OutputStream: it simply returns the provided buffer if it's
+// not NULL or creates a buffer of the given size otherwise
+template <typename T>
+wxStreamBuffer *
+CreateBufferIfNeeded(T& stream, wxStreamBuffer *buffer, size_t bufsize = 1024)
+{
+    return buffer ? buffer : new wxStreamBuffer(stream, bufsize);
+}
+
+} // anonymous namespace
+
+wxBufferedInputStream::wxBufferedInputStream(wxInputStream& stream,
                                              wxStreamBuffer *buffer)
-                     : wxFilterInputStream(s)
+                     : wxFilterInputStream(stream)
 {
-    if ( buffer )
-    {
-        // use the buffer provided by the user
-        m_i_streambuf = buffer;
-    }
-    else // create a default buffer
-    {
-        m_i_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::read);
+    m_i_streambuf = CreateBufferIfNeeded(*this, buffer);
+}
 
-        m_i_streambuf->SetBufferIO(1024);
-    }
+wxBufferedInputStream::wxBufferedInputStream(wxInputStream& stream,
+                                             size_t bufsize)
+                     : wxFilterInputStream(stream)
+{
+    m_i_streambuf = CreateBufferIfNeeded(*this, NULL, bufsize);
 }
 
 wxBufferedInputStream::~wxBufferedInputStream()
@@ -1248,12 +1260,15 @@ wxInputStream& wxBufferedInputStream::Read(void *buf, size_t size)
         size -= m_lastcount;
         buf = (char *)buf + m_lastcount;
 
-        // the call to wxStreamBuffer::Read() below will reset our m_lastcount,
-        // so save it
+        // the call to wxStreamBuffer::Read() below may reset our m_lastcount
+        // (but it also may not do it if the buffer is associated to another
+        // existing stream and wasn't created by us), so save it
         size_t countOld = m_lastcount;
 
-        m_i_streambuf->Read(buf, size);
+        // the new count of the bytes read is the count of bytes read this time
+        m_lastcount = m_i_streambuf->Read(buf, size);
 
+        // plus those we had read before
         m_lastcount += countOld;
     }
 
@@ -1317,20 +1332,18 @@ void wxBufferedInputStream::SetInputStreamBuffer(wxStreamBuffer *buffer)
 // wxBufferedOutputStream
 // ----------------------------------------------------------------------------
 
-wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream& s,
+wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream& stream,
                                                wxStreamBuffer *buffer)
-                      : wxFilterOutputStream(s)
+                      : wxFilterOutputStream(stream)
 {
-    if ( buffer )
-    {
-        m_o_streambuf = buffer;
-    }
-    else // create a default one
-    {
-        m_o_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::write);
+    m_o_streambuf = CreateBufferIfNeeded(*this, buffer);
+}
 
-        m_o_streambuf->SetBufferIO(1024);
-    }
+wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream& stream,
+                                               size_t bufsize)
+                      : wxFilterOutputStream(stream)
+{
+    m_o_streambuf = CreateBufferIfNeeded(*this, NULL, bufsize);
 }
 
 wxBufferedOutputStream::~wxBufferedOutputStream()