1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: "wxWindows stream" base classes
4 // Author: Guilhem Lavaux
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_WXSTREAM_H__
13 #define _WX_WXSTREAM_H__
20 #include "wx/object.h"
21 #include "wx/string.h"
22 #include "wx/filefn.h" // for off_t, wxInvalidOffset and wxSeekMode
24 class WXDLLEXPORT wxStreamBase
;
25 class WXDLLEXPORT wxInputStream
;
26 class WXDLLEXPORT wxOutputStream
;
28 typedef wxInputStream
& (*__wxInputManip
)(wxInputStream
&);
29 typedef wxOutputStream
& (*__wxOutputManip
)(wxOutputStream
&);
31 WXDLLEXPORT wxOutputStream
& wxEndL(wxOutputStream
& o_stream
);
33 // ---------------------------------------------------------------------------
35 // ---------------------------------------------------------------------------
37 class WXDLLEXPORT wxStreamBuffer
{
40 read
= 0, write
, read_write
46 wxStreamBuffer(wxStreamBase
& stream
, BufMode mode
);
47 wxStreamBuffer(BufMode mode
);
48 wxStreamBuffer(const wxStreamBuffer
& buf
);
54 size_t Read(void *buffer
, size_t size
);
55 size_t Read(wxStreamBuffer
*buf
);
56 size_t Write(const void *buffer
, size_t size
);
57 size_t Write(wxStreamBuffer
*buf
);
59 size_t WriteBack(const char *buffer
, size_t size
);
60 bool WriteBack(char c
);
64 off_t
Seek(off_t pos
, wxSeekMode mode
);
70 void SetBufferIO(char *buffer_start
, char *buffer_end
);
71 void SetBufferIO(size_t bufsize
);
72 char *GetBufferStart() const { return m_buffer_start
; }
73 char *GetBufferEnd() const { return m_buffer_end
; }
74 char *GetBufferPos() const { return m_buffer_pos
; }
75 off_t
GetIntPosition() const { return m_buffer_pos
-m_buffer_start
; }
76 void SetIntPosition(off_t pos
) { m_buffer_pos
= m_buffer_start
+pos
; }
77 size_t GetLastAccess() const { return m_buffer_end
-m_buffer_start
; }
79 void Fixed(bool fixed
) { m_fixed
= fixed
; }
80 void Flushable(bool f
) { m_flushable
= f
; }
89 wxStreamBase
*Stream() { return m_stream
; }
92 char *AllocSpaceWBack(size_t needed_size
);
93 size_t GetWBack(char *buf
, size_t bsize
);
95 void GetFromBuffer(void *buffer
, size_t size
);
96 void PutToBuffer(const void *buffer
, size_t size
);
99 char *m_buffer_start
, *m_buffer_end
, *m_buffer_pos
;
100 size_t m_buffer_size
;
103 size_t m_wbacksize
, m_wbackcur
;
105 bool m_fixed
, m_flushable
;
107 wxStreamBase
*m_stream
;
109 bool m_destroybuf
, m_destroystream
;
112 // ---------------------------------------------------------------------------
113 // wxStream: base classes
114 // ---------------------------------------------------------------------------
117 wxStream_NOERROR
= 0,
123 class WXDLLEXPORT wxStreamBase
{
126 virtual ~wxStreamBase();
128 wxStreamError
LastError() const { return m_lasterror
; }
129 virtual size_t StreamSize() const { return ~((size_t)0); }
132 friend class wxStreamBuffer
;
134 virtual size_t OnSysRead(void *buffer
, size_t bufsize
);
135 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
136 virtual off_t
OnSysSeek(off_t seek
, wxSeekMode mode
);
137 virtual off_t
OnSysTell() const;
141 wxStreamError m_lasterror
;
144 class WXDLLEXPORT wxInputStream
: public wxStreamBase
{
147 wxInputStream(wxStreamBuffer
*sbuf
);
148 virtual ~wxInputStream();
153 virtual wxInputStream
& Read(void *buffer
, size_t size
);
154 wxInputStream
& Read(wxOutputStream
& stream_out
);
156 // Position functions
157 off_t
SeekI(off_t pos
, wxSeekMode mode
= wxFromStart
);
161 wxStreamBuffer
*InputStreamBuffer() { return m_i_streambuf
; }
162 size_t LastRead() { return wxStreamBase::m_lastcount
; }
165 wxInputStream
& operator>>(wxOutputStream
& out
) { return Read(out
); }
166 wxInputStream
& operator>>(wxString
& line
);
167 wxInputStream
& operator>>(char& c
);
168 wxInputStream
& operator>>(short& i
);
169 wxInputStream
& operator>>(int& i
);
170 wxInputStream
& operator>>(long& i
);
171 wxInputStream
& operator>>(double& i
);
173 wxInputStream
& operator>>(wxObject
*& obj
);
176 wxInputStream
& operator>>(float& f
) { double d
; operator>>((double&)d
); f
= (float)d
; return *this; }
177 wxInputStream
& operator>>(unsigned char& c
) { return operator>>((char&)c
); }
178 wxInputStream
& operator>>(unsigned short& i
) { return operator>>((short&)i
); }
179 wxInputStream
& operator>>(unsigned int& i
) { return operator>>((int&)i
); }
180 wxInputStream
& operator>>(unsigned long& i
) { return operator>>((long&)i
); }
181 wxInputStream
& operator>>( __wxInputManip func
) { return func(*this); }
185 wxStreamBuffer
*m_i_streambuf
;
188 class WXDLLEXPORT wxOutputStream
: public wxStreamBase
{
191 wxOutputStream(wxStreamBuffer
*sbuf
);
192 virtual ~wxOutputStream();
194 virtual wxOutputStream
& Write(const void *buffer
, size_t size
);
195 wxOutputStream
& Write(wxInputStream
& stream_in
);
197 off_t
SeekO(off_t pos
, wxSeekMode mode
= wxFromStart
);
200 size_t LastWrite() const { return wxStreamBase::m_lastcount
; }
201 wxStreamBuffer
*OutputStreamBuffer() { return m_o_streambuf
; }
205 wxOutputStream
& operator<<(wxInputStream
& out
) { return Write(out
); }
206 wxOutputStream
& operator<<(const char *string
);
207 wxOutputStream
& operator<<(wxString
& string
);
208 wxOutputStream
& operator<<(char c
);
209 wxOutputStream
& operator<<(short i
);
210 wxOutputStream
& operator<<(int i
);
211 wxOutputStream
& operator<<(long i
);
212 wxOutputStream
& operator<<(double f
);
214 wxOutputStream
& operator<<(wxObject
& obj
);
217 wxOutputStream
& operator<<(float f
) { return operator<<((double)f
); }
218 wxOutputStream
& operator<<(unsigned char c
) { return operator<<((char)c
); }
219 wxOutputStream
& operator<<(unsigned short i
) { return operator<<((short)i
); }
220 wxOutputStream
& operator<<(unsigned int i
) { return operator<<((int)i
); }
221 wxOutputStream
& operator<<(unsigned long i
) { return operator<<((long)i
); }
222 wxOutputStream
& operator<<( __wxOutputManip func
) { return func(*this); }
226 wxStreamBuffer
*m_o_streambuf
;
229 // ---------------------------------------------------------------------------
231 // ---------------------------------------------------------------------------
233 class WXDLLEXPORT wxFilterInputStream
: public wxInputStream
{
235 wxFilterInputStream();
236 wxFilterInputStream(wxInputStream
& stream
);
237 ~wxFilterInputStream();
239 char Peek() { return m_parent_i_stream
->Peek(); }
241 wxStreamError
LastError() const { return m_parent_i_stream
->LastError(); }
242 size_t StreamSize() const { return m_parent_i_stream
->StreamSize(); }
245 wxInputStream
*m_parent_i_stream
;
248 class WXDLLEXPORT wxFilterOutputStream
: public wxOutputStream
{
250 wxFilterOutputStream();
251 wxFilterOutputStream(wxOutputStream
& stream
);
252 ~wxFilterOutputStream();
254 wxStreamError
LastError() const { return m_parent_o_stream
->LastError(); }
255 size_t StreamSize() const { return m_parent_o_stream
->StreamSize(); }
258 wxOutputStream
*m_parent_o_stream
;