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