+// ============================================================================
+// buffered streams
+// ============================================================================
+
+// ---------------------------------------------------------------------------
+// Stream buffer: this class can be derived from and passed to
+// wxBufferedStreams to implement custom buffering
+// ---------------------------------------------------------------------------
+
+class WXDLLIMPEXP_BASE wxStreamBuffer
+{
+public:
+ enum BufMode
+ {
+ read,
+ write,
+ read_write
+ };
+
+ wxStreamBuffer(wxStreamBase& stream, BufMode mode);
+ wxStreamBuffer(const wxStreamBuffer& buf);
+ virtual ~wxStreamBuffer();
+
+ // Filtered IO
+ virtual size_t Read(void *buffer, size_t size);
+ size_t Read(wxStreamBuffer *buf);
+ virtual size_t Write(const void *buffer, size_t size);
+ size_t Write(wxStreamBuffer *buf);
+
+ virtual char Peek();
+ virtual char GetChar();
+ virtual void PutChar(char c);
+ virtual wxFileOffset Tell() const;
+ virtual wxFileOffset Seek(wxFileOffset pos, wxSeekMode mode);
+
+ // Buffer control
+ void ResetBuffer();
+ void Truncate();
+
+ // NB: the buffer must always be allocated with malloc() if takeOwn is
+ // true as it will be deallocated by free()
+ void SetBufferIO(void *start, void *end, bool takeOwnership = false);
+ void SetBufferIO(void *start, size_t len, bool takeOwnership = false);
+ void SetBufferIO(size_t bufsize);
+ void *GetBufferStart() const { return m_buffer_start; }
+ void *GetBufferEnd() const { return m_buffer_end; }
+ void *GetBufferPos() const { return m_buffer_pos; }
+ size_t GetBufferSize() const { return m_buffer_size; }
+ size_t GetIntPosition() const { return m_buffer_pos - m_buffer_start; }
+ void SetIntPosition(size_t pos) { m_buffer_pos = m_buffer_start + pos; }
+ size_t GetLastAccess() const { return m_buffer_end - m_buffer_start; }
+ size_t GetBytesLeft() const { return m_buffer_end - m_buffer_pos; }
+
+ void Fixed(bool fixed) { m_fixed = fixed; }
+ void Flushable(bool f) { m_flushable = f; }
+
+ bool FlushBuffer();
+ bool FillBuffer();
+ size_t GetDataLeft();
+
+ // misc accessors
+ wxStreamBase *GetStream() const { return m_stream; }
+ bool HasBuffer() const { return m_buffer_size != 0; }
+
+ bool IsFixed() const { return m_fixed; }
+ bool IsFlushable() const { return m_flushable; }
+
+ // only for input/output buffers respectively, returns NULL otherwise
+ wxInputStream *GetInputStream() const;
+ wxOutputStream *GetOutputStream() const;
+
+#if WXWIN_COMPATIBILITY_2_6
+ // deprecated, for compatibility only
+ wxDEPRECATED( wxStreamBase *Stream() );
+#endif // WXWIN_COMPATIBILITY_2_6
+
+ // this constructs a dummy wxStreamBuffer, used by (and exists for)
+ // wxMemoryStreams only, don't use!
+ wxStreamBuffer(BufMode mode);
+
+protected:
+ void GetFromBuffer(void *buffer, size_t size);
+ void PutToBuffer(const void *buffer, size_t size);
+
+ // set the last error to the specified value if we didn't have it before
+ void SetError(wxStreamError err);
+
+ // common part of several ctors
+ void Init();
+
+ // init buffer variables to be empty
+ void InitBuffer();
+
+ // free the buffer (always safe to call)
+ void FreeBuffer();
+
+ // the buffer itself: the pointers to its start and end and the current
+ // position in the buffer
+ char *m_buffer_start,
+ *m_buffer_end,
+ *m_buffer_pos;
+
+ // the buffer size
+ // FIXME: isn't it the same as m_buffer_end - m_buffer_start? (VZ)
+ size_t m_buffer_size;
+
+ // the stream we're associated with
+ wxStreamBase *m_stream;
+
+ // its mode
+ BufMode m_mode;
+
+ // flags
+ bool m_destroybuf, // deallocate buffer?
+ m_fixed,
+ m_flushable;
+
+private:
+// Cannot use
+// DECLARE_NO_COPY_CLASS(wxStreamBuffer)
+// because copy constructor is explicitly declared above;
+// but no copy assignment operator is defined, so declare
+// it private to prevent the compiler from defining it:
+ wxStreamBuffer& operator=(const wxStreamBuffer&);
+};
+
+// ---------------------------------------------------------------------------
+// wxBufferedInputStream
+// ---------------------------------------------------------------------------
+
+class WXDLLIMPEXP_BASE wxBufferedInputStream : public wxFilterInputStream
+{
+public:
+ // if a non NULL buffer is given to the stream, it will be deleted by it
+ wxBufferedInputStream(wxInputStream& stream,
+ wxStreamBuffer *buffer = NULL);
+ virtual ~wxBufferedInputStream();
+
+ char Peek();
+ wxInputStream& Read(void *buffer, size_t size);
+
+ // Position functions
+ wxFileOffset SeekI(wxFileOffset pos, wxSeekMode mode = wxFromStart);
+ wxFileOffset TellI() const;
+ bool IsSeekable() const { return m_parent_i_stream->IsSeekable(); }
+
+ // the buffer given to the stream will be deleted by it
+ void SetInputStreamBuffer(wxStreamBuffer *buffer);
+ wxStreamBuffer *GetInputStreamBuffer() const { return m_i_streambuf; }
+
+#if WXWIN_COMPATIBILITY_2_6
+ // deprecated, for compatibility only
+ wxDEPRECATED( wxStreamBuffer *InputStreamBuffer() const );
+#endif // WXWIN_COMPATIBILITY_2_6
+
+protected:
+ virtual size_t OnSysRead(void *buffer, size_t bufsize);
+ virtual wxFileOffset OnSysSeek(wxFileOffset seek, wxSeekMode mode);
+ virtual wxFileOffset OnSysTell() const;
+
+ wxStreamBuffer *m_i_streambuf;
+
+ DECLARE_NO_COPY_CLASS(wxBufferedInputStream)
+};
+
+// ----------------------------------------------------------------------------
+// wxBufferedOutputStream
+// ----------------------------------------------------------------------------
+
+class WXDLLIMPEXP_BASE wxBufferedOutputStream : public wxFilterOutputStream
+{
+public:
+ // if a non NULL buffer is given to the stream, it will be deleted by it
+ wxBufferedOutputStream(wxOutputStream& stream,
+ wxStreamBuffer *buffer = NULL);
+ virtual ~wxBufferedOutputStream();
+
+ wxOutputStream& Write(const void *buffer, size_t size);
+
+ // Position functions
+ wxFileOffset SeekO(wxFileOffset pos, wxSeekMode mode = wxFromStart);
+ wxFileOffset TellO() const;
+ bool IsSeekable() const { return m_parent_o_stream->IsSeekable(); }
+
+ void Sync();
+ bool Close();
+
+ wxFileOffset GetLength() const;
+
+ // the buffer given to the stream will be deleted by it
+ void SetOutputStreamBuffer(wxStreamBuffer *buffer);
+ wxStreamBuffer *GetOutputStreamBuffer() const { return m_o_streambuf; }
+
+#if WXWIN_COMPATIBILITY_2_6
+ // deprecated, for compatibility only
+ wxDEPRECATED( wxStreamBuffer *OutputStreamBuffer() const );
+#endif // WXWIN_COMPATIBILITY_2_6
+
+protected:
+ virtual size_t OnSysWrite(const void *buffer, size_t bufsize);
+ virtual wxFileOffset OnSysSeek(wxFileOffset seek, wxSeekMode mode);
+ virtual wxFileOffset OnSysTell() const;
+
+ wxStreamBuffer *m_o_streambuf;
+
+ DECLARE_NO_COPY_CLASS(wxBufferedOutputStream)
+};
+
+#if WXWIN_COMPATIBILITY_2_6
+ inline wxStreamBase *wxStreamBuffer::Stream() { return m_stream; }
+ inline wxStreamBuffer *wxBufferedInputStream::InputStreamBuffer() const { return m_i_streambuf; }
+ inline wxStreamBuffer *wxBufferedOutputStream::OutputStreamBuffer() const { return m_o_streambuf; }
+#endif // WXWIN_COMPATIBILITY_2_6
+
+#endif // wxUSE_STREAMS