]> git.saurik.com Git - wxWidgets.git/blame - interface/wx/stream.h
use wxEventType coherently with the docs
[wxWidgets.git] / interface / wx / stream.h
CommitLineData
23324ae1
FM
1/////////////////////////////////////////////////////////////////////////////
2// Name: stream.h
c977fa84 3// Purpose: interface of wxStreamBase and its derived classes
23324ae1
FM
4// Author: wxWidgets team
5// RCS-ID: $Id$
6// Licence: wxWindows license
7/////////////////////////////////////////////////////////////////////////////
8
9/**
10 @class wxCountingOutputStream
7c913512 11
23324ae1 12 wxCountingOutputStream is a specialized output stream which does not write any
c977fa84
FM
13 data anywhere, instead it counts how many bytes would get written if this were a
14 normal stream. This can sometimes be useful or required if some data gets
15 serialized to a stream or compressed by using stream compression and thus the
16 final size of the stream cannot be known other than pretending to write the stream.
17 One case where the resulting size would have to be known is if the data has
18 to be written to a piece of memory and the memory has to be allocated before
19 writing to it (which is probably always the case when writing to a memory stream).
7c913512 20
23324ae1
FM
21 @library{wxbase}
22 @category{streams}
23*/
24class wxCountingOutputStream : public wxOutputStream
25{
26public:
27 /**
28 Creates a wxCountingOutputStream object.
29 */
30 wxCountingOutputStream();
31
32 /**
33 Destructor.
34 */
adaaa686 35 virtual ~wxCountingOutputStream();
23324ae1
FM
36
37 /**
38 Returns the current size of the stream.
39 */
328f5751 40 size_t GetSize() const;
23324ae1
FM
41};
42
43
e54c96f1 44
23324ae1
FM
45/**
46 @class wxBufferedInputStream
7c913512 47
23324ae1 48 This stream acts as a cache. It caches the bytes read from the specified
c977fa84 49 input stream (see wxFilterInputStream).
23324ae1
FM
50 It uses wxStreamBuffer and sets the default in-buffer size to 1024 bytes.
51 This class may not be used without some other stream to read the data
52 from (such as a file stream or a memory stream).
7c913512 53
23324ae1
FM
54 @library{wxbase}
55 @category{streams}
7c913512 56
e54c96f1 57 @see wxStreamBuffer, wxInputStream, wxBufferedOutputStream
23324ae1
FM
58*/
59class wxBufferedInputStream : public wxFilterInputStream
60{
61public:
c977fa84 62 /**
f42c1512
VZ
63 Constructor using the provided buffer or default.
64
65 @param stream
66 The associated low-level stream.
67 @param buffer
68 The buffer to use if non-@NULL. Notice that the ownership of this
69 buffer is taken by the stream, i.e. it will delete it. If this
70 parameter is @NULL a default 1KB buffer is used.
c977fa84
FM
71 */
72 wxBufferedInputStream(wxInputStream& stream,
73 wxStreamBuffer *buffer = NULL);
7c913512 74
f42c1512
VZ
75 /**
76 Constructor allowing to specify the size of the buffer.
77
78 This is just a more convenient alternative to creating a wxStreamBuffer
79 of the given size and using the other overloaded constructor of this
80 class.
81
82 @param stream
83 The associated low-level stream.
84 @param bufsize
85 The size of the buffer, in bytes.
86
87 @since 2.9.0
88 */
89 wxBufferedInputStream(wxInputStream& stream, size_t bufsize);
90
c977fa84
FM
91 /**
92 Destructor.
93 */
94 virtual ~wxBufferedInputStream();
23324ae1
FM
95};
96
97
e54c96f1 98
23324ae1
FM
99/**
100 @class wxStreamBuffer
7c913512 101
c977fa84 102 @todo WRITE A DESCRIPTION
7c913512 103
23324ae1
FM
104 @library{wxbase}
105 @category{streams}
7c913512 106
e54c96f1 107 @see wxStreamBase
23324ae1 108*/
7c913512 109class wxStreamBuffer
23324ae1
FM
110{
111public:
c977fa84
FM
112
113 /**
114 Constructor, creates a new stream buffer using @a stream as a parent stream
115 and mode as the IO mode.
116
117 @param stream
118 The parent stream.
119 @param mode
120 Can be: wxStreamBuffer::read, wxStreamBuffer::write, wxStreamBuffer::read_write.
121
122 One stream can have many stream buffers but only one is used internally
123 to pass IO call (e.g. wxInputStream::Read() -> wxStreamBuffer::Read()),
124 but you can call directly wxStreamBuffer::Read without any problems.
125 Note that all errors and messages linked to the stream are stored in the
126 stream, not the stream buffers:
127
128 @code
129 streambuffer.Read(...);
130 streambuffer2.Read(...); // This call erases previous error messages set by 'streambuffer'
131 @endcode
132
133 @see SetBufferIO()
134 */
135 wxStreamBuffer(wxStreamBase& stream, BufMode mode);
136
f42c1512
VZ
137 /**
138 Constructor for an input buffer of the specified size.
139
140 Using it is equivalent to using the constructor above with read mode
141 and calling SetBufferIO() but is more convenient.
142
143 @since 2.9.0
144 */
145 wxStreamBuffer(wxInputStream& stream, size_t bufsize);
146
147 /**
148 Constructor for an output buffer of the specified size.
149
150 Using it is equivalent to using the constructor above with write mode
151 and calling SetBufferIO() but is more convenient.
152
153 @since 2.9.0
154 */
155 wxStreamBuffer(wxOutputStream& stream, size_t bufsize);
156
c977fa84
FM
157 /**
158 Constructor; creates a new empty stream buffer which won't flush any data
159 to a stream. mode specifies the type of the buffer (read, write, read_write).
4701dc09 160
c977fa84
FM
161 This stream buffer has the advantage to be stream independent and to work
162 only on memory buffers but it is still compatible with the rest of the
163 wxStream classes. You can write, read to this special stream and it will
164 grow (if it is allowed by the user) its internal buffer.
165 Briefly, it has all functionality of a "normal" stream.
166
167 @warning
168 The "read_write" mode doesn't currently work for standalone stream buffers.
169
170 @see SetBufferIO()
171 */
172 wxStreamBuffer(BufMode mode);
173
23324ae1 174 /**
f42c1512 175 Copy constructor.
4701dc09
FM
176
177 This method initializes the stream buffer with the data of the specified
23324ae1
FM
178 stream buffer. The new stream buffer has the same attributes, size, position
179 and they share the same buffer. This will cause problems if the stream to
180 which the stream buffer belong is destroyed and the newly cloned stream
181 buffer continues to be used, trying to call functions in the (destroyed)
182 stream. It is advised to use this feature only in very local area of the
183 program.
23324ae1 184 */
7c913512 185 wxStreamBuffer(const wxStreamBuffer& buffer);
23324ae1
FM
186
187 /**
c977fa84
FM
188 Destructor.
189 It finalizes all IO calls and frees all internal buffers if necessary.
23324ae1
FM
190 */
191 wxStreamBuffer();
192
193 /**
194 Fill the IO buffer.
195 */
196 bool FillBuffer();
197
198 /**
7c913512 199 Toggles the fixed flag. Usually this flag is toggled at the same time as
23324ae1 200 @e flushable. This flag allows (when it has the @false value) or forbids
c977fa84
FM
201 (when it has the @true value) the stream buffer to resize dynamically the
202 IO buffer.
3c4f71cc 203
4cc4bfaf 204 @see SetBufferIO()
23324ae1
FM
205 */
206 void Fixed(bool fixed);
207
208 /**
209 Flushes the IO buffer.
210 */
211 bool FlushBuffer();
212
213 /**
c977fa84
FM
214 Toggles the flushable flag.
215 If @a flushable is disabled, no data are sent to the parent stream.
23324ae1
FM
216 */
217 void Flushable(bool flushable);
218
219 /**
220 Returns a pointer on the end of the stream buffer.
221 */
328f5751 222 void* GetBufferEnd() const;
23324ae1
FM
223
224 /**
225 Returns a pointer on the current position of the stream buffer.
226 */
328f5751 227 void* GetBufferPos() const;
23324ae1
FM
228
229 /**
230 Returns the size of the buffer.
231 */
328f5751 232 size_t GetBufferSize() const;
23324ae1
FM
233
234 /**
235 Returns a pointer on the start of the stream buffer.
236 */
328f5751 237 void* GetBufferStart() const;
23324ae1
FM
238
239 /**
c977fa84
FM
240 Gets a single char from the stream buffer. It acts like the Read() call.
241
242 @warning
243 You aren't directly notified if an error occurred during the IO call.
3c4f71cc 244
4cc4bfaf 245 @see Read()
23324ae1 246 */
adaaa686 247 virtual char GetChar();
23324ae1
FM
248
249 /**
250 Returns the amount of available data in the buffer.
251 */
252 size_t GetDataLeft();
253
254 /**
255 Returns the current position (counted in bytes) in the stream buffer.
256 */
5267aefd 257 size_t GetIntPosition() const;
23324ae1
FM
258
259 /**
260 Returns the amount of bytes read during the last IO call to the parent stream.
261 */
328f5751 262 size_t GetLastAccess() const;
23324ae1
FM
263
264 /**
265 Puts a single char to the stream buffer.
3c4f71cc 266
c977fa84
FM
267 @warning
268 You aren't directly notified if an error occurred during the IO call.
269
4cc4bfaf 270 @see Read()
23324ae1 271 */
adaaa686 272 virtual void PutChar(char c);
23324ae1 273
23324ae1 274 /**
c977fa84
FM
275 Reads a block of the specified size and stores the data in buffer.
276 This function tries to read from the buffer first and if more data has
277 been requested, reads more data from the associated stream and updates
278 the buffer accordingly until all requested data is read.
279
280 @return It returns the size of the data read. If the returned size is
281 different of the specified size, an error has occurred and
282 should be tested using GetLastError().
283 */
adaaa686 284 virtual size_t Read(void* buffer, size_t size);
c977fa84
FM
285
286 /**
287 Copies data to @a buffer.
288 The function returns when @a buffer is full or when there isn't
23324ae1 289 any more data in the current buffer.
3c4f71cc 290
4cc4bfaf 291 @see Write()
23324ae1 292 */
4cc4bfaf 293 Return value size_t Read(wxStreamBuffer* buffer);
23324ae1
FM
294
295 /**
296 Resets to the initial state variables concerning the buffer.
297 */
298 void ResetBuffer();
299
300 /**
301 Changes the current position.
c977fa84 302 Parameter @a mode may be one of the following:
3c4f71cc 303
c977fa84
FM
304 - @b wxFromStart: The position is counted from the start of the stream.
305 - @b wxFromCurrent: The position is counted from the current position of the stream.
306 - @b wxFromEnd: The position is counted from the end of the stream.
3c4f71cc 307
c977fa84
FM
308 @return Upon successful completion, it returns the new offset as
309 measured in bytes from the beginning of the stream.
310 Otherwise, it returns wxInvalidOffset.
311 */
18e8e19b 312 virtual wxFileOffset Seek(wxFileOffset pos, wxSeekMode mode);
3c4f71cc 313
c977fa84
FM
314 /**
315 Specifies which pointers to use for stream buffering.
316 You need to pass a pointer on the start of the buffer end and another
317 on the end. The object will use this buffer to cache stream data.
318 It may be used also as a source/destination buffer when you create an
319 empty stream buffer (See wxStreamBuffer::wxStreamBuffer).
3c4f71cc 320
c977fa84
FM
321 @remarks
322 When you use this function, you will have to destroy the IO buffers
323 yourself after the stream buffer is destroyed or don't use it anymore.
324 In the case you use it with an empty buffer, the stream buffer will not
325 resize it when it is full.
3c4f71cc 326
c977fa84 327 @see wxStreamBuffer(), Fixed(), Flushable()
23324ae1 328 */
c977fa84 329 void SetBufferIO(char* buffer_start, char* buffer_end);
23324ae1 330
23324ae1
FM
331 /**
332 Destroys or invalidates the previous IO buffer and allocates a new one of the
333 specified size.
3c4f71cc 334
c977fa84
FM
335 @warning
336 All previous pointers aren't valid anymore.
23324ae1 337
c977fa84
FM
338 @remarks
339 The created IO buffer is growable by the object.
23324ae1 340
c977fa84
FM
341 @see Fixed(), Flushable()
342 */
7c913512 343 void SetBufferIO(size_t bufsize);
23324ae1
FM
344
345 /**
346 Sets the current position (in bytes) in the stream buffer.
c977fa84
FM
347
348 @warning
349 Since it is a very low-level function, there is no check on the position:
350 specifying an invalid position can induce unexpected results.
23324ae1
FM
351 */
352 void SetIntPosition(size_t pos);
353
354 /**
355 Returns the parent stream of the stream buffer.
26818748 356 @deprecated use GetStream() instead
23324ae1
FM
357 */
358 wxStreamBase* Stream();
359
360 /**
361 Gets the current position in the stream. This position is calculated from
362 the @e real position in the stream and from the internal buffer position: so
363 it gives you the position in the @e real stream counted from the start of
364 the stream.
3c4f71cc 365
d29a9a8a 366 @return Returns the current position in the stream if possible,
c977fa84 367 wxInvalidOffset in the other case.
23324ae1 368 */
18e8e19b 369 virtual wxFileOffset Tell() const;
23324ae1
FM
370
371 /**
372 Truncates the buffer to the current position.
c977fa84
FM
373
374 @note Truncate() cannot be used to enlarge the buffer. This is
375 usually not needed since the buffer expands automatically.
23324ae1
FM
376 */
377 void Truncate();
378
23324ae1 379 /**
c977fa84
FM
380 Writes a block of the specified size using data of buffer.
381 The data are cached in a buffer before being sent in one block to the stream.
23324ae1 382 */
adaaa686 383 virtual size_t Write(const void* buffer, size_t size);
c977fa84
FM
384
385 /**
386 See Read().
387 */
4cc4bfaf 388 size_t Write(wxStreamBuffer* buffer);
23324ae1
FM
389};
390
391
e54c96f1 392
23324ae1
FM
393/**
394 @class wxOutputStream
7c913512 395
23324ae1 396 wxOutputStream is an abstract base class which may not be used directly.
7c913512 397
23324ae1
FM
398 @library{wxbase}
399 @category{streams}
400*/
401class wxOutputStream : public wxStreamBase
402{
403public:
404 /**
405 Creates a dummy wxOutputStream object.
406 */
407 wxOutputStream();
408
409 /**
410 Destructor.
411 */
adaaa686 412 virtual ~wxOutputStream();
23324ae1
FM
413
414 /**
c977fa84
FM
415 Closes the stream, returning @false if an error occurs.
416 The stream is closed implicitly in the destructor if Close() is not
23324ae1 417 called explicitly.
c977fa84 418
23324ae1
FM
419 If this stream wraps another stream or some other resource such
420 as a file, then the underlying resource is closed too if it is owned
421 by this stream, or left open otherwise.
422 */
adaaa686 423 virtual bool Close();
23324ae1
FM
424
425 /**
c977fa84
FM
426 Returns the number of bytes written during the last Write().
427 It may return 0 even if there is no error on the stream if it is
428 only temporarily impossible to write to it.
23324ae1 429 */
adaaa686 430 virtual size_t LastWrite() const;
23324ae1
FM
431
432 /**
433 Puts the specified character in the output queue and increments the
434 stream position.
435 */
4cc4bfaf 436 void PutC(char c);
23324ae1
FM
437
438 /**
439 Changes the stream current position.
3c4f71cc 440
7c913512 441 @param pos
4cc4bfaf 442 Offset to seek to.
7c913512 443 @param mode
4cc4bfaf 444 One of wxFromStart, wxFromEnd, wxFromCurrent.
3c4f71cc 445
d29a9a8a 446 @return The new stream position or wxInvalidOffset on error.
23324ae1 447 */
18e8e19b 448 virtual wxFileOffset SeekO(wxFileOffset pos, wxSeekMode mode = wxFromStart);
23324ae1
FM
449
450 /**
451 Returns the current stream position.
452 */
18e8e19b 453 virtual wxFileOffset TellO() const;
23324ae1 454
c977fa84
FM
455 /**
456 Writes up to the specified amount of bytes using the data of buffer.
457 Note that not all data can always be written so you must check the number
458 of bytes really written to the stream using LastWrite() when this function
459 returns.
460
461 In some cases (for example a write end of a pipe which is currently full)
462 it is even possible that there is no errors and zero bytes have been written.
463 This function returns a reference on the current object, so the user can
464 test any states of the stream right away.
465 */
466 wxOutputStream Write(const void* buffer, size_t size);
467
23324ae1 468 /**
7c913512 469 Reads data from the specified input stream and stores them
23324ae1
FM
470 in the current stream. The data is read until an error is raised
471 by one of the two streams.
472 */
7c913512 473 wxOutputStream Write(wxInputStream& stream_in);
23324ae1
FM
474};
475
476
c977fa84
FM
477/**
478 Enumeration values used by wxFilterClassFactory.
479*/
480enum wxStreamProtocolType
481{
482 wxSTREAM_PROTOCOL, //!< wxFileSystem protocol (should be only one).
483 wxSTREAM_MIMETYPE, //!< MIME types the stream handles.
484 wxSTREAM_ENCODING, //!< The HTTP Content-Encodings the stream handles.
485 wxSTREAM_FILEEXT //!< File extensions the stream handles.
486};
487
e54c96f1 488
23324ae1
FM
489/**
490 @class wxFilterClassFactory
7c913512 491
23324ae1
FM
492 Allows the creation of filter streams to handle compression formats such
493 as gzip and bzip2.
7c913512 494
23324ae1
FM
495 For example, given a filename you can search for a factory that will
496 handle it and create a stream to decompress it:
7c913512 497
23324ae1 498 @code
4701dc09 499 factory = wxFilterClassFactory::Find(filename, wxSTREAM_FILEEXT);
23324ae1
FM
500 if (factory)
501 stream = factory-NewStream(new wxFFileInputStream(filename));
502 @endcode
7c913512 503
c977fa84
FM
504 wxFilterClassFactory::Find can also search for a factory by MIME type,
505 HTTP encoding or by wxFileSystem protocol.
506 The available factories can be enumerated using wxFilterClassFactory::GetFirst()
507 and wxFilterClassFactory::GetNext().
7c913512 508
23324ae1 509 @library{wxbase}
c977fa84 510 @category{streams}
7c913512 511
c977fa84
FM
512 @see wxFilterInputStream, wxFilterOutputStream, wxArchiveClassFactory,
513 @ref overview_archive
23324ae1
FM
514*/
515class wxFilterClassFactory : public wxObject
516{
517public:
518 /**
519 Returns @true if this factory can handle the given protocol, MIME type, HTTP
520 encoding or file extension.
c977fa84
FM
521
522 When using @c wxSTREAM_FILEEXT for the second parameter, the first parameter
23324ae1
FM
523 can be a complete filename rather than just an extension.
524 */
525 bool CanHandle(const wxString& protocol,
328f5751 526 wxStreamProtocolType type = wxSTREAM_PROTOCOL) const;
23324ae1
FM
527
528 /**
529 A static member that finds a factory that can handle a given protocol, MIME
c977fa84
FM
530 type, HTTP encoding or file extension. Returns a pointer to the class
531 factory if found, or @NULL otherwise.
532 It does not give away ownership of the factory.
533
534 When using @c wxSTREAM_FILEEXT for the second parameter, the first parameter
23324ae1
FM
535 can be a complete filename rather than just an extension.
536 */
537 static const wxFilterClassFactory* Find(const wxString& protocol,
538 wxStreamProtocolType type = wxSTREAM_PROTOCOL);
539
540 //@{
541 /**
542 GetFirst and GetNext can be used to enumerate the available factories.
23324ae1 543 For example, to list them:
3c4f71cc 544
c977fa84
FM
545 @code
546 wxString list;
547 const wxFilterClassFactory *factory = wxFilterClassFactory::GetFirst();
548
549 while (factory) {
550 list << factory->GetProtocol() << _T("\n");
551 factory = factory->GetNext();
552 }
553 @endcode
554
23324ae1
FM
555 GetFirst()/GetNext() return a pointer to a factory or @NULL if no more
556 are available. They do not give away ownership of the factory.
557 */
328f5751
FM
558 static const wxFilterClassFactory* GetFirst() const;
559 const wxFilterClassFactory* GetNext() const;
23324ae1
FM
560 //@}
561
562 /**
c977fa84
FM
563 Returns the wxFileSystem protocol supported by this factory.
564 Equivalent to @code wxString(*GetProtocols()) @endcode.
23324ae1 565 */
328f5751 566 wxString GetProtocol() const;
23324ae1
FM
567
568 /**
569 Returns the protocols, MIME types, HTTP encodings or file extensions
c977fa84
FM
570 supported by this factory, as an array of null terminated strings.
571 It does not give away ownership of the array or strings.
572
23324ae1 573 For example, to list the file extensions a factory supports:
c977fa84
FM
574
575 @code
576 wxString list;
577 const wxChar *const *p;
578
579 for (p = factory->GetProtocols(wxSTREAM_FILEEXT); *p; p++)
580 list << *p << _T("\n");
581 @endcode
23324ae1 582 */
da1ed74c 583 virtual const wxChar * const* GetProtocols(wxStreamProtocolType type = wxSTREAM_PROTOCOL) const = 0;
23324ae1
FM
584
585 //@{
586 /**
587 Create a new input or output stream to decompress or compress a given stream.
c977fa84 588
23324ae1
FM
589 If the parent stream is passed as a pointer then the new filter stream
590 takes ownership of it. If it is passed by reference then it does not.
591 */
c977fa84
FM
592 wxFilterInputStream* NewStream(wxInputStream& stream) const;
593 wxFilterOutputStream* NewStream(wxOutputStream& stream) const;
594 wxFilterInputStream* NewStream(wxInputStream* stream) const;
595 wxFilterOutputStream* NewStream(wxOutputStream* stream) const;
23324ae1
FM
596 //@}
597
598 /**
4cc4bfaf 599 Remove the file extension of @a location if it is one of the file
23324ae1
FM
600 extensions handled by this factory.
601 */
328f5751 602 wxString PopExtension(const wxString& location) const;
23324ae1
FM
603
604 /**
c977fa84
FM
605 Adds this class factory to the list returned by GetFirst()/GetNext().
606
23324ae1 607 It is not necessary to do this to use the filter streams. It is usually
7c913512 608 used when implementing streams, typically the implementation will
23324ae1 609 add a static instance of its factory class.
c977fa84 610
23324ae1 611 It can also be used to change the order of a factory already in the list,
c977fa84
FM
612 bringing it to the front. This isn't a thread safe operation so can't be
613 done when other threads are running that will be using the list.
614
23324ae1
FM
615 The list does not take ownership of the factory.
616 */
617 void PushFront();
618
619 /**
c977fa84
FM
620 Removes this class factory from the list returned by GetFirst()/GetNext().
621 Removing from the list isn't a thread safe operation so can't be done
622 when other threads are running that will be using the list.
623
23324ae1
FM
624 The list does not own the factories, so removing a factory does not delete it.
625 */
626 void Remove();
627};
628
629
e54c96f1 630
23324ae1
FM
631/**
632 @class wxFilterOutputStream
7c913512 633
c977fa84
FM
634 A filter stream has the capability of a normal stream but it can be placed
635 on top of another stream. So, for example, it can compress, encrypt the data
636 which are passed to it and write them to another stream.
637
638 @note
639 The use of this class is exactly the same as of wxOutputStream.
640 Only a constructor differs and it is documented below.
7c913512 641
23324ae1
FM
642 @library{wxbase}
643 @category{streams}
7c913512 644
e54c96f1 645 @see wxFilterClassFactory, wxFilterInputStream
23324ae1
FM
646*/
647class wxFilterOutputStream : public wxOutputStream
648{
649public:
650 //@{
651 /**
652 Initializes a "filter" stream.
c977fa84 653
23324ae1
FM
654 If the parent stream is passed as a pointer then the new filter stream
655 takes ownership of it. If it is passed by reference then it does not.
656 */
657 wxFilterOutputStream(wxOutputStream& stream);
7c913512 658 wxFilterOutputStream(wxOutputStream* stream);
23324ae1
FM
659 //@}
660};
661
662
e54c96f1 663
23324ae1
FM
664/**
665 @class wxFilterInputStream
7c913512 666
23324ae1 667 A filter stream has the capability of a normal stream but it can be placed on
c977fa84
FM
668 top of another stream. So, for example, it can uncompress or decrypt the data which
669 are read from another stream and pass it to the requester.
670
671 @note
672 The interface of this class is the same as that of wxInputStream.
673 Only a constructor differs and it is documented below.
7c913512 674
23324ae1
FM
675 @library{wxbase}
676 @category{streams}
7c913512 677
e54c96f1 678 @see wxFilterClassFactory, wxFilterOutputStream
23324ae1
FM
679*/
680class wxFilterInputStream : public wxInputStream
681{
682public:
683 //@{
684 /**
685 Initializes a "filter" stream.
c977fa84 686
23324ae1
FM
687 If the parent stream is passed as a pointer then the new filter stream
688 takes ownership of it. If it is passed by reference then it does not.
689 */
690 wxFilterInputStream(wxInputStream& stream);
7c913512 691 wxFilterInputStream(wxInputStream* stream);
23324ae1
FM
692 //@}
693};
694
695
e54c96f1 696
23324ae1
FM
697/**
698 @class wxBufferedOutputStream
7c913512 699
23324ae1 700 This stream acts as a cache. It caches the bytes to be written to the specified
c977fa84
FM
701 output stream (See wxFilterOutputStream). The data is only written when the
702 cache is full, when the buffered stream is destroyed or when calling SeekO().
7c913512 703
23324ae1
FM
704 This class may not be used without some other stream to write the data
705 to (such as a file stream or a memory stream).
7c913512 706
23324ae1
FM
707 @library{wxbase}
708 @category{streams}
7c913512 709
e54c96f1 710 @see wxStreamBuffer, wxOutputStream
23324ae1
FM
711*/
712class wxBufferedOutputStream : public wxFilterOutputStream
713{
714public:
715 /**
f42c1512
VZ
716 Constructor using the provided buffer or default.
717
718 @param stream
719 The associated low-level stream.
720 @param buffer
721 The buffer to use if non-@NULL. Notice that the ownership of this
722 buffer is taken by the stream, i.e. it will delete it. If this
723 parameter is @NULL a default 1KB buffer is used.
23324ae1 724 */
98ccd545
FM
725 wxBufferedOutputStream(wxOutputStream& stream,
726 wxStreamBuffer *buffer = NULL);
f42c1512
VZ
727
728 /**
729 Constructor allowing to specify the size of the buffer.
730
731 This is just a more convenient alternative to creating a wxStreamBuffer
732 of the given size and using the other overloaded constructor of this
733 class.
734
735 @param stream
736 The associated low-level stream.
737 @param bufsize
738 The size of the buffer, in bytes.
739
740 @since 2.9.0
741 */
742 wxBufferedOutputStream(wxOutputStream& stream, size_t bufsize);
743
23324ae1
FM
744 /**
745 Destructor. Calls Sync() and destroys the internal buffer.
746 */
98ccd545 747 virtual ~wxBufferedOutputStream();
23324ae1
FM
748
749 /**
750 Calls Sync() and changes the stream position.
751 */
78e37b46 752 virtual wxFileOffset SeekO(wxFileOffset pos, wxSeekMode mode = wxFromStart);
23324ae1
FM
753
754 /**
755 Flushes the buffer and calls Sync() on the parent stream.
756 */
98ccd545 757 virtual void Sync();
23324ae1
FM
758};
759
760
e54c96f1 761
23324ae1
FM
762/**
763 @class wxInputStream
7c913512 764
23324ae1 765 wxInputStream is an abstract base class which may not be used directly.
7c913512 766
23324ae1
FM
767 @library{wxbase}
768 @category{streams}
769*/
770class wxInputStream : public wxStreamBase
771{
772public:
773 /**
774 Creates a dummy input stream.
775 */
776 wxInputStream();
777
778 /**
779 Destructor.
780 */
adaaa686 781 virtual ~wxInputStream();
23324ae1
FM
782
783 /**
784 Returns @true if some data is available in the stream right now, so that
785 calling Read() wouldn't block.
786 */
adaaa686 787 virtual bool CanRead() const;
23324ae1
FM
788
789 /**
790 Returns @true after an attempt has been made to read past the end of the
791 stream.
792 */
adaaa686 793 virtual bool Eof() const;
23324ae1
FM
794
795 /**
7c913512 796 Returns the first character in the input queue and removes it,
23324ae1
FM
797 blocking until it appears if necessary.
798 */
5267aefd 799 int GetC();
23324ae1
FM
800
801 /**
802 Returns the last number of bytes read.
803 */
adaaa686 804 virtual size_t LastRead() const;
23324ae1
FM
805
806 /**
807 Returns the first character in the input queue without removing it.
808 */
adaaa686 809 virtual char Peek();
23324ae1 810
c977fa84
FM
811 /**
812 Reads the specified amount of bytes and stores the data in buffer.
813
814 @warning
815 The buffer absolutely needs to have at least the specified size.
816
817 @return This function returns a reference on the current object, so the
818 user can test any states of the stream right away.
819 */
820 wxInputStream Read(void* buffer, size_t size);
821
23324ae1
FM
822 /**
823 Reads data from the input queue and stores it in the specified output stream.
824 The data is read until an error is raised by one of the two streams.
3c4f71cc 825
d29a9a8a 826 @return This function returns a reference on the current object, so the
c977fa84 827 user can test any states of the stream right away.
23324ae1 828 */
c977fa84 829 wxInputStream& Read(wxOutputStream& stream_out);
23324ae1
FM
830
831 /**
832 Changes the stream current position.
3c4f71cc 833
7c913512 834 @param pos
4cc4bfaf 835 Offset to seek to.
7c913512 836 @param mode
4cc4bfaf 837 One of wxFromStart, wxFromEnd, wxFromCurrent.
3c4f71cc 838
d29a9a8a 839 @return The new stream position or wxInvalidOffset on error.
23324ae1 840 */
18e8e19b 841 virtual wxFileOffset SeekI(wxFileOffset pos, wxSeekMode mode = wxFromStart);
23324ae1
FM
842
843 /**
844 Returns the current stream position.
845 */
18e8e19b 846 virtual wxFileOffset TellI() const;
23324ae1 847
c977fa84
FM
848 /**
849 This function is only useful in read mode.
850 It is the manager of the "Write-Back" buffer. This buffer acts like a
851 temporary buffer where data which has to be read during the next read IO
852 call are put. This is useful when you get a big block of data which you
853 didn't want to read: you can replace them at the top of the input queue
854 by this way.
855
856 Be very careful about this call in connection with calling SeekI() on
857 the same stream. Any call to SeekI() will invalidate any previous call
858 to this method (otherwise you could SeekI() to one position, "unread" a
859 few bytes there, SeekI() to another position and data would be either
860 lost or corrupted).
861
862 @return Returns the amount of bytes saved in the Write-Back buffer.
863 */
864 size_t Ungetch(const char* buffer, size_t size);
865
23324ae1
FM
866 /**
867 This function acts like the previous one except that it takes only one
868 character: it is sometimes shorter to use than the generic function.
869 */
7c913512 870 Return value bool Ungetch(char c);
23324ae1
FM
871};
872
873
c977fa84
FM
874/**
875 These enumeration values are returned by various functions in the context
876 of wxStream classes.
877*/
878enum wxStreamError
879{
880 wxSTREAM_NO_ERROR = 0, //!< No error occurred.
881 wxSTREAM_EOF, //!< EOF reached in Read() or similar.
882 wxSTREAM_WRITE_ERROR, //!< generic write error on the last write call.
883 wxSTREAM_READ_ERROR //!< generic read error on the last read call.
884};
e54c96f1 885
23324ae1
FM
886/**
887 @class wxStreamBase
7c913512 888
c977fa84
FM
889 This class is the base class of most stream related classes in wxWidgets.
890 It must not be used directly.
7c913512 891
23324ae1
FM
892 @library{wxbase}
893 @category{streams}
7c913512 894
e54c96f1 895 @see wxStreamBuffer
23324ae1 896*/
7c913512 897class wxStreamBase
23324ae1
FM
898{
899public:
900 /**
901 Creates a dummy stream object. It doesn't do anything.
902 */
903 wxStreamBase();
904
905 /**
906 Destructor.
907 */
adaaa686 908 virtual ~wxStreamBase();
23324ae1
FM
909
910 /**
911 This function returns the last error.
23324ae1 912 */
328f5751 913 wxStreamError GetLastError() const;
23324ae1
FM
914
915 /**
c977fa84
FM
916 Returns the length of the stream in bytes. If the length cannot be
917 determined (this is always the case for socket streams for example),
918 returns @c wxInvalidOffset.
3c4f71cc 919
1e24c2af 920 @since 2.5.4
23324ae1 921 */
adaaa686 922 virtual wxFileOffset GetLength() const;
23324ae1
FM
923
924 /**
c977fa84
FM
925 This function returns the size of the stream.
926 For example, for a file it is the size of the file.
927
928 @warning
929 There are streams which do not have size by definition, such as socket
930 streams. In that cases, GetSize returns 0 so you should always test its
931 return value.
23324ae1 932 */
adaaa686 933 virtual size_t GetSize() const;
23324ae1
FM
934
935 /**
936 Returns @true if no error occurred on the stream.
3c4f71cc 937
4cc4bfaf 938 @see GetLastError()
23324ae1 939 */
328f5751 940 virtual bool IsOk() const;
23324ae1
FM
941
942 /**
943 Returns @true if the streams supports seeking to arbitrary offsets.
944 */
adaaa686 945 virtual bool IsSeekable() const;
23324ae1
FM
946
947 /**
948 Internal function. It is called when the stream wants to read data of the
949 specified size. It should return the size that was actually read.
950 */
951 size_t OnSysRead(void* buffer, size_t bufsize);
952
953 /**
0004982c 954 See OnSysRead().
23324ae1 955 */
0004982c
FM
956 size_t OnSysWrite(const void* buffer, size_t bufsize);
957
958
959protected:
23324ae1
FM
960
961 /**
c977fa84 962 Internal function.
0004982c 963 It is called when the stream needs to change the current position.
23324ae1 964 */
0004982c 965 virtual wxFileOffset OnSysSeek(wxFileOffset pos, wxSeekMode mode);
23324ae1
FM
966
967 /**
0004982c
FM
968 Internal function.
969 It is called when the stream needs to know the real position.
23324ae1 970 */
0004982c 971 virtual wxFileOffset OnSysTell() const;
23324ae1 972};
e54c96f1 973