]> git.saurik.com Git - wxWidgets.git/blame - src/common/ffile.cpp
Fixes for previous fixes
[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>
55d99c7a 9// Licence: wxWindows licence
a1b82138
VZ
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
a1b82138
VZ
21 #pragma implementation "ffile.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
1e6feb95 31#if wxUSE_FFILE
a1b82138
VZ
32
33#ifndef WX_PRECOMP
0efe5ba7
VZ
34 #include "wx/intl.h"
35 #include "wx/log.h"
a1b82138
VZ
36#endif
37
38#include "wx/ffile.h"
39
40// ============================================================================
41// implementation
42// ============================================================================
43
44// ----------------------------------------------------------------------------
45// opening the file
46// ----------------------------------------------------------------------------
47
90e2cbf7 48wxFFile::wxFFile(const wxChar *filename, const wxChar *mode)
a1b82138
VZ
49{
50 Detach();
51
52 (void)Open(filename, mode);
53}
54
90e2cbf7 55bool wxFFile::Open(const wxChar *filename, const wxChar *mode)
a1b82138 56{
223d09f6 57 wxASSERT_MSG( !m_fp, wxT("should close or detach the old file first") );
a1b82138 58
90e2cbf7 59 m_fp = wxFopen(filename, mode);
a1b82138
VZ
60
61 if ( !m_fp )
62 {
63 wxLogSysError(_("can't open file '%s'"), filename);
64
65 return FALSE;
66 }
67
68 m_name = filename;
69
70 return TRUE;
71}
72
73bool wxFFile::Close()
74{
75 if ( IsOpened() )
76 {
d2e1ef19 77 if ( fclose(m_fp) != 0 )
a1b82138
VZ
78 {
79 wxLogSysError(_("can't close file '%s'"), m_name.c_str());
80
81 return FALSE;
82 }
83
84 Detach();
85 }
86
87 return TRUE;
88}
89
90// ----------------------------------------------------------------------------
91// read/write
92// ----------------------------------------------------------------------------
93
94bool wxFFile::ReadAll(wxString *str)
95{
223d09f6
KB
96 wxCHECK_MSG( str, FALSE, wxT("invalid parameter") );
97 wxCHECK_MSG( IsOpened(), FALSE, wxT("can't read from closed file") );
a1b82138
VZ
98
99 clearerr(m_fp);
100
101 str->Empty();
102 str->Alloc(Length());
103
104 wxChar buf[1024];
105 static const size_t nSize = WXSIZEOF(buf) - 1; // -1 for trailing '\0'
106 while ( !Eof() )
107 {
108 size_t nRead = fread(buf, sizeof(wxChar), nSize, m_fp);
109 if ( (nRead < nSize) && Error() )
110 {
111 wxLogSysError(_("Read error on file '%s'"), m_name.c_str());
112
113 return FALSE;
114 }
115 //else: just EOF
116
117 buf[nRead] = 0;
118 *str += buf;
119 }
120
121 return TRUE;
122}
123
124size_t wxFFile::Read(void *pBuf, size_t nCount)
125{
223d09f6
KB
126 wxCHECK_MSG( pBuf, FALSE, wxT("invalid parameter") );
127 wxCHECK_MSG( IsOpened(), FALSE, wxT("can't read from closed file") );
a1b82138
VZ
128
129 size_t nRead = fread(pBuf, 1, nCount, m_fp);
130 if ( (nRead < nCount) && Error() )
131 {
132 wxLogSysError(_("Read error on file '%s'"), m_name.c_str());
133 }
134
135 return nRead;
136}
137
138size_t wxFFile::Write(const void *pBuf, size_t nCount)
139{
223d09f6
KB
140 wxCHECK_MSG( pBuf, FALSE, wxT("invalid parameter") );
141 wxCHECK_MSG( IsOpened(), FALSE, wxT("can't write to closed file") );
a1b82138
VZ
142
143 size_t nWritten = fwrite(pBuf, 1, nCount, m_fp);
144 if ( nWritten < nCount )
145 {
146 wxLogSysError(_("Write error on file '%s'"), m_name.c_str());
147 }
148
149 return nWritten;
150}
151
152bool wxFFile::Flush()
153{
154 if ( IsOpened() )
155 {
5b96c684
MB
156 // fflush returns non-zero on error
157 //
158 if ( fflush(m_fp) )
a1b82138
VZ
159 {
160 wxLogSysError(_("failed to flush the file '%s'"), m_name.c_str());
161
162 return FALSE;
163 }
164 }
165
166 return TRUE;
167}
168
169// ----------------------------------------------------------------------------
170// seeking
171// ----------------------------------------------------------------------------
172
173bool wxFFile::Seek(long ofs, wxSeekMode mode)
174{
223d09f6 175 wxCHECK_MSG( IsOpened(), FALSE, wxT("can't seek on closed file") );
a1b82138
VZ
176
177 int origin;
178 switch ( mode )
179 {
180 default:
223d09f6 181 wxFAIL_MSG(wxT("unknown seek mode"));
a1b82138
VZ
182 // still fall through
183
184 case wxFromStart:
185 origin = SEEK_SET;
186 break;
187
188 case wxFromCurrent:
189 origin = SEEK_CUR;
190 break;
191
192 case wxFromEnd:
193 origin = SEEK_END;
194 break;
195 }
196
197 if ( fseek(m_fp, ofs, origin) != 0 )
198 {
199 wxLogSysError(_("Seek error on file '%s'"), m_name.c_str());
200
201 return FALSE;
202 }
203
204 return TRUE;
205}
206
207size_t wxFFile::Tell() const
208{
209 long rc = ftell(m_fp);
210 if ( rc == -1 )
211 {
212 wxLogSysError(_("Can't find current position in file '%s'"),
213 m_name.c_str());
214 }
215
216 return (size_t)rc;
217}
218
219size_t wxFFile::Length() const
220{
221 wxFFile& self = *(wxFFile *)this; // const_cast
222
223 size_t posOld = Tell();
224 if ( posOld != (size_t)-1 )
225 {
226 if ( self.SeekEnd() )
227 {
228 size_t len = Tell();
229
230 (void)self.Seek(posOld);
231
232 return len;
233 }
234 }
235
236 return (size_t)-1;
237}
238
1e6feb95 239#endif // wxUSE_FFILE