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