]> git.saurik.com Git - wxWidgets.git/blob - src/common/wfstream.cpp
wxAUI: Made fixed size floating panes work
[wxWidgets.git] / src / common / wfstream.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/fstream.cpp
3 // Purpose: "File stream" classes
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 11/07/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_STREAMS
20
21 #include "wx/wfstream.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/stream.h"
25 #endif
26
27 #include <stdio.h>
28
29 #if wxUSE_FILE
30
31 // ----------------------------------------------------------------------------
32 // wxFileInputStream
33 // ----------------------------------------------------------------------------
34
35 wxFileInputStream::wxFileInputStream(const wxString& fileName)
36 : wxInputStream()
37 {
38 m_file = new wxFile(fileName, wxFile::read);
39 m_file_destroy = true;
40 if ( !m_file->IsOpened() )
41 m_lasterror = wxSTREAM_READ_ERROR;
42 }
43
44 wxFileInputStream::wxFileInputStream()
45 : wxInputStream()
46 {
47 m_file_destroy = false;
48 m_file = NULL;
49 }
50
51 wxFileInputStream::wxFileInputStream(wxFile& file)
52 {
53 m_file = &file;
54 m_file_destroy = false;
55 }
56
57 wxFileInputStream::wxFileInputStream(int fd)
58 {
59 m_file = new wxFile(fd);
60 m_file_destroy = true;
61 }
62
63 wxFileInputStream::~wxFileInputStream()
64 {
65 if (m_file_destroy)
66 delete m_file;
67 }
68
69 wxFileOffset wxFileInputStream::GetLength() const
70 {
71 return m_file->Length();
72 }
73
74 size_t wxFileInputStream::OnSysRead(void *buffer, size_t size)
75 {
76 ssize_t ret = m_file->Read(buffer, size);
77
78 // NB: we can't use a switch here because HP-UX CC doesn't allow
79 // switching over long long (which size_t is in 64bit mode)
80
81 if ( !ret )
82 {
83 // nothing read, so nothing more to read
84 m_lasterror = wxSTREAM_EOF;
85 }
86 else if ( ret == wxInvalidOffset )
87 {
88 m_lasterror = wxSTREAM_READ_ERROR;
89 ret = 0;
90 }
91 else
92 {
93 // normal case
94 m_lasterror = wxSTREAM_NO_ERROR;
95 }
96
97 return ret;
98 }
99
100 wxFileOffset wxFileInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
101 {
102 return m_file->Seek(pos, mode);
103 }
104
105 wxFileOffset wxFileInputStream::OnSysTell() const
106 {
107 return m_file->Tell();
108 }
109
110 // ----------------------------------------------------------------------------
111 // wxFileOutputStream
112 // ----------------------------------------------------------------------------
113
114 wxFileOutputStream::wxFileOutputStream(const wxString& fileName)
115 {
116 m_file = new wxFile(fileName, wxFile::write);
117 m_file_destroy = true;
118
119 if (!m_file->IsOpened())
120 m_lasterror = wxSTREAM_WRITE_ERROR;
121 }
122
123 wxFileOutputStream::wxFileOutputStream(wxFile& file)
124 {
125 m_file = &file;
126 m_file_destroy = false;
127 }
128
129 wxFileOutputStream::wxFileOutputStream()
130 : wxOutputStream()
131 {
132 m_file_destroy = false;
133 m_file = NULL;
134 }
135
136 wxFileOutputStream::wxFileOutputStream(int fd)
137 {
138 m_file = new wxFile(fd);
139 m_file_destroy = true;
140 }
141
142 wxFileOutputStream::~wxFileOutputStream()
143 {
144 if (m_file_destroy)
145 {
146 Sync();
147 delete m_file;
148 }
149 }
150
151 size_t wxFileOutputStream::OnSysWrite(const void *buffer, size_t size)
152 {
153 size_t ret = m_file->Write(buffer, size);
154
155 m_lasterror = m_file->Error() ? wxSTREAM_WRITE_ERROR : wxSTREAM_NO_ERROR;
156
157 return ret;
158 }
159
160 wxFileOffset wxFileOutputStream::OnSysTell() const
161 {
162 return m_file->Tell();
163 }
164
165 wxFileOffset wxFileOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
166 {
167 return m_file->Seek(pos, mode);
168 }
169
170 void wxFileOutputStream::Sync()
171 {
172 wxOutputStream::Sync();
173 m_file->Flush();
174 }
175
176 wxFileOffset wxFileOutputStream::GetLength() const
177 {
178 return m_file->Length();
179 }
180
181 // ----------------------------------------------------------------------------
182 // wxTempFileOutputStream
183 // ----------------------------------------------------------------------------
184
185 wxTempFileOutputStream::wxTempFileOutputStream(const wxString& fileName)
186 {
187 m_file = new wxTempFile(fileName);
188
189 if (!m_file->IsOpened())
190 m_lasterror = wxSTREAM_WRITE_ERROR;
191 }
192
193 wxTempFileOutputStream::~wxTempFileOutputStream()
194 {
195 if (m_file->IsOpened())
196 Discard();
197 delete m_file;
198 }
199
200 size_t wxTempFileOutputStream::OnSysWrite(const void *buffer, size_t size)
201 {
202 if (IsOk() && m_file->Write(buffer, size))
203 return size;
204 m_lasterror = wxSTREAM_WRITE_ERROR;
205 return 0;
206 }
207
208 // ----------------------------------------------------------------------------
209 // wxFileStream
210 // ----------------------------------------------------------------------------
211
212 wxFileStream::wxFileStream(const wxString& fileName)
213 : wxFileInputStream(fileName)
214 {
215 wxFileOutputStream::m_file = wxFileInputStream::m_file;
216 }
217
218 #endif //wxUSE_FILE
219
220 #if wxUSE_FFILE
221
222 // ----------------------------------------------------------------------------
223 // wxFFileInputStream
224 // ----------------------------------------------------------------------------
225
226 wxFFileInputStream::wxFFileInputStream(const wxString& fileName,
227 const wxChar *mode)
228 : wxInputStream()
229 {
230 m_file = new wxFFile(fileName, mode);
231 m_file_destroy = true;
232
233 if (!m_file->IsOpened())
234 m_lasterror = wxSTREAM_WRITE_ERROR;
235 }
236
237 wxFFileInputStream::wxFFileInputStream()
238 : wxInputStream()
239 {
240 m_file = NULL;
241 m_file_destroy = false;
242 }
243
244 wxFFileInputStream::wxFFileInputStream(wxFFile& file)
245 {
246 m_file = &file;
247 m_file_destroy = false;
248 }
249
250 wxFFileInputStream::wxFFileInputStream(FILE *file)
251 {
252 m_file = new wxFFile(file);
253 m_file_destroy = true;
254 }
255
256 wxFFileInputStream::~wxFFileInputStream()
257 {
258 if (m_file_destroy)
259 delete m_file;
260 }
261
262 wxFileOffset wxFFileInputStream::GetLength() const
263 {
264 return m_file->Length();
265 }
266
267 size_t wxFFileInputStream::OnSysRead(void *buffer, size_t size)
268 {
269 ssize_t ret = m_file->Read(buffer, size);
270
271 // It is not safe to call Eof() if the file is not opened.
272 if (!m_file->IsOpened() || m_file->Eof())
273 m_lasterror = wxSTREAM_EOF;
274 if (ret == wxInvalidOffset)
275 {
276 m_lasterror = wxSTREAM_READ_ERROR;
277 ret = 0;
278 }
279
280 return ret;
281 }
282
283 wxFileOffset wxFFileInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
284 {
285 return m_file->Seek(pos, mode) ? m_file->Tell() : wxInvalidOffset;
286 }
287
288 wxFileOffset wxFFileInputStream::OnSysTell() const
289 {
290 return m_file->Tell();
291 }
292
293 // ----------------------------------------------------------------------------
294 // wxFFileOutputStream
295 // ----------------------------------------------------------------------------
296
297 wxFFileOutputStream::wxFFileOutputStream(const wxString& fileName,
298 const wxChar *mode)
299 {
300 m_file = new wxFFile(fileName, mode);
301 m_file_destroy = true;
302
303 if (!m_file->IsOpened())
304 {
305 m_lasterror = wxSTREAM_WRITE_ERROR;
306 }
307 else
308 {
309 if (m_file->Error())
310 m_lasterror = wxSTREAM_WRITE_ERROR;
311 }
312 }
313
314 wxFFileOutputStream::wxFFileOutputStream(wxFFile& file)
315 {
316 m_file = &file;
317 m_file_destroy = false;
318 }
319
320 wxFFileOutputStream::wxFFileOutputStream()
321 : wxOutputStream()
322 {
323 m_file = NULL;
324 m_file_destroy = false;
325 }
326
327 wxFFileOutputStream::wxFFileOutputStream(FILE *file)
328 {
329 m_file = new wxFFile(file);
330 m_file_destroy = true;
331 }
332
333 wxFFileOutputStream::~wxFFileOutputStream()
334 {
335 if (m_file_destroy)
336 {
337 Sync();
338 delete m_file;
339 }
340 }
341
342 size_t wxFFileOutputStream::OnSysWrite(const void *buffer, size_t size)
343 {
344 size_t ret = m_file->Write(buffer, size);
345 // It is not safe to call Error() if the file is not opened.
346 if (!m_file->IsOpened() || m_file->Error())
347 m_lasterror = wxSTREAM_WRITE_ERROR;
348 else
349 m_lasterror = wxSTREAM_NO_ERROR;
350 return ret;
351 }
352
353 wxFileOffset wxFFileOutputStream::OnSysTell() const
354 {
355 return m_file->Tell();
356 }
357
358 wxFileOffset wxFFileOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
359 {
360 return m_file->Seek(pos, mode) ? m_file->Tell() : wxInvalidOffset;
361 }
362
363 void wxFFileOutputStream::Sync()
364 {
365 wxOutputStream::Sync();
366 m_file->Flush();
367 }
368
369 wxFileOffset wxFFileOutputStream::GetLength() const
370 {
371 return m_file->Length();
372 }
373
374 // ----------------------------------------------------------------------------
375 // wxFFileStream
376 // ----------------------------------------------------------------------------
377
378 wxFFileStream::wxFFileStream(const wxString& fileName)
379 : wxFFileInputStream(fileName)
380 {
381 wxFFileOutputStream::m_file = wxFFileInputStream::m_file;
382 }
383
384 #endif //wxUSE_FFILE
385
386 #endif // wxUSE_STREAMS