]> git.saurik.com Git - wxWidgets.git/blob - include/wx/stream.h
Added serial code for wxList and wxHashTable to source tree
[wxWidgets.git] / include / wx / stream.h
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
12 #ifndef _WX_WXSTREAM_H__
13 #define _WX_WXSTREAM_H__
14
15 #ifdef __GNUG__
16 #pragma interface
17 #endif
18
19 #include <stdio.h>
20 #include "wx/object.h"
21 #include "wx/string.h"
22 #include "wx/filefn.h" // for off_t, wxInvalidOffset and wxSeekMode
23
24 class WXDLLEXPORT wxStreamBase;
25 class WXDLLEXPORT wxInputStream;
26 class WXDLLEXPORT wxOutputStream;
27
28 typedef wxInputStream& (*__wxInputManip)(wxInputStream&);
29 typedef wxOutputStream& (*__wxOutputManip)(wxOutputStream&);
30
31 WXDLLEXPORT wxOutputStream& wxEndL(wxOutputStream& o_stream);
32
33 // ---------------------------------------------------------------------------
34 // Stream buffer
35 // ---------------------------------------------------------------------------
36
37 class WXDLLEXPORT wxStreamBuffer {
38 public:
39 typedef enum {
40 read = 0, write, read_write
41 } BufMode;
42
43 // -----------
44 // ctor & dtor
45 // -----------
46 wxStreamBuffer(wxStreamBase& stream, BufMode mode);
47 wxStreamBuffer(BufMode mode);
48 wxStreamBuffer(const wxStreamBuffer& buf);
49 ~wxStreamBuffer();
50
51 // -----------
52 // Filtered IO
53 // -----------
54 size_t Read(void *buffer, size_t size);
55 size_t Read(wxStreamBuffer *buf);
56 size_t Write(const void *buffer, size_t size);
57 size_t Write(wxStreamBuffer *buf);
58
59 size_t WriteBack(const char *buffer, size_t size);
60 bool WriteBack(char c);
61 char GetChar();
62 void PutChar(char c);
63 off_t Tell() const;
64 off_t Seek(off_t pos, wxSeekMode mode);
65
66 // --------------
67 // Buffer control
68 // --------------
69 void ResetBuffer();
70 void SetBufferIO(char *buffer_start, char *buffer_end);
71 void SetBufferIO(size_t bufsize);
72 char *GetBufferStart() const { return m_buffer_start; }
73 char *GetBufferEnd() const { return m_buffer_end; }
74 char *GetBufferPos() const { return m_buffer_pos; }
75 off_t GetIntPosition() const { return m_buffer_pos-m_buffer_start; }
76 void SetIntPosition(off_t pos) { m_buffer_pos = m_buffer_start+pos; }
77 size_t GetLastAccess() const { return m_buffer_end-m_buffer_start; }
78
79 void Fixed(bool fixed) { m_fixed = fixed; }
80 void Flushable(bool f) { m_flushable = f; }
81
82 bool FlushBuffer();
83 bool FillBuffer();
84 size_t GetDataLeft();
85
86 // --------------
87 // Administration
88 // --------------
89 wxStreamBase *Stream() { return m_stream; }
90
91 protected:
92 char *AllocSpaceWBack(size_t needed_size);
93 size_t GetWBack(char *buf, size_t bsize);
94
95 void GetFromBuffer(void *buffer, size_t size);
96 void PutToBuffer(const void *buffer, size_t size);
97
98 protected:
99 char *m_buffer_start, *m_buffer_end, *m_buffer_pos;
100 size_t m_buffer_size;
101
102 char *m_wback;
103 size_t m_wbacksize, m_wbackcur;
104
105 bool m_fixed, m_flushable;
106
107 wxStreamBase *m_stream;
108 BufMode m_mode;
109 bool m_destroybuf, m_destroystream;
110 };
111
112 // ---------------------------------------------------------------------------
113 // wxStream: base classes
114 // ---------------------------------------------------------------------------
115
116 typedef enum {
117 wxStream_NOERROR,
118 wxStream_EOF
119 } wxStreamError;
120
121 class WXDLLEXPORT wxStreamBase {
122 public:
123 wxStreamBase();
124 virtual ~wxStreamBase();
125
126 wxStreamError LastError() const { return m_lasterror; }
127 virtual size_t StreamSize() const { return ~((size_t)0); }
128
129 protected:
130 friend class wxStreamBuffer;
131
132 virtual size_t OnSysRead(void *buffer, size_t bufsize);
133 virtual size_t OnSysWrite(const void *buffer, size_t bufsize);
134 virtual off_t OnSysSeek(off_t seek, wxSeekMode mode);
135 virtual off_t OnSysTell() const;
136
137 protected:
138 size_t m_lastcount;
139 wxStreamError m_lasterror;
140 };
141
142 class WXDLLEXPORT wxInputStream: public wxStreamBase {
143 public:
144 wxInputStream();
145 wxInputStream(wxStreamBuffer *sbuf);
146 virtual ~wxInputStream();
147
148 // IO functions
149 virtual char Peek();
150 char GetC();
151 wxInputStream& Read(void *buffer, size_t size);
152 wxInputStream& Read(wxOutputStream& stream_out);
153
154 // Position functions
155 off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart);
156 off_t TellI() const;
157
158 // State functions
159 wxStreamBuffer *InputStreamBuffer() { return m_i_streambuf; }
160 size_t LastRead() { return wxStreamBase::m_lastcount; }
161
162 // Operators
163 wxInputStream& operator>>(wxOutputStream& out) { return Read(out); }
164 wxInputStream& operator>>(wxString& line);
165 wxInputStream& operator>>(char& c);
166 wxInputStream& operator>>(short& i);
167 wxInputStream& operator>>(int& i);
168 wxInputStream& operator>>(long& i);
169 wxInputStream& operator>>(double& i);
170 #if wxUSE_SERIAL
171 wxInputStream& operator>>(wxObject *& obj);
172 #endif
173
174 wxInputStream& operator>>(float& f) { double d; operator>>((double&)d); f = (float)d; return *this; }
175 wxInputStream& operator>>(unsigned char& c) { return operator>>((char&)c); }
176 wxInputStream& operator>>(unsigned short& i) { return operator>>((short&)i); }
177 wxInputStream& operator>>(unsigned int& i) { return operator>>((int&)i); }
178 wxInputStream& operator>>(unsigned long& i) { return operator>>((long&)i); }
179 wxInputStream& operator>>( __wxInputManip func) { return func(*this); }
180
181 protected:
182 bool m_i_destroybuf;
183 wxStreamBuffer *m_i_streambuf;
184 };
185
186 class WXDLLEXPORT wxOutputStream: public wxStreamBase {
187 public:
188 wxOutputStream();
189 wxOutputStream(wxStreamBuffer *sbuf);
190 virtual ~wxOutputStream();
191
192 wxOutputStream& Write(const void *buffer, size_t size);
193 wxOutputStream& Write(wxInputStream& stream_in);
194
195 off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart);
196 off_t TellO() const;
197
198 size_t LastWrite() const { return wxStreamBase::m_lastcount; }
199 wxStreamBuffer *OutputStreamBuffer() { return m_o_streambuf; }
200
201 void Sync();
202
203 wxOutputStream& operator<<(wxInputStream& out) { return Write(out); }
204 wxOutputStream& operator<<(const char *string);
205 wxOutputStream& operator<<(wxString& string);
206 wxOutputStream& operator<<(char c);
207 wxOutputStream& operator<<(short i);
208 wxOutputStream& operator<<(int i);
209 wxOutputStream& operator<<(long i);
210 wxOutputStream& operator<<(double f);
211 #if wxUSE_SERIAL
212 wxOutputStream& operator<<(wxObject& obj);
213 #endif
214
215 wxOutputStream& operator<<(float f) { return operator<<((double)f); }
216 wxOutputStream& operator<<(unsigned char c) { return operator<<((char)c); }
217 wxOutputStream& operator<<(unsigned short i) { return operator<<((short)i); }
218 wxOutputStream& operator<<(unsigned int i) { return operator<<((int)i); }
219 wxOutputStream& operator<<(unsigned long i) { return operator<<((long)i); }
220 wxOutputStream& operator<<( __wxOutputManip func) { return func(*this); }
221
222 protected:
223 bool m_o_destroybuf;
224 wxStreamBuffer *m_o_streambuf;
225 };
226
227 // ---------------------------------------------------------------------------
228 // "Filter" streams
229 // ---------------------------------------------------------------------------
230
231 class WXDLLEXPORT wxFilterInputStream: public wxInputStream {
232 public:
233 wxFilterInputStream();
234 wxFilterInputStream(wxInputStream& stream);
235 ~wxFilterInputStream();
236
237 char Peek() { return m_parent_i_stream->Peek(); }
238
239 wxStreamError LastError() const { return m_parent_i_stream->LastError(); }
240 size_t StreamSize() const { return m_parent_i_stream->StreamSize(); }
241
242 protected:
243 wxInputStream *m_parent_i_stream;
244 };
245
246 class WXDLLEXPORT wxFilterOutputStream: public wxOutputStream {
247 public:
248 wxFilterOutputStream();
249 wxFilterOutputStream(wxOutputStream& stream);
250 ~wxFilterOutputStream();
251
252 wxStreamError LastError() const { return m_parent_o_stream->LastError(); }
253 size_t StreamSize() const { return m_parent_o_stream->StreamSize(); }
254
255 protected:
256 wxOutputStream *m_parent_o_stream;
257 };
258
259 #endif