]>
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 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(fileName
)
225 wxFileOutputStream::m_file
= wxFileInputStream::m_file
;
228 bool wxFileStream::IsOk() const
230 return wxFileOutputStream::IsOk() && wxFileInputStream::IsOk();
237 // ----------------------------------------------------------------------------
238 // wxFFileInputStream
239 // ----------------------------------------------------------------------------
241 wxFFileInputStream::wxFFileInputStream(const wxString
& fileName
,
242 const wxString
& mode
)
245 m_file
= new wxFFile(fileName
, mode
);
246 m_file_destroy
= true;
248 if (!m_file
->IsOpened())
249 m_lasterror
= wxSTREAM_WRITE_ERROR
;
252 wxFFileInputStream::wxFFileInputStream()
256 m_file_destroy
= false;
259 wxFFileInputStream::wxFFileInputStream(wxFFile
& file
)
262 m_file_destroy
= false;
265 wxFFileInputStream::wxFFileInputStream(FILE *file
)
267 m_file
= new wxFFile(file
);
268 m_file_destroy
= true;
271 wxFFileInputStream::~wxFFileInputStream()
277 wxFileOffset
wxFFileInputStream::GetLength() const
279 return m_file
->Length();
282 size_t wxFFileInputStream::OnSysRead(void *buffer
, size_t size
)
284 ssize_t ret
= m_file
->Read(buffer
, size
);
286 // It is not safe to call Eof() if the file is not opened.
287 if (!m_file
->IsOpened() || m_file
->Eof())
288 m_lasterror
= wxSTREAM_EOF
;
289 if (ret
== wxInvalidOffset
)
291 m_lasterror
= wxSTREAM_READ_ERROR
;
298 wxFileOffset
wxFFileInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
300 return m_file
->Seek(pos
, mode
) ? m_file
->Tell() : wxInvalidOffset
;
303 wxFileOffset
wxFFileInputStream::OnSysTell() const
305 return m_file
->Tell();
308 bool wxFFileInputStream::IsOk() const
310 return wxStreamBase::IsOk() && m_file
->IsOpened();
313 // ----------------------------------------------------------------------------
314 // wxFFileOutputStream
315 // ----------------------------------------------------------------------------
317 wxFFileOutputStream::wxFFileOutputStream(const wxString
& fileName
,
318 const wxString
& mode
)
320 m_file
= new wxFFile(fileName
, mode
);
321 m_file_destroy
= true;
323 if (!m_file
->IsOpened())
325 m_lasterror
= wxSTREAM_WRITE_ERROR
;
330 m_lasterror
= wxSTREAM_WRITE_ERROR
;
334 wxFFileOutputStream::wxFFileOutputStream(wxFFile
& file
)
337 m_file_destroy
= false;
340 wxFFileOutputStream::wxFFileOutputStream()
344 m_file_destroy
= false;
347 wxFFileOutputStream::wxFFileOutputStream(FILE *file
)
349 m_file
= new wxFFile(file
);
350 m_file_destroy
= true;
353 wxFFileOutputStream::~wxFFileOutputStream()
362 size_t wxFFileOutputStream::OnSysWrite(const void *buffer
, size_t size
)
364 size_t ret
= m_file
->Write(buffer
, size
);
365 // It is not safe to call Error() if the file is not opened.
366 if (!m_file
->IsOpened() || m_file
->Error())
367 m_lasterror
= wxSTREAM_WRITE_ERROR
;
369 m_lasterror
= wxSTREAM_NO_ERROR
;
373 wxFileOffset
wxFFileOutputStream::OnSysTell() const
375 return m_file
->Tell();
378 wxFileOffset
wxFFileOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
380 return m_file
->Seek(pos
, mode
) ? m_file
->Tell() : wxInvalidOffset
;
383 void wxFFileOutputStream::Sync()
385 wxOutputStream::Sync();
389 wxFileOffset
wxFFileOutputStream::GetLength() const
391 return m_file
->Length();
394 bool wxFFileOutputStream::IsOk() const
396 return wxStreamBase::IsOk() && m_file
->IsOpened();
399 // ----------------------------------------------------------------------------
401 // ----------------------------------------------------------------------------
403 wxFFileStream::wxFFileStream(const wxString
& fileName
)
404 : wxFFileInputStream(fileName
)
406 wxFFileOutputStream::m_file
= wxFFileInputStream::m_file
;
409 bool wxFFileStream::IsOk() const
411 return wxFFileOutputStream::IsOk() && wxFFileInputStream::IsOk();
416 #endif // wxUSE_STREAMS