]> git.saurik.com Git - wxWidgets.git/blob - include/wx/stream.h
check for empty data pointer in Len() and Empty()
[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 wxInputStream;
25 class WXDLLEXPORT wxOutputStream;
26
27 typedef wxInputStream& (*__wxInputManip)(wxInputStream&);
28 typedef wxOutputStream& (*__wxOutputManip)(wxOutputStream&);
29
30 wxOutputStream& WXDLLEXPORT wxEndL(wxOutputStream& o_stream);
31
32 // Disable warnings such as
33 // 'wxFilterStream' : inherits 'wxFilterInputStream::Peek' via dominance
34
35 #ifdef _MSC_VER
36 #pragma warning(disable:4250)
37 #endif
38
39 // ---------------------------------------------------------------------------
40 // Stream buffer
41 // ---------------------------------------------------------------------------
42
43 class WXDLLEXPORT wxStreamBuffer {
44 public:
45 wxStreamBuffer(wxInputStream& stream);
46 wxStreamBuffer(wxOutputStream& stream);
47 ~wxStreamBuffer();
48
49 void Read(void *buffer, size_t size);
50 void Write(const void *buffer, size_t size);
51 void WriteBack(char c);
52
53 void SetBufferIO(char *buffer_start, char *buffer_end);
54 void SetBufferIO(size_t bufsize);
55 void ResetBuffer();
56
57 void SetBufferPosition(char *buffer_position)
58 { m_buffer_pos = buffer_position; }
59 void SetIntPosition(size_t pos)
60 { m_buffer_pos = m_buffer_start + pos; }
61 char *GetBufferPosition() const { return m_buffer_pos; }
62 size_t GetIntPosition() const { return m_buffer_pos - m_buffer_start; }
63
64 char *GetBufferStart() const { return m_buffer_start; }
65 char *GetBufferEnd() const { return m_buffer_end; }
66 size_t GetBufferSize() const { return m_buffer_size; }
67 size_t GetLastAccess() const { return m_buffer_end - m_buffer_start; }
68
69 protected:
70 char *m_buffer_start, *m_buffer_end, *m_buffer_pos;
71 size_t m_buffer_size;
72
73 wxInputStream *m_istream;
74 wxOutputStream *m_ostream;
75 };
76
77 // ---------------------------------------------------------------------------
78 // wxStream: base classes
79 // ---------------------------------------------------------------------------
80
81 class WXDLLEXPORT wxInputStream {
82 public:
83 wxInputStream();
84 virtual ~wxInputStream();
85
86 // IO functions
87 virtual char Peek() { return 0; }
88 virtual char GetC();
89 virtual wxInputStream& Read(void *buffer, size_t size);
90 wxInputStream& Read(wxOutputStream& stream_out);
91
92 // Position functions
93 virtual off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart);
94 virtual off_t TellI() const;
95
96 // State functions
97 bool Eof() const { return m_eof; }
98 size_t LastRead() { return m_lastread; }
99 wxStreamBuffer *InputStreamBuffer() { return m_i_streambuf; }
100
101 // Operators
102 wxInputStream& operator>>(wxOutputStream& out) { return Read(out); }
103 wxInputStream& operator>>(wxString& line);
104 wxInputStream& operator>>(char& c);
105 wxInputStream& operator>>(short& i);
106 wxInputStream& operator>>(int& i);
107 wxInputStream& operator>>(long& i);
108 wxInputStream& operator>>(float& i);
109 #if USE_SERIAL
110 wxInputStream& operator>>(wxObject *& obj);
111 #endif
112
113 wxInputStream& operator>>(unsigned char& c) { return operator>>((char&)c); }
114 wxInputStream& operator>>(unsigned short& i) { return operator>>((short&)i); }
115 wxInputStream& operator>>(unsigned int& i) { return operator>>((int&)i); }
116 wxInputStream& operator>>(unsigned long& i) { return operator>>((long&)i); }
117 wxInputStream& operator>>( __wxInputManip func) { return func(*this); }
118
119 protected:
120 friend class wxStreamBuffer;
121 friend class wxFilterInputStream;
122
123 wxInputStream(wxStreamBuffer *buffer);
124
125 virtual size_t DoRead(void *WXUNUSED(buffer), size_t WXUNUSED(size) )
126 { return 0; }
127 virtual off_t DoSeekInput( off_t WXUNUSED(pos), wxSeekMode WXUNUSED(mode) )
128 { return wxInvalidOffset; }
129 virtual off_t DoTellInput() const
130 { return wxInvalidOffset; }
131
132 protected:
133 bool m_eof, m_i_destroybuf;
134 size_t m_lastread;
135 wxStreamBuffer *m_i_streambuf;
136 };
137
138 class WXDLLEXPORT wxOutputStream {
139 public:
140 wxOutputStream();
141 virtual ~wxOutputStream();
142
143 virtual wxOutputStream& Write(const void *buffer, size_t size);
144 wxOutputStream& Write(wxInputStream& stream_in);
145
146 virtual off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart);
147 virtual off_t TellO() const;
148
149 virtual bool Bad() const { return m_bad; }
150 virtual size_t LastWrite() const { return m_lastwrite; }
151 wxStreamBuffer *OutputStreamBuffer() { return m_o_streambuf; }
152
153 virtual void Sync();
154
155 wxOutputStream& operator<<(wxInputStream& out) { return Write(out); }
156 wxOutputStream& operator<<(const char *string);
157 wxOutputStream& operator<<(wxString& string);
158 wxOutputStream& operator<<(char c);
159 wxOutputStream& operator<<(short i);
160 wxOutputStream& operator<<(int i);
161 wxOutputStream& operator<<(long i);
162 wxOutputStream& operator<<(double f);
163 #if USE_SERIAL
164 wxOutputStream& operator<<(wxObject& obj);
165 #endif
166
167 wxOutputStream& operator<<(float f) { return operator<<((double)f); }
168 wxOutputStream& operator<<(unsigned char c) { return operator<<((char)c); }
169 wxOutputStream& operator<<(unsigned short i) { return operator<<((short)i); }
170 wxOutputStream& operator<<(unsigned int i) { return operator<<((int)i); }
171 wxOutputStream& operator<<(unsigned long i) { return operator<<((long)i); }
172 wxOutputStream& operator<<( __wxOutputManip func) { return func(*this); }
173
174 protected:
175 friend class wxStreamBuffer;
176 friend class wxFilterOutputStream;
177
178 wxOutputStream(wxStreamBuffer *buffer);
179
180 virtual size_t DoWrite( const void *WXUNUSED(buffer), size_t WXUNUSED(size) )
181 { return 0; }
182 virtual off_t DoSeekOutput( off_t WXUNUSED(pos), wxSeekMode WXUNUSED(mode) )
183 { return wxInvalidOffset; }
184 virtual off_t DoTellOutput() const
185 { return wxInvalidOffset; }
186
187 protected:
188 bool m_bad, m_o_destroybuf;
189 size_t m_lastwrite;
190 wxStreamBuffer *m_o_streambuf;
191 };
192
193 class WXDLLEXPORT wxStream: public virtual wxInputStream,
194 public virtual wxOutputStream
195 {
196 public:
197 wxStream();
198 };
199
200 // ---------------------------------------------------------------------------
201 // "Filter" streams
202 // ---------------------------------------------------------------------------
203
204 class WXDLLEXPORT wxFilterInputStream: public virtual wxInputStream {
205 public:
206 wxFilterInputStream();
207 wxFilterInputStream(wxInputStream& stream);
208 ~wxFilterInputStream();
209
210 char Peek() { return m_parent_i_stream->Peek(); }
211
212 bool Eof() const { return m_parent_i_stream->Eof(); }
213 size_t LastRead() const { return m_parent_i_stream->LastRead(); }
214 off_t TellI() const { return m_parent_i_stream->TellI(); }
215
216 protected:
217 size_t DoRead(void *buffer, size_t size);
218 off_t DoSeekInput(off_t pos, wxSeekMode mode);
219 off_t DoTellInput() const;
220
221 protected:
222 wxInputStream *m_parent_i_stream;
223 };
224
225 class WXDLLEXPORT wxFilterOutputStream: public virtual wxOutputStream {
226 public:
227 wxFilterOutputStream();
228 wxFilterOutputStream(wxOutputStream& stream);
229 virtual ~wxFilterOutputStream();
230
231 bool Bad() const { return m_parent_o_stream->Bad(); }
232 size_t LastWrite() const { return m_parent_o_stream->LastWrite(); }
233 off_t TellO() const { return m_parent_o_stream->TellO(); }
234
235 protected:
236 // The forward is implicitely done by wxStreamBuffer.
237
238 size_t DoWrite(const void *buffer, size_t size);
239 off_t DoSeekOutput(off_t pos, wxSeekMode mode);
240 off_t DoTellOutput() const;
241
242 protected:
243 wxOutputStream *m_parent_o_stream;
244 };
245
246 class WXDLLEXPORT wxFilterStream: public wxStream,
247 public virtual wxFilterInputStream,
248 public virtual wxFilterOutputStream {
249 public:
250 wxFilterStream(wxStream& stream);
251 wxFilterStream();
252 };
253
254 #ifdef _MSC_VER
255 #pragma warning(default:4250)
256 #endif
257
258 #endif