]> git.saurik.com Git - wxWidgets.git/blame - include/wx/stream.h
superposition of text styles in wxTextCtrl now works as expected (and as documented)
[wxWidgets.git] / include / wx / stream.h
CommitLineData
32fc4afb 1/////////////////////////////////////////////////////////////////////////////
49e399d8 2// Name: wx/stream.h
32fc4afb
GL
3// Purpose: "wxWindows stream" base classes
4// Author: Guilhem Lavaux
5// Modified by:
6// Created: 11/07/98
7// RCS-ID: $Id$
8// Copyright: (c) Guilhem Lavaux
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
34138703
JS
12#ifndef _WX_WXSTREAM_H__
13#define _WX_WXSTREAM_H__
32fc4afb
GL
14
15#ifdef __GNUG__
79c3e0e1 16#pragma interface
32fc4afb
GL
17#endif
18
ce4169a4
RR
19#include "wx/defs.h"
20
21#if wxUSE_STREAMS
22
32fc4afb 23#include <stdio.h>
45ea509a
VZ
24#include "wx/object.h"
25#include "wx/string.h"
1678ad78
GL
26#include "wx/filefn.h" // for off_t, wxInvalidOffset and wxSeekMode
27
75ed1d15 28class WXDLLEXPORT wxStreamBase;
1678ad78
GL
29class WXDLLEXPORT wxInputStream;
30class WXDLLEXPORT wxOutputStream;
31
32typedef wxInputStream& (*__wxInputManip)(wxInputStream&);
33typedef wxOutputStream& (*__wxOutputManip)(wxOutputStream&);
34
184b5d99 35WXDLLEXPORT wxOutputStream& wxEndL(wxOutputStream& o_stream);
1678ad78 36
f4ada568
GL
37// ---------------------------------------------------------------------------
38// wxStream: base classes
39// ---------------------------------------------------------------------------
40
49e399d8 41enum wxStreamError
cd6ce4a9 42{
47fc03d2
VZ
43 wxSTREAM_NO_ERROR = 0,
44 wxSTREAM_NO_ERR = wxSTREAM_NO_ERROR,
45 wxSTREAM_NOERROR = wxSTREAM_NO_ERROR,
657d2097 46
47fc03d2 47 wxSTREAM_EOF,
657d2097 48
47fc03d2
VZ
49 wxSTREAM_WRITE_ERROR,
50 wxSTREAM_WRITE_ERR = wxSTREAM_WRITE_ERROR,
657d2097 51
47fc03d2
VZ
52 wxSTREAM_READ_ERROR,
53 wxSTREAM_READ_ERR = wxSTREAM_READ_ERROR
49e399d8 54};
75ed1d15 55
cd6ce4a9
VZ
56// compatibility
57#define wxStream_NOERROR wxSTREAM_NOERROR
58#define wxStream_EOF wxSTREAM_EOF
59#define wxStream_WRITE_ERR wxSTREAM_WRITE_ERROR
60#define wxStream_READ_ERR wxSTREAM_READ_ERROR
61
657d2097 62class WXDLLEXPORT wxStreamBase
c7a9fa36
RR
63{
64public:
65 wxStreamBase();
66 virtual ~wxStreamBase();
75ed1d15 67
cd6ce4a9 68 // error testing
cd6ce4a9
VZ
69 wxStreamError GetLastError() const { return m_lasterror; }
70 bool IsOk() const { return LastError() == wxSTREAM_NOERROR; }
71 bool operator!() const { return LastError() != wxSTREAM_NOERROR; }
72
47fc03d2
VZ
73 virtual size_t GetSize() const { return 0; }
74
75 // deprecated, for compatibility only
76 wxStreamError LastError() const { return m_lasterror; }
c7a9fa36 77 size_t StreamSize() const { return GetSize(); }
75ed1d15 78
c7a9fa36 79protected:
47fc03d2
VZ
80 // VZ: these functions are really pure virtual and shouldn't be declared
81 // in the base class because it creates ambiguties in stream classes
82 // deriving from both wxInputStream and wxOutputStream
83#if 0
c7a9fa36
RR
84 virtual size_t OnSysRead(void *buffer, size_t bufsize);
85 virtual size_t OnSysWrite(const void *buffer, size_t bufsize);
47fc03d2
VZ
86#endif
87
c7a9fa36
RR
88 virtual off_t OnSysSeek(off_t seek, wxSeekMode mode);
89 virtual off_t OnSysTell() const;
75ed1d15 90
c7a9fa36 91 friend class wxStreamBuffer;
75ed1d15 92
c7a9fa36
RR
93 size_t m_lastcount;
94 wxStreamError m_lasterror;
75ed1d15
GL
95};
96
47fc03d2 97class WXDLLEXPORT wxInputStream : /* virtual */ public wxStreamBase
c7a9fa36
RR
98{
99public:
100 wxInputStream();
101 virtual ~wxInputStream();
32fc4afb 102
cd6ce4a9
VZ
103 // is the stream at EOF?
104 virtual bool Eof() const;
105
c7a9fa36
RR
106 // IO functions
107 virtual char Peek();
108 char GetC();
109 virtual wxInputStream& Read(void *buffer, size_t size);
110 wxInputStream& Read(wxOutputStream& stream_out);
32fc4afb 111
c7a9fa36
RR
112 // Position functions
113 virtual off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart);
114 virtual off_t TellI() const;
32fc4afb 115
c7a9fa36
RR
116 // State functions
117 virtual size_t LastRead() { return wxStreamBase::m_lastcount; }
fae05df5 118
c7a9fa36
RR
119 // Ungetch
120 size_t Ungetch(const void *buffer, size_t size);
121 bool Ungetch(char c);
0cd9bfe8 122
c7a9fa36
RR
123 // Operators
124 wxInputStream& operator>>(wxOutputStream& out) { return Read(out); }
47d67540 125#if wxUSE_SERIAL
c7a9fa36 126 wxInputStream& operator>>(wxObject *& obj);
fcc6dddd 127#endif
c7a9fa36 128 wxInputStream& operator>>( __wxInputManip func) { return func(*this); }
fae05df5 129
c7a9fa36 130protected:
47fc03d2
VZ
131 // to be implemented in the derived classes (it should have been pure
132 // virtual)
133 virtual size_t OnSysRead(void *buffer, size_t bufsize);
134
c7a9fa36
RR
135 // Ungetch managers
136 char *m_wback;
137 size_t m_wbacksize;
138 size_t m_wbackcur;
fae05df5 139
c7a9fa36 140 char *AllocSpaceWBack(size_t needed_size);
49e399d8 141 size_t GetWBack(void *buf, size_t bsize);
47fc03d2
VZ
142
143 friend class wxStreamBuffer;
32fc4afb
GL
144};
145
47fc03d2 146class WXDLLEXPORT wxOutputStream : /* virtual */ public wxStreamBase
c7a9fa36
RR
147{
148public:
149 wxOutputStream();
150 virtual ~wxOutputStream();
32fc4afb 151
c7a9fa36
RR
152 void PutC(char c);
153 virtual wxOutputStream& Write(const void *buffer, size_t size);
154 wxOutputStream& Write(wxInputStream& stream_in);
32fc4afb 155
c7a9fa36
RR
156 virtual off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart);
157 virtual off_t TellO() const;
1678ad78 158
c7a9fa36 159 virtual size_t LastWrite() const { return wxStreamBase::m_lastcount; }
32fc4afb 160
c7a9fa36 161 virtual void Sync();
32fc4afb 162
c7a9fa36 163 wxOutputStream& operator<<(wxInputStream& out) { return Write(out); }
47d67540 164#if wxUSE_SERIAL
c7a9fa36 165 wxOutputStream& operator<<(wxObject& obj);
fcc6dddd 166#endif
c7a9fa36 167 wxOutputStream& operator<<( __wxOutputManip func) { return func(*this); }
47fc03d2
VZ
168
169protected:
170 // to be implemented in the derived classes (it should have been pure
171 // virtual)
172 virtual size_t OnSysWrite(const void *buffer, size_t bufsize);
173
174 friend class wxStreamBuffer;
32fc4afb
GL
175};
176
e2acb9ae
RR
177// ---------------------------------------------------------------------------
178// A stream for measuring streamed output
179// ---------------------------------------------------------------------------
180
47fc03d2 181class WXDLLEXPORT wxCountingOutputStream : public wxOutputStream
c7a9fa36
RR
182{
183public:
184 wxCountingOutputStream();
e2acb9ae 185
c7a9fa36
RR
186 size_t GetSize() const;
187 bool Ok() const { return TRUE; }
e2acb9ae 188
c7a9fa36 189protected:
47fc03d2
VZ
190 virtual size_t OnSysWrite(const void *buffer, size_t size);
191 virtual off_t OnSysSeek(off_t pos, wxSeekMode mode);
192 virtual off_t OnSysTell() const;
e2acb9ae 193
c7a9fa36 194 size_t m_currentPos;
e2acb9ae
RR
195};
196
f4ada568
GL
197// ---------------------------------------------------------------------------
198// "Filter" streams
199// ---------------------------------------------------------------------------
32fc4afb 200
47fc03d2 201class WXDLLEXPORT wxFilterInputStream : public wxInputStream
c7a9fa36
RR
202{
203public:
204 wxFilterInputStream();
205 wxFilterInputStream(wxInputStream& stream);
47fc03d2 206 virtual ~wxFilterInputStream();
32fc4afb 207
c7a9fa36 208 char Peek() { return m_parent_i_stream->Peek(); }
32fc4afb 209
c7a9fa36 210 size_t GetSize() const { return m_parent_i_stream->GetSize(); }
84b46c35 211
47fc03d2
VZ
212 wxInputStream *GetFilterInputStream() const { return m_parent_i_stream; }
213
c7a9fa36
RR
214protected:
215 wxInputStream *m_parent_i_stream;
32fc4afb
GL
216};
217
47fc03d2 218class WXDLLEXPORT wxFilterOutputStream : public wxOutputStream
c7a9fa36
RR
219{
220public:
221 wxFilterOutputStream();
222 wxFilterOutputStream(wxOutputStream& stream);
47fc03d2 223 virtual ~wxFilterOutputStream();
1678ad78 224
c7a9fa36 225 size_t GetSize() const { return m_parent_o_stream->GetSize(); }
84b46c35 226
47fc03d2
VZ
227 wxOutputStream *GetFilterOutputStream() const { return m_parent_o_stream; }
228
c7a9fa36
RR
229protected:
230 wxOutputStream *m_parent_o_stream;
32fc4afb
GL
231};
232
fae05df5 233// ---------------------------------------------------------------------------
47fc03d2
VZ
234// Stream buffer: this class can be derived from and passed to
235// wxBufferedStreams to implement custom buffering
fae05df5
GL
236// ---------------------------------------------------------------------------
237
657d2097 238class WXDLLEXPORT wxStreamBuffer
c7a9fa36
RR
239{
240public:
49e399d8
VZ
241 enum BufMode
242 {
243 read,
244 write,
245 read_write
246 };
c7a9fa36
RR
247
248 wxStreamBuffer(wxStreamBase& stream, BufMode mode);
249 wxStreamBuffer(BufMode mode);
250 wxStreamBuffer(const wxStreamBuffer& buf);
47fc03d2 251 virtual ~wxStreamBuffer();
c7a9fa36
RR
252
253 // Filtered IO
47fc03d2 254 virtual size_t Read(void *buffer, size_t size);
c7a9fa36 255 size_t Read(wxStreamBuffer *buf);
47fc03d2 256 virtual size_t Write(const void *buffer, size_t size);
c7a9fa36
RR
257 size_t Write(wxStreamBuffer *buf);
258
47fc03d2
VZ
259 virtual char Peek();
260 virtual char GetChar();
261 virtual void PutChar(char c);
262 virtual off_t Tell() const;
263 virtual off_t Seek(off_t pos, wxSeekMode mode);
c7a9fa36
RR
264
265 // Buffer control
266 void ResetBuffer();
49e399d8
VZ
267
268 // NB: the buffer must always be allocated with malloc() if takeOwn is
269 // TRUE as it will be deallocated by free()
270 void SetBufferIO(void *start, void *end, bool takeOwnership = FALSE);
67c8c225 271 void SetBufferIO(void *start, size_t len, bool takeOwnership = FALSE);
c7a9fa36 272 void SetBufferIO(size_t bufsize);
49e399d8
VZ
273 void *GetBufferStart() const { return m_buffer_start; }
274 void *GetBufferEnd() const { return m_buffer_end; }
275 void *GetBufferPos() const { return m_buffer_pos; }
67c8c225 276 size_t GetBufferSize() const { return m_buffer_size; }
49e399d8
VZ
277 size_t GetIntPosition() const { return m_buffer_pos - m_buffer_start; }
278 void SetIntPosition(size_t pos) { m_buffer_pos = m_buffer_start + pos; }
279 size_t GetLastAccess() const { return m_buffer_end - m_buffer_start; }
280 size_t GetBytesLeft() const { return m_buffer_end - m_buffer_pos; }
c7a9fa36
RR
281
282 void Fixed(bool fixed) { m_fixed = fixed; }
283 void Flushable(bool f) { m_flushable = f; }
284
285 bool FlushBuffer();
286 bool FillBuffer();
287 size_t GetDataLeft();
288
49e399d8
VZ
289 // misc accessors
290 wxStreamBase *GetStream() const { return m_stream; }
291 bool HasBuffer() const { return m_buffer_size != 0; }
292
293 bool IsFixed() const { return m_fixed; }
294 bool IsFlushable() const { return m_flushable; }
295
47fc03d2
VZ
296 // only for input/output buffers respectively, returns NULL otherwise
297 wxInputStream *GetInputStream() const;
298 wxOutputStream *GetOutputStream() const;
299
49e399d8 300 // deprecated, for compatibility only
c7a9fa36
RR
301 wxStreamBase *Stream() { return m_stream; }
302
303protected:
304 void GetFromBuffer(void *buffer, size_t size);
305 void PutToBuffer(const void *buffer, size_t size);
306
49e399d8
VZ
307 // set the last error to the specified value if we didn't have it before
308 void SetError(wxStreamError err);
309
310 // common part of several ctors
311 void Init();
c7a9fa36 312
49e399d8
VZ
313 // init buffer variables to be empty
314 void InitBuffer();
c7a9fa36 315
49e399d8
VZ
316 // free the buffer (always safe to call)
317 void FreeBuffer();
318
319 // the buffer itself: the pointers to its start and end and the current
320 // position in the buffer
321 char *m_buffer_start,
322 *m_buffer_end,
323 *m_buffer_pos;
324
325 // the buffer size
326 // FIXME: isn't it the same as m_buffer_end - m_buffer_start? (VZ)
327 size_t m_buffer_size;
328
329 // the stream we're associated with
c7a9fa36 330 wxStreamBase *m_stream;
49e399d8
VZ
331
332 // its mode
c7a9fa36 333 BufMode m_mode;
49e399d8
VZ
334
335 // flags
336 bool m_destroybuf, // deallocate buffer?
337 m_destroystream, // delete associated stream?
338 m_fixed,
339 m_flushable;
fae05df5
GL
340};
341
342// ---------------------------------------------------------------------------
343// wxBufferedStreams
344// ---------------------------------------------------------------------------
345
47fc03d2 346class WXDLLEXPORT wxBufferedInputStream : public wxFilterInputStream
c7a9fa36
RR
347{
348public:
47fc03d2
VZ
349 // if a non NULL buffer is given to the stream, it will be deleted by it
350 wxBufferedInputStream(wxInputStream& stream,
351 wxStreamBuffer *buffer = NULL);
352 virtual ~wxBufferedInputStream();
fae05df5 353
c7a9fa36
RR
354 char Peek();
355 wxInputStream& Read(void *buffer, size_t size);
657d2097 356
c7a9fa36
RR
357 // Position functions
358 off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart);
359 off_t TellI() const;
fae05df5 360
47fc03d2
VZ
361 // the buffer given to the stream will be deleted by it
362 void SetInputStreamBuffer(wxStreamBuffer *buffer);
363 wxStreamBuffer *GetInputStreamBuffer() const { return m_i_streambuf; }
364
365 // deprecated, for compatibility only
c7a9fa36 366 wxStreamBuffer *InputStreamBuffer() const { return m_i_streambuf; }
fae05df5 367
c7a9fa36 368protected:
47fc03d2
VZ
369 virtual size_t OnSysRead(void *buffer, size_t bufsize);
370 virtual off_t OnSysSeek(off_t seek, wxSeekMode mode);
371 virtual off_t OnSysTell() const;
fae05df5 372
c7a9fa36 373 wxStreamBuffer *m_i_streambuf;
fae05df5
GL
374};
375
47fc03d2 376class WXDLLEXPORT wxBufferedOutputStream : public wxFilterOutputStream
c7a9fa36
RR
377{
378public:
47fc03d2
VZ
379 // if a non NULL buffer is given to the stream, it will be deleted by it
380 wxBufferedOutputStream(wxOutputStream& stream,
381 wxStreamBuffer *buffer = NULL);
382 virtual ~wxBufferedOutputStream();
fae05df5 383
c7a9fa36 384 wxOutputStream& Write(const void *buffer, size_t size);
657d2097 385
c7a9fa36
RR
386 // Position functions
387 off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart);
388 off_t TellO() const;
fae05df5 389
c7a9fa36 390 void Sync();
fae05df5 391
c7a9fa36 392 size_t GetSize() const;
657d2097 393
47fc03d2
VZ
394 // the buffer given to the stream will be deleted by it
395 void SetOutputStreamBuffer(wxStreamBuffer *buffer);
396 wxStreamBuffer *GetOutputStreamBuffer() const { return m_o_streambuf; }
397
398 // deprecated, for compatibility only
c7a9fa36 399 wxStreamBuffer *OutputStreamBuffer() const { return m_o_streambuf; }
fae05df5 400
c7a9fa36 401protected:
47fc03d2
VZ
402 virtual size_t OnSysWrite(const void *buffer, size_t bufsize);
403 virtual off_t OnSysSeek(off_t seek, wxSeekMode mode);
404 virtual off_t OnSysTell() const;
fae05df5 405
c7a9fa36 406 wxStreamBuffer *m_o_streambuf;
fae05df5
GL
407};
408
47fc03d2
VZ
409#endif // wxUSE_STREAMS
410
411#endif // _WX_WXSTREAM_H__
ce4169a4 412