]>
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 // ----------------------------------------------------------------------------
111 // wxFileOutputStream
112 // ----------------------------------------------------------------------------
114 wxFileOutputStream::wxFileOutputStream(const wxString
& fileName
)
116 m_file
= new wxFile(fileName
, wxFile::write
);
117 m_file_destroy
= true;
119 if (!m_file
->IsOpened())
120 m_lasterror
= wxSTREAM_WRITE_ERROR
;
123 wxFileOutputStream::wxFileOutputStream(wxFile
& file
)
126 m_file_destroy
= false;
129 wxFileOutputStream::wxFileOutputStream()
132 m_file_destroy
= false;
136 wxFileOutputStream::wxFileOutputStream(int fd
)
138 m_file
= new wxFile(fd
);
139 m_file_destroy
= true;
142 wxFileOutputStream::~wxFileOutputStream()
151 size_t wxFileOutputStream::OnSysWrite(const void *buffer
, size_t size
)
153 size_t ret
= m_file
->Write(buffer
, size
);
155 m_lasterror
= m_file
->Error() ? wxSTREAM_WRITE_ERROR
: wxSTREAM_NO_ERROR
;
160 wxFileOffset
wxFileOutputStream::OnSysTell() const
162 return m_file
->Tell();
165 wxFileOffset
wxFileOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
167 return m_file
->Seek(pos
, mode
);
170 void wxFileOutputStream::Sync()
172 wxOutputStream::Sync();
176 wxFileOffset
wxFileOutputStream::GetLength() const
178 return m_file
->Length();
181 // ----------------------------------------------------------------------------
182 // wxTempFileOutputStream
183 // ----------------------------------------------------------------------------
185 wxTempFileOutputStream::wxTempFileOutputStream(const wxString
& fileName
)
187 m_file
= new wxTempFile(fileName
);
189 if (!m_file
->IsOpened())
190 m_lasterror
= wxSTREAM_WRITE_ERROR
;
193 wxTempFileOutputStream::~wxTempFileOutputStream()
195 if (m_file
->IsOpened())
200 size_t wxTempFileOutputStream::OnSysWrite(const void *buffer
, size_t size
)
202 if (IsOk() && m_file
->Write(buffer
, size
))
204 m_lasterror
= wxSTREAM_WRITE_ERROR
;
208 // ----------------------------------------------------------------------------
210 // ----------------------------------------------------------------------------
212 wxFileStream::wxFileStream(const wxString
& fileName
)
213 : wxFileInputStream(fileName
)
215 wxFileOutputStream::m_file
= wxFileInputStream::m_file
;
222 // ----------------------------------------------------------------------------
223 // wxFFileInputStream
224 // ----------------------------------------------------------------------------
226 wxFFileInputStream::wxFFileInputStream(const wxString
& fileName
,
230 m_file
= new wxFFile(fileName
, mode
);
231 m_file_destroy
= true;
233 if (!m_file
->IsOpened())
234 m_lasterror
= wxSTREAM_WRITE_ERROR
;
237 wxFFileInputStream::wxFFileInputStream()
241 m_file_destroy
= false;
244 wxFFileInputStream::wxFFileInputStream(wxFFile
& file
)
247 m_file_destroy
= false;
250 wxFFileInputStream::wxFFileInputStream(FILE *file
)
252 m_file
= new wxFFile(file
);
253 m_file_destroy
= true;
256 wxFFileInputStream::~wxFFileInputStream()
262 wxFileOffset
wxFFileInputStream::GetLength() const
264 return m_file
->Length();
267 size_t wxFFileInputStream::OnSysRead(void *buffer
, size_t size
)
269 ssize_t ret
= m_file
->Read(buffer
, size
);
271 // It is not safe to call Eof() if the file is not opened.
272 if (!m_file
->IsOpened() || m_file
->Eof())
273 m_lasterror
= wxSTREAM_EOF
;
274 if (ret
== wxInvalidOffset
)
276 m_lasterror
= wxSTREAM_READ_ERROR
;
283 wxFileOffset
wxFFileInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
285 return m_file
->Seek(pos
, mode
) ? m_file
->Tell() : wxInvalidOffset
;
288 wxFileOffset
wxFFileInputStream::OnSysTell() const
290 return m_file
->Tell();
293 // ----------------------------------------------------------------------------
294 // wxFFileOutputStream
295 // ----------------------------------------------------------------------------
297 wxFFileOutputStream::wxFFileOutputStream(const wxString
& fileName
,
300 m_file
= new wxFFile(fileName
, mode
);
301 m_file_destroy
= true;
303 if (!m_file
->IsOpened())
305 m_lasterror
= wxSTREAM_WRITE_ERROR
;
310 m_lasterror
= wxSTREAM_WRITE_ERROR
;
314 wxFFileOutputStream::wxFFileOutputStream(wxFFile
& file
)
317 m_file_destroy
= false;
320 wxFFileOutputStream::wxFFileOutputStream()
324 m_file_destroy
= false;
327 wxFFileOutputStream::wxFFileOutputStream(FILE *file
)
329 m_file
= new wxFFile(file
);
330 m_file_destroy
= true;
333 wxFFileOutputStream::~wxFFileOutputStream()
342 size_t wxFFileOutputStream::OnSysWrite(const void *buffer
, size_t size
)
344 size_t ret
= m_file
->Write(buffer
, size
);
345 // It is not safe to call Error() if the file is not opened.
346 if (!m_file
->IsOpened() || m_file
->Error())
347 m_lasterror
= wxSTREAM_WRITE_ERROR
;
349 m_lasterror
= wxSTREAM_NO_ERROR
;
353 wxFileOffset
wxFFileOutputStream::OnSysTell() const
355 return m_file
->Tell();
358 wxFileOffset
wxFFileOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
360 return m_file
->Seek(pos
, mode
) ? m_file
->Tell() : wxInvalidOffset
;
363 void wxFFileOutputStream::Sync()
365 wxOutputStream::Sync();
369 wxFileOffset
wxFFileOutputStream::GetLength() const
371 return m_file
->Length();
374 // ----------------------------------------------------------------------------
376 // ----------------------------------------------------------------------------
378 wxFFileStream::wxFFileStream(const wxString
& fileName
)
379 : wxFFileInputStream(fileName
)
381 wxFFileOutputStream::m_file
= wxFFileInputStream::m_file
;
386 #endif // wxUSE_STREAMS