]>
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 | ||
44 | // ---------------------------------------------------------------------------- | |
45 | // opening the file | |
46 | // ---------------------------------------------------------------------------- | |
47 | ||
90e2cbf7 | 48 | wxFFile::wxFFile(const wxChar *filename, const wxChar *mode) |
a1b82138 VZ |
49 | { |
50 | Detach(); | |
51 | ||
52 | (void)Open(filename, mode); | |
53 | } | |
54 | ||
90e2cbf7 | 55 | bool wxFFile::Open(const wxChar *filename, const wxChar *mode) |
a1b82138 | 56 | { |
223d09f6 | 57 | wxASSERT_MSG( !m_fp, wxT("should close or detach the old file first") ); |
a1b82138 | 58 | |
90e2cbf7 | 59 | m_fp = wxFopen(filename, mode); |
a1b82138 VZ |
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 | { | |
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 | ||
94 | bool wxFFile::ReadAll(wxString *str) | |
95 | { | |
223d09f6 KB |
96 | wxCHECK_MSG( str, FALSE, wxT("invalid parameter") ); |
97 | wxCHECK_MSG( IsOpened(), FALSE, wxT("can't read from closed file") ); | |
a1b82138 VZ |
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 | { | |
223d09f6 KB |
126 | wxCHECK_MSG( pBuf, FALSE, wxT("invalid parameter") ); |
127 | wxCHECK_MSG( IsOpened(), FALSE, wxT("can't read from closed file") ); | |
a1b82138 VZ |
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 | { | |
223d09f6 KB |
140 | wxCHECK_MSG( pBuf, FALSE, wxT("invalid parameter") ); |
141 | wxCHECK_MSG( IsOpened(), FALSE, wxT("can't write to closed file") ); | |
a1b82138 VZ |
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 | { | |
5b96c684 MB |
156 | // fflush returns non-zero on error |
157 | // | |
158 | if ( fflush(m_fp) ) | |
a1b82138 VZ |
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 | { | |
223d09f6 | 175 | wxCHECK_MSG( IsOpened(), FALSE, wxT("can't seek on closed file") ); |
a1b82138 VZ |
176 | |
177 | int origin; | |
178 | switch ( mode ) | |
179 | { | |
180 | default: | |
223d09f6 | 181 | wxFAIL_MSG(wxT("unknown seek mode")); |
a1b82138 VZ |
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 | { | |
de2ce07c VZ |
209 | wxCHECK_MSG( IsOpened(), (size_t)-1, |
210 | _T("wxFFile::Tell(): file is closed!") ); | |
211 | ||
a1b82138 VZ |
212 | long rc = ftell(m_fp); |
213 | if ( rc == -1 ) | |
214 | { | |
215 | wxLogSysError(_("Can't find current position in file '%s'"), | |
216 | m_name.c_str()); | |
217 | } | |
218 | ||
219 | return (size_t)rc; | |
220 | } | |
221 | ||
222 | size_t wxFFile::Length() const | |
223 | { | |
de2ce07c VZ |
224 | wxCHECK_MSG( IsOpened(), (size_t)-1, |
225 | _T("wxFFile::Length(): file is closed!") ); | |
226 | ||
a1b82138 VZ |
227 | wxFFile& self = *(wxFFile *)this; // const_cast |
228 | ||
229 | size_t posOld = Tell(); | |
230 | if ( posOld != (size_t)-1 ) | |
231 | { | |
232 | if ( self.SeekEnd() ) | |
233 | { | |
234 | size_t len = Tell(); | |
235 | ||
236 | (void)self.Seek(posOld); | |
237 | ||
238 | return len; | |
239 | } | |
240 | } | |
241 | ||
242 | return (size_t)-1; | |
243 | } | |
244 | ||
1e6feb95 | 245 | #endif // wxUSE_FFILE |