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 wxInputStream
;
25 class WXDLLEXPORT wxOutputStream
;
27 typedef wxInputStream
& (*__wxInputManip
)(wxInputStream
&);
28 typedef wxOutputStream
& (*__wxOutputManip
)(wxOutputStream
&);
30 wxOutputStream
& WXDLLEXPORT
wxEndL(wxOutputStream
& o_stream
);
32 // Disable warnings such as
33 // 'wxFilterStream' : inherits 'wxFilterInputStream::Peek' via dominance
36 #pragma warning(disable:4250)
39 // ---------------------------------------------------------------------------
41 // ---------------------------------------------------------------------------
43 class WXDLLEXPORT wxStreamBuffer
{
45 wxStreamBuffer(wxInputStream
& stream
);
46 wxStreamBuffer(wxOutputStream
& stream
);
49 void Read(void *buffer
, size_t size
);
50 void Write(const void *buffer
, size_t size
);
51 void WriteBack(char c
);
53 void SetBufferIO(char *buffer_start
, char *buffer_end
);
54 void SetBufferIO(size_t bufsize
);
57 void SetBufferPosition(char *buffer_position
)
58 { m_buffer_pos
= buffer_position
; }
59 void SetIntPosition(size_t pos
)
60 { m_buffer_pos
= m_buffer_start
+ pos
; }
61 char *GetBufferPosition() const { return m_buffer_pos
; }
62 size_t GetIntPosition() const { return m_buffer_pos
- m_buffer_start
; }
64 char *GetBufferStart() const { return m_buffer_start
; }
65 char *GetBufferEnd() const { return m_buffer_end
; }
66 size_t GetBufferSize() const { return m_buffer_size
; }
67 size_t GetLastAccess() const { return m_buffer_end
- m_buffer_start
; }
70 char *m_buffer_start
, *m_buffer_end
, *m_buffer_pos
;
73 wxInputStream
*m_istream
;
74 wxOutputStream
*m_ostream
;
77 // ---------------------------------------------------------------------------
78 // wxStream: base classes
79 // ---------------------------------------------------------------------------
81 class WXDLLEXPORT wxInputStream
{
84 virtual ~wxInputStream();
87 virtual char Peek() { return 0; }
89 virtual wxInputStream
& Read(void *buffer
, size_t size
);
90 wxInputStream
& Read(wxOutputStream
& stream_out
);
93 virtual off_t
SeekI(off_t pos
, wxSeekMode mode
= wxFromStart
);
94 virtual off_t
TellI() const;
97 bool Eof() const { return m_eof
; }
98 size_t LastRead() { return m_lastread
; }
99 wxStreamBuffer
*InputStreamBuffer() { return m_i_streambuf
; }
102 wxInputStream
& operator>>(wxOutputStream
& out
) { return Read(out
); }
103 wxInputStream
& operator>>(wxString
& line
);
104 wxInputStream
& operator>>(char& c
);
105 wxInputStream
& operator>>(short& i
);
106 wxInputStream
& operator>>(int& i
);
107 wxInputStream
& operator>>(long& i
);
108 wxInputStream
& operator>>(float& i
);
110 wxInputStream
& operator>>(wxObject
*& obj
);
113 wxInputStream
& operator>>(unsigned char& c
) { return operator>>((char&)c
); }
114 wxInputStream
& operator>>(unsigned short& i
) { return operator>>((short&)i
); }
115 wxInputStream
& operator>>(unsigned int& i
) { return operator>>((int&)i
); }
116 wxInputStream
& operator>>(unsigned long& i
) { return operator>>((long&)i
); }
117 wxInputStream
& operator>>( __wxInputManip func
) { return func(*this); }
120 friend class wxStreamBuffer
;
121 friend class wxFilterInputStream
;
123 wxInputStream(wxStreamBuffer
*buffer
);
125 virtual size_t DoRead(void *WXUNUSED(buffer
), size_t WXUNUSED(size
) )
127 virtual off_t
DoSeekInput( off_t
WXUNUSED(pos
), wxSeekMode
WXUNUSED(mode
) )
128 { return wxInvalidOffset
; }
129 virtual off_t
DoTellInput() const
130 { return wxInvalidOffset
; }
133 bool m_eof
, m_i_destroybuf
;
135 wxStreamBuffer
*m_i_streambuf
;
138 class WXDLLEXPORT wxOutputStream
{
141 virtual ~wxOutputStream();
143 virtual wxOutputStream
& Write(const void *buffer
, size_t size
);
144 wxOutputStream
& Write(wxInputStream
& stream_in
);
146 virtual off_t
SeekO(off_t pos
, wxSeekMode mode
= wxFromStart
);
147 virtual off_t
TellO() const;
149 virtual bool Bad() const { return m_bad
; }
150 virtual size_t LastWrite() const { return m_lastwrite
; }
151 wxStreamBuffer
*OutputStreamBuffer() { return m_o_streambuf
; }
155 wxOutputStream
& operator<<(wxInputStream
& out
) { return Write(out
); }
156 wxOutputStream
& operator<<(const char *string
);
157 wxOutputStream
& operator<<(wxString
& string
);
158 wxOutputStream
& operator<<(char c
);
159 wxOutputStream
& operator<<(short i
);
160 wxOutputStream
& operator<<(int i
);
161 wxOutputStream
& operator<<(long i
);
162 wxOutputStream
& operator<<(double f
);
164 wxOutputStream
& operator<<(wxObject
& obj
);
167 wxOutputStream
& operator<<(float f
) { return operator<<((double)f
); }
168 wxOutputStream
& operator<<(unsigned char c
) { return operator<<((char)c
); }
169 wxOutputStream
& operator<<(unsigned short i
) { return operator<<((short)i
); }
170 wxOutputStream
& operator<<(unsigned int i
) { return operator<<((int)i
); }
171 wxOutputStream
& operator<<(unsigned long i
) { return operator<<((long)i
); }
172 wxOutputStream
& operator<<( __wxOutputManip func
) { return func(*this); }
175 friend class wxStreamBuffer
;
176 friend class wxFilterOutputStream
;
178 wxOutputStream(wxStreamBuffer
*buffer
);
180 virtual size_t DoWrite( const void *WXUNUSED(buffer
), size_t WXUNUSED(size
) )
182 virtual off_t
DoSeekOutput( off_t
WXUNUSED(pos
), wxSeekMode
WXUNUSED(mode
) )
183 { return wxInvalidOffset
; }
184 virtual off_t
DoTellOutput() const
185 { return wxInvalidOffset
; }
188 bool m_bad
, m_o_destroybuf
;
190 wxStreamBuffer
*m_o_streambuf
;
193 class WXDLLEXPORT wxStream
: public virtual wxInputStream
,
194 public virtual wxOutputStream
200 // ---------------------------------------------------------------------------
202 // ---------------------------------------------------------------------------
204 class WXDLLEXPORT wxFilterInputStream
: public virtual wxInputStream
{
206 wxFilterInputStream();
207 wxFilterInputStream(wxInputStream
& stream
);
208 ~wxFilterInputStream();
210 char Peek() { return m_parent_i_stream
->Peek(); }
212 bool Eof() const { return m_parent_i_stream
->Eof(); }
213 size_t LastRead() const { return m_parent_i_stream
->LastRead(); }
214 off_t
TellI() const { return m_parent_i_stream
->TellI(); }
217 size_t DoRead(void *buffer
, size_t size
);
218 off_t
DoSeekInput(off_t pos
, wxSeekMode mode
);
219 off_t
DoTellInput() const;
222 wxInputStream
*m_parent_i_stream
;
225 class WXDLLEXPORT wxFilterOutputStream
: public virtual wxOutputStream
{
227 wxFilterOutputStream();
228 wxFilterOutputStream(wxOutputStream
& stream
);
229 virtual ~wxFilterOutputStream();
231 bool Bad() const { return m_parent_o_stream
->Bad(); }
232 size_t LastWrite() const { return m_parent_o_stream
->LastWrite(); }
233 off_t
TellO() const { return m_parent_o_stream
->TellO(); }
236 // The forward is implicitely done by wxStreamBuffer.
238 size_t DoWrite(const void *buffer
, size_t size
);
239 off_t
DoSeekOutput(off_t pos
, wxSeekMode mode
);
240 off_t
DoTellOutput() const;
243 wxOutputStream
*m_parent_o_stream
;
246 class WXDLLEXPORT wxFilterStream
: public wxStream
,
247 public virtual wxFilterInputStream
,
248 public virtual wxFilterOutputStream
{
250 wxFilterStream(wxStream
& stream
);
255 #pragma warning(default:4250)