]>
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 #include "wx/msw/mslu.h"
41 // ============================================================================
43 // ============================================================================
45 // ----------------------------------------------------------------------------
46 // seek and tell with large file support if available
47 // ----------------------------------------------------------------------------
50 # define wxFseek fseeko
51 # define wxFtell ftello
53 # define wxFseek fseek
54 # define wxFtell ftell
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 wxFFile::wxFFile(const wxString
& filename
, const wxString
& mode
)
65 (void)Open(filename
, mode
);
68 bool wxFFile::Open(const wxString
& filename
, const wxString
& mode
)
70 wxASSERT_MSG( !m_fp
, wxT("should close or detach the old file first") );
72 m_fp
= wxFopen(filename
, mode
);
76 wxLogSysError(_("can't open file '%s'"), filename
);
90 if ( fclose(m_fp
) != 0 )
92 wxLogSysError(_("can't close file '%s'"), m_name
.c_str());
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 bool wxFFile::ReadAll(wxString
*str
, const wxMBConv
& conv
)
109 wxCHECK_MSG( str
, false, wxT("invalid parameter") );
110 wxCHECK_MSG( IsOpened(), false, wxT("can't read from closed file") );
111 wxCHECK_MSG( Length() >= 0, false, wxT("invalid length") );
112 size_t length
= wx_truncate_cast(size_t, Length());
113 wxCHECK_MSG( (wxFileOffset
)length
== Length(), false, wxT("huge file not supported") );
117 wxCharBuffer
buf(length
+ 1);
119 // note that real length may be less than file length for text files with DOS EOLs
120 // ('\r's get dropped by CRT when reading which means that we have
121 // realLen = fileLen - numOfLinesInTheFile)
122 length
= fread(buf
.data(), sizeof(char), length
, m_fp
);
126 wxLogSysError(_("Read error on file '%s'"), m_name
.c_str());
131 buf
.data()[length
] = 0;
132 *str
= wxString(buf
, conv
);
137 size_t wxFFile::Read(void *pBuf
, size_t nCount
)
139 wxCHECK_MSG( pBuf
, 0, wxT("invalid parameter") );
140 wxCHECK_MSG( IsOpened(), 0, wxT("can't read from closed file") );
142 size_t nRead
= fread(pBuf
, 1, nCount
, m_fp
);
143 if ( (nRead
< nCount
) && Error() )
145 wxLogSysError(_("Read error on file '%s'"), m_name
.c_str());
151 size_t wxFFile::Write(const void *pBuf
, size_t nCount
)
153 wxCHECK_MSG( pBuf
, 0, wxT("invalid parameter") );
154 wxCHECK_MSG( IsOpened(), 0, wxT("can't write to closed file") );
156 size_t nWritten
= fwrite(pBuf
, 1, nCount
, m_fp
);
157 if ( nWritten
< nCount
)
159 wxLogSysError(_("Write error on file '%s'"), m_name
.c_str());
165 bool wxFFile::Write(const wxString
& s
, const wxMBConv
& conv
)
167 const wxWX2MBbuf buf
= s
.mb_str(conv
);
171 const size_t size
= strlen(buf
); // FIXME: use buf.length() when available
172 return Write(buf
, size
) == size
;
175 bool wxFFile::Flush()
179 if ( fflush(m_fp
) != 0 )
181 wxLogSysError(_("failed to flush the file '%s'"), m_name
.c_str());
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
194 bool wxFFile::Seek(wxFileOffset ofs
, wxSeekMode mode
)
196 wxCHECK_MSG( IsOpened(), false, wxT("can't seek on closed file") );
202 wxFAIL_MSG(wxT("unknown seek mode"));
203 // still fall through
219 if ((long)ofs
!= ofs
)
221 wxLogError(_("Seek error on file '%s' (large files not supported by stdio)"), m_name
.c_str());
226 if ( wxFseek(m_fp
, (long)ofs
, origin
) != 0 )
228 if ( wxFseek(m_fp
, ofs
, origin
) != 0 )
231 wxLogSysError(_("Seek error on file '%s'"), m_name
.c_str());
239 wxFileOffset
wxFFile::Tell() const
241 wxCHECK_MSG( IsOpened(), wxInvalidOffset
,
242 _T("wxFFile::Tell(): file is closed!") );
244 wxFileOffset rc
= wxFtell(m_fp
);
245 if ( rc
== wxInvalidOffset
)
247 wxLogSysError(_("Can't find current position in file '%s'"),
254 wxFileOffset
wxFFile::Length() const
256 wxCHECK_MSG( IsOpened(), wxInvalidOffset
,
257 _T("wxFFile::Length(): file is closed!") );
259 wxFFile
& self
= *(wxFFile
*)this; // const_cast
261 wxFileOffset posOld
= Tell();
262 if ( posOld
!= wxInvalidOffset
)
264 if ( self
.SeekEnd() )
266 wxFileOffset len
= Tell();
268 (void)self
.Seek(posOld
);
274 return wxInvalidOffset
;
277 #endif // wxUSE_FFILE