]>
git.saurik.com Git - wxWidgets.git/blob - src/common/wfstream.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/fstream.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 (wxStreamBase::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 (wxStreamBase::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(fileName
)
225 wxFileOutputStream::m_file
= wxFileInputStream::m_file
;
232 // ----------------------------------------------------------------------------
233 // wxFFileInputStream
234 // ----------------------------------------------------------------------------
236 wxFFileInputStream::wxFFileInputStream(const wxString
& fileName
,
237 const wxString
& mode
)
240 m_file
= new wxFFile(fileName
, mode
);
241 m_file_destroy
= true;
243 if (!m_file
->IsOpened())
244 m_lasterror
= wxSTREAM_WRITE_ERROR
;
247 wxFFileInputStream::wxFFileInputStream()
251 m_file_destroy
= false;
254 wxFFileInputStream::wxFFileInputStream(wxFFile
& file
)
257 m_file_destroy
= false;
260 wxFFileInputStream::wxFFileInputStream(FILE *file
)
262 m_file
= new wxFFile(file
);
263 m_file_destroy
= true;
266 wxFFileInputStream::~wxFFileInputStream()
272 wxFileOffset
wxFFileInputStream::GetLength() const
274 return m_file
->Length();
277 size_t wxFFileInputStream::OnSysRead(void *buffer
, size_t size
)
279 ssize_t ret
= m_file
->Read(buffer
, size
);
281 // It is not safe to call Eof() if the file is not opened.
282 if (!m_file
->IsOpened() || m_file
->Eof())
283 m_lasterror
= wxSTREAM_EOF
;
284 if (ret
== wxInvalidOffset
)
286 m_lasterror
= wxSTREAM_READ_ERROR
;
293 wxFileOffset
wxFFileInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
295 return m_file
->Seek(pos
, mode
) ? m_file
->Tell() : wxInvalidOffset
;
298 wxFileOffset
wxFFileInputStream::OnSysTell() const
300 return m_file
->Tell();
303 bool wxFFileInputStream::IsOk() const
305 return (wxStreamBase::IsOk() && m_file
->IsOpened());
308 // ----------------------------------------------------------------------------
309 // wxFFileOutputStream
310 // ----------------------------------------------------------------------------
312 wxFFileOutputStream::wxFFileOutputStream(const wxString
& fileName
,
313 const wxString
& mode
)
315 m_file
= new wxFFile(fileName
, mode
);
316 m_file_destroy
= true;
318 if (!m_file
->IsOpened())
320 m_lasterror
= wxSTREAM_WRITE_ERROR
;
325 m_lasterror
= wxSTREAM_WRITE_ERROR
;
329 wxFFileOutputStream::wxFFileOutputStream(wxFFile
& file
)
332 m_file_destroy
= false;
335 wxFFileOutputStream::wxFFileOutputStream()
339 m_file_destroy
= false;
342 wxFFileOutputStream::wxFFileOutputStream(FILE *file
)
344 m_file
= new wxFFile(file
);
345 m_file_destroy
= true;
348 wxFFileOutputStream::~wxFFileOutputStream()
357 size_t wxFFileOutputStream::OnSysWrite(const void *buffer
, size_t size
)
359 size_t ret
= m_file
->Write(buffer
, size
);
360 // It is not safe to call Error() if the file is not opened.
361 if (!m_file
->IsOpened() || m_file
->Error())
362 m_lasterror
= wxSTREAM_WRITE_ERROR
;
364 m_lasterror
= wxSTREAM_NO_ERROR
;
368 wxFileOffset
wxFFileOutputStream::OnSysTell() const
370 return m_file
->Tell();
373 wxFileOffset
wxFFileOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
375 return m_file
->Seek(pos
, mode
) ? m_file
->Tell() : wxInvalidOffset
;
378 void wxFFileOutputStream::Sync()
380 wxOutputStream::Sync();
384 wxFileOffset
wxFFileOutputStream::GetLength() const
386 return m_file
->Length();
389 bool wxFFileOutputStream::IsOk() const
391 return (wxStreamBase::IsOk() && m_file
->IsOpened());
394 // ----------------------------------------------------------------------------
396 // ----------------------------------------------------------------------------
398 wxFFileStream::wxFFileStream(const wxString
& fileName
)
399 : wxFFileInputStream(fileName
)
401 wxFFileOutputStream::m_file
= wxFFileInputStream::m_file
;
406 #endif // wxUSE_STREAMS