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 // ---------------------------------------------------------------------------
34 // ---------------------------------------------------------------------------
36 class WXDLLEXPORT wxStreamBuffer
{
38 wxStreamBuffer(wxInputStream
& stream
);
39 wxStreamBuffer(wxOutputStream
& stream
);
42 void Read(void *buffer
, size_t size
);
43 void Write(const void *buffer
, size_t size
);
44 void WriteBack(char c
);
46 void SetBufferIO(char *buffer_start
, char *buffer_end
);
47 void SetBufferIO(size_t bufsize
);
50 void SetBufferPosition(char *buffer_position
)
51 { m_buffer_pos
= buffer_position
; }
52 void SetIntPosition(size_t pos
)
53 { m_buffer_pos
= m_buffer_start
+ pos
; }
54 char *GetBufferPosition() const { return m_buffer_pos
; }
55 size_t GetIntPosition() const { return m_buffer_pos
- m_buffer_start
; }
57 char *GetBufferStart() const { return m_buffer_start
; }
58 char *GetBufferEnd() const { return m_buffer_end
; }
59 size_t GetBufferSize() const { return m_buffer_size
; }
60 size_t GetLastAccess() const { return m_buffer_end
- m_buffer_start
; }
63 char *m_buffer_start
, *m_buffer_end
, *m_buffer_pos
;
66 wxInputStream
*m_istream
;
67 wxOutputStream
*m_ostream
;
70 // ---------------------------------------------------------------------------
71 // wxStream: base classes
72 // ---------------------------------------------------------------------------
74 class WXDLLEXPORT wxInputStream
{
77 virtual ~wxInputStream();
80 virtual char Peek() { return 0; }
82 virtual wxInputStream
& Read(void *buffer
, size_t size
);
83 wxInputStream
& Read(wxOutputStream
& stream_out
);
86 virtual off_t
SeekI(off_t pos
, wxSeekMode mode
= wxFromStart
);
87 virtual off_t
TellI() const;
90 bool Eof() const { return m_eof
; }
91 size_t LastRead() { return m_lastread
; }
92 wxStreamBuffer
*InputStreamBuffer() { return m_i_streambuf
; }
95 wxInputStream
& operator>>(wxOutputStream
& out
) { return Read(out
); }
96 wxInputStream
& operator>>(wxString
& line
);
97 wxInputStream
& operator>>(char& c
);
98 wxInputStream
& operator>>(short& i
);
99 wxInputStream
& operator>>(int& i
);
100 wxInputStream
& operator>>(long& i
);
101 wxInputStream
& operator>>(float& i
);
103 wxInputStream
& operator>>(wxObject
*& obj
);
106 wxInputStream
& operator>>(unsigned char& c
) { return operator>>((char&)c
); }
107 wxInputStream
& operator>>(unsigned short& i
) { return operator>>((short&)i
); }
108 wxInputStream
& operator>>(unsigned int& i
) { return operator>>((int&)i
); }
109 wxInputStream
& operator>>(unsigned long& i
) { return operator>>((long&)i
); }
110 wxInputStream
& operator>>( __wxInputManip func
) { return func(*this); }
113 friend class wxStreamBuffer
;
114 friend class wxFilterInputStream
;
116 wxInputStream(wxStreamBuffer
*buffer
);
118 virtual size_t DoRead(void *WXUNUSED(buffer
), size_t WXUNUSED(size
) )
120 virtual off_t
DoSeekInput( off_t
WXUNUSED(pos
), wxSeekMode
WXUNUSED(mode
) )
121 { return wxInvalidOffset
; }
122 virtual off_t
DoTellInput() const
123 { return wxInvalidOffset
; }
126 bool m_eof
, m_i_destroybuf
;
128 wxStreamBuffer
*m_i_streambuf
;
131 class WXDLLEXPORT wxOutputStream
{
134 virtual ~wxOutputStream();
136 virtual wxOutputStream
& Write(const void *buffer
, size_t size
);
137 wxOutputStream
& Write(wxInputStream
& stream_in
);
139 virtual off_t
SeekO(off_t pos
, wxSeekMode mode
= wxFromStart
);
140 virtual off_t
TellO() const;
142 virtual bool Bad() const { return m_bad
; }
143 virtual size_t LastWrite() const { return m_lastwrite
; }
144 wxStreamBuffer
*OutputStreamBuffer() { return m_o_streambuf
; }
148 wxOutputStream
& operator<<(wxInputStream
& out
) { return Write(out
); }
149 wxOutputStream
& operator<<(const char *string
);
150 wxOutputStream
& operator<<(wxString
& string
);
151 wxOutputStream
& operator<<(char c
);
152 wxOutputStream
& operator<<(short i
);
153 wxOutputStream
& operator<<(int i
);
154 wxOutputStream
& operator<<(long i
);
155 wxOutputStream
& operator<<(double f
);
157 wxOutputStream
& operator<<(wxObject
& obj
);
160 wxOutputStream
& operator<<(float f
) { return operator<<((double)f
); }
161 wxOutputStream
& operator<<(unsigned char c
) { return operator<<((char)c
); }
162 wxOutputStream
& operator<<(unsigned short i
) { return operator<<((short)i
); }
163 wxOutputStream
& operator<<(unsigned int i
) { return operator<<((int)i
); }
164 wxOutputStream
& operator<<(unsigned long i
) { return operator<<((long)i
); }
165 wxOutputStream
& operator<<( __wxOutputManip func
) { return func(*this); }
168 friend class wxStreamBuffer
;
169 friend class wxFilterOutputStream
;
171 wxOutputStream(wxStreamBuffer
*buffer
);
173 virtual size_t DoWrite( const void *WXUNUSED(buffer
), size_t WXUNUSED(size
) )
175 virtual off_t
DoSeekOutput( off_t
WXUNUSED(pos
), wxSeekMode
WXUNUSED(mode
) )
176 { return wxInvalidOffset
; }
177 virtual off_t
DoTellOutput() const
178 { return wxInvalidOffset
; }
181 bool m_bad
, m_o_destroybuf
;
183 wxStreamBuffer
*m_o_streambuf
;
186 class wxStream
: virtual public wxInputStream
,
187 virtual public wxOutputStream
193 // ---------------------------------------------------------------------------
195 // ---------------------------------------------------------------------------
197 class WXDLLEXPORT wxFilterInputStream
: virtual public wxInputStream
{
199 wxFilterInputStream();
200 wxFilterInputStream(wxInputStream
& stream
);
201 ~wxFilterInputStream();
203 char Peek() { return m_parent_i_stream
->Peek(); }
205 bool Eof() const { return m_parent_i_stream
->Eof(); }
206 size_t LastRead() const { return m_parent_i_stream
->LastRead(); }
207 off_t
TellI() const { return m_parent_i_stream
->TellI(); }
210 size_t DoRead(void *buffer
, size_t size
);
211 off_t
DoSeekInput(off_t pos
, wxSeekMode mode
);
212 off_t
DoTellInput() const;
215 wxInputStream
*m_parent_i_stream
;
218 class WXDLLEXPORT wxFilterOutputStream
: virtual public wxOutputStream
{
220 wxFilterOutputStream();
221 wxFilterOutputStream(wxOutputStream
& stream
);
222 virtual ~wxFilterOutputStream();
224 bool Bad() const { return m_parent_o_stream
->Bad(); }
225 size_t LastWrite() const { return m_parent_o_stream
->LastWrite(); }
226 off_t
TellO() const { return m_parent_o_stream
->TellO(); }
229 // The forward is implicitely done by wxStreamBuffer.
231 size_t DoWrite(const void *buffer
, size_t size
);
232 off_t
DoSeekOutput(off_t pos
, wxSeekMode mode
);
233 off_t
DoTellOutput() const;
236 wxOutputStream
*m_parent_o_stream
;
239 class WXDLLEXPORT wxFilterStream
: public wxStream
,
240 virtual public wxFilterInputStream
,
241 virtual public wxFilterOutputStream
{
243 wxFilterStream(wxStream
& stream
);