]>
git.saurik.com Git - wxWidgets.git/blob - src/common/ffile.cpp
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 // ============================================================================
38 // ============================================================================
40 // ----------------------------------------------------------------------------
41 // seek and tell with large file support if available
42 // ----------------------------------------------------------------------------
45 # define wxFseek fseeko
46 # define wxFtell ftello
48 # define wxFseek fseek
49 # define wxFtell ftell
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 wxFFile::wxFFile(const wxChar
*filename
, const wxChar
*mode
)
60 (void)Open(filename
, mode
);
63 bool wxFFile::Open(const wxChar
*filename
, const wxChar
*mode
)
65 wxASSERT_MSG( !m_fp
, wxT("should close or detach the old file first") );
67 m_fp
= wxFopen(filename
, mode
);
71 wxLogSysError(_("can't open file '%s'"), filename
);
85 if ( fclose(m_fp
) != 0 )
87 wxLogSysError(_("can't close file '%s'"), m_name
.c_str());
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 bool wxFFile::ReadAll(wxString
*str
, wxMBConv
& conv
)
104 wxCHECK_MSG( str
, false, wxT("invalid parameter") );
105 wxCHECK_MSG( IsOpened(), false, wxT("can't read from closed file") );
106 wxCHECK_MSG( Length() >= 0, false, wxT("invalid length") );
107 size_t length
= (size_t)Length();
108 wxCHECK_MSG( (wxFileOffset
)length
== Length(), false, wxT("huge file not supported") );
112 wxCharBuffer
buf(length
+ 1);
114 // note that real length may be less than file length for text files with DOS EOLs
115 // ('\r's get dropped by CRT when reading which means that we have
116 // realLen = fileLen - numOfLinesInTheFile)
117 length
= fread(buf
.data(), sizeof(char), length
, m_fp
);
121 wxLogSysError(_("Read error on file '%s'"), m_name
.c_str());
126 buf
.data()[length
] = 0;
127 *str
= wxString(buf
, conv
);
132 size_t wxFFile::Read(void *pBuf
, size_t nCount
)
134 wxCHECK_MSG( pBuf
, 0, wxT("invalid parameter") );
135 wxCHECK_MSG( IsOpened(), 0, wxT("can't read from closed file") );
137 size_t nRead
= fread(pBuf
, 1, nCount
, m_fp
);
138 if ( (nRead
< nCount
) && Error() )
140 wxLogSysError(_("Read error on file '%s'"), m_name
.c_str());
146 size_t wxFFile::Write(const void *pBuf
, size_t nCount
)
148 wxCHECK_MSG( pBuf
, 0, wxT("invalid parameter") );
149 wxCHECK_MSG( IsOpened(), 0, wxT("can't write to closed file") );
151 size_t nWritten
= fwrite(pBuf
, 1, nCount
, m_fp
);
152 if ( nWritten
< nCount
)
154 wxLogSysError(_("Write error on file '%s'"), m_name
.c_str());
160 bool wxFFile::Flush()
164 // fflush returns non-zero on error
168 wxLogSysError(_("failed to flush the file '%s'"), m_name
.c_str());
177 // ----------------------------------------------------------------------------
179 // ----------------------------------------------------------------------------
181 bool wxFFile::Seek(wxFileOffset ofs
, wxSeekMode mode
)
183 wxCHECK_MSG( IsOpened(), false, wxT("can't seek on closed file") );
189 wxFAIL_MSG(wxT("unknown seek mode"));
190 // still fall through
206 if ((long)ofs
!= ofs
)
208 wxLogError(_("Seek error on file '%s' (large files not supported by stdio)"), m_name
.c_str());
213 if ( wxFseek(m_fp
, (long)ofs
, origin
) != 0 )
215 if ( wxFseek(m_fp
, ofs
, origin
) != 0 )
218 wxLogSysError(_("Seek error on file '%s'"), m_name
.c_str());
226 wxFileOffset
wxFFile::Tell() const
228 wxCHECK_MSG( IsOpened(), wxInvalidOffset
,
229 _T("wxFFile::Tell(): file is closed!") );
231 wxFileOffset rc
= wxFtell(m_fp
);
232 if ( rc
== wxInvalidOffset
)
234 wxLogSysError(_("Can't find current position in file '%s'"),
241 wxFileOffset
wxFFile::Length() const
243 wxCHECK_MSG( IsOpened(), wxInvalidOffset
,
244 _T("wxFFile::Length(): file is closed!") );
246 wxFFile
& self
= *(wxFFile
*)this; // const_cast
248 wxFileOffset posOld
= Tell();
249 if ( posOld
!= wxInvalidOffset
)
251 if ( self
.SeekEnd() )
253 wxFileOffset len
= Tell();
255 (void)self
.Seek(posOld
);
261 return wxInvalidOffset
;
264 #endif // wxUSE_FFILE