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