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