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