]> git.saurik.com Git - wxWidgets.git/blame - src/common/ffile.cpp
Temp. fix for ..\..\zlib problem.
[wxWidgets.git] / src / common / ffile.cpp
CommitLineData
a1b82138
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: ffile.cpp
3// Purpose: wxFFile - encapsulates "FILE *" IO stream
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>
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
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
31#if wxUSE_FILE
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
48wxFFile::wxFFile(const wxChar *filename, const char *mode)
49{
50 Detach();
51
52 (void)Open(filename, mode);
53}
54
55bool wxFFile::Open(const wxChar *filename, const char *mode)
56{
57 wxASSERT_MSG( !m_fp, _T("should close or detach the old file first") );
58
59 m_fp = fopen(filename, mode);
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{
96 wxCHECK_MSG( str, FALSE, _T("invalid parameter") );
97 wxCHECK_MSG( IsOpened(), FALSE, _T("can't read from closed file") );
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{
126 wxCHECK_MSG( pBuf, FALSE, _T("invalid parameter") );
127 wxCHECK_MSG( IsOpened(), FALSE, _T("can't read from closed file") );
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{
140 wxCHECK_MSG( pBuf, FALSE, _T("invalid parameter") );
141 wxCHECK_MSG( IsOpened(), FALSE, _T("can't write to closed file") );
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 {
156 if ( !fflush(m_fp) )
157 {
158 wxLogSysError(_("failed to flush the file '%s'"), m_name.c_str());
159
160 return FALSE;
161 }
162 }
163
164 return TRUE;
165}
166
167// ----------------------------------------------------------------------------
168// seeking
169// ----------------------------------------------------------------------------
170
171bool wxFFile::Seek(long ofs, wxSeekMode mode)
172{
173 wxCHECK_MSG( IsOpened(), FALSE, _T("can't seek on closed file") );
174
175 int origin;
176 switch ( mode )
177 {
178 default:
179 wxFAIL_MSG(_T("unknown seek mode"));
180 // still fall through
181
182 case wxFromStart:
183 origin = SEEK_SET;
184 break;
185
186 case wxFromCurrent:
187 origin = SEEK_CUR;
188 break;
189
190 case wxFromEnd:
191 origin = SEEK_END;
192 break;
193 }
194
195 if ( fseek(m_fp, ofs, origin) != 0 )
196 {
197 wxLogSysError(_("Seek error on file '%s'"), m_name.c_str());
198
199 return FALSE;
200 }
201
202 return TRUE;
203}
204
205size_t wxFFile::Tell() const
206{
207 long rc = ftell(m_fp);
208 if ( rc == -1 )
209 {
210 wxLogSysError(_("Can't find current position in file '%s'"),
211 m_name.c_str());
212 }
213
214 return (size_t)rc;
215}
216
217size_t wxFFile::Length() const
218{
219 wxFFile& self = *(wxFFile *)this; // const_cast
220
221 size_t posOld = Tell();
222 if ( posOld != (size_t)-1 )
223 {
224 if ( self.SeekEnd() )
225 {
226 size_t len = Tell();
227
228 (void)self.Seek(posOld);
229
230 return len;
231 }
232 }
233
234 return (size_t)-1;
235}
236
237#endif // wxUSE_FILE