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