+ if (m_owns)
+ delete m_parent_o_stream;
+}
+
+// ----------------------------------------------------------------------------
+// wxFilterClassFactoryBase
+// ----------------------------------------------------------------------------
+
+IMPLEMENT_ABSTRACT_CLASS(wxFilterClassFactoryBase, wxObject)
+
+wxString wxFilterClassFactoryBase::PopExtension(const wxString& location) const
+{
+ return location.substr(0, FindExtension(location));
+}
+
+wxString::size_type wxFilterClassFactoryBase::FindExtension(
+ const wxString& location) const
+{
+ for (const wxChar *const *p = GetProtocols(wxSTREAM_FILEEXT); *p; p++)
+ {
+ if ( location.EndsWith(*p) )
+ return location.length() - wxStrlen(*p);
+ }
+
+ return wxString::npos;
+}
+
+bool wxFilterClassFactoryBase::CanHandle(const wxString& protocol,
+ wxStreamProtocolType type) const
+{
+ if (type == wxSTREAM_FILEEXT)
+ return FindExtension(protocol) != wxString::npos;
+ else
+ for (const wxChar *const *p = GetProtocols(type); *p; p++)
+ if (protocol == *p)
+ return true;
+
+ return false;
+}
+
+// ----------------------------------------------------------------------------
+// wxFilterClassFactory
+// ----------------------------------------------------------------------------
+
+IMPLEMENT_ABSTRACT_CLASS(wxFilterClassFactory, wxFilterClassFactoryBase)
+
+wxFilterClassFactory *wxFilterClassFactory::sm_first = NULL;
+
+void wxFilterClassFactory::Remove()
+{
+ if (m_next != this)
+ {
+ wxFilterClassFactory **pp = &sm_first;
+
+ while (*pp != this)
+ pp = &(*pp)->m_next;
+
+ *pp = m_next;
+
+ m_next = this;
+ }
+}
+
+// ----------------------------------------------------------------------------
+// wxBufferedInputStream
+// ----------------------------------------------------------------------------
+
+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(bufsize, stream);
+}
+
+} // anonymous namespace
+
+wxBufferedInputStream::wxBufferedInputStream(wxInputStream& stream,
+ wxStreamBuffer *buffer)
+ : wxFilterInputStream(stream)
+{
+ m_i_streambuf = CreateBufferIfNeeded(*this, buffer);
+}
+
+wxBufferedInputStream::wxBufferedInputStream(wxInputStream& stream,
+ size_t bufsize)
+ : wxFilterInputStream(stream)
+{
+ m_i_streambuf = CreateBufferIfNeeded(*this, NULL, bufsize);
+}
+
+wxBufferedInputStream::~wxBufferedInputStream()
+{
+ m_parent_i_stream->SeekI(-(wxFileOffset)m_i_streambuf->GetBytesLeft(),
+ wxFromCurrent);
+
+ delete m_i_streambuf;
+}
+
+char wxBufferedInputStream::Peek()
+{
+ return m_i_streambuf->Peek();
+}
+
+wxInputStream& wxBufferedInputStream::Read(void *buf, size_t size)
+{
+ // reset the error flag
+ Reset();
+
+ // first read from the already cached data
+ m_lastcount = GetWBack(buf, size);
+
+ // do we have to read anything more?
+ if ( m_lastcount < size )
+ {
+ size -= m_lastcount;
+ buf = (char *)buf + m_lastcount;
+
+ // 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;
+
+ // 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;
+ }
+
+ return *this;
+}
+
+wxFileOffset wxBufferedInputStream::SeekI(wxFileOffset pos, wxSeekMode mode)
+{
+ // RR: Look at wxInputStream for comments.
+
+ if (m_lasterror==wxSTREAM_EOF)
+ Reset();
+
+ if (m_wback)
+ {
+ wxLogDebug( wxT("Seeking in stream which has data written back to it.") );
+
+ free(m_wback);
+ m_wback = NULL;
+ m_wbacksize = 0;
+ m_wbackcur = 0;
+ }
+
+ return m_i_streambuf->Seek(pos, mode);
+}
+
+wxFileOffset wxBufferedInputStream::TellI() const
+{
+ wxFileOffset pos = m_i_streambuf->Tell();
+
+ if (pos != wxInvalidOffset)
+ pos -= (m_wbacksize - m_wbackcur);
+
+ return pos;
+}
+
+size_t wxBufferedInputStream::OnSysRead(void *buffer, size_t bufsize)
+{
+ return m_parent_i_stream->Read(buffer, bufsize).LastRead();
+}
+
+wxFileOffset wxBufferedInputStream::OnSysSeek(wxFileOffset seek, wxSeekMode mode)
+{
+ return m_parent_i_stream->SeekI(seek, mode);
+}
+
+wxFileOffset wxBufferedInputStream::OnSysTell() const
+{
+ return m_parent_i_stream->TellI();
+}
+
+void wxBufferedInputStream::SetInputStreamBuffer(wxStreamBuffer *buffer)
+{
+ wxCHECK_RET( buffer, wxT("wxBufferedInputStream needs buffer") );
+
+ delete m_i_streambuf;
+ m_i_streambuf = buffer;
+}
+
+// ----------------------------------------------------------------------------
+// wxBufferedOutputStream
+// ----------------------------------------------------------------------------
+
+wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream& stream,
+ wxStreamBuffer *buffer)
+ : wxFilterOutputStream(stream)
+{
+ m_o_streambuf = CreateBufferIfNeeded(*this, buffer);
+}
+
+wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream& stream,
+ size_t bufsize)
+ : wxFilterOutputStream(stream)
+{
+ m_o_streambuf = CreateBufferIfNeeded(*this, NULL, bufsize);
+}
+
+wxBufferedOutputStream::~wxBufferedOutputStream()
+{
+ Sync();
+ delete m_o_streambuf;
+}
+
+bool wxBufferedOutputStream::Close()
+{
+ Sync();
+ return IsOk();
+}
+
+
+wxOutputStream& wxBufferedOutputStream::Write(const void *buffer, size_t size)
+{
+ m_lastcount = 0;
+ m_o_streambuf->Write(buffer, size);
+ return *this;
+}
+
+wxFileOffset wxBufferedOutputStream::SeekO(wxFileOffset pos, wxSeekMode mode)
+{
+ Sync();
+ return m_o_streambuf->Seek(pos, mode);
+}
+
+wxFileOffset wxBufferedOutputStream::TellO() const
+{
+ return m_o_streambuf->Tell();
+}
+
+void wxBufferedOutputStream::Sync()
+{
+ m_o_streambuf->FlushBuffer();
+ m_parent_o_stream->Sync();
+}
+
+size_t wxBufferedOutputStream::OnSysWrite(const void *buffer, size_t bufsize)
+{
+ return m_parent_o_stream->Write(buffer, bufsize).LastWrite();
+}
+
+wxFileOffset wxBufferedOutputStream::OnSysSeek(wxFileOffset seek, wxSeekMode mode)
+{
+ return m_parent_o_stream->SeekO(seek, mode);
+}
+
+wxFileOffset wxBufferedOutputStream::OnSysTell() const
+{
+ return m_parent_o_stream->TellO();
+}
+
+wxFileOffset wxBufferedOutputStream::GetLength() const
+{
+ return m_parent_o_stream->GetLength() + m_o_streambuf->GetIntPosition();
+}
+
+void wxBufferedOutputStream::SetOutputStreamBuffer(wxStreamBuffer *buffer)
+{
+ wxCHECK_RET( buffer, wxT("wxBufferedOutputStream needs buffer") );
+
+ delete m_o_streambuf;
+ m_o_streambuf = buffer;