]>
git.saurik.com Git - wxWidgets.git/blob - src/common/wfstream.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: "File stream" classes
4 // Author: Julian Smart
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "wfstream.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/stream.h"
27 #include "wx/wfstream.h"
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 wxFileInputStream::wxFileInputStream(const wxString
& fileName
)
38 m_file
= new wxFile(fileName
, wxFile::read
);
39 m_file_destroy
= true;
42 wxFileInputStream::wxFileInputStream()
45 m_file_destroy
= false;
49 wxFileInputStream::wxFileInputStream(wxFile
& file
)
52 m_file_destroy
= false;
55 wxFileInputStream::wxFileInputStream(int fd
)
57 m_file
= new wxFile(fd
);
58 m_file_destroy
= true;
61 wxFileInputStream::~wxFileInputStream()
67 wxFileOffset
wxFileInputStream::GetLength() const
69 return m_file
->Length();
72 size_t wxFileInputStream::OnSysRead(void *buffer
, size_t size
)
74 ssize_t ret
= m_file
->Read(buffer
, size
);
76 // NB: we can't use a switch here because HP-UX CC doesn't allow
77 // switching over long long (which size_t is in 64bit mode)
81 // nothing read, so nothing more to read
82 m_lasterror
= wxSTREAM_EOF
;
84 else if ( ret
== wxInvalidOffset
)
86 m_lasterror
= wxSTREAM_READ_ERROR
;
92 m_lasterror
= wxSTREAM_NO_ERROR
;
98 wxFileOffset
wxFileInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
100 return m_file
->Seek(pos
, mode
);
103 wxFileOffset
wxFileInputStream::OnSysTell() const
105 return m_file
->Tell();
108 // ----------------------------------------------------------------------------
109 // wxFileOutputStream
110 // ----------------------------------------------------------------------------
112 wxFileOutputStream::wxFileOutputStream(const wxString
& fileName
)
114 m_file
= new wxFile(fileName
, wxFile::write
);
115 m_file_destroy
= true;
117 if (!m_file
->IsOpened())
119 m_lasterror
= wxSTREAM_WRITE_ERROR
;
124 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 // ----------------------------------------------------------------------------
187 // wxTempFileOutputStream
188 // ----------------------------------------------------------------------------
190 wxTempFileOutputStream::wxTempFileOutputStream(const wxString
& fileName
)
192 m_file
= new wxTempFile(fileName
);
194 if (!m_file
->IsOpened())
195 m_lasterror
= wxSTREAM_WRITE_ERROR
;
198 wxTempFileOutputStream::~wxTempFileOutputStream()
200 if (m_file
->IsOpened())
205 size_t wxTempFileOutputStream::OnSysWrite(const void *buffer
, size_t size
)
207 if (IsOk() && m_file
->Write(buffer
, size
))
209 m_lasterror
= wxSTREAM_WRITE_ERROR
;
213 // ----------------------------------------------------------------------------
215 // ----------------------------------------------------------------------------
217 wxFileStream::wxFileStream(const wxString
& fileName
)
218 : wxFileInputStream(fileName
)
220 wxFileOutputStream::m_file
= wxFileInputStream::m_file
;
227 // ----------------------------------------------------------------------------
228 // wxFFileInputStream
229 // ----------------------------------------------------------------------------
231 wxFFileInputStream::wxFFileInputStream(const wxString
& fileName
,
235 m_file
= new wxFFile(fileName
, mode
);
236 m_file_destroy
= true;
239 wxFFileInputStream::wxFFileInputStream()
243 m_file_destroy
= false;
246 wxFFileInputStream::wxFFileInputStream(wxFFile
& file
)
249 m_file_destroy
= false;
252 wxFFileInputStream::wxFFileInputStream(FILE *file
)
254 m_file
= new wxFFile(file
);
255 m_file_destroy
= true;
258 wxFFileInputStream::~wxFFileInputStream()
264 wxFileOffset
wxFFileInputStream::GetLength() const
266 return m_file
->Length();
269 size_t wxFFileInputStream::OnSysRead(void *buffer
, size_t size
)
271 ssize_t ret
= m_file
->Read(buffer
, size
);
273 // It is not safe to call Eof() if the file is not opened.
274 if (!m_file
->IsOpened() || m_file
->Eof())
275 m_lasterror
= wxSTREAM_EOF
;
276 if (ret
== wxInvalidOffset
)
278 m_lasterror
= wxSTREAM_READ_ERROR
;
285 wxFileOffset
wxFFileInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
287 return m_file
->Seek(pos
, mode
) ? m_file
->Tell() : wxInvalidOffset
;
290 wxFileOffset
wxFFileInputStream::OnSysTell() const
292 return m_file
->Tell();
295 // ----------------------------------------------------------------------------
296 // wxFFileOutputStream
297 // ----------------------------------------------------------------------------
299 wxFFileOutputStream::wxFFileOutputStream(const wxString
& fileName
,
302 m_file
= new wxFFile(fileName
, mode
);
303 m_file_destroy
= true;
305 if (!m_file
->IsOpened())
307 m_lasterror
= wxSTREAM_WRITE_ERROR
;
312 m_lasterror
= wxSTREAM_WRITE_ERROR
;
316 wxFFileOutputStream::wxFFileOutputStream(wxFFile
& file
)
319 m_file_destroy
= false;
322 wxFFileOutputStream::wxFFileOutputStream()
326 m_file_destroy
= false;
329 wxFFileOutputStream::wxFFileOutputStream(FILE *file
)
331 m_file
= new wxFFile(file
);
332 m_file_destroy
= true;
335 wxFFileOutputStream::~wxFFileOutputStream()
344 size_t wxFFileOutputStream::OnSysWrite(const void *buffer
, size_t size
)
346 size_t ret
= m_file
->Write(buffer
, size
);
347 // It is not safe to call Error() if the file is not opened.
348 if (!m_file
->IsOpened() || m_file
->Error())
349 m_lasterror
= wxSTREAM_WRITE_ERROR
;
351 m_lasterror
= wxSTREAM_NO_ERROR
;
355 wxFileOffset
wxFFileOutputStream::OnSysTell() const
357 return m_file
->Tell();
360 wxFileOffset
wxFFileOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
362 return m_file
->Seek(pos
, mode
) ? m_file
->Tell() : wxInvalidOffset
;
365 void wxFFileOutputStream::Sync()
367 wxOutputStream::Sync();
371 wxFileOffset
wxFFileOutputStream::GetLength() const
373 return m_file
->Length();
376 // ----------------------------------------------------------------------------
378 // ----------------------------------------------------------------------------
380 wxFFileStream::wxFFileStream(const wxString
& fileName
)
381 : wxFFileInputStream(fileName
)
383 wxFFileOutputStream::m_file
= wxFFileInputStream::m_file
;
388 #endif // wxUSE_STREAMS