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