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
{
46 wxStreamBuffer(wxStreamBase
& stream
, BufMode mode
);
52 void Read(void *buffer
, size_t size
);
53 void Write(const void *buffer
, size_t size
);
54 bool WriteBack(const char *buffer
, size_t size
);
55 bool WriteBack(char c
);
57 off_t
Seek(off_t pos
, wxSeekMode mode
);
63 void SetBufferIO(char *buffer_start
, char *buffer_end
);
64 void SetBufferIO(size_t bufsize
);
65 char *GetBufferStart() const { return m_buffer_start
; }
66 char *GetBufferEnd() const { return m_buffer_end
; }
67 char *GetBufferPos() const { return m_buffer_pos
; }
68 off_t
GetIntPosition() const { return m_buffer_pos
-m_buffer_start
; }
69 void SetIntPosition(off_t pos
) { m_buffer_pos
= m_buffer_start
+pos
; }
70 size_t GetLastAccess() const { return m_buffer_end
-m_buffer_start
; }
71 void Fixed(bool fixed
) { m_fixed
= fixed
; }
75 size_t GetDataLeft() const;
78 char *AllocSpaceWBack(size_t needed_size
);
79 size_t GetWBack(char *buf
, size_t bsize
);
81 void GetFromBuffer(void *buffer
, size_t size
);
82 void PutToBuffer(const void *buffer
, size_t size
);
85 char *m_buffer_start
, *m_buffer_end
, *m_buffer_pos
;
89 size_t m_wbacksize
, m_wbackcur
;
93 wxStreamBase
*m_stream
;
97 // ---------------------------------------------------------------------------
98 // wxStream: base classes
99 // ---------------------------------------------------------------------------
106 class WXDLLEXPORT wxStreamBase
{
109 virtual ~wxStreamBase();
111 wxStreamError
LastError() { return m_lasterror
; }
114 friend class wxStreamBuffer
;
116 virtual size_t OnSysRead(void *buffer
, size_t bufsize
);
117 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
118 virtual off_t
OnSysSeek(off_t seek
, wxSeekMode mode
);
119 virtual off_t
OnSysTell();
123 wxStreamError m_lasterror
;
126 class WXDLLEXPORT wxInputStream
: public wxStreamBase
{
129 wxInputStream(wxStreamBuffer
*sbuf
);
130 virtual ~wxInputStream();
135 wxInputStream
& Read(void *buffer
, size_t size
);
136 wxInputStream
& Read(wxOutputStream
& stream_out
);
138 // Position functions
139 off_t
SeekI(off_t pos
, wxSeekMode mode
= wxFromStart
);
143 wxStreamBuffer
*InputStreamBuffer() { return m_i_streambuf
; }
144 size_t LastRead() { return wxStreamBase::m_lastcount
; }
147 wxInputStream
& operator>>(wxOutputStream
& out
) { return Read(out
); }
148 wxInputStream
& operator>>(wxString
& line
);
149 wxInputStream
& operator>>(char& c
);
150 wxInputStream
& operator>>(short& i
);
151 wxInputStream
& operator>>(int& i
);
152 wxInputStream
& operator>>(long& i
);
153 wxInputStream
& operator>>(double& i
);
155 wxInputStream
& operator>>(wxObject
*& obj
);
158 wxInputStream
& operator>>(float& f
) { double d
; operator>>((double&)d
); f
= (float)d
; return *this; }
159 wxInputStream
& operator>>(unsigned char& c
) { return operator>>((char&)c
); }
160 wxInputStream
& operator>>(unsigned short& i
) { return operator>>((short&)i
); }
161 wxInputStream
& operator>>(unsigned int& i
) { return operator>>((int&)i
); }
162 wxInputStream
& operator>>(unsigned long& i
) { return operator>>((long&)i
); }
163 wxInputStream
& operator>>( __wxInputManip func
) { return func(*this); }
167 wxStreamBuffer
*m_i_streambuf
;
170 class WXDLLEXPORT wxOutputStream
: public wxStreamBase
{
173 wxOutputStream(wxStreamBuffer
*sbuf
);
174 virtual ~wxOutputStream();
176 wxOutputStream
& Write(const void *buffer
, size_t size
);
177 wxOutputStream
& Write(wxInputStream
& stream_in
);
179 off_t
SeekO(off_t pos
, wxSeekMode mode
= wxFromStart
);
182 size_t LastWrite() const { return wxStreamBase::m_lastcount
; }
183 wxStreamBuffer
*OutputStreamBuffer() { return m_o_streambuf
; }
187 wxOutputStream
& operator<<(wxInputStream
& out
) { return Write(out
); }
188 wxOutputStream
& operator<<(const char *string
);
189 wxOutputStream
& operator<<(wxString
& string
);
190 wxOutputStream
& operator<<(char c
);
191 wxOutputStream
& operator<<(short i
);
192 wxOutputStream
& operator<<(int i
);
193 wxOutputStream
& operator<<(long i
);
194 wxOutputStream
& operator<<(double f
);
196 wxOutputStream
& operator<<(wxObject
& obj
);
199 wxOutputStream
& operator<<(float f
) { return operator<<((double)f
); }
200 wxOutputStream
& operator<<(unsigned char c
) { return operator<<((char)c
); }
201 wxOutputStream
& operator<<(unsigned short i
) { return operator<<((short)i
); }
202 wxOutputStream
& operator<<(unsigned int i
) { return operator<<((int)i
); }
203 wxOutputStream
& operator<<(unsigned long i
) { return operator<<((long)i
); }
204 wxOutputStream
& operator<<( __wxOutputManip func
) { return func(*this); }
208 wxStreamBuffer
*m_o_streambuf
;
211 // ---------------------------------------------------------------------------
213 // ---------------------------------------------------------------------------
215 class WXDLLEXPORT wxFilterInputStream
: public wxInputStream
{
217 wxFilterInputStream();
218 wxFilterInputStream(wxInputStream
& stream
);
219 ~wxFilterInputStream();
221 char Peek() { return m_parent_i_stream
->Peek(); }
224 wxInputStream
*m_parent_i_stream
;
227 class WXDLLEXPORT wxFilterOutputStream
: public wxOutputStream
{
229 wxFilterOutputStream();
230 wxFilterOutputStream(wxOutputStream
& stream
);
231 ~wxFilterOutputStream();
234 wxOutputStream
*m_parent_o_stream
;