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