]>
git.saurik.com Git - wxWidgets.git/blob - src/common/ffile.cpp
5957e34df2b055d598aca229f272a260e1692c87
1 /////////////////////////////////////////////////////////////////////////////
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
+ 1);
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(), sizeof(char), length
, m_fp
);
114 wxLogSysError(_("Read error on file '%s'"), m_name
.c_str());
119 buf
.data()[length
] = 0;
120 *str
= wxString(buf
, conv
);
125 size_t wxFFile::Read(void *pBuf
, size_t nCount
)
127 wxCHECK_MSG( pBuf
, 0, wxT("invalid parameter") );
128 wxCHECK_MSG( IsOpened(), 0, wxT("can't read from closed file") );
130 size_t nRead
= fread(pBuf
, 1, nCount
, m_fp
);
131 if ( (nRead
< nCount
) && Error() )
133 wxLogSysError(_("Read error on file '%s'"), m_name
.c_str());
139 size_t wxFFile::Write(const void *pBuf
, size_t nCount
)
141 wxCHECK_MSG( pBuf
, 0, wxT("invalid parameter") );
142 wxCHECK_MSG( IsOpened(), 0, wxT("can't write to closed file") );
144 size_t nWritten
= fwrite(pBuf
, 1, nCount
, m_fp
);
145 if ( nWritten
< nCount
)
147 wxLogSysError(_("Write error on file '%s'"), m_name
.c_str());
153 bool wxFFile::Write(const wxString
& s
, const wxMBConv
& conv
)
155 const wxWX2MBbuf buf
= s
.mb_str(conv
);
159 const size_t size
= strlen(buf
); // FIXME: use buf.length() when available
160 return Write(buf
, size
) == size
;
163 bool wxFFile::Flush()
167 if ( fflush(m_fp
) != 0 )
169 wxLogSysError(_("failed to flush the file '%s'"), m_name
.c_str());
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
182 bool wxFFile::Seek(wxFileOffset ofs
, wxSeekMode mode
)
184 wxCHECK_MSG( IsOpened(), false, wxT("can't seek on closed file") );
190 wxFAIL_MSG(wxT("unknown seek mode"));
191 // still fall through
206 #ifndef wxHAS_LARGE_FFILES
207 if ((long)ofs
!= ofs
)
209 wxLogError(_("Seek error on file '%s' (large files not supported by stdio)"), m_name
.c_str());
214 if ( wxFseek(m_fp
, (long)ofs
, origin
) != 0 )
216 if ( wxFseek(m_fp
, ofs
, origin
) != 0 )
219 wxLogSysError(_("Seek error on file '%s'"), m_name
.c_str());
227 wxFileOffset
wxFFile::Tell() const
229 wxCHECK_MSG( IsOpened(), wxInvalidOffset
,
230 wxT("wxFFile::Tell(): file is closed!") );
232 wxFileOffset rc
= wxFtell(m_fp
);
233 if ( rc
== wxInvalidOffset
)
235 wxLogSysError(_("Can't find current position in file '%s'"),
242 wxFileOffset
wxFFile::Length() const
244 wxCHECK_MSG( IsOpened(), wxInvalidOffset
,
245 wxT("wxFFile::Length(): file is closed!") );
247 wxFFile
& self
= *const_cast<wxFFile
*>(this);
249 wxFileOffset posOld
= Tell();
250 if ( posOld
!= wxInvalidOffset
)
252 if ( self
.SeekEnd() )
254 wxFileOffset len
= Tell();
256 (void)self
.Seek(posOld
);
262 return wxInvalidOffset
;
265 #endif // wxUSE_FFILE