]>
git.saurik.com Git - wxWidgets.git/blob - src/common/wfstream.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/wfstream.cpp
3 // Purpose: "File stream" classes
4 // Author: Julian Smart
7 // Copyright: (c) Guilhem Lavaux
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
20 #include "wx/wfstream.h"
23 #include "wx/stream.h"
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 wxFileInputStream::wxFileInputStream(const wxString
& fileName
)
37 m_file
= new wxFile(fileName
, wxFile::read
);
38 m_file_destroy
= true;
39 if ( !m_file
->IsOpened() )
40 m_lasterror
= wxSTREAM_READ_ERROR
;
43 wxFileInputStream::wxFileInputStream()
46 m_file_destroy
= false;
50 wxFileInputStream::wxFileInputStream(wxFile
& file
)
53 m_file_destroy
= false;
56 wxFileInputStream::wxFileInputStream(int fd
)
58 m_file
= new wxFile(fd
);
59 m_file_destroy
= true;
62 wxFileInputStream::~wxFileInputStream()
68 wxFileOffset
wxFileInputStream::GetLength() const
70 return m_file
->Length();
73 size_t wxFileInputStream::OnSysRead(void *buffer
, size_t size
)
75 ssize_t ret
= m_file
->Read(buffer
, size
);
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)
82 // nothing read, so nothing more to read
83 m_lasterror
= wxSTREAM_EOF
;
85 else if ( ret
== wxInvalidOffset
)
87 m_lasterror
= wxSTREAM_READ_ERROR
;
93 m_lasterror
= wxSTREAM_NO_ERROR
;
99 wxFileOffset
wxFileInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
101 return m_file
->Seek(pos
, mode
);
104 wxFileOffset
wxFileInputStream::OnSysTell() const
106 return m_file
->Tell();
109 bool wxFileInputStream::IsOk() const
111 return wxInputStream::IsOk() && m_file
->IsOpened();
114 // ----------------------------------------------------------------------------
115 // wxFileOutputStream
116 // ----------------------------------------------------------------------------
118 wxFileOutputStream::wxFileOutputStream(const wxString
& fileName
)
120 m_file
= new wxFile(fileName
, wxFile::write
);
121 m_file_destroy
= true;
123 if (!m_file
->IsOpened())
124 m_lasterror
= wxSTREAM_WRITE_ERROR
;
127 wxFileOutputStream::wxFileOutputStream(wxFile
& file
)
130 m_file_destroy
= false;
133 wxFileOutputStream::wxFileOutputStream()
136 m_file_destroy
= false;
140 wxFileOutputStream::wxFileOutputStream(int fd
)
142 m_file
= new wxFile(fd
);
143 m_file_destroy
= true;
146 wxFileOutputStream::~wxFileOutputStream()
155 size_t wxFileOutputStream::OnSysWrite(const void *buffer
, size_t size
)
157 size_t ret
= m_file
->Write(buffer
, size
);
159 m_lasterror
= m_file
->Error() ? wxSTREAM_WRITE_ERROR
: wxSTREAM_NO_ERROR
;
164 wxFileOffset
wxFileOutputStream::OnSysTell() const
166 return m_file
->Tell();
169 wxFileOffset
wxFileOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
171 return m_file
->Seek(pos
, mode
);
174 void wxFileOutputStream::Sync()
176 wxOutputStream::Sync();
180 wxFileOffset
wxFileOutputStream::GetLength() const
182 return m_file
->Length();
185 bool wxFileOutputStream::IsOk() const
187 return wxOutputStream::IsOk() && m_file
->IsOpened();
190 // ----------------------------------------------------------------------------
191 // wxTempFileOutputStream
192 // ----------------------------------------------------------------------------
194 wxTempFileOutputStream::wxTempFileOutputStream(const wxString
& fileName
)
196 m_file
= new wxTempFile(fileName
);
198 if (!m_file
->IsOpened())
199 m_lasterror
= wxSTREAM_WRITE_ERROR
;
202 wxTempFileOutputStream::~wxTempFileOutputStream()
204 if (m_file
->IsOpened())
209 size_t wxTempFileOutputStream::OnSysWrite(const void *buffer
, size_t size
)
211 if (IsOk() && m_file
->Write(buffer
, size
))
213 m_lasterror
= wxSTREAM_WRITE_ERROR
;
217 // ----------------------------------------------------------------------------
219 // ----------------------------------------------------------------------------
221 wxFileStream::wxFileStream(const wxString
& fileName
)
222 : wxFileInputStream(),
225 wxFileOutputStream::m_file
=
226 wxFileInputStream::m_file
= new wxFile(fileName
, wxFile::read_write
);
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;
234 bool wxFileStream::IsOk() const
236 return wxFileOutputStream::IsOk() && wxFileInputStream::IsOk();
243 // ----------------------------------------------------------------------------
244 // wxFFileInputStream
245 // ----------------------------------------------------------------------------
247 wxFFileInputStream::wxFFileInputStream(const wxString
& fileName
,
248 const wxString
& mode
)
251 m_file
= new wxFFile(fileName
, mode
);
252 m_file_destroy
= true;
254 if (!m_file
->IsOpened())
255 m_lasterror
= wxSTREAM_WRITE_ERROR
;
258 wxFFileInputStream::wxFFileInputStream()
262 m_file_destroy
= false;
265 wxFFileInputStream::wxFFileInputStream(wxFFile
& file
)
268 m_file_destroy
= false;
271 wxFFileInputStream::wxFFileInputStream(FILE *file
)
273 m_file
= new wxFFile(file
);
274 m_file_destroy
= true;
277 wxFFileInputStream::~wxFFileInputStream()
283 wxFileOffset
wxFFileInputStream::GetLength() const
285 return m_file
->Length();
288 size_t wxFFileInputStream::OnSysRead(void *buffer
, size_t size
)
290 ssize_t ret
= m_file
->Read(buffer
, size
);
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
)
297 m_lasterror
= wxSTREAM_READ_ERROR
;
304 wxFileOffset
wxFFileInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
306 return m_file
->Seek(pos
, mode
) ? m_file
->Tell() : wxInvalidOffset
;
309 wxFileOffset
wxFFileInputStream::OnSysTell() const
311 return m_file
->Tell();
314 bool wxFFileInputStream::IsOk() const
316 return wxStreamBase::IsOk() && m_file
->IsOpened();
319 // ----------------------------------------------------------------------------
320 // wxFFileOutputStream
321 // ----------------------------------------------------------------------------
323 wxFFileOutputStream::wxFFileOutputStream(const wxString
& fileName
,
324 const wxString
& mode
)
326 m_file
= new wxFFile(fileName
, mode
);
327 m_file_destroy
= true;
329 if (!m_file
->IsOpened())
331 m_lasterror
= wxSTREAM_WRITE_ERROR
;
336 m_lasterror
= wxSTREAM_WRITE_ERROR
;
340 wxFFileOutputStream::wxFFileOutputStream(wxFFile
& file
)
343 m_file_destroy
= false;
346 wxFFileOutputStream::wxFFileOutputStream()
350 m_file_destroy
= false;
353 wxFFileOutputStream::wxFFileOutputStream(FILE *file
)
355 m_file
= new wxFFile(file
);
356 m_file_destroy
= true;
359 wxFFileOutputStream::~wxFFileOutputStream()
368 size_t wxFFileOutputStream::OnSysWrite(const void *buffer
, size_t size
)
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
;
375 m_lasterror
= wxSTREAM_NO_ERROR
;
379 wxFileOffset
wxFFileOutputStream::OnSysTell() const
381 return m_file
->Tell();
384 wxFileOffset
wxFFileOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
386 return m_file
->Seek(pos
, mode
) ? m_file
->Tell() : wxInvalidOffset
;
389 void wxFFileOutputStream::Sync()
391 wxOutputStream::Sync();
395 wxFileOffset
wxFFileOutputStream::GetLength() const
397 return m_file
->Length();
400 bool wxFFileOutputStream::IsOk() const
402 return wxStreamBase::IsOk() && m_file
->IsOpened();
405 // ----------------------------------------------------------------------------
407 // ----------------------------------------------------------------------------
409 wxFFileStream::wxFFileStream(const wxString
& fileName
, const wxString
& mode
)
410 : wxFFileInputStream(),
411 wxFFileOutputStream()
413 wxASSERT_MSG( mode
.find_first_of('+') != wxString::npos
,
414 "must be opened in read-write mode for this class to work" );
416 wxFFileOutputStream::m_file
=
417 wxFFileInputStream::m_file
= new wxFFile(fileName
, mode
);
419 // see comment in wxFileStream ctor
420 wxFFileInputStream::m_file_destroy
= true;
423 bool wxFFileStream::IsOk() const
425 return wxFFileOutputStream::IsOk() && wxFFileInputStream::IsOk();
430 #endif // wxUSE_STREAMS