]>
git.saurik.com Git - wxWidgets.git/blob - src/common/wfstream.cpp
1eca40cd08a51c5e993f2d36b5e0beca9350f435
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/wfstream.cpp
3 // Purpose: "File stream" classes
4 // Author: Julian Smart
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/wfstream.h"
24 #include "wx/stream.h"
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 wxFileInputStream::wxFileInputStream(const wxString
& fileName
)
38 m_file
= new wxFile(fileName
, wxFile::read
);
39 m_file_destroy
= true;
40 if ( !m_file
->IsOpened() )
41 m_lasterror
= wxSTREAM_READ_ERROR
;
44 wxFileInputStream::wxFileInputStream()
47 m_file_destroy
= false;
51 wxFileInputStream::wxFileInputStream(wxFile
& file
)
54 m_file_destroy
= false;
57 wxFileInputStream::wxFileInputStream(int fd
)
59 m_file
= new wxFile(fd
);
60 m_file_destroy
= true;
63 wxFileInputStream::~wxFileInputStream()
69 wxFileOffset
wxFileInputStream::GetLength() const
71 return m_file
->Length();
74 size_t wxFileInputStream::OnSysRead(void *buffer
, size_t size
)
76 ssize_t ret
= m_file
->Read(buffer
, size
);
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)
83 // nothing read, so nothing more to read
84 m_lasterror
= wxSTREAM_EOF
;
86 else if ( ret
== wxInvalidOffset
)
88 m_lasterror
= wxSTREAM_READ_ERROR
;
94 m_lasterror
= wxSTREAM_NO_ERROR
;
100 wxFileOffset
wxFileInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
102 return m_file
->Seek(pos
, mode
);
105 wxFileOffset
wxFileInputStream::OnSysTell() const
107 return m_file
->Tell();
110 bool wxFileInputStream::IsOk() const
112 return wxInputStream::IsOk() && m_file
->IsOpened();
115 // ----------------------------------------------------------------------------
116 // wxFileOutputStream
117 // ----------------------------------------------------------------------------
119 wxFileOutputStream::wxFileOutputStream(const wxString
& fileName
)
121 m_file
= new wxFile(fileName
, wxFile::write
);
122 m_file_destroy
= true;
124 if (!m_file
->IsOpened())
125 m_lasterror
= wxSTREAM_WRITE_ERROR
;
128 wxFileOutputStream::wxFileOutputStream(wxFile
& file
)
131 m_file_destroy
= false;
134 wxFileOutputStream::wxFileOutputStream()
137 m_file_destroy
= false;
141 wxFileOutputStream::wxFileOutputStream(int fd
)
143 m_file
= new wxFile(fd
);
144 m_file_destroy
= true;
147 wxFileOutputStream::~wxFileOutputStream()
156 size_t wxFileOutputStream::OnSysWrite(const void *buffer
, size_t size
)
158 size_t ret
= m_file
->Write(buffer
, size
);
160 m_lasterror
= m_file
->Error() ? wxSTREAM_WRITE_ERROR
: wxSTREAM_NO_ERROR
;
165 wxFileOffset
wxFileOutputStream::OnSysTell() const
167 return m_file
->Tell();
170 wxFileOffset
wxFileOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
172 return m_file
->Seek(pos
, mode
);
175 void wxFileOutputStream::Sync()
177 wxOutputStream::Sync();
181 wxFileOffset
wxFileOutputStream::GetLength() const
183 return m_file
->Length();
186 bool wxFileOutputStream::IsOk() const
188 return wxOutputStream::IsOk() && m_file
->IsOpened();
191 // ----------------------------------------------------------------------------
192 // wxTempFileOutputStream
193 // ----------------------------------------------------------------------------
195 wxTempFileOutputStream::wxTempFileOutputStream(const wxString
& fileName
)
197 m_file
= new wxTempFile(fileName
);
199 if (!m_file
->IsOpened())
200 m_lasterror
= wxSTREAM_WRITE_ERROR
;
203 wxTempFileOutputStream::~wxTempFileOutputStream()
205 if (m_file
->IsOpened())
210 size_t wxTempFileOutputStream::OnSysWrite(const void *buffer
, size_t size
)
212 if (IsOk() && m_file
->Write(buffer
, size
))
214 m_lasterror
= wxSTREAM_WRITE_ERROR
;
218 // ----------------------------------------------------------------------------
220 // ----------------------------------------------------------------------------
222 wxFileStream::wxFileStream(const wxString
& fileName
)
223 : wxFileInputStream(),
226 wxFileOutputStream::m_file
=
227 wxFileInputStream::m_file
= new wxFile(fileName
, wxFile::read_write
);
229 // this is a bit ugly as streams are symmetric but we still have to delete
230 // the file we created above exactly once so we decide to (arbitrarily) do
231 // it in wxFileInputStream
232 wxFileInputStream::m_file_destroy
= true;
235 bool wxFileStream::IsOk() const
237 return wxFileOutputStream::IsOk() && wxFileInputStream::IsOk();
244 // ----------------------------------------------------------------------------
245 // wxFFileInputStream
246 // ----------------------------------------------------------------------------
248 wxFFileInputStream::wxFFileInputStream(const wxString
& fileName
,
249 const wxString
& mode
)
252 m_file
= new wxFFile(fileName
, mode
);
253 m_file_destroy
= true;
255 if (!m_file
->IsOpened())
256 m_lasterror
= wxSTREAM_WRITE_ERROR
;
259 wxFFileInputStream::wxFFileInputStream()
263 m_file_destroy
= false;
266 wxFFileInputStream::wxFFileInputStream(wxFFile
& file
)
269 m_file_destroy
= false;
272 wxFFileInputStream::wxFFileInputStream(FILE *file
)
274 m_file
= new wxFFile(file
);
275 m_file_destroy
= true;
278 wxFFileInputStream::~wxFFileInputStream()
284 wxFileOffset
wxFFileInputStream::GetLength() const
286 return m_file
->Length();
289 size_t wxFFileInputStream::OnSysRead(void *buffer
, size_t size
)
291 ssize_t ret
= m_file
->Read(buffer
, size
);
293 // It is not safe to call Eof() if the file is not opened.
294 if (!m_file
->IsOpened() || m_file
->Eof())
295 m_lasterror
= wxSTREAM_EOF
;
296 if (ret
== wxInvalidOffset
)
298 m_lasterror
= wxSTREAM_READ_ERROR
;
305 wxFileOffset
wxFFileInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
307 return m_file
->Seek(pos
, mode
) ? m_file
->Tell() : wxInvalidOffset
;
310 wxFileOffset
wxFFileInputStream::OnSysTell() const
312 return m_file
->Tell();
315 bool wxFFileInputStream::IsOk() const
317 return wxStreamBase::IsOk() && m_file
->IsOpened();
320 // ----------------------------------------------------------------------------
321 // wxFFileOutputStream
322 // ----------------------------------------------------------------------------
324 wxFFileOutputStream::wxFFileOutputStream(const wxString
& fileName
,
325 const wxString
& mode
)
327 m_file
= new wxFFile(fileName
, mode
);
328 m_file_destroy
= true;
330 if (!m_file
->IsOpened())
332 m_lasterror
= wxSTREAM_WRITE_ERROR
;
337 m_lasterror
= wxSTREAM_WRITE_ERROR
;
341 wxFFileOutputStream::wxFFileOutputStream(wxFFile
& file
)
344 m_file_destroy
= false;
347 wxFFileOutputStream::wxFFileOutputStream()
351 m_file_destroy
= false;
354 wxFFileOutputStream::wxFFileOutputStream(FILE *file
)
356 m_file
= new wxFFile(file
);
357 m_file_destroy
= true;
360 wxFFileOutputStream::~wxFFileOutputStream()
369 size_t wxFFileOutputStream::OnSysWrite(const void *buffer
, size_t size
)
371 size_t ret
= m_file
->Write(buffer
, size
);
372 // It is not safe to call Error() if the file is not opened.
373 if (!m_file
->IsOpened() || m_file
->Error())
374 m_lasterror
= wxSTREAM_WRITE_ERROR
;
376 m_lasterror
= wxSTREAM_NO_ERROR
;
380 wxFileOffset
wxFFileOutputStream::OnSysTell() const
382 return m_file
->Tell();
385 wxFileOffset
wxFFileOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
387 return m_file
->Seek(pos
, mode
) ? m_file
->Tell() : wxInvalidOffset
;
390 void wxFFileOutputStream::Sync()
392 wxOutputStream::Sync();
396 wxFileOffset
wxFFileOutputStream::GetLength() const
398 return m_file
->Length();
401 bool wxFFileOutputStream::IsOk() const
403 return wxStreamBase::IsOk() && m_file
->IsOpened();
406 // ----------------------------------------------------------------------------
408 // ----------------------------------------------------------------------------
410 wxFFileStream::wxFFileStream(const wxString
& fileName
, const wxString
& mode
)
411 : wxFFileInputStream(),
412 wxFFileOutputStream()
414 wxASSERT_MSG( mode
.find_first_of('+') != wxString::npos
,
415 "must be opened in read-write mode for this class to work" );
417 wxFFileOutputStream::m_file
=
418 wxFFileInputStream::m_file
= new wxFFile(fileName
, mode
);
420 // see comment in wxFileStream ctor
421 wxFFileInputStream::m_file_destroy
= true;
424 bool wxFFileStream::IsOk() const
426 return wxFFileOutputStream::IsOk() && wxFFileInputStream::IsOk();
431 #endif // wxUSE_STREAMS