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