]>
Commit | Line | Data |
---|---|---|
32fc4afb GL |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: stream.h | |
3 | // Purpose: "wxWindows stream" base classes | |
4 | // Author: Guilhem Lavaux | |
5 | // Modified by: | |
6 | // Created: 11/07/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Guilhem Lavaux | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
34138703 JS |
12 | #ifndef _WX_WXSTREAM_H__ |
13 | #define _WX_WXSTREAM_H__ | |
32fc4afb GL |
14 | |
15 | #ifdef __GNUG__ | |
79c3e0e1 | 16 | #pragma interface |
32fc4afb GL |
17 | #endif |
18 | ||
ce4169a4 RR |
19 | #include "wx/defs.h" |
20 | ||
21 | #if wxUSE_STREAMS | |
22 | ||
32fc4afb | 23 | #include <stdio.h> |
45ea509a VZ |
24 | #include "wx/object.h" |
25 | #include "wx/string.h" | |
1678ad78 GL |
26 | #include "wx/filefn.h" // for off_t, wxInvalidOffset and wxSeekMode |
27 | ||
75ed1d15 | 28 | class WXDLLEXPORT wxStreamBase; |
1678ad78 GL |
29 | class WXDLLEXPORT wxInputStream; |
30 | class WXDLLEXPORT wxOutputStream; | |
31 | ||
32 | typedef wxInputStream& (*__wxInputManip)(wxInputStream&); | |
33 | typedef wxOutputStream& (*__wxOutputManip)(wxOutputStream&); | |
34 | ||
184b5d99 | 35 | WXDLLEXPORT wxOutputStream& wxEndL(wxOutputStream& o_stream); |
1678ad78 | 36 | |
f4ada568 GL |
37 | // --------------------------------------------------------------------------- |
38 | // wxStream: base classes | |
39 | // --------------------------------------------------------------------------- | |
40 | ||
23a54e14 RR |
41 | #define wxStream_NOERROR wxSTREAM_NOERROR |
42 | #define wxStream_EOF wxSTREAM_EOF | |
43 | #define wxStream_WRITE_ERR wxSTREAM_WRITE_ERROR | |
44 | #define wxStream_READ_ERR wxSTREAM_READ_ERROR | |
a533f5c1 | 45 | |
75ed1d15 | 46 | typedef enum { |
926c550d GL |
47 | wxStream_NOERROR = 0, |
48 | wxStream_EOF, | |
49 | wxStream_WRITE_ERR, | |
50 | wxStream_READ_ERR | |
75ed1d15 GL |
51 | } wxStreamError; |
52 | ||
53 | class WXDLLEXPORT wxStreamBase { | |
54 | public: | |
55 | wxStreamBase(); | |
56 | virtual ~wxStreamBase(); | |
57 | ||
23a54e14 | 58 | bool operator!() const { return (LastError() != wxSTREAM_NOERROR); } |
84b46c35 | 59 | wxStreamError LastError() const { return m_lasterror; } |
cd25b18c | 60 | virtual size_t GetSize() const { return ~((size_t)0); } |
e179bd65 | 61 | size_t StreamSize() const { return GetSize(); } |
75ed1d15 GL |
62 | |
63 | protected: | |
75ed1d15 GL |
64 | |
65 | virtual size_t OnSysRead(void *buffer, size_t bufsize); | |
66 | virtual size_t OnSysWrite(const void *buffer, size_t bufsize); | |
67 | virtual off_t OnSysSeek(off_t seek, wxSeekMode mode); | |
84b46c35 | 68 | virtual off_t OnSysTell() const; |
75ed1d15 GL |
69 | |
70 | protected: | |
fae05df5 GL |
71 | friend class wxStreamBuffer; |
72 | ||
75ed1d15 GL |
73 | size_t m_lastcount; |
74 | wxStreamError m_lasterror; | |
75 | }; | |
76 | ||
77 | class WXDLLEXPORT wxInputStream: public wxStreamBase { | |
32fc4afb GL |
78 | public: |
79 | wxInputStream(); | |
80 | virtual ~wxInputStream(); | |
81 | ||
1678ad78 | 82 | // IO functions |
75ed1d15 GL |
83 | virtual char Peek(); |
84 | char GetC(); | |
41895a05 | 85 | virtual wxInputStream& Read(void *buffer, size_t size); |
32fc4afb GL |
86 | wxInputStream& Read(wxOutputStream& stream_out); |
87 | ||
1678ad78 | 88 | // Position functions |
fae05df5 GL |
89 | virtual off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart); |
90 | virtual off_t TellI() const; | |
32fc4afb | 91 | |
1678ad78 | 92 | // State functions |
fae05df5 GL |
93 | virtual size_t LastRead() { return wxStreamBase::m_lastcount; } |
94 | ||
95 | // Ungetch | |
8f7173ab | 96 | size_t Ungetch(const void *buffer, size_t size); |
fae05df5 | 97 | bool Ungetch(char c); |
0cd9bfe8 | 98 | |
1678ad78 | 99 | // Operators |
0cd9bfe8 | 100 | wxInputStream& operator>>(wxOutputStream& out) { return Read(out); } |
47d67540 | 101 | #if wxUSE_SERIAL |
123a7fdd | 102 | wxInputStream& operator>>(wxObject *& obj); |
fcc6dddd | 103 | #endif |
1678ad78 GL |
104 | wxInputStream& operator>>( __wxInputManip func) { return func(*this); } |
105 | ||
106 | protected: | |
fae05df5 GL |
107 | // Ungetch managers |
108 | char *m_wback; | |
109 | size_t m_wbacksize; | |
110 | size_t m_wbackcur; | |
111 | ||
112 | char *AllocSpaceWBack(size_t needed_size); | |
113 | size_t GetWBack(char *buf, size_t bsize); | |
114 | ||
32fc4afb GL |
115 | }; |
116 | ||
75ed1d15 | 117 | class WXDLLEXPORT wxOutputStream: public wxStreamBase { |
32fc4afb GL |
118 | public: |
119 | wxOutputStream(); | |
120 | virtual ~wxOutputStream(); | |
121 | ||
41895a05 | 122 | virtual wxOutputStream& Write(const void *buffer, size_t size); |
32fc4afb GL |
123 | wxOutputStream& Write(wxInputStream& stream_in); |
124 | ||
fae05df5 GL |
125 | virtual off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart); |
126 | virtual off_t TellO() const; | |
1678ad78 | 127 | |
fae05df5 | 128 | virtual size_t LastWrite() const { return wxStreamBase::m_lastcount; } |
32fc4afb | 129 | |
fae05df5 | 130 | virtual void Sync(); |
32fc4afb | 131 | |
1678ad78 | 132 | wxOutputStream& operator<<(wxInputStream& out) { return Write(out); } |
47d67540 | 133 | #if wxUSE_SERIAL |
123a7fdd | 134 | wxOutputStream& operator<<(wxObject& obj); |
fcc6dddd | 135 | #endif |
1678ad78 | 136 | wxOutputStream& operator<<( __wxOutputManip func) { return func(*this); } |
32fc4afb GL |
137 | }; |
138 | ||
e2acb9ae RR |
139 | // --------------------------------------------------------------------------- |
140 | // A stream for measuring streamed output | |
141 | // --------------------------------------------------------------------------- | |
142 | ||
143 | class wxCountingOutputStream: public wxOutputStream { | |
144 | public: | |
145 | wxCountingOutputStream(); | |
146 | ||
147 | size_t GetSize() const; | |
148 | bool Ok() const { return TRUE; } | |
149 | ||
150 | protected: | |
151 | ||
152 | size_t OnSysWrite(const void *buffer, size_t size); | |
153 | off_t OnSysSeek(off_t pos, wxSeekMode mode); | |
154 | off_t OnSysTell() const; | |
155 | ||
156 | protected: | |
157 | size_t m_currentPos; | |
158 | }; | |
159 | ||
160 | ||
f4ada568 GL |
161 | // --------------------------------------------------------------------------- |
162 | // "Filter" streams | |
163 | // --------------------------------------------------------------------------- | |
32fc4afb | 164 | |
75ed1d15 | 165 | class WXDLLEXPORT wxFilterInputStream: public wxInputStream { |
32fc4afb | 166 | public: |
f4ada568 | 167 | wxFilterInputStream(); |
32fc4afb | 168 | wxFilterInputStream(wxInputStream& stream); |
f4ada568 | 169 | ~wxFilterInputStream(); |
32fc4afb | 170 | |
f4ada568 | 171 | char Peek() { return m_parent_i_stream->Peek(); } |
32fc4afb | 172 | |
84b46c35 | 173 | wxStreamError LastError() const { return m_parent_i_stream->LastError(); } |
cd25b18c | 174 | size_t GetSize() const { return m_parent_i_stream->GetSize(); } |
84b46c35 | 175 | |
32fc4afb GL |
176 | protected: |
177 | wxInputStream *m_parent_i_stream; | |
178 | }; | |
179 | ||
75ed1d15 | 180 | class WXDLLEXPORT wxFilterOutputStream: public wxOutputStream { |
32fc4afb | 181 | public: |
f4ada568 | 182 | wxFilterOutputStream(); |
32fc4afb | 183 | wxFilterOutputStream(wxOutputStream& stream); |
75ed1d15 | 184 | ~wxFilterOutputStream(); |
1678ad78 | 185 | |
84b46c35 | 186 | wxStreamError LastError() const { return m_parent_o_stream->LastError(); } |
cd25b18c | 187 | size_t GetSize() const { return m_parent_o_stream->GetSize(); } |
84b46c35 | 188 | |
32fc4afb GL |
189 | protected: |
190 | wxOutputStream *m_parent_o_stream; | |
191 | }; | |
192 | ||
fae05df5 GL |
193 | // --------------------------------------------------------------------------- |
194 | // Stream buffer | |
195 | // --------------------------------------------------------------------------- | |
196 | ||
197 | class WXDLLEXPORT wxStreamBuffer { | |
198 | public: | |
199 | typedef enum { | |
200 | read = 0, write, read_write | |
201 | } BufMode; | |
202 | ||
203 | // ----------- | |
204 | // ctor & dtor | |
205 | // ----------- | |
206 | wxStreamBuffer(wxStreamBase& stream, BufMode mode); | |
207 | wxStreamBuffer(BufMode mode); | |
208 | wxStreamBuffer(const wxStreamBuffer& buf); | |
209 | ~wxStreamBuffer(); | |
210 | ||
211 | // ----------- | |
212 | // Filtered IO | |
213 | // ----------- | |
214 | size_t Read(void *buffer, size_t size); | |
215 | size_t Read(wxStreamBuffer *buf); | |
216 | size_t Write(const void *buffer, size_t size); | |
217 | size_t Write(wxStreamBuffer *buf); | |
218 | ||
219 | char GetChar(); | |
220 | void PutChar(char c); | |
221 | off_t Tell() const; | |
222 | off_t Seek(off_t pos, wxSeekMode mode); | |
223 | ||
224 | // -------------- | |
225 | // Buffer control | |
226 | // -------------- | |
227 | void ResetBuffer(); | |
228 | void SetBufferIO(char *buffer_start, char *buffer_end); | |
229 | void SetBufferIO(size_t bufsize); | |
230 | char *GetBufferStart() const { return m_buffer_start; } | |
231 | char *GetBufferEnd() const { return m_buffer_end; } | |
232 | char *GetBufferPos() const { return m_buffer_pos; } | |
233 | off_t GetIntPosition() const { return m_buffer_pos-m_buffer_start; } | |
234 | void SetIntPosition(off_t pos) { m_buffer_pos = m_buffer_start+pos; } | |
235 | size_t GetLastAccess() const { return m_buffer_end-m_buffer_start; } | |
236 | ||
237 | void Fixed(bool fixed) { m_fixed = fixed; } | |
238 | void Flushable(bool f) { m_flushable = f; } | |
239 | ||
240 | bool FlushBuffer(); | |
241 | bool FillBuffer(); | |
242 | size_t GetDataLeft(); | |
243 | ||
244 | // -------------- | |
245 | // Administration | |
246 | // -------------- | |
247 | wxStreamBase *Stream() { return m_stream; } | |
248 | ||
249 | protected: | |
250 | void GetFromBuffer(void *buffer, size_t size); | |
251 | void PutToBuffer(const void *buffer, size_t size); | |
252 | ||
253 | protected: | |
254 | char *m_buffer_start, *m_buffer_end, *m_buffer_pos; | |
255 | size_t m_buffer_size; | |
256 | ||
257 | char *m_wback; | |
258 | size_t m_wbacksize, m_wbackcur; | |
259 | ||
260 | bool m_fixed, m_flushable; | |
261 | ||
262 | wxStreamBase *m_stream; | |
263 | BufMode m_mode; | |
264 | bool m_destroybuf, m_destroystream; | |
265 | }; | |
266 | ||
267 | // --------------------------------------------------------------------------- | |
268 | // wxBufferedStreams | |
269 | // --------------------------------------------------------------------------- | |
270 | ||
271 | class wxBufferedInputStream: public wxFilterInputStream { | |
272 | public: | |
273 | wxBufferedInputStream(wxInputStream& stream); | |
274 | ~wxBufferedInputStream(); | |
275 | ||
276 | wxInputStream& Read(void *buffer, size_t size); | |
277 | ||
278 | // Position functions | |
279 | off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart); | |
280 | off_t TellI() const; | |
281 | ||
282 | wxStreamBuffer *InputStreamBuffer() const { return m_i_streambuf; } | |
283 | ||
284 | protected: | |
285 | size_t OnSysRead(void *buffer, size_t bufsize); | |
286 | off_t OnSysSeek(off_t seek, wxSeekMode mode); | |
287 | off_t OnSysTell() const; | |
288 | ||
289 | protected: | |
290 | wxStreamBuffer *m_i_streambuf; | |
291 | }; | |
292 | ||
293 | class wxBufferedOutputStream: public wxFilterOutputStream { | |
294 | public: | |
295 | wxBufferedOutputStream(wxOutputStream& stream); | |
296 | ~wxBufferedOutputStream(); | |
297 | ||
298 | wxOutputStream& Write(const void *buffer, size_t size); | |
299 | ||
300 | // Position functions | |
301 | off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart); | |
302 | off_t TellO() const; | |
303 | ||
304 | void Sync(); | |
305 | ||
306 | wxStreamBuffer *OutputStreamBuffer() const { return m_o_streambuf; } | |
307 | ||
308 | protected: | |
309 | size_t OnSysWrite(const void *buffer, size_t bufsize); | |
310 | off_t OnSysSeek(off_t seek, wxSeekMode mode); | |
311 | off_t OnSysTell() const; | |
312 | ||
313 | protected: | |
314 | wxStreamBuffer *m_o_streambuf; | |
315 | }; | |
316 | ||
32fc4afb | 317 | #endif |
ce4169a4 RR |
318 | // wxUSE_STREAMS |
319 | ||
320 | #endif | |
321 | // _WX_WXSTREAM_H__ |