]>
Commit | Line | Data |
---|---|---|
32fc4afb | 1 | ///////////////////////////////////////////////////////////////////////////// |
49e399d8 | 2 | // Name: wx/stream.h |
2b5f62a0 VZ |
3 | // Purpose: stream classes |
4 | // Author: Guilhem Lavaux, Guillermo Rodriguez Garcia, Vadim Zeitlin | |
32fc4afb GL |
5 | // Modified by: |
6 | // Created: 11/07/98 | |
32fc4afb | 7 | // Copyright: (c) Guilhem Lavaux |
65571936 | 8 | // Licence: wxWindows licence |
32fc4afb GL |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
34138703 JS |
11 | #ifndef _WX_WXSTREAM_H__ |
12 | #define _WX_WXSTREAM_H__ | |
32fc4afb | 13 | |
ce4169a4 RR |
14 | #include "wx/defs.h" |
15 | ||
16 | #if wxUSE_STREAMS | |
17 | ||
32fc4afb | 18 | #include <stdio.h> |
45ea509a VZ |
19 | #include "wx/object.h" |
20 | #include "wx/string.h" | |
30984dea | 21 | #include "wx/filefn.h" // for wxFileOffset, wxInvalidOffset and wxSeekMode |
1678ad78 | 22 | |
b5dbe15d VS |
23 | class WXDLLIMPEXP_FWD_BASE wxStreamBase; |
24 | class WXDLLIMPEXP_FWD_BASE wxInputStream; | |
25 | class WXDLLIMPEXP_FWD_BASE wxOutputStream; | |
1678ad78 GL |
26 | |
27 | typedef wxInputStream& (*__wxInputManip)(wxInputStream&); | |
28 | typedef wxOutputStream& (*__wxOutputManip)(wxOutputStream&); | |
29 | ||
bddd7a8d | 30 | WXDLLIMPEXP_BASE wxOutputStream& wxEndL(wxOutputStream& o_stream); |
1678ad78 | 31 | |
2b5f62a0 VZ |
32 | // ---------------------------------------------------------------------------- |
33 | // constants | |
34 | // ---------------------------------------------------------------------------- | |
f4ada568 | 35 | |
49e399d8 | 36 | enum wxStreamError |
cd6ce4a9 | 37 | { |
2b5f62a0 VZ |
38 | wxSTREAM_NO_ERROR = 0, // stream is in good state |
39 | wxSTREAM_EOF, // EOF reached in Read() or similar | |
40 | wxSTREAM_WRITE_ERROR, // generic write error | |
41 | wxSTREAM_READ_ERROR // generic read error | |
49e399d8 | 42 | }; |
75ed1d15 | 43 | |
6ea48c51 MW |
44 | const int wxEOF = -1; |
45 | ||
2b5f62a0 VZ |
46 | // ============================================================================ |
47 | // base stream classes: wxInputStream and wxOutputStream | |
48 | // ============================================================================ | |
49 | ||
50 | // --------------------------------------------------------------------------- | |
51 | // wxStreamBase: common (but non virtual!) base for all stream classes | |
52 | // --------------------------------------------------------------------------- | |
cd6ce4a9 | 53 | |
6285e36b | 54 | class WXDLLIMPEXP_BASE wxStreamBase : public wxObject |
c7a9fa36 RR |
55 | { |
56 | public: | |
57 | wxStreamBase(); | |
58 | virtual ~wxStreamBase(); | |
75ed1d15 | 59 | |
cd6ce4a9 | 60 | // error testing |
cd6ce4a9 | 61 | wxStreamError GetLastError() const { return m_lasterror; } |
d65fd4e9 | 62 | virtual bool IsOk() const { return GetLastError() == wxSTREAM_NO_ERROR; } |
2b5f62a0 | 63 | bool operator!() const { return !IsOk(); } |
cd6ce4a9 | 64 | |
2b5f62a0 | 65 | // reset the stream state |
90693f47 | 66 | void Reset(wxStreamError error = wxSTREAM_NO_ERROR) { m_lasterror = error; } |
2b5f62a0 | 67 | |
f6e7cd0a | 68 | // this doesn't make sense for all streams, always test its return value |
588066b7 VZ |
69 | virtual size_t GetSize() const; |
70 | virtual wxFileOffset GetLength() const { return wxInvalidOffset; } | |
47fc03d2 | 71 | |
3c70014d MW |
72 | // returns true if the streams supports seeking to arbitrary offsets |
73 | virtual bool IsSeekable() const { return false; } | |
74 | ||
c7a9fa36 | 75 | protected: |
4004775e RL |
76 | virtual wxFileOffset OnSysSeek(wxFileOffset seek, wxSeekMode mode); |
77 | virtual wxFileOffset OnSysTell() const; | |
75ed1d15 | 78 | |
c7a9fa36 RR |
79 | size_t m_lastcount; |
80 | wxStreamError m_lasterror; | |
2b5f62a0 VZ |
81 | |
82 | friend class wxStreamBuffer; | |
22f3361e | 83 | |
6285e36b | 84 | DECLARE_ABSTRACT_CLASS(wxStreamBase) |
c0c133e1 | 85 | wxDECLARE_NO_COPY_CLASS(wxStreamBase); |
75ed1d15 GL |
86 | }; |
87 | ||
2b5f62a0 VZ |
88 | // ---------------------------------------------------------------------------- |
89 | // wxInputStream: base class for the input streams | |
90 | // ---------------------------------------------------------------------------- | |
91 | ||
bddd7a8d | 92 | class WXDLLIMPEXP_BASE wxInputStream : public wxStreamBase |
c7a9fa36 RR |
93 | { |
94 | public: | |
2b5f62a0 | 95 | // ctor and dtor, nothing exciting |
c7a9fa36 RR |
96 | wxInputStream(); |
97 | virtual ~wxInputStream(); | |
32fc4afb | 98 | |
cd6ce4a9 | 99 | |
c7a9fa36 | 100 | // IO functions |
2b5f62a0 VZ |
101 | // ------------ |
102 | ||
103 | // return a character from the stream without removing it, i.e. it will | |
104 | // still be returned by the next call to GetC() | |
105 | // | |
106 | // blocks until something appears in the stream if necessary, if nothing | |
107 | // ever does (i.e. EOF) LastRead() will return 0 (and the return value is | |
108 | // undefined), otherwise 1 | |
c7a9fa36 | 109 | virtual char Peek(); |
2b5f62a0 | 110 | |
6ea48c51 | 111 | // return one byte from the stream, blocking until it appears if |
2b5f62a0 VZ |
112 | // necessary |
113 | // | |
6ea48c51 MW |
114 | // on success returns a value between 0 - 255, or wxEOF on EOF or error. |
115 | int GetC(); | |
2b5f62a0 VZ |
116 | |
117 | // read at most the given number of bytes from the stream | |
118 | // | |
119 | // there are 2 possible situations here: either there is nothing at all in | |
120 | // the stream right now in which case Read() blocks until something appears | |
121 | // (use CanRead() to avoid this) or there is already some data available in | |
122 | // the stream and then Read() doesn't block but returns just the data it | |
123 | // can read without waiting for more | |
124 | // | |
125 | // in any case, if there are not enough bytes in the stream right now, | |
126 | // LastRead() value will be less than size but greater than 0. If it is 0, | |
127 | // it means that EOF has been reached. | |
c7a9fa36 | 128 | virtual wxInputStream& Read(void *buffer, size_t size); |
32fc4afb | 129 | |
cc437b96 VZ |
130 | // Read exactly the given number of bytes, unlike Read(), which may read |
131 | // less than the requested amount of data without returning an error, this | |
132 | // method either reads all the data or returns false. | |
133 | bool ReadAll(void *buffer, size_t size); | |
134 | ||
2b5f62a0 VZ |
135 | // copy the entire contents of this stream into streamOut, stopping only |
136 | // when EOF is reached or an error occurs | |
137 | wxInputStream& Read(wxOutputStream& streamOut); | |
138 | ||
139 | ||
140 | // status functions | |
141 | // ---------------- | |
142 | ||
143 | // returns the number of bytes read by the last call to Read(), GetC() or | |
144 | // Peek() | |
145 | // | |
146 | // this should be used to discover whether that call succeeded in reading | |
147 | // all the requested data or not | |
148 | virtual size_t LastRead() const { return wxStreamBase::m_lastcount; } | |
149 | ||
d775fa82 | 150 | // returns true if some data is available in the stream right now, so that |
2b5f62a0 VZ |
151 | // calling Read() wouldn't block |
152 | virtual bool CanRead() const; | |
32fc4afb | 153 | |
2b5f62a0 VZ |
154 | // is the stream at EOF? |
155 | // | |
156 | // note that this cannot be really implemented for all streams and | |
157 | // CanRead() is more reliable than Eof() | |
158 | virtual bool Eof() const; | |
159 | ||
160 | ||
161 | // write back buffer | |
162 | // ----------------- | |
fae05df5 | 163 | |
2b5f62a0 VZ |
164 | // put back the specified number of bytes into the stream, they will be |
165 | // fetched by the next call to the read functions | |
166 | // | |
167 | // returns the number of bytes really stuffed back | |
c7a9fa36 | 168 | size_t Ungetch(const void *buffer, size_t size); |
2b5f62a0 VZ |
169 | |
170 | // put back the specified character in the stream | |
171 | // | |
d775fa82 | 172 | // returns true if ok, false on error |
c7a9fa36 | 173 | bool Ungetch(char c); |
0cd9bfe8 | 174 | |
2b5f62a0 VZ |
175 | |
176 | // position functions | |
177 | // ------------------ | |
178 | ||
179 | // move the stream pointer to the given position (if the stream supports | |
180 | // it) | |
181 | // | |
182 | // returns wxInvalidOffset on error | |
4004775e | 183 | virtual wxFileOffset SeekI(wxFileOffset pos, wxSeekMode mode = wxFromStart); |
2b5f62a0 VZ |
184 | |
185 | // return the current position of the stream pointer or wxInvalidOffset | |
4004775e | 186 | virtual wxFileOffset TellI() const; |
2b5f62a0 VZ |
187 | |
188 | ||
189 | // stream-like operators | |
190 | // --------------------- | |
191 | ||
c7a9fa36 | 192 | wxInputStream& operator>>(wxOutputStream& out) { return Read(out); } |
2b5f62a0 | 193 | wxInputStream& operator>>(__wxInputManip func) { return func(*this); } |
fae05df5 | 194 | |
c7a9fa36 | 195 | protected: |
2b5f62a0 VZ |
196 | // do read up to size bytes of data into the provided buffer |
197 | // | |
3103e8a9 | 198 | // this method should return 0 if EOF has been reached or an error occurred |
2b5f62a0 VZ |
199 | // (m_lasterror should be set accordingly as well) or the number of bytes |
200 | // read | |
201 | virtual size_t OnSysRead(void *buffer, size_t size) = 0; | |
202 | ||
203 | // write-back buffer support | |
204 | // ------------------------- | |
205 | ||
206 | // return the pointer to a buffer big enough to hold sizeNeeded bytes | |
207 | char *AllocSpaceWBack(size_t sizeNeeded); | |
208 | ||
209 | // read up to size data from the write back buffer, return the number of | |
210 | // bytes read | |
211 | size_t GetWBack(void *buf, size_t size); | |
47fc03d2 | 212 | |
2b5f62a0 | 213 | // write back buffer or NULL if none |
c7a9fa36 | 214 | char *m_wback; |
2b5f62a0 VZ |
215 | |
216 | // the size of the buffer | |
c7a9fa36 | 217 | size_t m_wbacksize; |
fae05df5 | 218 | |
2b5f62a0 VZ |
219 | // the current position in the buffer |
220 | size_t m_wbackcur; | |
47fc03d2 VZ |
221 | |
222 | friend class wxStreamBuffer; | |
fc7a2a60 | 223 | |
6285e36b | 224 | DECLARE_ABSTRACT_CLASS(wxInputStream) |
c0c133e1 | 225 | wxDECLARE_NO_COPY_CLASS(wxInputStream); |
32fc4afb GL |
226 | }; |
227 | ||
2b5f62a0 VZ |
228 | // ---------------------------------------------------------------------------- |
229 | // wxOutputStream: base for the output streams | |
230 | // ---------------------------------------------------------------------------- | |
231 | ||
bddd7a8d | 232 | class WXDLLIMPEXP_BASE wxOutputStream : public wxStreamBase |
c7a9fa36 RR |
233 | { |
234 | public: | |
235 | wxOutputStream(); | |
236 | virtual ~wxOutputStream(); | |
32fc4afb | 237 | |
c7a9fa36 RR |
238 | void PutC(char c); |
239 | virtual wxOutputStream& Write(const void *buffer, size_t size); | |
cc437b96 VZ |
240 | |
241 | // This is ReadAll() equivalent for Write(): it either writes exactly the | |
242 | // given number of bytes or returns false, unlike Write() which can write | |
243 | // less data than requested but still return without error. | |
244 | bool WriteAll(const void *buffer, size_t size); | |
245 | ||
c7a9fa36 | 246 | wxOutputStream& Write(wxInputStream& stream_in); |
32fc4afb | 247 | |
4004775e RL |
248 | virtual wxFileOffset SeekO(wxFileOffset pos, wxSeekMode mode = wxFromStart); |
249 | virtual wxFileOffset TellO() const; | |
1678ad78 | 250 | |
c7a9fa36 | 251 | virtual size_t LastWrite() const { return wxStreamBase::m_lastcount; } |
32fc4afb | 252 | |
c7a9fa36 | 253 | virtual void Sync(); |
8f0ff178 | 254 | virtual bool Close() { return true; } |
32fc4afb | 255 | |
c7a9fa36 | 256 | wxOutputStream& operator<<(wxInputStream& out) { return Write(out); } |
c7a9fa36 | 257 | wxOutputStream& operator<<( __wxOutputManip func) { return func(*this); } |
47fc03d2 VZ |
258 | |
259 | protected: | |
260 | // to be implemented in the derived classes (it should have been pure | |
261 | // virtual) | |
262 | virtual size_t OnSysWrite(const void *buffer, size_t bufsize); | |
263 | ||
264 | friend class wxStreamBuffer; | |
fc7a2a60 | 265 | |
6285e36b | 266 | DECLARE_ABSTRACT_CLASS(wxOutputStream) |
c0c133e1 | 267 | wxDECLARE_NO_COPY_CLASS(wxOutputStream); |
32fc4afb GL |
268 | }; |
269 | ||
2b5f62a0 VZ |
270 | // ============================================================================ |
271 | // helper stream classes | |
272 | // ============================================================================ | |
273 | ||
e2acb9ae RR |
274 | // --------------------------------------------------------------------------- |
275 | // A stream for measuring streamed output | |
276 | // --------------------------------------------------------------------------- | |
277 | ||
bddd7a8d | 278 | class WXDLLIMPEXP_BASE wxCountingOutputStream : public wxOutputStream |
c7a9fa36 RR |
279 | { |
280 | public: | |
281 | wxCountingOutputStream(); | |
e2acb9ae | 282 | |
9bc3af3e | 283 | virtual wxFileOffset GetLength() const; |
b7cacb43 | 284 | bool Ok() const { return IsOk(); } |
9bc3af3e | 285 | virtual bool IsOk() const { return true; } |
e2acb9ae | 286 | |
c7a9fa36 | 287 | protected: |
47fc03d2 | 288 | virtual size_t OnSysWrite(const void *buffer, size_t size); |
4004775e RL |
289 | virtual wxFileOffset OnSysSeek(wxFileOffset pos, wxSeekMode mode); |
290 | virtual wxFileOffset OnSysTell() const; | |
e2acb9ae | 291 | |
9bc3af3e VZ |
292 | size_t m_currentPos, |
293 | m_lastPos; | |
fc7a2a60 | 294 | |
6285e36b | 295 | DECLARE_DYNAMIC_CLASS(wxCountingOutputStream) |
c0c133e1 | 296 | wxDECLARE_NO_COPY_CLASS(wxCountingOutputStream); |
e2acb9ae RR |
297 | }; |
298 | ||
f4ada568 GL |
299 | // --------------------------------------------------------------------------- |
300 | // "Filter" streams | |
301 | // --------------------------------------------------------------------------- | |
32fc4afb | 302 | |
bddd7a8d | 303 | class WXDLLIMPEXP_BASE wxFilterInputStream : public wxInputStream |
c7a9fa36 RR |
304 | { |
305 | public: | |
306 | wxFilterInputStream(); | |
307 | wxFilterInputStream(wxInputStream& stream); | |
166c3ef0 | 308 | wxFilterInputStream(wxInputStream *stream); |
47fc03d2 | 309 | virtual ~wxFilterInputStream(); |
32fc4afb | 310 | |
c7a9fa36 | 311 | char Peek() { return m_parent_i_stream->Peek(); } |
32fc4afb | 312 | |
588066b7 | 313 | wxFileOffset GetLength() const { return m_parent_i_stream->GetLength(); } |
84b46c35 | 314 | |
47fc03d2 VZ |
315 | wxInputStream *GetFilterInputStream() const { return m_parent_i_stream; } |
316 | ||
c7a9fa36 RR |
317 | protected: |
318 | wxInputStream *m_parent_i_stream; | |
166c3ef0 | 319 | bool m_owns; |
22f3361e | 320 | |
6285e36b | 321 | DECLARE_ABSTRACT_CLASS(wxFilterInputStream) |
c0c133e1 | 322 | wxDECLARE_NO_COPY_CLASS(wxFilterInputStream); |
32fc4afb GL |
323 | }; |
324 | ||
bddd7a8d | 325 | class WXDLLIMPEXP_BASE wxFilterOutputStream : public wxOutputStream |
c7a9fa36 RR |
326 | { |
327 | public: | |
328 | wxFilterOutputStream(); | |
329 | wxFilterOutputStream(wxOutputStream& stream); | |
166c3ef0 | 330 | wxFilterOutputStream(wxOutputStream *stream); |
47fc03d2 | 331 | virtual ~wxFilterOutputStream(); |
1678ad78 | 332 | |
588066b7 | 333 | wxFileOffset GetLength() const { return m_parent_o_stream->GetLength(); } |
84b46c35 | 334 | |
47fc03d2 VZ |
335 | wxOutputStream *GetFilterOutputStream() const { return m_parent_o_stream; } |
336 | ||
166c3ef0 MW |
337 | bool Close(); |
338 | ||
c7a9fa36 RR |
339 | protected: |
340 | wxOutputStream *m_parent_o_stream; | |
166c3ef0 | 341 | bool m_owns; |
22f3361e | 342 | |
6285e36b | 343 | DECLARE_ABSTRACT_CLASS(wxFilterOutputStream) |
c0c133e1 | 344 | wxDECLARE_NO_COPY_CLASS(wxFilterOutputStream); |
32fc4afb GL |
345 | }; |
346 | ||
166c3ef0 MW |
347 | enum wxStreamProtocolType |
348 | { | |
489a164c MW |
349 | wxSTREAM_PROTOCOL, // wxFileSystem protocol (should be only one) |
350 | wxSTREAM_MIMETYPE, // MIME types the stream handles | |
351 | wxSTREAM_ENCODING, // The HTTP Content-Encodings the stream handles | |
352 | wxSTREAM_FILEEXT // File extensions the stream handles | |
166c3ef0 MW |
353 | }; |
354 | ||
355 | void WXDLLIMPEXP_BASE wxUseFilterClasses(); | |
356 | ||
58211774 | 357 | class WXDLLIMPEXP_BASE wxFilterClassFactoryBase : public wxObject |
166c3ef0 MW |
358 | { |
359 | public: | |
58211774 | 360 | virtual ~wxFilterClassFactoryBase() { } |
166c3ef0 MW |
361 | |
362 | wxString GetProtocol() const { return wxString(*GetProtocols()); } | |
a8d2cf4e | 363 | wxString PopExtension(const wxString& location) const; |
166c3ef0 MW |
364 | |
365 | virtual const wxChar * const *GetProtocols(wxStreamProtocolType type | |
366 | = wxSTREAM_PROTOCOL) const = 0; | |
367 | ||
86501081 | 368 | bool CanHandle(const wxString& protocol, |
166c3ef0 MW |
369 | wxStreamProtocolType type |
370 | = wxSTREAM_PROTOCOL) const; | |
371 | ||
58211774 | 372 | protected: |
86501081 | 373 | wxString::size_type FindExtension(const wxString& location) const; |
58211774 MW |
374 | |
375 | DECLARE_ABSTRACT_CLASS(wxFilterClassFactoryBase) | |
376 | }; | |
377 | ||
378 | class WXDLLIMPEXP_BASE wxFilterClassFactory : public wxFilterClassFactoryBase | |
379 | { | |
380 | public: | |
381 | virtual ~wxFilterClassFactory() { } | |
382 | ||
383 | virtual wxFilterInputStream *NewStream(wxInputStream& stream) const = 0; | |
384 | virtual wxFilterOutputStream *NewStream(wxOutputStream& stream) const = 0; | |
385 | virtual wxFilterInputStream *NewStream(wxInputStream *stream) const = 0; | |
386 | virtual wxFilterOutputStream *NewStream(wxOutputStream *stream) const = 0; | |
387 | ||
86501081 | 388 | static const wxFilterClassFactory *Find(const wxString& protocol, |
166c3ef0 MW |
389 | wxStreamProtocolType type |
390 | = wxSTREAM_PROTOCOL); | |
391 | ||
392 | static const wxFilterClassFactory *GetFirst(); | |
393 | const wxFilterClassFactory *GetNext() const { return m_next; } | |
394 | ||
395 | void PushFront() { Remove(); m_next = sm_first; sm_first = this; } | |
396 | void Remove(); | |
397 | ||
398 | protected: | |
399 | wxFilterClassFactory() : m_next(this) { } | |
400 | ||
401 | wxFilterClassFactory& operator=(const wxFilterClassFactory&) | |
402 | { return *this; } | |
403 | ||
404 | private: | |
405 | static wxFilterClassFactory *sm_first; | |
406 | wxFilterClassFactory *m_next; | |
407 | ||
408 | DECLARE_ABSTRACT_CLASS(wxFilterClassFactory) | |
409 | }; | |
410 | ||
2b5f62a0 VZ |
411 | // ============================================================================ |
412 | // buffered streams | |
413 | // ============================================================================ | |
414 | ||
fae05df5 | 415 | // --------------------------------------------------------------------------- |
47fc03d2 VZ |
416 | // Stream buffer: this class can be derived from and passed to |
417 | // wxBufferedStreams to implement custom buffering | |
fae05df5 GL |
418 | // --------------------------------------------------------------------------- |
419 | ||
bddd7a8d | 420 | class WXDLLIMPEXP_BASE wxStreamBuffer |
c7a9fa36 RR |
421 | { |
422 | public: | |
49e399d8 VZ |
423 | enum BufMode |
424 | { | |
425 | read, | |
426 | write, | |
427 | read_write | |
428 | }; | |
c7a9fa36 | 429 | |
f42c1512 VZ |
430 | wxStreamBuffer(wxStreamBase& stream, BufMode mode) |
431 | { | |
432 | InitWithStream(stream, mode); | |
433 | } | |
434 | ||
36f062d3 | 435 | wxStreamBuffer(size_t bufsize, wxInputStream& stream) |
f42c1512 VZ |
436 | { |
437 | InitWithStream(stream, read); | |
438 | SetBufferIO(bufsize); | |
439 | } | |
440 | ||
36f062d3 | 441 | wxStreamBuffer(size_t bufsize, wxOutputStream& stream) |
f42c1512 VZ |
442 | { |
443 | InitWithStream(stream, write); | |
444 | SetBufferIO(bufsize); | |
445 | } | |
446 | ||
c7a9fa36 | 447 | wxStreamBuffer(const wxStreamBuffer& buf); |
47fc03d2 | 448 | virtual ~wxStreamBuffer(); |
c7a9fa36 RR |
449 | |
450 | // Filtered IO | |
47fc03d2 | 451 | virtual size_t Read(void *buffer, size_t size); |
c7a9fa36 | 452 | size_t Read(wxStreamBuffer *buf); |
47fc03d2 | 453 | virtual size_t Write(const void *buffer, size_t size); |
c7a9fa36 RR |
454 | size_t Write(wxStreamBuffer *buf); |
455 | ||
47fc03d2 VZ |
456 | virtual char Peek(); |
457 | virtual char GetChar(); | |
458 | virtual void PutChar(char c); | |
4004775e RL |
459 | virtual wxFileOffset Tell() const; |
460 | virtual wxFileOffset Seek(wxFileOffset pos, wxSeekMode mode); | |
c7a9fa36 RR |
461 | |
462 | // Buffer control | |
463 | void ResetBuffer(); | |
df18cc7a | 464 | void Truncate(); |
49e399d8 VZ |
465 | |
466 | // NB: the buffer must always be allocated with malloc() if takeOwn is | |
d775fa82 WS |
467 | // true as it will be deallocated by free() |
468 | void SetBufferIO(void *start, void *end, bool takeOwnership = false); | |
469 | void SetBufferIO(void *start, size_t len, bool takeOwnership = false); | |
c7a9fa36 | 470 | void SetBufferIO(size_t bufsize); |
49e399d8 VZ |
471 | void *GetBufferStart() const { return m_buffer_start; } |
472 | void *GetBufferEnd() const { return m_buffer_end; } | |
473 | void *GetBufferPos() const { return m_buffer_pos; } | |
b23bc769 | 474 | size_t GetBufferSize() const { return m_buffer_end - m_buffer_start; } |
49e399d8 VZ |
475 | size_t GetIntPosition() const { return m_buffer_pos - m_buffer_start; } |
476 | void SetIntPosition(size_t pos) { m_buffer_pos = m_buffer_start + pos; } | |
477 | size_t GetLastAccess() const { return m_buffer_end - m_buffer_start; } | |
478 | size_t GetBytesLeft() const { return m_buffer_end - m_buffer_pos; } | |
c7a9fa36 RR |
479 | |
480 | void Fixed(bool fixed) { m_fixed = fixed; } | |
481 | void Flushable(bool f) { m_flushable = f; } | |
482 | ||
483 | bool FlushBuffer(); | |
484 | bool FillBuffer(); | |
485 | size_t GetDataLeft(); | |
486 | ||
49e399d8 VZ |
487 | // misc accessors |
488 | wxStreamBase *GetStream() const { return m_stream; } | |
b23bc769 | 489 | bool HasBuffer() const { return m_buffer_start != m_buffer_end; } |
49e399d8 VZ |
490 | |
491 | bool IsFixed() const { return m_fixed; } | |
492 | bool IsFlushable() const { return m_flushable; } | |
493 | ||
47fc03d2 VZ |
494 | // only for input/output buffers respectively, returns NULL otherwise |
495 | wxInputStream *GetInputStream() const; | |
496 | wxOutputStream *GetOutputStream() const; | |
497 | ||
40ff126a | 498 | #if WXWIN_COMPATIBILITY_2_6 |
49e399d8 | 499 | // deprecated, for compatibility only |
40ff126a WS |
500 | wxDEPRECATED( wxStreamBase *Stream() ); |
501 | #endif // WXWIN_COMPATIBILITY_2_6 | |
c7a9fa36 | 502 | |
2b5f62a0 VZ |
503 | // this constructs a dummy wxStreamBuffer, used by (and exists for) |
504 | // wxMemoryStreams only, don't use! | |
505 | wxStreamBuffer(BufMode mode); | |
506 | ||
c7a9fa36 RR |
507 | protected: |
508 | void GetFromBuffer(void *buffer, size_t size); | |
509 | void PutToBuffer(const void *buffer, size_t size); | |
510 | ||
49e399d8 VZ |
511 | // set the last error to the specified value if we didn't have it before |
512 | void SetError(wxStreamError err); | |
513 | ||
514 | // common part of several ctors | |
515 | void Init(); | |
c7a9fa36 | 516 | |
f42c1512 VZ |
517 | // common part of ctors taking wxStreamBase parameter |
518 | void InitWithStream(wxStreamBase& stream, BufMode mode); | |
519 | ||
49e399d8 VZ |
520 | // init buffer variables to be empty |
521 | void InitBuffer(); | |
c7a9fa36 | 522 | |
49e399d8 VZ |
523 | // free the buffer (always safe to call) |
524 | void FreeBuffer(); | |
525 | ||
526 | // the buffer itself: the pointers to its start and end and the current | |
527 | // position in the buffer | |
528 | char *m_buffer_start, | |
529 | *m_buffer_end, | |
530 | *m_buffer_pos; | |
531 | ||
49e399d8 | 532 | // the stream we're associated with |
c7a9fa36 | 533 | wxStreamBase *m_stream; |
49e399d8 VZ |
534 | |
535 | // its mode | |
c7a9fa36 | 536 | BufMode m_mode; |
49e399d8 VZ |
537 | |
538 | // flags | |
539 | bool m_destroybuf, // deallocate buffer? | |
49e399d8 VZ |
540 | m_fixed, |
541 | m_flushable; | |
22f3361e | 542 | |
f42c1512 | 543 | |
c0c133e1 | 544 | wxDECLARE_NO_ASSIGN_CLASS(wxStreamBuffer); |
fae05df5 GL |
545 | }; |
546 | ||
547 | // --------------------------------------------------------------------------- | |
2b5f62a0 | 548 | // wxBufferedInputStream |
fae05df5 GL |
549 | // --------------------------------------------------------------------------- |
550 | ||
bddd7a8d | 551 | class WXDLLIMPEXP_BASE wxBufferedInputStream : public wxFilterInputStream |
c7a9fa36 RR |
552 | { |
553 | public: | |
f42c1512 VZ |
554 | // create a buffered stream on top of the specified low-level stream |
555 | // | |
556 | // if a non NULL buffer is given to the stream, it will be deleted by it, | |
557 | // otherwise a default 1KB buffer will be used | |
47fc03d2 VZ |
558 | wxBufferedInputStream(wxInputStream& stream, |
559 | wxStreamBuffer *buffer = NULL); | |
f42c1512 VZ |
560 | |
561 | // ctor allowing to specify the buffer size, it's just a more convenient | |
562 | // alternative to creating wxStreamBuffer, calling its SetBufferIO(bufsize) | |
563 | // and using the ctor above | |
564 | wxBufferedInputStream(wxInputStream& stream, size_t bufsize); | |
565 | ||
566 | ||
47fc03d2 | 567 | virtual ~wxBufferedInputStream(); |
fae05df5 | 568 | |
c7a9fa36 RR |
569 | char Peek(); |
570 | wxInputStream& Read(void *buffer, size_t size); | |
657d2097 | 571 | |
c7a9fa36 | 572 | // Position functions |
4004775e RL |
573 | wxFileOffset SeekI(wxFileOffset pos, wxSeekMode mode = wxFromStart); |
574 | wxFileOffset TellI() const; | |
3c70014d | 575 | bool IsSeekable() const { return m_parent_i_stream->IsSeekable(); } |
fae05df5 | 576 | |
47fc03d2 VZ |
577 | // the buffer given to the stream will be deleted by it |
578 | void SetInputStreamBuffer(wxStreamBuffer *buffer); | |
579 | wxStreamBuffer *GetInputStreamBuffer() const { return m_i_streambuf; } | |
580 | ||
40ff126a | 581 | #if WXWIN_COMPATIBILITY_2_6 |
47fc03d2 | 582 | // deprecated, for compatibility only |
40ff126a WS |
583 | wxDEPRECATED( wxStreamBuffer *InputStreamBuffer() const ); |
584 | #endif // WXWIN_COMPATIBILITY_2_6 | |
fae05df5 | 585 | |
c7a9fa36 | 586 | protected: |
47fc03d2 | 587 | virtual size_t OnSysRead(void *buffer, size_t bufsize); |
4004775e RL |
588 | virtual wxFileOffset OnSysSeek(wxFileOffset seek, wxSeekMode mode); |
589 | virtual wxFileOffset OnSysTell() const; | |
fae05df5 | 590 | |
c7a9fa36 | 591 | wxStreamBuffer *m_i_streambuf; |
22f3361e | 592 | |
c0c133e1 | 593 | wxDECLARE_NO_COPY_CLASS(wxBufferedInputStream); |
fae05df5 GL |
594 | }; |
595 | ||
2b5f62a0 VZ |
596 | // ---------------------------------------------------------------------------- |
597 | // wxBufferedOutputStream | |
598 | // ---------------------------------------------------------------------------- | |
599 | ||
bddd7a8d | 600 | class WXDLLIMPEXP_BASE wxBufferedOutputStream : public wxFilterOutputStream |
c7a9fa36 RR |
601 | { |
602 | public: | |
f42c1512 VZ |
603 | // create a buffered stream on top of the specified low-level stream |
604 | // | |
605 | // if a non NULL buffer is given to the stream, it will be deleted by it, | |
606 | // otherwise a default 1KB buffer will be used | |
47fc03d2 VZ |
607 | wxBufferedOutputStream(wxOutputStream& stream, |
608 | wxStreamBuffer *buffer = NULL); | |
f42c1512 VZ |
609 | |
610 | // ctor allowing to specify the buffer size, it's just a more convenient | |
611 | // alternative to creating wxStreamBuffer, calling its SetBufferIO(bufsize) | |
612 | // and using the ctor above | |
613 | wxBufferedOutputStream(wxOutputStream& stream, size_t bufsize); | |
614 | ||
47fc03d2 | 615 | virtual ~wxBufferedOutputStream(); |
fae05df5 | 616 | |
c7a9fa36 | 617 | wxOutputStream& Write(const void *buffer, size_t size); |
657d2097 | 618 | |
c7a9fa36 | 619 | // Position functions |
4004775e RL |
620 | wxFileOffset SeekO(wxFileOffset pos, wxSeekMode mode = wxFromStart); |
621 | wxFileOffset TellO() const; | |
3c70014d | 622 | bool IsSeekable() const { return m_parent_o_stream->IsSeekable(); } |
fae05df5 | 623 | |
c7a9fa36 | 624 | void Sync(); |
8f0ff178 | 625 | bool Close(); |
fae05df5 | 626 | |
588066b7 | 627 | wxFileOffset GetLength() const; |
657d2097 | 628 | |
47fc03d2 VZ |
629 | // the buffer given to the stream will be deleted by it |
630 | void SetOutputStreamBuffer(wxStreamBuffer *buffer); | |
631 | wxStreamBuffer *GetOutputStreamBuffer() const { return m_o_streambuf; } | |
632 | ||
40ff126a | 633 | #if WXWIN_COMPATIBILITY_2_6 |
47fc03d2 | 634 | // deprecated, for compatibility only |
40ff126a WS |
635 | wxDEPRECATED( wxStreamBuffer *OutputStreamBuffer() const ); |
636 | #endif // WXWIN_COMPATIBILITY_2_6 | |
fae05df5 | 637 | |
c7a9fa36 | 638 | protected: |
47fc03d2 | 639 | virtual size_t OnSysWrite(const void *buffer, size_t bufsize); |
4004775e RL |
640 | virtual wxFileOffset OnSysSeek(wxFileOffset seek, wxSeekMode mode); |
641 | virtual wxFileOffset OnSysTell() const; | |
fae05df5 | 642 | |
c7a9fa36 | 643 | wxStreamBuffer *m_o_streambuf; |
22f3361e | 644 | |
c0c133e1 | 645 | wxDECLARE_NO_COPY_CLASS(wxBufferedOutputStream); |
fae05df5 GL |
646 | }; |
647 | ||
40ff126a WS |
648 | #if WXWIN_COMPATIBILITY_2_6 |
649 | inline wxStreamBase *wxStreamBuffer::Stream() { return m_stream; } | |
650 | inline wxStreamBuffer *wxBufferedInputStream::InputStreamBuffer() const { return m_i_streambuf; } | |
651 | inline wxStreamBuffer *wxBufferedOutputStream::OutputStreamBuffer() const { return m_o_streambuf; } | |
652 | #endif // WXWIN_COMPATIBILITY_2_6 | |
653 | ||
f5ef4d69 VZ |
654 | // --------------------------------------------------------------------------- |
655 | // wxWrapperInputStream: forwards all IO to another stream. | |
656 | // --------------------------------------------------------------------------- | |
657 | ||
658 | class WXDLLIMPEXP_BASE wxWrapperInputStream : public wxFilterInputStream | |
659 | { | |
660 | public: | |
661 | // Constructor fully initializing the stream. The overload taking pointer | |
662 | // takes ownership of the parent stream, the one taking reference does not. | |
663 | // | |
664 | // Notice that this class also has a default ctor but it's protected as the | |
665 | // derived class is supposed to take care of calling InitParentStream() if | |
666 | // it's used. | |
667 | wxWrapperInputStream(wxInputStream& stream); | |
668 | wxWrapperInputStream(wxInputStream* stream); | |
669 | ||
670 | // Override the base class methods to forward to the wrapped stream. | |
671 | virtual wxFileOffset GetLength() const; | |
672 | virtual bool IsSeekable() const; | |
673 | ||
674 | protected: | |
675 | virtual size_t OnSysRead(void *buffer, size_t size); | |
676 | virtual wxFileOffset OnSysSeek(wxFileOffset pos, wxSeekMode mode); | |
677 | virtual wxFileOffset OnSysTell() const; | |
678 | ||
679 | // Ensure that our own last error is the same as that of the real stream. | |
680 | // | |
681 | // This method is const because the error must be updated even from const | |
682 | // methods (in other words, it really should have been mutable in the first | |
683 | // place). | |
684 | void SynchronizeLastError() const | |
685 | { | |
686 | const_cast<wxWrapperInputStream*>(this)-> | |
687 | Reset(m_parent_i_stream->GetLastError()); | |
688 | } | |
689 | ||
690 | // Default constructor, use InitParentStream() later. | |
691 | wxWrapperInputStream(); | |
692 | ||
693 | // Set up the wrapped stream for an object initialized using the default | |
694 | // constructor. The ownership logic is the same as above. | |
695 | void InitParentStream(wxInputStream& stream); | |
696 | void InitParentStream(wxInputStream* stream); | |
697 | ||
698 | wxDECLARE_NO_COPY_CLASS(wxWrapperInputStream); | |
699 | }; | |
700 | ||
701 | ||
47fc03d2 VZ |
702 | #endif // wxUSE_STREAMS |
703 | ||
704 | #endif // _WX_WXSTREAM_H__ |