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