]>
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
7 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
35 #include "wx/msw/mslu.h"
40 // ============================================================================
42 // ============================================================================
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 wxFFile::wxFFile(const wxString
& filename
, const wxString
& mode
)
52 (void)Open(filename
, mode
);
55 bool wxFFile::Open(const wxString
& filename
, const wxString
& mode
)
57 wxASSERT_MSG( !m_fp
, wxT("should close or detach the old file first") );
59 FILE* const fp
= wxFopen(filename
, mode
);
63 wxLogSysError(_("can't open file '%s'"), filename
);
77 if ( fclose(m_fp
) != 0 )
79 wxLogSysError(_("can't close file '%s'"), m_name
.c_str());
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
94 bool wxFFile::ReadAll(wxString
*str
, const wxMBConv
& conv
)
96 wxCHECK_MSG( str
, false, wxT("invalid parameter") );
97 wxCHECK_MSG( IsOpened(), false, wxT("can't read from closed file") );
98 wxCHECK_MSG( Length() >= 0, false, wxT("invalid length") );
99 size_t length
= wx_truncate_cast(size_t, Length());
100 wxCHECK_MSG( (wxFileOffset
)length
== Length(), false, wxT("huge file not supported") );
104 wxCharBuffer
buf(length
);
106 // note that real length may be less than file length for text files with DOS EOLs
107 // ('\r's get dropped by CRT when reading which means that we have
108 // realLen = fileLen - numOfLinesInTheFile)
109 length
= fread(buf
.data(), 1, length
, m_fp
);
113 wxLogSysError(_("Read error on file '%s'"), m_name
.c_str());
118 buf
.data()[length
] = 0;
120 wxString
strTmp(buf
, conv
);
126 size_t wxFFile::Read(void *pBuf
, size_t nCount
)
128 wxCHECK_MSG( pBuf
, 0, wxT("invalid parameter") );
129 wxCHECK_MSG( IsOpened(), 0, wxT("can't read from closed file") );
131 size_t nRead
= fread(pBuf
, 1, nCount
, m_fp
);
132 if ( (nRead
< nCount
) && Error() )
134 wxLogSysError(_("Read error on file '%s'"), m_name
.c_str());
140 size_t wxFFile::Write(const void *pBuf
, size_t nCount
)
142 wxCHECK_MSG( pBuf
, 0, wxT("invalid parameter") );
143 wxCHECK_MSG( IsOpened(), 0, wxT("can't write to closed file") );
145 size_t nWritten
= fwrite(pBuf
, 1, nCount
, m_fp
);
146 if ( nWritten
< nCount
)
148 wxLogSysError(_("Write error on file '%s'"), m_name
.c_str());
154 bool wxFFile::Write(const wxString
& s
, const wxMBConv
& conv
)
156 const wxWX2MBbuf buf
= s
.mb_str(conv
);
160 const size_t size
= strlen(buf
); // FIXME: use buf.length() when available
161 return Write(buf
, size
) == size
;
164 bool wxFFile::Flush()
168 if ( fflush(m_fp
) != 0 )
170 wxLogSysError(_("failed to flush the file '%s'"), m_name
.c_str());
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
183 bool wxFFile::Seek(wxFileOffset ofs
, wxSeekMode mode
)
185 wxCHECK_MSG( IsOpened(), false, wxT("can't seek on closed file") );
191 wxFAIL_MSG(wxT("unknown seek mode"));
192 // still fall through
207 #ifndef wxHAS_LARGE_FFILES
208 if ((long)ofs
!= ofs
)
210 wxLogError(_("Seek error on file '%s' (large files not supported by stdio)"), m_name
.c_str());
215 if ( wxFseek(m_fp
, (long)ofs
, origin
) != 0 )
217 if ( wxFseek(m_fp
, ofs
, origin
) != 0 )
220 wxLogSysError(_("Seek error on file '%s'"), m_name
.c_str());
228 wxFileOffset
wxFFile::Tell() const
230 wxCHECK_MSG( IsOpened(), wxInvalidOffset
,
231 wxT("wxFFile::Tell(): file is closed!") );
233 wxFileOffset rc
= wxFtell(m_fp
);
234 if ( rc
== wxInvalidOffset
)
236 wxLogSysError(_("Can't find current position in file '%s'"),
243 wxFileOffset
wxFFile::Length() const
245 wxCHECK_MSG( IsOpened(), wxInvalidOffset
,
246 wxT("wxFFile::Length(): file is closed!") );
248 wxFFile
& self
= *const_cast<wxFFile
*>(this);
250 wxFileOffset posOld
= Tell();
251 if ( posOld
!= wxInvalidOffset
)
253 if ( self
.SeekEnd() )
255 wxFileOffset len
= Tell();
257 (void)self
.Seek(posOld
);
263 return wxInvalidOffset
;
266 #endif // wxUSE_FFILE