]> git.saurik.com Git - wxWidgets.git/blame - include/wx/stream.h
cosmetic, added some comment
[wxWidgets.git] / include / wx / stream.h
CommitLineData
32fc4afb
GL
1/////////////////////////////////////////////////////////////////////////////
2// Name: stream.h
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
a533f5c1
RR
41#define wxStream_NOERROR wxSTR_NOERROR
42#define wxStream_EOF wxSTR_EOF
43#define wxStream_WRITE_ERR wxSTR_WRITE_ERROR
44#define wxStream_READ_ERR wxSTR_READ_ERROR
45
75ed1d15 46typedef enum {
926c550d
GL
47 wxStream_NOERROR = 0,
48 wxStream_EOF,
49 wxStream_WRITE_ERR,
50 wxStream_READ_ERR
75ed1d15
GL
51} wxStreamError;
52
53class WXDLLEXPORT wxStreamBase {
54 public:
55 wxStreamBase();
56 virtual ~wxStreamBase();
57
84b46c35
GL
58 wxStreamError LastError() const { return m_lasterror; }
59 virtual size_t StreamSize() const { return ~((size_t)0); }
75ed1d15
GL
60
61 protected:
75ed1d15
GL
62
63 virtual size_t OnSysRead(void *buffer, size_t bufsize);
64 virtual size_t OnSysWrite(const void *buffer, size_t bufsize);
65 virtual off_t OnSysSeek(off_t seek, wxSeekMode mode);
84b46c35 66 virtual off_t OnSysTell() const;
75ed1d15
GL
67
68 protected:
fae05df5
GL
69 friend class wxStreamBuffer;
70
75ed1d15
GL
71 size_t m_lastcount;
72 wxStreamError m_lasterror;
73};
74
75class WXDLLEXPORT wxInputStream: public wxStreamBase {
32fc4afb
GL
76 public:
77 wxInputStream();
78 virtual ~wxInputStream();
79
1678ad78 80 // IO functions
75ed1d15
GL
81 virtual char Peek();
82 char GetC();
41895a05 83 virtual wxInputStream& Read(void *buffer, size_t size);
32fc4afb
GL
84 wxInputStream& Read(wxOutputStream& stream_out);
85
1678ad78 86 // Position functions
fae05df5
GL
87 virtual off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart);
88 virtual off_t TellI() const;
32fc4afb 89
1678ad78 90 // State functions
fae05df5
GL
91 virtual size_t LastRead() { return wxStreamBase::m_lastcount; }
92
93 // Ungetch
94 size_t Ungetch(void *buffer, size_t size);
95 bool Ungetch(char c);
0cd9bfe8 96
1678ad78 97 // Operators
0cd9bfe8 98 wxInputStream& operator>>(wxOutputStream& out) { return Read(out); }
47d67540 99#if wxUSE_SERIAL
123a7fdd 100 wxInputStream& operator>>(wxObject *& obj);
fcc6dddd 101#endif
1678ad78
GL
102 wxInputStream& operator>>( __wxInputManip func) { return func(*this); }
103
104 protected:
fae05df5
GL
105 // Ungetch managers
106 char *m_wback;
107 size_t m_wbacksize;
108 size_t m_wbackcur;
109
110 char *AllocSpaceWBack(size_t needed_size);
111 size_t GetWBack(char *buf, size_t bsize);
112
32fc4afb
GL
113};
114
75ed1d15 115class WXDLLEXPORT wxOutputStream: public wxStreamBase {
32fc4afb
GL
116 public:
117 wxOutputStream();
118 virtual ~wxOutputStream();
119
41895a05 120 virtual wxOutputStream& Write(const void *buffer, size_t size);
32fc4afb
GL
121 wxOutputStream& Write(wxInputStream& stream_in);
122
fae05df5
GL
123 virtual off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart);
124 virtual off_t TellO() const;
1678ad78 125
fae05df5 126 virtual size_t LastWrite() const { return wxStreamBase::m_lastcount; }
32fc4afb 127
fae05df5 128 virtual void Sync();
32fc4afb 129
1678ad78 130 wxOutputStream& operator<<(wxInputStream& out) { return Write(out); }
47d67540 131#if wxUSE_SERIAL
123a7fdd 132 wxOutputStream& operator<<(wxObject& obj);
fcc6dddd 133#endif
1678ad78 134 wxOutputStream& operator<<( __wxOutputManip func) { return func(*this); }
32fc4afb
GL
135};
136
f4ada568
GL
137// ---------------------------------------------------------------------------
138// "Filter" streams
139// ---------------------------------------------------------------------------
32fc4afb 140
75ed1d15 141class WXDLLEXPORT wxFilterInputStream: public wxInputStream {
32fc4afb 142 public:
f4ada568 143 wxFilterInputStream();
32fc4afb 144 wxFilterInputStream(wxInputStream& stream);
f4ada568 145 ~wxFilterInputStream();
32fc4afb 146
f4ada568 147 char Peek() { return m_parent_i_stream->Peek(); }
32fc4afb 148
84b46c35
GL
149 wxStreamError LastError() const { return m_parent_i_stream->LastError(); }
150 size_t StreamSize() const { return m_parent_i_stream->StreamSize(); }
151
32fc4afb
GL
152 protected:
153 wxInputStream *m_parent_i_stream;
154};
155
75ed1d15 156class WXDLLEXPORT wxFilterOutputStream: public wxOutputStream {
32fc4afb 157 public:
f4ada568 158 wxFilterOutputStream();
32fc4afb 159 wxFilterOutputStream(wxOutputStream& stream);
75ed1d15 160 ~wxFilterOutputStream();
1678ad78 161
84b46c35
GL
162 wxStreamError LastError() const { return m_parent_o_stream->LastError(); }
163 size_t StreamSize() const { return m_parent_o_stream->StreamSize(); }
164
32fc4afb
GL
165 protected:
166 wxOutputStream *m_parent_o_stream;
167};
168
fae05df5
GL
169// ---------------------------------------------------------------------------
170// Stream buffer
171// ---------------------------------------------------------------------------
172
173class WXDLLEXPORT wxStreamBuffer {
174 public:
175 typedef enum {
176 read = 0, write, read_write
177 } BufMode;
178
179 // -----------
180 // ctor & dtor
181 // -----------
182 wxStreamBuffer(wxStreamBase& stream, BufMode mode);
183 wxStreamBuffer(BufMode mode);
184 wxStreamBuffer(const wxStreamBuffer& buf);
185 ~wxStreamBuffer();
186
187 // -----------
188 // Filtered IO
189 // -----------
190 size_t Read(void *buffer, size_t size);
191 size_t Read(wxStreamBuffer *buf);
192 size_t Write(const void *buffer, size_t size);
193 size_t Write(wxStreamBuffer *buf);
194
195 char GetChar();
196 void PutChar(char c);
197 off_t Tell() const;
198 off_t Seek(off_t pos, wxSeekMode mode);
199
200 // --------------
201 // Buffer control
202 // --------------
203 void ResetBuffer();
204 void SetBufferIO(char *buffer_start, char *buffer_end);
205 void SetBufferIO(size_t bufsize);
206 char *GetBufferStart() const { return m_buffer_start; }
207 char *GetBufferEnd() const { return m_buffer_end; }
208 char *GetBufferPos() const { return m_buffer_pos; }
209 off_t GetIntPosition() const { return m_buffer_pos-m_buffer_start; }
210 void SetIntPosition(off_t pos) { m_buffer_pos = m_buffer_start+pos; }
211 size_t GetLastAccess() const { return m_buffer_end-m_buffer_start; }
212
213 void Fixed(bool fixed) { m_fixed = fixed; }
214 void Flushable(bool f) { m_flushable = f; }
215
216 bool FlushBuffer();
217 bool FillBuffer();
218 size_t GetDataLeft();
219
220 // --------------
221 // Administration
222 // --------------
223 wxStreamBase *Stream() { return m_stream; }
224
225 protected:
226 void GetFromBuffer(void *buffer, size_t size);
227 void PutToBuffer(const void *buffer, size_t size);
228
229 protected:
230 char *m_buffer_start, *m_buffer_end, *m_buffer_pos;
231 size_t m_buffer_size;
232
233 char *m_wback;
234 size_t m_wbacksize, m_wbackcur;
235
236 bool m_fixed, m_flushable;
237
238 wxStreamBase *m_stream;
239 BufMode m_mode;
240 bool m_destroybuf, m_destroystream;
241};
242
243// ---------------------------------------------------------------------------
244// wxBufferedStreams
245// ---------------------------------------------------------------------------
246
247class wxBufferedInputStream: public wxFilterInputStream {
248 public:
249 wxBufferedInputStream(wxInputStream& stream);
250 ~wxBufferedInputStream();
251
252 wxInputStream& Read(void *buffer, size_t size);
253
254 // Position functions
255 off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart);
256 off_t TellI() const;
257
258 wxStreamBuffer *InputStreamBuffer() const { return m_i_streambuf; }
259
260 protected:
261 size_t OnSysRead(void *buffer, size_t bufsize);
262 off_t OnSysSeek(off_t seek, wxSeekMode mode);
263 off_t OnSysTell() const;
264
265 protected:
266 wxStreamBuffer *m_i_streambuf;
267};
268
269class wxBufferedOutputStream: public wxFilterOutputStream {
270 public:
271 wxBufferedOutputStream(wxOutputStream& stream);
272 ~wxBufferedOutputStream();
273
274 wxOutputStream& Write(const void *buffer, size_t size);
275
276 // Position functions
277 off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart);
278 off_t TellO() const;
279
280 void Sync();
281
282 wxStreamBuffer *OutputStreamBuffer() const { return m_o_streambuf; }
283
284 protected:
285 size_t OnSysWrite(const void *buffer, size_t bufsize);
286 off_t OnSysSeek(off_t seek, wxSeekMode mode);
287 off_t OnSysTell() const;
288
289 protected:
290 wxStreamBuffer *m_o_streambuf;
291};
292
32fc4afb 293#endif
ce4169a4
RR
294 // wxUSE_STREAMS
295
296#endif
297 // _WX_WXSTREAM_H__