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