]>
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "ffile.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
40 // ============================================================================
42 // ============================================================================
44 // ----------------------------------------------------------------------------
45 // seek and tell with large file support if available
46 // ----------------------------------------------------------------------------
49 # define wxFseek fseeko
50 # define wxFtell ftello
52 # define wxFseek fseek
53 # define wxFtell ftell
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 wxFFile::wxFFile(const wxChar
*filename
, const wxChar
*mode
)
64 (void)Open(filename
, mode
);
67 bool wxFFile::Open(const wxChar
*filename
, const wxChar
*mode
)
69 wxASSERT_MSG( !m_fp
, wxT("should close or detach the old file first") );
71 m_fp
= wxFopen(filename
, mode
);
75 wxLogSysError(_("can't open file '%s'"), filename
);
89 if ( fclose(m_fp
) != 0 )
91 wxLogSysError(_("can't close file '%s'"), m_name
.c_str());
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 bool wxFFile::ReadAll(wxString
*str
)
108 wxCHECK_MSG( str
, false, wxT("invalid parameter") );
109 wxCHECK_MSG( IsOpened(), false, wxT("can't read from closed file") );
110 wxCHECK_MSG( Length() >= 0, false, wxT("invalid length") );
111 size_t length
= (size_t)Length();
112 wxCHECK_MSG( length
== Length(), false, wxT("huge file not supported") );
120 static const size_t nSize
= WXSIZEOF(buf
) - 1; // -1 for trailing '\0'
123 size_t nRead
= fread(buf
, sizeof(wxChar
), nSize
, m_fp
);
124 if ( (nRead
< nSize
) && Error() )
126 wxLogSysError(_("Read error on file '%s'"), m_name
.c_str());
139 size_t wxFFile::Read(void *pBuf
, size_t nCount
)
141 wxCHECK_MSG( pBuf
, FALSE
, wxT("invalid parameter") );
142 wxCHECK_MSG( IsOpened(), FALSE
, wxT("can't read from closed file") );
144 size_t nRead
= fread(pBuf
, 1, nCount
, m_fp
);
145 if ( (nRead
< nCount
) && Error() )
147 wxLogSysError(_("Read error on file '%s'"), m_name
.c_str());
153 size_t wxFFile::Write(const void *pBuf
, size_t nCount
)
155 wxCHECK_MSG( pBuf
, FALSE
, wxT("invalid parameter") );
156 wxCHECK_MSG( IsOpened(), FALSE
, wxT("can't write to closed file") );
158 size_t nWritten
= fwrite(pBuf
, 1, nCount
, m_fp
);
159 if ( nWritten
< nCount
)
161 wxLogSysError(_("Write error on file '%s'"), m_name
.c_str());
167 bool wxFFile::Flush()
171 // fflush returns non-zero on error
175 wxLogSysError(_("failed to flush the file '%s'"), m_name
.c_str());
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
188 bool wxFFile::Seek(wxFileOffset ofs
, wxSeekMode mode
)
190 wxCHECK_MSG( IsOpened(), false, wxT("can't seek on closed file") );
196 wxFAIL_MSG(wxT("unknown seek mode"));
197 // still fall through
213 if ((long)ofs
!= ofs
)
215 wxLogError(_("Seek error on file '%s' (large files not supported by stdio)"), m_name
.c_str());
220 if ( wxFseek(m_fp
, (long)ofs
, origin
) != 0 )
222 if ( wxFseek(m_fp
, ofs
, origin
) != 0 )
225 wxLogSysError(_("Seek error on file '%s'"), m_name
.c_str());
233 wxFileOffset
wxFFile::Tell() const
235 wxCHECK_MSG( IsOpened(), wxInvalidOffset
,
236 _T("wxFFile::Tell(): file is closed!") );
238 wxFileOffset rc
= wxFtell(m_fp
);
239 if ( rc
== wxInvalidOffset
)
241 wxLogSysError(_("Can't find current position in file '%s'"),
248 wxFileOffset
wxFFile::Length() const
250 wxCHECK_MSG( IsOpened(), wxInvalidOffset
,
251 _T("wxFFile::Length(): file is closed!") );
253 wxFFile
& self
= *(wxFFile
*)this; // const_cast
255 wxFileOffset posOld
= Tell();
256 if ( posOld
!= wxInvalidOffset
)
258 if ( self
.SeekEnd() )
260 wxFileOffset len
= Tell();
262 (void)self
.Seek(posOld
);
268 return wxInvalidOffset
;
271 #endif // wxUSE_FFILE