]>
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 | |
657d2097 | 45 | |
75ed1d15 | 46 | typedef enum { |
c7a9fa36 RR |
47 | wxSTREAM_NO_ERROR = 0, |
48 | wxSTREAM_NO_ERR = wxSTREAM_NO_ERROR, | |
49 | wxSTREAM_NOERROR = wxSTREAM_NO_ERROR, | |
657d2097 | 50 | |
c7a9fa36 | 51 | wxSTREAM_EOF, |
657d2097 | 52 | |
c7a9fa36 RR |
53 | wxSTREAM_WRITE_ERROR, |
54 | wxSTREAM_WRITE_ERR = wxSTREAM_WRITE_ERROR, | |
657d2097 | 55 | |
c7a9fa36 | 56 | wxSTREAM_READ_ERROR, |
657d2097 DW |
57 | wxSTREAM_READ_ERR = wxSTREAM_READ_ERROR |
58 | ||
75ed1d15 GL |
59 | } wxStreamError; |
60 | ||
657d2097 | 61 | class WXDLLEXPORT wxStreamBase |
c7a9fa36 RR |
62 | { |
63 | public: | |
64 | wxStreamBase(); | |
65 | virtual ~wxStreamBase(); | |
75ed1d15 | 66 | |
c7a9fa36 RR |
67 | bool operator!() const { return (LastError() != wxSTREAM_NOERROR); } |
68 | wxStreamError LastError() const { return m_lasterror; } | |
69 | virtual size_t GetSize() const { return ~((size_t)0); } | |
70 | size_t StreamSize() const { return GetSize(); } | |
75ed1d15 | 71 | |
c7a9fa36 RR |
72 | protected: |
73 | virtual size_t OnSysRead(void *buffer, size_t bufsize); | |
74 | virtual size_t OnSysWrite(const void *buffer, size_t bufsize); | |
75 | virtual off_t OnSysSeek(off_t seek, wxSeekMode mode); | |
76 | virtual off_t OnSysTell() const; | |
75ed1d15 | 77 | |
c7a9fa36 | 78 | friend class wxStreamBuffer; |
75ed1d15 | 79 | |
c7a9fa36 RR |
80 | size_t m_lastcount; |
81 | wxStreamError m_lasterror; | |
75ed1d15 GL |
82 | }; |
83 | ||
657d2097 | 84 | class WXDLLEXPORT wxInputStream: public wxStreamBase |
c7a9fa36 RR |
85 | { |
86 | public: | |
87 | wxInputStream(); | |
88 | virtual ~wxInputStream(); | |
32fc4afb | 89 | |
c7a9fa36 RR |
90 | // IO functions |
91 | virtual char Peek(); | |
92 | char GetC(); | |
93 | virtual wxInputStream& Read(void *buffer, size_t size); | |
94 | wxInputStream& Read(wxOutputStream& stream_out); | |
32fc4afb | 95 | |
c7a9fa36 RR |
96 | // Position functions |
97 | virtual off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart); | |
98 | virtual off_t TellI() const; | |
32fc4afb | 99 | |
c7a9fa36 RR |
100 | // State functions |
101 | virtual size_t LastRead() { return wxStreamBase::m_lastcount; } | |
fae05df5 | 102 | |
c7a9fa36 RR |
103 | // Ungetch |
104 | size_t Ungetch(const void *buffer, size_t size); | |
105 | bool Ungetch(char c); | |
0cd9bfe8 | 106 | |
c7a9fa36 RR |
107 | // Operators |
108 | wxInputStream& operator>>(wxOutputStream& out) { return Read(out); } | |
47d67540 | 109 | #if wxUSE_SERIAL |
c7a9fa36 | 110 | wxInputStream& operator>>(wxObject *& obj); |
fcc6dddd | 111 | #endif |
c7a9fa36 | 112 | wxInputStream& operator>>( __wxInputManip func) { return func(*this); } |
fae05df5 | 113 | |
c7a9fa36 RR |
114 | protected: |
115 | // Ungetch managers | |
116 | char *m_wback; | |
117 | size_t m_wbacksize; | |
118 | size_t m_wbackcur; | |
fae05df5 | 119 | |
c7a9fa36 RR |
120 | char *AllocSpaceWBack(size_t needed_size); |
121 | size_t GetWBack(char *buf, size_t bsize); | |
32fc4afb GL |
122 | }; |
123 | ||
657d2097 | 124 | class WXDLLEXPORT wxOutputStream: public wxStreamBase |
c7a9fa36 RR |
125 | { |
126 | public: | |
127 | wxOutputStream(); | |
128 | virtual ~wxOutputStream(); | |
32fc4afb | 129 | |
c7a9fa36 RR |
130 | void PutC(char c); |
131 | virtual wxOutputStream& Write(const void *buffer, size_t size); | |
132 | wxOutputStream& Write(wxInputStream& stream_in); | |
32fc4afb | 133 | |
c7a9fa36 RR |
134 | virtual off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart); |
135 | virtual off_t TellO() const; | |
1678ad78 | 136 | |
c7a9fa36 | 137 | virtual size_t LastWrite() const { return wxStreamBase::m_lastcount; } |
32fc4afb | 138 | |
c7a9fa36 | 139 | virtual void Sync(); |
32fc4afb | 140 | |
c7a9fa36 | 141 | wxOutputStream& operator<<(wxInputStream& out) { return Write(out); } |
47d67540 | 142 | #if wxUSE_SERIAL |
c7a9fa36 | 143 | wxOutputStream& operator<<(wxObject& obj); |
fcc6dddd | 144 | #endif |
c7a9fa36 | 145 | wxOutputStream& operator<<( __wxOutputManip func) { return func(*this); } |
32fc4afb GL |
146 | }; |
147 | ||
e2acb9ae RR |
148 | // --------------------------------------------------------------------------- |
149 | // A stream for measuring streamed output | |
150 | // --------------------------------------------------------------------------- | |
151 | ||
88413fec | 152 | class WXDLLEXPORT wxCountingOutputStream: public wxOutputStream |
c7a9fa36 RR |
153 | { |
154 | public: | |
155 | wxCountingOutputStream(); | |
e2acb9ae | 156 | |
c7a9fa36 RR |
157 | size_t GetSize() const; |
158 | bool Ok() const { return TRUE; } | |
e2acb9ae | 159 | |
c7a9fa36 RR |
160 | protected: |
161 | size_t OnSysWrite(const void *buffer, size_t size); | |
162 | off_t OnSysSeek(off_t pos, wxSeekMode mode); | |
163 | off_t OnSysTell() const; | |
e2acb9ae | 164 | |
c7a9fa36 | 165 | size_t m_currentPos; |
e2acb9ae RR |
166 | }; |
167 | ||
f4ada568 GL |
168 | // --------------------------------------------------------------------------- |
169 | // "Filter" streams | |
170 | // --------------------------------------------------------------------------- | |
32fc4afb | 171 | |
657d2097 | 172 | class WXDLLEXPORT wxFilterInputStream: public wxInputStream |
c7a9fa36 RR |
173 | { |
174 | public: | |
175 | wxFilterInputStream(); | |
176 | wxFilterInputStream(wxInputStream& stream); | |
177 | ~wxFilterInputStream(); | |
32fc4afb | 178 | |
c7a9fa36 | 179 | char Peek() { return m_parent_i_stream->Peek(); } |
32fc4afb | 180 | |
c7a9fa36 | 181 | size_t GetSize() const { return m_parent_i_stream->GetSize(); } |
84b46c35 | 182 | |
c7a9fa36 RR |
183 | protected: |
184 | wxInputStream *m_parent_i_stream; | |
32fc4afb GL |
185 | }; |
186 | ||
657d2097 | 187 | class WXDLLEXPORT wxFilterOutputStream: public wxOutputStream |
c7a9fa36 RR |
188 | { |
189 | public: | |
190 | wxFilterOutputStream(); | |
191 | wxFilterOutputStream(wxOutputStream& stream); | |
192 | ~wxFilterOutputStream(); | |
1678ad78 | 193 | |
c7a9fa36 | 194 | size_t GetSize() const { return m_parent_o_stream->GetSize(); } |
84b46c35 | 195 | |
c7a9fa36 RR |
196 | protected: |
197 | wxOutputStream *m_parent_o_stream; | |
32fc4afb GL |
198 | }; |
199 | ||
fae05df5 GL |
200 | // --------------------------------------------------------------------------- |
201 | // Stream buffer | |
202 | // --------------------------------------------------------------------------- | |
203 | ||
657d2097 | 204 | class WXDLLEXPORT wxStreamBuffer |
c7a9fa36 RR |
205 | { |
206 | public: | |
207 | typedef enum { | |
208 | read = 0, write, read_write | |
209 | } BufMode; | |
210 | ||
211 | wxStreamBuffer(wxStreamBase& stream, BufMode mode); | |
212 | wxStreamBuffer(BufMode mode); | |
213 | wxStreamBuffer(const wxStreamBuffer& buf); | |
214 | ~wxStreamBuffer(); | |
215 | ||
216 | // Filtered IO | |
217 | size_t Read(void *buffer, size_t size); | |
218 | size_t Read(wxStreamBuffer *buf); | |
219 | size_t Write(const void *buffer, size_t size); | |
220 | size_t Write(wxStreamBuffer *buf); | |
221 | ||
222 | char Peek(); | |
223 | char GetChar(); | |
224 | void PutChar(char c); | |
225 | off_t Tell() const; | |
226 | off_t Seek(off_t pos, wxSeekMode mode); | |
227 | ||
228 | // Buffer control | |
229 | void ResetBuffer(); | |
230 | void SetBufferIO(char *buffer_start, char *buffer_end); | |
231 | void SetBufferIO(size_t bufsize); | |
232 | char *GetBufferStart() const { return m_buffer_start; } | |
233 | char *GetBufferEnd() const { return m_buffer_end; } | |
234 | char *GetBufferPos() const { return m_buffer_pos; } | |
235 | off_t GetIntPosition() const { return m_buffer_pos-m_buffer_start; } | |
236 | void SetIntPosition(off_t pos) { m_buffer_pos = m_buffer_start+pos; } | |
237 | size_t GetLastAccess() const { return m_buffer_end-m_buffer_start; } | |
238 | ||
239 | void Fixed(bool fixed) { m_fixed = fixed; } | |
240 | void Flushable(bool f) { m_flushable = f; } | |
241 | ||
242 | bool FlushBuffer(); | |
243 | bool FillBuffer(); | |
244 | size_t GetDataLeft(); | |
245 | ||
246 | // Misc. | |
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 | char *m_buffer_start, *m_buffer_end, *m_buffer_pos; | |
254 | size_t m_buffer_size; | |
255 | ||
256 | bool m_fixed, m_flushable; | |
257 | ||
258 | wxStreamBase *m_stream; | |
259 | BufMode m_mode; | |
260 | bool m_destroybuf, m_destroystream; | |
fae05df5 GL |
261 | }; |
262 | ||
263 | // --------------------------------------------------------------------------- | |
264 | // wxBufferedStreams | |
265 | // --------------------------------------------------------------------------- | |
266 | ||
24e148ad | 267 | class WXDLLEXPORT wxBufferedInputStream: public wxFilterInputStream |
c7a9fa36 RR |
268 | { |
269 | public: | |
270 | wxBufferedInputStream(wxInputStream& stream); | |
271 | ~wxBufferedInputStream(); | |
fae05df5 | 272 | |
c7a9fa36 RR |
273 | char Peek(); |
274 | wxInputStream& Read(void *buffer, size_t size); | |
657d2097 | 275 | |
c7a9fa36 RR |
276 | // Position functions |
277 | off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart); | |
278 | off_t TellI() const; | |
fae05df5 | 279 | |
c7a9fa36 | 280 | wxStreamBuffer *InputStreamBuffer() const { return m_i_streambuf; } |
fae05df5 | 281 | |
c7a9fa36 RR |
282 | protected: |
283 | size_t OnSysRead(void *buffer, size_t bufsize); | |
284 | off_t OnSysSeek(off_t seek, wxSeekMode mode); | |
657d2097 | 285 | off_t OnSysTell() const; |
fae05df5 | 286 | |
c7a9fa36 | 287 | wxStreamBuffer *m_i_streambuf; |
fae05df5 GL |
288 | }; |
289 | ||
24e148ad | 290 | class WXDLLEXPORT wxBufferedOutputStream: public wxFilterOutputStream |
c7a9fa36 RR |
291 | { |
292 | public: | |
293 | wxBufferedOutputStream(wxOutputStream& stream); | |
294 | ~wxBufferedOutputStream(); | |
fae05df5 | 295 | |
c7a9fa36 | 296 | wxOutputStream& Write(const void *buffer, size_t size); |
657d2097 | 297 | |
c7a9fa36 RR |
298 | // Position functions |
299 | off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart); | |
300 | off_t TellO() const; | |
fae05df5 | 301 | |
c7a9fa36 | 302 | void Sync(); |
fae05df5 | 303 | |
c7a9fa36 | 304 | size_t GetSize() const; |
657d2097 | 305 | |
c7a9fa36 | 306 | wxStreamBuffer *OutputStreamBuffer() const { return m_o_streambuf; } |
fae05df5 | 307 | |
c7a9fa36 RR |
308 | protected: |
309 | size_t OnSysWrite(const void *buffer, size_t bufsize); | |
310 | off_t OnSysSeek(off_t seek, wxSeekMode mode); | |
657d2097 | 311 | off_t OnSysTell() const; |
fae05df5 | 312 | |
c7a9fa36 | 313 | wxStreamBuffer *m_o_streambuf; |
fae05df5 GL |
314 | }; |
315 | ||
32fc4afb | 316 | #endif |
ce4169a4 RR |
317 | // wxUSE_STREAMS |
318 | ||
319 | #endif | |
320 | // _WX_WXSTREAM_H__ |