]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
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 | 60 | wxFFile::wxFFile(const wxChar *filename, const wxChar *mode) |
a1b82138 VZ |
61 | { |
62 | Detach(); | |
63 | ||
64 | (void)Open(filename, mode); | |
65 | } | |
66 | ||
90e2cbf7 | 67 | bool 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 | ||
85 | bool 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 | ||
106 | bool wxFFile::ReadAll(wxString *str) | |
107 | { | |
a62848fd WS |
108 | wxCHECK_MSG( str, false, wxT("invalid parameter") ); |
109 | wxCHECK_MSG( IsOpened(), false, wxT("can't read from closed file") ); | |
a1b82138 VZ |
110 | |
111 | clearerr(m_fp); | |
112 | ||
113 | str->Empty(); | |
114 | str->Alloc(Length()); | |
115 | ||
116 | wxChar buf[1024]; | |
117 | static const size_t nSize = WXSIZEOF(buf) - 1; // -1 for trailing '\0' | |
118 | while ( !Eof() ) | |
119 | { | |
120 | size_t nRead = fread(buf, sizeof(wxChar), nSize, m_fp); | |
121 | if ( (nRead < nSize) && Error() ) | |
122 | { | |
123 | wxLogSysError(_("Read error on file '%s'"), m_name.c_str()); | |
124 | ||
a62848fd | 125 | return false; |
a1b82138 VZ |
126 | } |
127 | //else: just EOF | |
128 | ||
129 | buf[nRead] = 0; | |
130 | *str += buf; | |
131 | } | |
132 | ||
a62848fd | 133 | return true; |
a1b82138 VZ |
134 | } |
135 | ||
136 | size_t wxFFile::Read(void *pBuf, size_t nCount) | |
137 | { | |
223d09f6 KB |
138 | wxCHECK_MSG( pBuf, FALSE, wxT("invalid parameter") ); |
139 | wxCHECK_MSG( IsOpened(), FALSE, 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 | ||
150 | size_t wxFFile::Write(const void *pBuf, size_t nCount) | |
151 | { | |
223d09f6 KB |
152 | wxCHECK_MSG( pBuf, FALSE, wxT("invalid parameter") ); |
153 | wxCHECK_MSG( IsOpened(), FALSE, 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 | ||
164 | bool 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 | 185 | bool 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 | ||
70a7bd90 VZ |
209 | #ifndef HAVE_FSEEKO |
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 | } | |
216 | #endif | |
217 | ||
218 | if ( wxFseek(m_fp, ofs, origin) != 0 ) | |
a1b82138 VZ |
219 | { |
220 | wxLogSysError(_("Seek error on file '%s'"), m_name.c_str()); | |
221 | ||
a62848fd | 222 | return false; |
a1b82138 VZ |
223 | } |
224 | ||
a62848fd | 225 | return true; |
a1b82138 VZ |
226 | } |
227 | ||
70a7bd90 | 228 | wxFileOffset wxFFile::Tell() const |
a1b82138 | 229 | { |
70a7bd90 | 230 | wxCHECK_MSG( IsOpened(), wxInvalidOffset, |
de2ce07c VZ |
231 | _T("wxFFile::Tell(): file is closed!") ); |
232 | ||
70a7bd90 VZ |
233 | wxFileOffset rc = wxFtell(m_fp); |
234 | if ( rc == wxInvalidOffset ) | |
a1b82138 VZ |
235 | { |
236 | wxLogSysError(_("Can't find current position in file '%s'"), | |
237 | m_name.c_str()); | |
238 | } | |
239 | ||
70a7bd90 | 240 | return rc; |
a1b82138 VZ |
241 | } |
242 | ||
70a7bd90 | 243 | wxFileOffset wxFFile::Length() const |
a1b82138 | 244 | { |
70a7bd90 | 245 | wxCHECK_MSG( IsOpened(), wxInvalidOffset, |
de2ce07c VZ |
246 | _T("wxFFile::Length(): file is closed!") ); |
247 | ||
a1b82138 VZ |
248 | wxFFile& self = *(wxFFile *)this; // const_cast |
249 | ||
70a7bd90 VZ |
250 | wxFileOffset posOld = Tell(); |
251 | if ( posOld != wxInvalidOffset ) | |
a1b82138 VZ |
252 | { |
253 | if ( self.SeekEnd() ) | |
254 | { | |
70a7bd90 | 255 | wxFileOffset len = Tell(); |
a1b82138 VZ |
256 | |
257 | (void)self.Seek(posOld); | |
258 | ||
259 | return len; | |
260 | } | |
261 | } | |
262 | ||
70a7bd90 | 263 | return wxInvalidOffset; |
a1b82138 VZ |
264 | } |
265 | ||
1e6feb95 | 266 | #endif // wxUSE_FFILE |