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