]> git.saurik.com Git - wxWidgets.git/blob - include/wx/stream.h
#include markup fix.
[wxWidgets.git] / include / wx / stream.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/stream.h
3 // Purpose: stream classes
4 // Author: Guilhem Lavaux, Guillermo Rodriguez Garcia, Vadim Zeitlin
5 // Modified by:
6 // Created: 11/07/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_WXSTREAM_H__
13 #define _WX_WXSTREAM_H__
14
15 #include "wx/defs.h"
16
17 #if wxUSE_STREAMS
18
19 #include <stdio.h>
20 #include "wx/object.h"
21 #include "wx/string.h"
22 #include "wx/filefn.h" // for wxFileOffset, wxInvalidOffset and wxSeekMode
23
24 class WXDLLIMPEXP_BASE wxStreamBase;
25 class WXDLLIMPEXP_BASE wxInputStream;
26 class WXDLLIMPEXP_BASE wxOutputStream;
27
28 typedef wxInputStream& (*__wxInputManip)(wxInputStream&);
29 typedef wxOutputStream& (*__wxOutputManip)(wxOutputStream&);
30
31 WXDLLIMPEXP_BASE wxOutputStream& wxEndL(wxOutputStream& o_stream);
32
33 // ----------------------------------------------------------------------------
34 // constants
35 // ----------------------------------------------------------------------------
36
37 enum wxStreamError
38 {
39 wxSTREAM_NO_ERROR = 0, // stream is in good state
40 wxSTREAM_EOF, // EOF reached in Read() or similar
41 wxSTREAM_WRITE_ERROR, // generic write error
42 wxSTREAM_READ_ERROR // generic read error
43 };
44
45 // compatibility
46 #if WXWIN_COMPATIBILITY_2_2
47 #define wxStream_NOERROR wxSTREAM_NOERROR
48 #define wxStream_EOF wxSTREAM_EOF
49 #define wxStream_WRITE_ERR wxSTREAM_WRITE_ERROR
50 #define wxStream_READ_ERR wxSTREAM_READ_ERROR
51
52 #define wxSTREAM_NO_ERR wxSTREAM_NO_ERROR
53 #define wxSTREAM_NOERROR wxSTREAM_NO_ERROR
54 #define wxSTREAM_WRITE_ERR wxSTREAM_WRITE_ERROR
55 #define wxSTREAM_READ_ERR wxSTREAM_READ_ERROR
56 #endif // WXWIN_COMPATIBILITY_2_2
57
58 // ============================================================================
59 // base stream classes: wxInputStream and wxOutputStream
60 // ============================================================================
61
62 // ---------------------------------------------------------------------------
63 // wxStreamBase: common (but non virtual!) base for all stream classes
64 // ---------------------------------------------------------------------------
65
66 class WXDLLIMPEXP_BASE wxStreamBase
67 {
68 public:
69 wxStreamBase();
70 virtual ~wxStreamBase();
71
72 // error testing
73 wxStreamError GetLastError() const { return m_lasterror; }
74 bool IsOk() const { return GetLastError() == wxSTREAM_NO_ERROR; }
75 bool operator!() const { return !IsOk(); }
76
77 // reset the stream state
78 void Reset() { m_lasterror = wxSTREAM_NO_ERROR; }
79
80 // this doesn't make sense for all streams, always test its return value
81 virtual size_t GetSize() const;
82 virtual wxFileOffset GetLength() const { return wxInvalidOffset; }
83
84 // returns true if the streams supports seeking to arbitrary offsets
85 virtual bool IsSeekable() const { return false; }
86
87 #if WXWIN_COMPATIBILITY_2_2
88 // deprecated, for compatibility only
89 wxDEPRECATED( wxStreamError LastError() const );
90 wxDEPRECATED( size_t StreamSize() const );
91 #endif // WXWIN_COMPATIBILITY_2_2
92
93
94 // Reserved for future use
95 virtual void ReservedStreamFunc1() {}
96 virtual void ReservedStreamFunc2() {}
97 virtual void ReservedStreamFunc3() {}
98 virtual void ReservedStreamFunc4() {}
99 virtual void ReservedStreamFunc5() {}
100 virtual void ReservedStreamFunc6() {}
101 virtual void ReservedStreamFunc7() {}
102 virtual void ReservedStreamFunc8() {}
103 virtual void ReservedStreamFunc9() {}
104
105 protected:
106 virtual wxFileOffset OnSysSeek(wxFileOffset seek, wxSeekMode mode);
107 virtual wxFileOffset OnSysTell() const;
108
109 size_t m_lastcount;
110 wxStreamError m_lasterror;
111
112 friend class wxStreamBuffer;
113
114 DECLARE_NO_COPY_CLASS(wxStreamBase)
115 };
116
117 // ----------------------------------------------------------------------------
118 // wxInputStream: base class for the input streams
119 // ----------------------------------------------------------------------------
120
121 class WXDLLIMPEXP_BASE wxInputStream : public wxStreamBase
122 {
123 public:
124 // ctor and dtor, nothing exciting
125 wxInputStream();
126 virtual ~wxInputStream();
127
128
129 // IO functions
130 // ------------
131
132 // return a character from the stream without removing it, i.e. it will
133 // still be returned by the next call to GetC()
134 //
135 // blocks until something appears in the stream if necessary, if nothing
136 // ever does (i.e. EOF) LastRead() will return 0 (and the return value is
137 // undefined), otherwise 1
138 virtual char Peek();
139
140 // return one character from the stream, blocking until it appears if
141 // necessary
142 //
143 // if EOF, return value is undefined and LastRead() will return 0 and not 1
144 char GetC();
145
146 // read at most the given number of bytes from the stream
147 //
148 // there are 2 possible situations here: either there is nothing at all in
149 // the stream right now in which case Read() blocks until something appears
150 // (use CanRead() to avoid this) or there is already some data available in
151 // the stream and then Read() doesn't block but returns just the data it
152 // can read without waiting for more
153 //
154 // in any case, if there are not enough bytes in the stream right now,
155 // LastRead() value will be less than size but greater than 0. If it is 0,
156 // it means that EOF has been reached.
157 virtual wxInputStream& Read(void *buffer, size_t size);
158
159 // copy the entire contents of this stream into streamOut, stopping only
160 // when EOF is reached or an error occurs
161 wxInputStream& Read(wxOutputStream& streamOut);
162
163
164 // status functions
165 // ----------------
166
167 // returns the number of bytes read by the last call to Read(), GetC() or
168 // Peek()
169 //
170 // this should be used to discover whether that call succeeded in reading
171 // all the requested data or not
172 virtual size_t LastRead() const { return wxStreamBase::m_lastcount; }
173
174 // returns true if some data is available in the stream right now, so that
175 // calling Read() wouldn't block
176 virtual bool CanRead() const;
177
178 // is the stream at EOF?
179 //
180 // note that this cannot be really implemented for all streams and
181 // CanRead() is more reliable than Eof()
182 virtual bool Eof() const;
183
184
185 // write back buffer
186 // -----------------
187
188 // put back the specified number of bytes into the stream, they will be
189 // fetched by the next call to the read functions
190 //
191 // returns the number of bytes really stuffed back
192 size_t Ungetch(const void *buffer, size_t size);
193
194 // put back the specified character in the stream
195 //
196 // returns true if ok, false on error
197 bool Ungetch(char c);
198
199
200 // position functions
201 // ------------------
202
203 // move the stream pointer to the given position (if the stream supports
204 // it)
205 //
206 // returns wxInvalidOffset on error
207 virtual wxFileOffset SeekI(wxFileOffset pos, wxSeekMode mode = wxFromStart);
208
209 // return the current position of the stream pointer or wxInvalidOffset
210 virtual wxFileOffset TellI() const;
211
212
213 // stream-like operators
214 // ---------------------
215
216 wxInputStream& operator>>(wxOutputStream& out) { return Read(out); }
217 wxInputStream& operator>>(__wxInputManip func) { return func(*this); }
218
219 protected:
220 // do read up to size bytes of data into the provided buffer
221 //
222 // this method should return 0 if EOF has been reached or an error occurred
223 // (m_lasterror should be set accordingly as well) or the number of bytes
224 // read
225 virtual size_t OnSysRead(void *buffer, size_t size) = 0;
226
227 // write-back buffer support
228 // -------------------------
229
230 // return the pointer to a buffer big enough to hold sizeNeeded bytes
231 char *AllocSpaceWBack(size_t sizeNeeded);
232
233 // read up to size data from the write back buffer, return the number of
234 // bytes read
235 size_t GetWBack(void *buf, size_t size);
236
237 // write back buffer or NULL if none
238 char *m_wback;
239
240 // the size of the buffer
241 size_t m_wbacksize;
242
243 // the current position in the buffer
244 size_t m_wbackcur;
245
246 friend class wxStreamBuffer;
247
248 DECLARE_NO_COPY_CLASS(wxInputStream)
249 };
250
251 // ----------------------------------------------------------------------------
252 // wxOutputStream: base for the output streams
253 // ----------------------------------------------------------------------------
254
255 class WXDLLIMPEXP_BASE wxOutputStream : public wxStreamBase
256 {
257 public:
258 wxOutputStream();
259 virtual ~wxOutputStream();
260
261 void PutC(char c);
262 virtual wxOutputStream& Write(const void *buffer, size_t size);
263 wxOutputStream& Write(wxInputStream& stream_in);
264
265 virtual wxFileOffset SeekO(wxFileOffset pos, wxSeekMode mode = wxFromStart);
266 virtual wxFileOffset TellO() const;
267
268 virtual size_t LastWrite() const { return wxStreamBase::m_lastcount; }
269
270 virtual void Sync();
271 virtual bool Close() { return true; }
272
273 wxOutputStream& operator<<(wxInputStream& out) { return Write(out); }
274 wxOutputStream& operator<<( __wxOutputManip func) { return func(*this); }
275
276 protected:
277 // to be implemented in the derived classes (it should have been pure
278 // virtual)
279 virtual size_t OnSysWrite(const void *buffer, size_t bufsize);
280
281 friend class wxStreamBuffer;
282
283 DECLARE_NO_COPY_CLASS(wxOutputStream)
284 };
285
286 // ============================================================================
287 // helper stream classes
288 // ============================================================================
289
290 // ---------------------------------------------------------------------------
291 // A stream for measuring streamed output
292 // ---------------------------------------------------------------------------
293
294 class WXDLLIMPEXP_BASE wxCountingOutputStream : public wxOutputStream
295 {
296 public:
297 wxCountingOutputStream();
298
299 wxFileOffset GetLength() const;
300 bool Ok() const { return true; }
301
302 protected:
303 virtual size_t OnSysWrite(const void *buffer, size_t size);
304 virtual wxFileOffset OnSysSeek(wxFileOffset pos, wxSeekMode mode);
305 virtual wxFileOffset OnSysTell() const;
306
307 size_t m_currentPos;
308
309 DECLARE_NO_COPY_CLASS(wxCountingOutputStream)
310 };
311
312 // ---------------------------------------------------------------------------
313 // "Filter" streams
314 // ---------------------------------------------------------------------------
315
316 class WXDLLIMPEXP_BASE wxFilterInputStream : public wxInputStream
317 {
318 public:
319 wxFilterInputStream();
320 wxFilterInputStream(wxInputStream& stream);
321 virtual ~wxFilterInputStream();
322
323 char Peek() { return m_parent_i_stream->Peek(); }
324
325 wxFileOffset GetLength() const { return m_parent_i_stream->GetLength(); }
326
327 wxInputStream *GetFilterInputStream() const { return m_parent_i_stream; }
328
329 protected:
330 wxInputStream *m_parent_i_stream;
331
332 DECLARE_NO_COPY_CLASS(wxFilterInputStream)
333 };
334
335 class WXDLLIMPEXP_BASE wxFilterOutputStream : public wxOutputStream
336 {
337 public:
338 wxFilterOutputStream();
339 wxFilterOutputStream(wxOutputStream& stream);
340 virtual ~wxFilterOutputStream();
341
342 wxFileOffset GetLength() const { return m_parent_o_stream->GetLength(); }
343
344 wxOutputStream *GetFilterOutputStream() const { return m_parent_o_stream; }
345
346 protected:
347 wxOutputStream *m_parent_o_stream;
348
349 DECLARE_NO_COPY_CLASS(wxFilterOutputStream)
350 };
351
352 // ============================================================================
353 // buffered streams
354 // ============================================================================
355
356 // ---------------------------------------------------------------------------
357 // Stream buffer: this class can be derived from and passed to
358 // wxBufferedStreams to implement custom buffering
359 // ---------------------------------------------------------------------------
360
361 class WXDLLIMPEXP_BASE wxStreamBuffer
362 {
363 public:
364 enum BufMode
365 {
366 read,
367 write,
368 read_write
369 };
370
371 wxStreamBuffer(wxStreamBase& stream, BufMode mode);
372 wxStreamBuffer(const wxStreamBuffer& buf);
373 virtual ~wxStreamBuffer();
374
375 // Filtered IO
376 virtual size_t Read(void *buffer, size_t size);
377 size_t Read(wxStreamBuffer *buf);
378 virtual size_t Write(const void *buffer, size_t size);
379 size_t Write(wxStreamBuffer *buf);
380
381 virtual char Peek();
382 virtual char GetChar();
383 virtual void PutChar(char c);
384 virtual wxFileOffset Tell() const;
385 virtual wxFileOffset Seek(wxFileOffset pos, wxSeekMode mode);
386
387 // Buffer control
388 void ResetBuffer();
389
390 // NB: the buffer must always be allocated with malloc() if takeOwn is
391 // true as it will be deallocated by free()
392 void SetBufferIO(void *start, void *end, bool takeOwnership = false);
393 void SetBufferIO(void *start, size_t len, bool takeOwnership = false);
394 void SetBufferIO(size_t bufsize);
395 void *GetBufferStart() const { return m_buffer_start; }
396 void *GetBufferEnd() const { return m_buffer_end; }
397 void *GetBufferPos() const { return m_buffer_pos; }
398 size_t GetBufferSize() const { return m_buffer_size; }
399 size_t GetIntPosition() const { return m_buffer_pos - m_buffer_start; }
400 void SetIntPosition(size_t pos) { m_buffer_pos = m_buffer_start + pos; }
401 size_t GetLastAccess() const { return m_buffer_end - m_buffer_start; }
402 size_t GetBytesLeft() const { return m_buffer_end - m_buffer_pos; }
403
404 void Fixed(bool fixed) { m_fixed = fixed; }
405 void Flushable(bool f) { m_flushable = f; }
406
407 bool FlushBuffer();
408 bool FillBuffer();
409 size_t GetDataLeft();
410
411 // misc accessors
412 wxStreamBase *GetStream() const { return m_stream; }
413 bool HasBuffer() const { return m_buffer_size != 0; }
414
415 bool IsFixed() const { return m_fixed; }
416 bool IsFlushable() const { return m_flushable; }
417
418 // only for input/output buffers respectively, returns NULL otherwise
419 wxInputStream *GetInputStream() const;
420 wxOutputStream *GetOutputStream() const;
421
422 // deprecated, for compatibility only
423 wxStreamBase *Stream() { return m_stream; }
424
425 // this constructs a dummy wxStreamBuffer, used by (and exists for)
426 // wxMemoryStreams only, don't use!
427 wxStreamBuffer(BufMode mode);
428
429 protected:
430 void GetFromBuffer(void *buffer, size_t size);
431 void PutToBuffer(const void *buffer, size_t size);
432
433 // set the last error to the specified value if we didn't have it before
434 void SetError(wxStreamError err);
435
436 // common part of several ctors
437 void Init();
438
439 // init buffer variables to be empty
440 void InitBuffer();
441
442 // free the buffer (always safe to call)
443 void FreeBuffer();
444
445 // the buffer itself: the pointers to its start and end and the current
446 // position in the buffer
447 char *m_buffer_start,
448 *m_buffer_end,
449 *m_buffer_pos;
450
451 // the buffer size
452 // FIXME: isn't it the same as m_buffer_end - m_buffer_start? (VZ)
453 size_t m_buffer_size;
454
455 // the stream we're associated with
456 wxStreamBase *m_stream;
457
458 // its mode
459 BufMode m_mode;
460
461 // flags
462 bool m_destroybuf, // deallocate buffer?
463 m_fixed,
464 m_flushable;
465
466 private:
467 // Cannot use
468 // DECLARE_NO_COPY_CLASS(wxStreamBuffer)
469 // because copy constructor is explicitly declared above;
470 // but no copy assignment operator is defined, so declare
471 // it private to prevent the compiler from defining it:
472 wxStreamBuffer& operator=(const wxStreamBuffer&);
473 };
474
475 // ---------------------------------------------------------------------------
476 // wxBufferedInputStream
477 // ---------------------------------------------------------------------------
478
479 class WXDLLIMPEXP_BASE wxBufferedInputStream : public wxFilterInputStream
480 {
481 public:
482 // if a non NULL buffer is given to the stream, it will be deleted by it
483 wxBufferedInputStream(wxInputStream& stream,
484 wxStreamBuffer *buffer = NULL);
485 virtual ~wxBufferedInputStream();
486
487 char Peek();
488 wxInputStream& Read(void *buffer, size_t size);
489
490 // Position functions
491 wxFileOffset SeekI(wxFileOffset pos, wxSeekMode mode = wxFromStart);
492 wxFileOffset TellI() const;
493 bool IsSeekable() const { return m_parent_i_stream->IsSeekable(); }
494
495 // the buffer given to the stream will be deleted by it
496 void SetInputStreamBuffer(wxStreamBuffer *buffer);
497 wxStreamBuffer *GetInputStreamBuffer() const { return m_i_streambuf; }
498
499 // deprecated, for compatibility only
500 wxStreamBuffer *InputStreamBuffer() const { return m_i_streambuf; }
501
502 protected:
503 virtual size_t OnSysRead(void *buffer, size_t bufsize);
504 virtual wxFileOffset OnSysSeek(wxFileOffset seek, wxSeekMode mode);
505 virtual wxFileOffset OnSysTell() const;
506
507 wxStreamBuffer *m_i_streambuf;
508
509 DECLARE_NO_COPY_CLASS(wxBufferedInputStream)
510 };
511
512 // ----------------------------------------------------------------------------
513 // wxBufferedOutputStream
514 // ----------------------------------------------------------------------------
515
516 class WXDLLIMPEXP_BASE wxBufferedOutputStream : public wxFilterOutputStream
517 {
518 public:
519 // if a non NULL buffer is given to the stream, it will be deleted by it
520 wxBufferedOutputStream(wxOutputStream& stream,
521 wxStreamBuffer *buffer = NULL);
522 virtual ~wxBufferedOutputStream();
523
524 wxOutputStream& Write(const void *buffer, size_t size);
525
526 // Position functions
527 wxFileOffset SeekO(wxFileOffset pos, wxSeekMode mode = wxFromStart);
528 wxFileOffset TellO() const;
529 bool IsSeekable() const { return m_parent_o_stream->IsSeekable(); }
530
531 void Sync();
532 bool Close();
533
534 wxFileOffset GetLength() const;
535
536 // the buffer given to the stream will be deleted by it
537 void SetOutputStreamBuffer(wxStreamBuffer *buffer);
538 wxStreamBuffer *GetOutputStreamBuffer() const { return m_o_streambuf; }
539
540 // deprecated, for compatibility only
541 wxStreamBuffer *OutputStreamBuffer() const { return m_o_streambuf; }
542
543 protected:
544 virtual size_t OnSysWrite(const void *buffer, size_t bufsize);
545 virtual wxFileOffset OnSysSeek(wxFileOffset seek, wxSeekMode mode);
546 virtual wxFileOffset OnSysTell() const;
547
548 wxStreamBuffer *m_o_streambuf;
549
550 DECLARE_NO_COPY_CLASS(wxBufferedOutputStream)
551 };
552
553 #endif // wxUSE_STREAMS
554
555 #endif // _WX_WXSTREAM_H__
556