]>
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 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
190 wxFileStream::wxFileStream(const wxString
& fileName
)
191 : wxFileInputStream(fileName
)
193 wxFileOutputStream::m_file
= wxFileInputStream::m_file
;
200 // ----------------------------------------------------------------------------
201 // wxFFileInputStream
202 // ----------------------------------------------------------------------------
204 wxFFileInputStream::wxFFileInputStream(const wxString
& fileName
,
208 m_file
= new wxFFile(fileName
, mode
);
209 m_file_destroy
= true;
212 wxFFileInputStream::wxFFileInputStream()
216 m_file_destroy
= false;
219 wxFFileInputStream::wxFFileInputStream(wxFFile
& file
)
222 m_file_destroy
= false;
225 wxFFileInputStream::wxFFileInputStream(FILE *file
)
227 m_file
= new wxFFile(file
);
228 m_file_destroy
= true;
231 wxFFileInputStream::~wxFFileInputStream()
237 wxFileOffset
wxFFileInputStream::GetLength() const
239 return m_file
->Length();
242 size_t wxFFileInputStream::OnSysRead(void *buffer
, size_t size
)
244 ssize_t ret
= m_file
->Read(buffer
, size
);
246 // It is not safe to call Eof() if the file is not opened.
247 if (!m_file
->IsOpened() || m_file
->Eof())
248 m_lasterror
= wxSTREAM_EOF
;
249 if (ret
== wxInvalidOffset
)
251 m_lasterror
= wxSTREAM_READ_ERROR
;
258 wxFileOffset
wxFFileInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
260 return m_file
->Seek(pos
, mode
) ? m_file
->Tell() : wxInvalidOffset
;
263 wxFileOffset
wxFFileInputStream::OnSysTell() const
265 return m_file
->Tell();
268 // ----------------------------------------------------------------------------
269 // wxFFileOutputStream
270 // ----------------------------------------------------------------------------
272 wxFFileOutputStream::wxFFileOutputStream(const wxString
& fileName
,
275 m_file
= new wxFFile(fileName
, mode
);
276 m_file_destroy
= true;
278 if (!m_file
->IsOpened())
280 m_lasterror
= wxSTREAM_WRITE_ERROR
;
285 m_lasterror
= wxSTREAM_WRITE_ERROR
;
289 wxFFileOutputStream::wxFFileOutputStream(wxFFile
& file
)
292 m_file_destroy
= false;
295 wxFFileOutputStream::wxFFileOutputStream()
299 m_file_destroy
= false;
302 wxFFileOutputStream::wxFFileOutputStream(FILE *file
)
304 m_file
= new wxFFile(file
);
305 m_file_destroy
= true;
308 wxFFileOutputStream::~wxFFileOutputStream()
317 size_t wxFFileOutputStream::OnSysWrite(const void *buffer
, size_t size
)
319 size_t ret
= m_file
->Write(buffer
, size
);
320 // It is not safe to call Error() if the file is not opened.
321 if (!m_file
->IsOpened() || m_file
->Error())
322 m_lasterror
= wxSTREAM_WRITE_ERROR
;
324 m_lasterror
= wxSTREAM_NO_ERROR
;
328 wxFileOffset
wxFFileOutputStream::OnSysTell() const
330 return m_file
->Tell();
333 wxFileOffset
wxFFileOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
335 return m_file
->Seek(pos
, mode
) ? m_file
->Tell() : wxInvalidOffset
;
338 void wxFFileOutputStream::Sync()
340 wxOutputStream::Sync();
344 wxFileOffset
wxFFileOutputStream::GetLength() const
346 return m_file
->Length();
349 // ----------------------------------------------------------------------------
351 // ----------------------------------------------------------------------------
353 wxFFileStream::wxFFileStream(const wxString
& fileName
)
354 : wxFFileInputStream(fileName
)
356 wxFFileOutputStream::m_file
= wxFFileInputStream::m_file
;
361 #endif // wxUSE_STREAMS