]>
git.saurik.com Git - wxWidgets.git/blob - src/common/ffile.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/ffile.cpp
3 // Purpose: wxFFile encapsulates "FILE *" IO stream
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
36 #include "wx/msw/mslu.h"
41 // ============================================================================
43 // ============================================================================
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 wxFFile::wxFFile(const wxString
& filename
, const wxString
& mode
)
53 (void)Open(filename
, mode
);
56 bool wxFFile::Open(const wxString
& filename
, const wxString
& mode
)
58 wxASSERT_MSG( !m_fp
, wxT("should close or detach the old file first") );
60 m_fp
= wxFopen(filename
, mode
);
64 wxLogSysError(_("can't open file '%s'"), filename
);
78 if ( fclose(m_fp
) != 0 )
80 wxLogSysError(_("can't close file '%s'"), m_name
.c_str());
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 bool wxFFile::ReadAll(wxString
*str
, const wxMBConv
& conv
)
97 wxCHECK_MSG( str
, false, wxT("invalid parameter") );
98 wxCHECK_MSG( IsOpened(), false, wxT("can't read from closed file") );
99 wxCHECK_MSG( Length() >= 0, false, wxT("invalid length") );
100 size_t length
= wx_truncate_cast(size_t, Length());
101 wxCHECK_MSG( (wxFileOffset
)length
== Length(), false, wxT("huge file not supported") );
105 wxCharBuffer
buf(length
);
107 // note that real length may be less than file length for text files with DOS EOLs
108 // ('\r's get dropped by CRT when reading which means that we have
109 // realLen = fileLen - numOfLinesInTheFile)
110 length
= fread(buf
.data(), 1, length
, m_fp
);
114 wxLogSysError(_("Read error on file '%s'"), m_name
.c_str());
119 buf
.data()[length
] = 0;
121 wxString
strTmp(buf
, conv
);
127 size_t wxFFile::Read(void *pBuf
, size_t nCount
)
129 wxCHECK_MSG( pBuf
, 0, wxT("invalid parameter") );
130 wxCHECK_MSG( IsOpened(), 0, wxT("can't read from closed file") );
132 size_t nRead
= fread(pBuf
, 1, nCount
, m_fp
);
133 if ( (nRead
< nCount
) && Error() )
135 wxLogSysError(_("Read error on file '%s'"), m_name
.c_str());
141 size_t wxFFile::Write(const void *pBuf
, size_t nCount
)
143 wxCHECK_MSG( pBuf
, 0, wxT("invalid parameter") );
144 wxCHECK_MSG( IsOpened(), 0, wxT("can't write to closed file") );
146 size_t nWritten
= fwrite(pBuf
, 1, nCount
, m_fp
);
147 if ( nWritten
< nCount
)
149 wxLogSysError(_("Write error on file '%s'"), m_name
.c_str());
155 bool wxFFile::Write(const wxString
& s
, const wxMBConv
& conv
)
157 const wxWX2MBbuf buf
= s
.mb_str(conv
);
161 const size_t size
= strlen(buf
); // FIXME: use buf.length() when available
162 return Write(buf
, size
) == size
;
165 bool wxFFile::Flush()
169 if ( fflush(m_fp
) != 0 )
171 wxLogSysError(_("failed to flush the file '%s'"), m_name
.c_str());
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
184 bool wxFFile::Seek(wxFileOffset ofs
, wxSeekMode mode
)
186 wxCHECK_MSG( IsOpened(), false, wxT("can't seek on closed file") );
192 wxFAIL_MSG(wxT("unknown seek mode"));
193 // still fall through
208 #ifndef wxHAS_LARGE_FFILES
209 if ((long)ofs
!= ofs
)
211 wxLogError(_("Seek error on file '%s' (large files not supported by stdio)"), m_name
.c_str());
216 if ( wxFseek(m_fp
, (long)ofs
, origin
) != 0 )
218 if ( wxFseek(m_fp
, ofs
, origin
) != 0 )
221 wxLogSysError(_("Seek error on file '%s'"), m_name
.c_str());
229 wxFileOffset
wxFFile::Tell() const
231 wxCHECK_MSG( IsOpened(), wxInvalidOffset
,
232 wxT("wxFFile::Tell(): file is closed!") );
234 wxFileOffset rc
= wxFtell(m_fp
);
235 if ( rc
== wxInvalidOffset
)
237 wxLogSysError(_("Can't find current position in file '%s'"),
244 wxFileOffset
wxFFile::Length() const
246 wxCHECK_MSG( IsOpened(), wxInvalidOffset
,
247 wxT("wxFFile::Length(): file is closed!") );
249 wxFFile
& self
= *const_cast<wxFFile
*>(this);
251 wxFileOffset posOld
= Tell();
252 if ( posOld
!= wxInvalidOffset
)
254 if ( self
.SeekEnd() )
256 wxFileOffset len
= Tell();
258 (void)self
.Seek(posOld
);
264 return wxInvalidOffset
;
267 #endif // wxUSE_FFILE