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__
24 #include "wx/object.h"
25 #include "wx/string.h"
26 #include "wx/filefn.h" // for off_t, wxInvalidOffset and wxSeekMode
28 class WXDLLEXPORT wxStreamBase
;
29 class WXDLLEXPORT wxInputStream
;
30 class WXDLLEXPORT wxOutputStream
;
32 typedef wxInputStream
& (*__wxInputManip
)(wxInputStream
&);
33 typedef wxOutputStream
& (*__wxOutputManip
)(wxOutputStream
&);
35 WXDLLEXPORT wxOutputStream
& wxEndL(wxOutputStream
& o_stream
);
37 // ---------------------------------------------------------------------------
39 // ---------------------------------------------------------------------------
41 class WXDLLEXPORT wxStreamBuffer
{
44 read
= 0, write
, read_write
50 wxStreamBuffer(wxStreamBase
& stream
, BufMode mode
);
51 wxStreamBuffer(BufMode mode
);
52 wxStreamBuffer(const wxStreamBuffer
& buf
);
58 size_t Read(void *buffer
, size_t size
);
59 size_t Read(wxStreamBuffer
*buf
);
60 size_t Write(const void *buffer
, size_t size
);
61 size_t Write(wxStreamBuffer
*buf
);
63 size_t WriteBack(const char *buffer
, size_t size
);
64 bool WriteBack(char c
);
68 off_t
Seek(off_t pos
, wxSeekMode mode
);
74 void SetBufferIO(char *buffer_start
, char *buffer_end
);
75 void SetBufferIO(size_t bufsize
);
76 char *GetBufferStart() const { return m_buffer_start
; }
77 char *GetBufferEnd() const { return m_buffer_end
; }
78 char *GetBufferPos() const { return m_buffer_pos
; }
79 off_t
GetIntPosition() const { return m_buffer_pos
-m_buffer_start
; }
80 void SetIntPosition(off_t pos
) { m_buffer_pos
= m_buffer_start
+pos
; }
81 size_t GetLastAccess() const { return m_buffer_end
-m_buffer_start
; }
83 void Fixed(bool fixed
) { m_fixed
= fixed
; }
84 void Flushable(bool f
) { m_flushable
= f
; }
93 wxStreamBase
*Stream() { return m_stream
; }
96 char *AllocSpaceWBack(size_t needed_size
);
97 size_t GetWBack(char *buf
, size_t bsize
);
99 void GetFromBuffer(void *buffer
, size_t size
);
100 void PutToBuffer(const void *buffer
, size_t size
);
103 char *m_buffer_start
, *m_buffer_end
, *m_buffer_pos
;
104 size_t m_buffer_size
;
107 size_t m_wbacksize
, m_wbackcur
;
109 bool m_fixed
, m_flushable
;
111 wxStreamBase
*m_stream
;
113 bool m_destroybuf
, m_destroystream
;
116 // ---------------------------------------------------------------------------
117 // wxStream: base classes
118 // ---------------------------------------------------------------------------
120 #define wxStream_NOERROR wxSTR_NOERROR
121 #define wxStream_EOF wxSTR_EOF
122 #define wxStream_WRITE_ERR wxSTR_WRITE_ERROR
123 #define wxStream_READ_ERR wxSTR_READ_ERROR
126 wxStream_NOERROR
= 0,
132 class WXDLLEXPORT wxStreamBase
{
135 virtual ~wxStreamBase();
137 wxStreamError
LastError() const { return m_lasterror
; }
138 virtual size_t StreamSize() const { return ~((size_t)0); }
141 friend class wxStreamBuffer
;
143 virtual size_t OnSysRead(void *buffer
, size_t bufsize
);
144 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
145 virtual off_t
OnSysSeek(off_t seek
, wxSeekMode mode
);
146 virtual off_t
OnSysTell() const;
150 wxStreamError m_lasterror
;
153 class WXDLLEXPORT wxInputStream
: public wxStreamBase
{
156 wxInputStream(wxStreamBuffer
*sbuf
);
157 virtual ~wxInputStream();
162 virtual wxInputStream
& Read(void *buffer
, size_t size
);
163 wxInputStream
& Read(wxOutputStream
& stream_out
);
166 // Position functions
167 off_t
SeekI(off_t pos
, wxSeekMode mode
= wxFromStart
);
171 wxStreamBuffer
*InputStreamBuffer() { return m_i_streambuf
; }
172 size_t LastRead() { return wxStreamBase::m_lastcount
; }
175 wxInputStream
& operator>>(wxOutputStream
& out
) { return Read(out
); }
176 wxInputStream
& operator>>(wxString
& line
);
177 wxInputStream
& operator>>(char& c
);
178 wxInputStream
& operator>>(signed short& i
);
179 wxInputStream
& operator>>(signed int& i
);
180 wxInputStream
& operator>>(signed long& i
);
181 wxInputStream
& operator>>(unsigned char& c
);
182 wxInputStream
& operator>>(unsigned short& i
);
183 wxInputStream
& operator>>(unsigned int& i
);
184 wxInputStream
& operator>>(unsigned long& i
);
185 wxInputStream
& operator>>(double& i
);
186 wxInputStream
& operator>>(float& f
) { double d
; operator>>((double&)d
); f
= (float)d
; return *this; }
188 wxInputStream
& operator>>(wxObject
*& obj
);
190 wxInputStream
& operator>>( __wxInputManip func
) { return func(*this); }
194 wxStreamBuffer
*m_i_streambuf
;
197 class WXDLLEXPORT wxOutputStream
: public wxStreamBase
{
200 wxOutputStream(wxStreamBuffer
*sbuf
);
201 virtual ~wxOutputStream();
203 virtual wxOutputStream
& Write(const void *buffer
, size_t size
);
204 wxOutputStream
& Write(wxInputStream
& stream_in
);
205 void WriteLine(const wxString
& line
);
207 off_t
SeekO(off_t pos
, wxSeekMode mode
= wxFromStart
);
210 size_t LastWrite() const { return wxStreamBase::m_lastcount
; }
211 wxStreamBuffer
*OutputStreamBuffer() { return m_o_streambuf
; }
215 wxOutputStream
& operator<<(wxInputStream
& out
) { return Write(out
); }
216 wxOutputStream
& operator<<(const char *string
);
217 wxOutputStream
& operator<<(wxString
& string
);
218 wxOutputStream
& operator<<(char c
);
219 wxOutputStream
& operator<<(signed short i
);
220 wxOutputStream
& operator<<(signed int i
);
221 wxOutputStream
& operator<<(signed long i
);
222 wxOutputStream
& operator<<(unsigned char c
);
223 wxOutputStream
& operator<<(unsigned short i
);
224 wxOutputStream
& operator<<(unsigned int i
);
225 wxOutputStream
& operator<<(unsigned long i
);
226 wxOutputStream
& operator<<(double f
);
227 wxOutputStream
& operator<<(float f
) { return operator<<((double)f
); }
229 wxOutputStream
& operator<<(wxObject
& obj
);
231 wxOutputStream
& operator<<( __wxOutputManip func
) { return func(*this); }
235 wxStreamBuffer
*m_o_streambuf
;
238 // ---------------------------------------------------------------------------
240 // ---------------------------------------------------------------------------
242 class WXDLLEXPORT wxFilterInputStream
: public wxInputStream
{
244 wxFilterInputStream();
245 wxFilterInputStream(wxInputStream
& stream
);
246 ~wxFilterInputStream();
248 char Peek() { return m_parent_i_stream
->Peek(); }
250 wxStreamError
LastError() const { return m_parent_i_stream
->LastError(); }
251 size_t StreamSize() const { return m_parent_i_stream
->StreamSize(); }
254 wxInputStream
*m_parent_i_stream
;
257 class WXDLLEXPORT wxFilterOutputStream
: public wxOutputStream
{
259 wxFilterOutputStream();
260 wxFilterOutputStream(wxOutputStream
& stream
);
261 ~wxFilterOutputStream();
263 wxStreamError
LastError() const { return m_parent_o_stream
->LastError(); }
264 size_t StreamSize() const { return m_parent_o_stream
->StreamSize(); }
267 wxOutputStream
*m_parent_o_stream
;