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 wxOutputStream
& WXDLLEXPORT
wxEndL(wxOutputStream
& o_stream
);
33 // ---------------------------------------------------------------------------
35 // ---------------------------------------------------------------------------
37 class WXDLLEXPORT wxStreamBuffer
{
40 read
, write
, read_write
46 wxStreamBuffer(wxStreamBase
& stream
, BufMode mode
);
47 wxStreamBuffer(BufMode mode
);
48 wxStreamBuffer(const wxStreamBuffer
& buf
);
54 void Read(void *buffer
, size_t size
);
55 void Write(const void *buffer
, size_t size
);
56 bool WriteBack(const char *buffer
, size_t size
);
57 bool WriteBack(char c
);
61 off_t
Seek(off_t pos
, wxSeekMode mode
);
67 void SetBufferIO(char *buffer_start
, char *buffer_end
);
68 void SetBufferIO(size_t bufsize
);
69 char *GetBufferStart() const { return m_buffer_start
; }
70 char *GetBufferEnd() const { return m_buffer_end
; }
71 char *GetBufferPos() const { return m_buffer_pos
; }
72 off_t
GetIntPosition() const { return m_buffer_pos
-m_buffer_start
; }
73 void SetIntPosition(off_t pos
) { m_buffer_pos
= m_buffer_start
+pos
; }
74 size_t GetLastAccess() const { return m_buffer_end
-m_buffer_start
; }
76 void Fixed(bool fixed
) { m_fixed
= fixed
; }
77 void Flushable(bool f
) { m_flushable
= f
; }
81 size_t GetDataLeft() const;
84 char *AllocSpaceWBack(size_t needed_size
);
85 size_t GetWBack(char *buf
, size_t bsize
);
87 void GetFromBuffer(void *buffer
, size_t size
);
88 void PutToBuffer(const void *buffer
, size_t size
);
91 char *m_buffer_start
, *m_buffer_end
, *m_buffer_pos
;
95 size_t m_wbacksize
, m_wbackcur
;
97 bool m_fixed
, m_flushable
;
99 wxStreamBase
*m_stream
;
104 // ---------------------------------------------------------------------------
105 // wxStream: base classes
106 // ---------------------------------------------------------------------------
113 class WXDLLEXPORT wxStreamBase
{
116 virtual ~wxStreamBase();
118 wxStreamError
LastError() const { return m_lasterror
; }
119 virtual size_t StreamSize() const { return ~((size_t)0); }
122 friend class wxStreamBuffer
;
124 virtual size_t OnSysRead(void *buffer
, size_t bufsize
);
125 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
126 virtual off_t
OnSysSeek(off_t seek
, wxSeekMode mode
);
127 virtual off_t
OnSysTell() const;
131 wxStreamError m_lasterror
;
134 class WXDLLEXPORT wxInputStream
: public wxStreamBase
{
137 wxInputStream(wxStreamBuffer
*sbuf
);
138 virtual ~wxInputStream();
143 wxInputStream
& Read(void *buffer
, size_t size
);
144 wxInputStream
& Read(wxOutputStream
& stream_out
);
146 // Position functions
147 off_t
SeekI(off_t pos
, wxSeekMode mode
= wxFromStart
);
151 wxStreamBuffer
*InputStreamBuffer() { return m_i_streambuf
; }
152 size_t LastRead() { return wxStreamBase::m_lastcount
; }
155 wxInputStream
& operator>>(wxOutputStream
& out
) { return Read(out
); }
156 wxInputStream
& operator>>(wxString
& line
);
157 wxInputStream
& operator>>(char& c
);
158 wxInputStream
& operator>>(short& i
);
159 wxInputStream
& operator>>(int& i
);
160 wxInputStream
& operator>>(long& i
);
161 wxInputStream
& operator>>(double& i
);
163 wxInputStream
& operator>>(wxObject
*& obj
);
166 wxInputStream
& operator>>(float& f
) { double d
; operator>>((double&)d
); f
= (float)d
; return *this; }
167 wxInputStream
& operator>>(unsigned char& c
) { return operator>>((char&)c
); }
168 wxInputStream
& operator>>(unsigned short& i
) { return operator>>((short&)i
); }
169 wxInputStream
& operator>>(unsigned int& i
) { return operator>>((int&)i
); }
170 wxInputStream
& operator>>(unsigned long& i
) { return operator>>((long&)i
); }
171 wxInputStream
& operator>>( __wxInputManip func
) { return func(*this); }
175 wxStreamBuffer
*m_i_streambuf
;
178 class WXDLLEXPORT wxOutputStream
: public wxStreamBase
{
181 wxOutputStream(wxStreamBuffer
*sbuf
);
182 virtual ~wxOutputStream();
184 wxOutputStream
& Write(const void *buffer
, size_t size
);
185 wxOutputStream
& Write(wxInputStream
& stream_in
);
187 off_t
SeekO(off_t pos
, wxSeekMode mode
= wxFromStart
);
190 size_t LastWrite() const { return wxStreamBase::m_lastcount
; }
191 wxStreamBuffer
*OutputStreamBuffer() { return m_o_streambuf
; }
195 wxOutputStream
& operator<<(wxInputStream
& out
) { return Write(out
); }
196 wxOutputStream
& operator<<(const char *string
);
197 wxOutputStream
& operator<<(wxString
& string
);
198 wxOutputStream
& operator<<(char c
);
199 wxOutputStream
& operator<<(short i
);
200 wxOutputStream
& operator<<(int i
);
201 wxOutputStream
& operator<<(long i
);
202 wxOutputStream
& operator<<(double f
);
204 wxOutputStream
& operator<<(wxObject
& obj
);
207 wxOutputStream
& operator<<(float f
) { return operator<<((double)f
); }
208 wxOutputStream
& operator<<(unsigned char c
) { return operator<<((char)c
); }
209 wxOutputStream
& operator<<(unsigned short i
) { return operator<<((short)i
); }
210 wxOutputStream
& operator<<(unsigned int i
) { return operator<<((int)i
); }
211 wxOutputStream
& operator<<(unsigned long i
) { return operator<<((long)i
); }
212 wxOutputStream
& operator<<( __wxOutputManip func
) { return func(*this); }
216 wxStreamBuffer
*m_o_streambuf
;
219 // ---------------------------------------------------------------------------
221 // ---------------------------------------------------------------------------
223 class WXDLLEXPORT wxFilterInputStream
: public wxInputStream
{
225 wxFilterInputStream();
226 wxFilterInputStream(wxInputStream
& stream
);
227 ~wxFilterInputStream();
229 char Peek() { return m_parent_i_stream
->Peek(); }
231 wxStreamError
LastError() const { return m_parent_i_stream
->LastError(); }
232 size_t StreamSize() const { return m_parent_i_stream
->StreamSize(); }
235 wxInputStream
*m_parent_i_stream
;
238 class WXDLLEXPORT wxFilterOutputStream
: public wxOutputStream
{
240 wxFilterOutputStream();
241 wxFilterOutputStream(wxOutputStream
& stream
);
242 ~wxFilterOutputStream();
244 wxStreamError
LastError() const { return m_parent_o_stream
->LastError(); }
245 size_t StreamSize() const { return m_parent_o_stream
->StreamSize(); }
248 wxOutputStream
*m_parent_o_stream
;