]> git.saurik.com Git - wxWidgets.git/blob - src/common/ffile.cpp
implemented fallback for AlphaBlend() for the systems not supporting it
[wxWidgets.git] / src / common / ffile.cpp
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 licence
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_FFILE
32
33 #ifndef WX_PRECOMP
34 #include "wx/intl.h"
35 #include "wx/log.h"
36 #endif
37
38 #include "wx/ffile.h"
39
40 // ============================================================================
41 // implementation
42 // ============================================================================
43
44 // ----------------------------------------------------------------------------
45 // opening the file
46 // ----------------------------------------------------------------------------
47
48 wxFFile::wxFFile(const wxChar *filename, const wxChar *mode)
49 {
50 Detach();
51
52 (void)Open(filename, mode);
53 }
54
55 bool wxFFile::Open(const wxChar *filename, const wxChar *mode)
56 {
57 wxASSERT_MSG( !m_fp, wxT("should close or detach the old file first") );
58
59 m_fp = wxFopen(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
73 bool wxFFile::Close()
74 {
75 if ( IsOpened() )
76 {
77 if ( fclose(m_fp) != 0 )
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
94 bool wxFFile::ReadAll(wxString *str)
95 {
96 wxCHECK_MSG( str, FALSE, wxT("invalid parameter") );
97 wxCHECK_MSG( IsOpened(), FALSE, wxT("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
124 size_t wxFFile::Read(void *pBuf, size_t nCount)
125 {
126 wxCHECK_MSG( pBuf, FALSE, wxT("invalid parameter") );
127 wxCHECK_MSG( IsOpened(), FALSE, wxT("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
138 size_t wxFFile::Write(const void *pBuf, size_t nCount)
139 {
140 wxCHECK_MSG( pBuf, FALSE, wxT("invalid parameter") );
141 wxCHECK_MSG( IsOpened(), FALSE, wxT("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
152 bool wxFFile::Flush()
153 {
154 if ( IsOpened() )
155 {
156 // fflush returns non-zero on error
157 //
158 if ( fflush(m_fp) )
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
173 bool wxFFile::Seek(long ofs, wxSeekMode mode)
174 {
175 wxCHECK_MSG( IsOpened(), FALSE, wxT("can't seek on closed file") );
176
177 int origin;
178 switch ( mode )
179 {
180 default:
181 wxFAIL_MSG(wxT("unknown seek mode"));
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
207 size_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
219 size_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
239 #endif // wxUSE_FFILE