]> git.saurik.com Git - wxWidgets.git/blame - src/common/ffile.cpp
gcc signed/unsigned warning fix
[wxWidgets.git] / src / common / ffile.cpp
CommitLineData
a1b82138
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: ffile.cpp
90e2cbf7 3// Purpose: wxFFile encapsulates "FILE *" IO stream
a1b82138
VZ
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 14.07.99
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 9// Licence: wxWindows licence
a1b82138
VZ
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
a1b82138
VZ
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
1e6feb95 27#if wxUSE_FFILE
a1b82138
VZ
28
29#ifndef WX_PRECOMP
0efe5ba7
VZ
30 #include "wx/intl.h"
31 #include "wx/log.h"
a1b82138
VZ
32#endif
33
89220a94
CE
34#ifdef __WINDOWS__
35#include "wx/msw/mslu.h"
36#endif
37
a1b82138
VZ
38#include "wx/ffile.h"
39
40// ============================================================================
41// implementation
42// ============================================================================
43
70a7bd90
VZ
44// ----------------------------------------------------------------------------
45// seek and tell with large file support if available
46// ----------------------------------------------------------------------------
47
48#ifdef HAVE_FSEEKO
49# define wxFseek fseeko
50# define wxFtell ftello
51#else
52# define wxFseek fseek
53# define wxFtell ftell
54#endif
55
a1b82138
VZ
56// ----------------------------------------------------------------------------
57// opening the file
58// ----------------------------------------------------------------------------
59
90e2cbf7 60wxFFile::wxFFile(const wxChar *filename, const wxChar *mode)
a1b82138
VZ
61{
62 Detach();
63
64 (void)Open(filename, mode);
65}
66
90e2cbf7 67bool wxFFile::Open(const wxChar *filename, const wxChar *mode)
a1b82138 68{
223d09f6 69 wxASSERT_MSG( !m_fp, wxT("should close or detach the old file first") );
a1b82138 70
90e2cbf7 71 m_fp = wxFopen(filename, mode);
a1b82138
VZ
72
73 if ( !m_fp )
74 {
75 wxLogSysError(_("can't open file '%s'"), filename);
76
a62848fd 77 return false;
a1b82138
VZ
78 }
79
80 m_name = filename;
81
a62848fd 82 return true;
a1b82138
VZ
83}
84
85bool wxFFile::Close()
86{
87 if ( IsOpened() )
88 {
d2e1ef19 89 if ( fclose(m_fp) != 0 )
a1b82138
VZ
90 {
91 wxLogSysError(_("can't close file '%s'"), m_name.c_str());
92
a62848fd 93 return false;
a1b82138
VZ
94 }
95
96 Detach();
97 }
98
a62848fd 99 return true;
a1b82138
VZ
100}
101
102// ----------------------------------------------------------------------------
103// read/write
104// ----------------------------------------------------------------------------
105
830f8f11 106bool wxFFile::ReadAll(wxString *str, const wxMBConv& conv)
a1b82138 107{
a62848fd
WS
108 wxCHECK_MSG( str, false, wxT("invalid parameter") );
109 wxCHECK_MSG( IsOpened(), false, wxT("can't read from closed file") );
11846af3 110 wxCHECK_MSG( Length() >= 0, false, wxT("invalid length") );
17a1ebd1 111 size_t length = wx_truncate_cast(size_t, Length());
c3816108 112 wxCHECK_MSG( (wxFileOffset)length == Length(), false, wxT("huge file not supported") );
a1b82138
VZ
113
114 clearerr(m_fp);
115
2a9c0905 116 wxCharBuffer buf(length + 1);
f281f29f 117
2a9c0905 118 // note that real length may be less than file length for text files with DOS EOLs
f281f29f
VZ
119 // ('\r's get dropped by CRT when reading which means that we have
120 // realLen = fileLen - numOfLinesInTheFile)
2a9c0905 121 length = fread(buf.data(), sizeof(char), length, m_fp);
f281f29f
VZ
122
123 if ( Error() )
a1b82138 124 {
3e15dde3 125 wxLogSysError(_("Read error on file '%s'"), m_name.c_str());
a1b82138 126
3e15dde3 127 return false;
a1b82138
VZ
128 }
129
2a9c0905 130 buf.data()[length] = 0;
3e15dde3
VZ
131 *str = wxString(buf, conv);
132
a62848fd 133 return true;
a1b82138
VZ
134}
135
136size_t wxFFile::Read(void *pBuf, size_t nCount)
137{
2a9c0905
WS
138 wxCHECK_MSG( pBuf, 0, wxT("invalid parameter") );
139 wxCHECK_MSG( IsOpened(), 0, wxT("can't read from closed file") );
a1b82138
VZ
140
141 size_t nRead = fread(pBuf, 1, nCount, m_fp);
142 if ( (nRead < nCount) && Error() )
143 {
144 wxLogSysError(_("Read error on file '%s'"), m_name.c_str());
145 }
146
147 return nRead;
148}
149
150size_t wxFFile::Write(const void *pBuf, size_t nCount)
151{
2a9c0905
WS
152 wxCHECK_MSG( pBuf, 0, wxT("invalid parameter") );
153 wxCHECK_MSG( IsOpened(), 0, wxT("can't write to closed file") );
a1b82138
VZ
154
155 size_t nWritten = fwrite(pBuf, 1, nCount, m_fp);
156 if ( nWritten < nCount )
157 {
158 wxLogSysError(_("Write error on file '%s'"), m_name.c_str());
159 }
160
161 return nWritten;
162}
163
164bool wxFFile::Flush()
165{
166 if ( IsOpened() )
167 {
5b96c684
MB
168 // fflush returns non-zero on error
169 //
170 if ( fflush(m_fp) )
a1b82138
VZ
171 {
172 wxLogSysError(_("failed to flush the file '%s'"), m_name.c_str());
173
a62848fd 174 return false;
a1b82138
VZ
175 }
176 }
177
a62848fd 178 return true;
a1b82138
VZ
179}
180
181// ----------------------------------------------------------------------------
182// seeking
183// ----------------------------------------------------------------------------
184
70a7bd90 185bool wxFFile::Seek(wxFileOffset ofs, wxSeekMode mode)
a1b82138 186{
a62848fd 187 wxCHECK_MSG( IsOpened(), false, wxT("can't seek on closed file") );
a1b82138
VZ
188
189 int origin;
190 switch ( mode )
191 {
192 default:
223d09f6 193 wxFAIL_MSG(wxT("unknown seek mode"));
a1b82138
VZ
194 // still fall through
195
196 case wxFromStart:
197 origin = SEEK_SET;
198 break;
199
200 case wxFromCurrent:
201 origin = SEEK_CUR;
202 break;
203
204 case wxFromEnd:
205 origin = SEEK_END;
206 break;
207 }
208
11846af3 209#ifndef HAVE_FSEEKO
70a7bd90
VZ
210 if ((long)ofs != ofs)
211 {
212 wxLogError(_("Seek error on file '%s' (large files not supported by stdio)"), m_name.c_str());
213
214 return false;
215 }
70a7bd90 216
11846af3
WS
217 if ( wxFseek(m_fp, (long)ofs, origin) != 0 )
218#else
70a7bd90 219 if ( wxFseek(m_fp, ofs, origin) != 0 )
11846af3 220#endif
a1b82138
VZ
221 {
222 wxLogSysError(_("Seek error on file '%s'"), m_name.c_str());
223
a62848fd 224 return false;
a1b82138
VZ
225 }
226
a62848fd 227 return true;
a1b82138
VZ
228}
229
70a7bd90 230wxFileOffset wxFFile::Tell() const
a1b82138 231{
70a7bd90 232 wxCHECK_MSG( IsOpened(), wxInvalidOffset,
de2ce07c
VZ
233 _T("wxFFile::Tell(): file is closed!") );
234
70a7bd90
VZ
235 wxFileOffset rc = wxFtell(m_fp);
236 if ( rc == wxInvalidOffset )
a1b82138
VZ
237 {
238 wxLogSysError(_("Can't find current position in file '%s'"),
239 m_name.c_str());
240 }
241
70a7bd90 242 return rc;
a1b82138
VZ
243}
244
70a7bd90 245wxFileOffset wxFFile::Length() const
a1b82138 246{
70a7bd90 247 wxCHECK_MSG( IsOpened(), wxInvalidOffset,
de2ce07c
VZ
248 _T("wxFFile::Length(): file is closed!") );
249
a1b82138
VZ
250 wxFFile& self = *(wxFFile *)this; // const_cast
251
70a7bd90
VZ
252 wxFileOffset posOld = Tell();
253 if ( posOld != wxInvalidOffset )
a1b82138
VZ
254 {
255 if ( self.SeekEnd() )
256 {
70a7bd90 257 wxFileOffset len = Tell();
a1b82138
VZ
258
259 (void)self.Seek(posOld);
260
261 return len;
262 }
263 }
264
11846af3 265 return wxInvalidOffset;
a1b82138
VZ
266}
267
1e6feb95 268#endif // wxUSE_FFILE