]>
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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
22 #include "wx/stream.h"
23 #include "wx/wfstream.h"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 wxFileInputStream::wxFileInputStream(const wxString
& fileName
)
34 m_file
= new wxFile(fileName
, wxFile::read
);
35 m_file_destroy
= true;
36 if ( !m_file
->IsOpened() )
37 m_lasterror
= wxSTREAM_READ_ERROR
;
40 wxFileInputStream::wxFileInputStream()
43 m_file_destroy
= false;
47 wxFileInputStream::wxFileInputStream(wxFile
& file
)
50 m_file_destroy
= false;
53 wxFileInputStream::wxFileInputStream(int fd
)
55 m_file
= new wxFile(fd
);
56 m_file_destroy
= true;
59 wxFileInputStream::~wxFileInputStream()
65 wxFileOffset
wxFileInputStream::GetLength() const
67 return m_file
->Length();
70 size_t wxFileInputStream::OnSysRead(void *buffer
, size_t size
)
72 ssize_t ret
= m_file
->Read(buffer
, size
);
74 // NB: we can't use a switch here because HP-UX CC doesn't allow
75 // switching over long long (which size_t is in 64bit mode)
79 // nothing read, so nothing more to read
80 m_lasterror
= wxSTREAM_EOF
;
82 else if ( ret
== wxInvalidOffset
)
84 m_lasterror
= wxSTREAM_READ_ERROR
;
90 m_lasterror
= wxSTREAM_NO_ERROR
;
96 wxFileOffset
wxFileInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
98 return m_file
->Seek(pos
, mode
);
101 wxFileOffset
wxFileInputStream::OnSysTell() const
103 return m_file
->Tell();
106 // ----------------------------------------------------------------------------
107 // wxFileOutputStream
108 // ----------------------------------------------------------------------------
110 wxFileOutputStream::wxFileOutputStream(const wxString
& fileName
)
112 m_file
= new wxFile(fileName
, wxFile::write
);
113 m_file_destroy
= true;
115 if (!m_file
->IsOpened())
116 m_lasterror
= wxSTREAM_WRITE_ERROR
;
119 wxFileOutputStream::wxFileOutputStream(wxFile
& file
)
122 m_file_destroy
= false;
125 wxFileOutputStream::wxFileOutputStream()
128 m_file_destroy
= false;
132 wxFileOutputStream::wxFileOutputStream(int fd
)
134 m_file
= new wxFile(fd
);
135 m_file_destroy
= true;
138 wxFileOutputStream::~wxFileOutputStream()
147 size_t wxFileOutputStream::OnSysWrite(const void *buffer
, size_t size
)
149 size_t ret
= m_file
->Write(buffer
, size
);
151 m_lasterror
= m_file
->Error() ? wxSTREAM_WRITE_ERROR
: wxSTREAM_NO_ERROR
;
156 wxFileOffset
wxFileOutputStream::OnSysTell() const
158 return m_file
->Tell();
161 wxFileOffset
wxFileOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
163 return m_file
->Seek(pos
, mode
);
166 void wxFileOutputStream::Sync()
168 wxOutputStream::Sync();
172 wxFileOffset
wxFileOutputStream::GetLength() const
174 return m_file
->Length();
177 // ----------------------------------------------------------------------------
178 // wxTempFileOutputStream
179 // ----------------------------------------------------------------------------
181 wxTempFileOutputStream::wxTempFileOutputStream(const wxString
& fileName
)
183 m_file
= new wxTempFile(fileName
);
185 if (!m_file
->IsOpened())
186 m_lasterror
= wxSTREAM_WRITE_ERROR
;
189 wxTempFileOutputStream::~wxTempFileOutputStream()
191 if (m_file
->IsOpened())
196 size_t wxTempFileOutputStream::OnSysWrite(const void *buffer
, size_t size
)
198 if (IsOk() && m_file
->Write(buffer
, size
))
200 m_lasterror
= wxSTREAM_WRITE_ERROR
;
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
208 wxFileStream::wxFileStream(const wxString
& fileName
)
209 : wxFileInputStream(fileName
)
211 wxFileOutputStream::m_file
= wxFileInputStream::m_file
;
218 // ----------------------------------------------------------------------------
219 // wxFFileInputStream
220 // ----------------------------------------------------------------------------
222 wxFFileInputStream::wxFFileInputStream(const wxString
& fileName
,
226 m_file
= new wxFFile(fileName
, mode
);
227 m_file_destroy
= true;
229 if (!m_file
->IsOpened())
230 m_lasterror
= wxSTREAM_WRITE_ERROR
;
233 wxFFileInputStream::wxFFileInputStream()
237 m_file_destroy
= false;
240 wxFFileInputStream::wxFFileInputStream(wxFFile
& file
)
243 m_file_destroy
= false;
246 wxFFileInputStream::wxFFileInputStream(FILE *file
)
248 m_file
= new wxFFile(file
);
249 m_file_destroy
= true;
252 wxFFileInputStream::~wxFFileInputStream()
258 wxFileOffset
wxFFileInputStream::GetLength() const
260 return m_file
->Length();
263 size_t wxFFileInputStream::OnSysRead(void *buffer
, size_t size
)
265 ssize_t ret
= m_file
->Read(buffer
, size
);
267 // It is not safe to call Eof() if the file is not opened.
268 if (!m_file
->IsOpened() || m_file
->Eof())
269 m_lasterror
= wxSTREAM_EOF
;
270 if (ret
== wxInvalidOffset
)
272 m_lasterror
= wxSTREAM_READ_ERROR
;
279 wxFileOffset
wxFFileInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
281 return m_file
->Seek(pos
, mode
) ? m_file
->Tell() : wxInvalidOffset
;
284 wxFileOffset
wxFFileInputStream::OnSysTell() const
286 return m_file
->Tell();
289 // ----------------------------------------------------------------------------
290 // wxFFileOutputStream
291 // ----------------------------------------------------------------------------
293 wxFFileOutputStream::wxFFileOutputStream(const wxString
& fileName
,
296 m_file
= new wxFFile(fileName
, mode
);
297 m_file_destroy
= true;
299 if (!m_file
->IsOpened())
301 m_lasterror
= wxSTREAM_WRITE_ERROR
;
306 m_lasterror
= wxSTREAM_WRITE_ERROR
;
310 wxFFileOutputStream::wxFFileOutputStream(wxFFile
& file
)
313 m_file_destroy
= false;
316 wxFFileOutputStream::wxFFileOutputStream()
320 m_file_destroy
= false;
323 wxFFileOutputStream::wxFFileOutputStream(FILE *file
)
325 m_file
= new wxFFile(file
);
326 m_file_destroy
= true;
329 wxFFileOutputStream::~wxFFileOutputStream()
338 size_t wxFFileOutputStream::OnSysWrite(const void *buffer
, size_t size
)
340 size_t ret
= m_file
->Write(buffer
, size
);
341 // It is not safe to call Error() if the file is not opened.
342 if (!m_file
->IsOpened() || m_file
->Error())
343 m_lasterror
= wxSTREAM_WRITE_ERROR
;
345 m_lasterror
= wxSTREAM_NO_ERROR
;
349 wxFileOffset
wxFFileOutputStream::OnSysTell() const
351 return m_file
->Tell();
354 wxFileOffset
wxFFileOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
356 return m_file
->Seek(pos
, mode
) ? m_file
->Tell() : wxInvalidOffset
;
359 void wxFFileOutputStream::Sync()
361 wxOutputStream::Sync();
365 wxFileOffset
wxFFileOutputStream::GetLength() const
367 return m_file
->Length();
370 // ----------------------------------------------------------------------------
372 // ----------------------------------------------------------------------------
374 wxFFileStream::wxFFileStream(const wxString
& fileName
)
375 : wxFFileInputStream(fileName
)
377 wxFFileOutputStream::m_file
= wxFFileInputStream::m_file
;
382 #endif // wxUSE_STREAMS