]> git.saurik.com Git - wxWidgets.git/blob - src/common/ffile.cpp
Added calendar contributed by Lorne White
[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 license
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_FILE
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 char *mode)
49 {
50 Detach();
51
52 (void)Open(filename, mode);
53 }
54
55 bool wxFFile::Open(const wxChar *filename, const char *mode)
56 {
57 wxASSERT_MSG( !m_fp, wxT("should close or detach the old file first") );
58
59 #if wxUSE_UNICODE
60 char *tmp_fname;
61 size_t fname_len;
62
63 fname_len = wxStrlen(filename)+1;
64 tmp_fname = new char[fname_len];
65 wxWX2MB(tmp_fname, filename, fname_len);
66
67 m_fp = fopen(tmp_fname, mode);
68
69 delete tmp_fname;
70 #else
71 m_fp = fopen(filename, mode);
72 #endif
73
74
75 if ( !m_fp )
76 {
77 wxLogSysError(_("can't open file '%s'"), filename);
78
79 return FALSE;
80 }
81
82 m_name = filename;
83
84 return TRUE;
85 }
86
87 bool wxFFile::Close()
88 {
89 if ( IsOpened() )
90 {
91 if ( fclose(m_fp) != 0 )
92 {
93 wxLogSysError(_("can't close file '%s'"), m_name.c_str());
94
95 return FALSE;
96 }
97
98 Detach();
99 }
100
101 return TRUE;
102 }
103
104 // ----------------------------------------------------------------------------
105 // read/write
106 // ----------------------------------------------------------------------------
107
108 bool wxFFile::ReadAll(wxString *str)
109 {
110 wxCHECK_MSG( str, FALSE, wxT("invalid parameter") );
111 wxCHECK_MSG( IsOpened(), FALSE, wxT("can't read from closed file") );
112
113 clearerr(m_fp);
114
115 str->Empty();
116 str->Alloc(Length());
117
118 wxChar buf[1024];
119 static const size_t nSize = WXSIZEOF(buf) - 1; // -1 for trailing '\0'
120 while ( !Eof() )
121 {
122 size_t nRead = fread(buf, sizeof(wxChar), nSize, m_fp);
123 if ( (nRead < nSize) && Error() )
124 {
125 wxLogSysError(_("Read error on file '%s'"), m_name.c_str());
126
127 return FALSE;
128 }
129 //else: just EOF
130
131 buf[nRead] = 0;
132 *str += buf;
133 }
134
135 return TRUE;
136 }
137
138 size_t wxFFile::Read(void *pBuf, size_t nCount)
139 {
140 wxCHECK_MSG( pBuf, FALSE, wxT("invalid parameter") );
141 wxCHECK_MSG( IsOpened(), FALSE, wxT("can't read from closed file") );
142
143 size_t nRead = fread(pBuf, 1, nCount, m_fp);
144 if ( (nRead < nCount) && Error() )
145 {
146 wxLogSysError(_("Read error on file '%s'"), m_name.c_str());
147 }
148
149 return nRead;
150 }
151
152 size_t wxFFile::Write(const void *pBuf, size_t nCount)
153 {
154 wxCHECK_MSG( pBuf, FALSE, wxT("invalid parameter") );
155 wxCHECK_MSG( IsOpened(), FALSE, wxT("can't write to closed file") );
156
157 size_t nWritten = fwrite(pBuf, 1, nCount, m_fp);
158 if ( nWritten < nCount )
159 {
160 wxLogSysError(_("Write error on file '%s'"), m_name.c_str());
161 }
162
163 return nWritten;
164 }
165
166 bool wxFFile::Flush()
167 {
168 if ( IsOpened() )
169 {
170 // fflush returns non-zero on error
171 //
172 if ( fflush(m_fp) )
173 {
174 wxLogSysError(_("failed to flush the file '%s'"), m_name.c_str());
175
176 return FALSE;
177 }
178 }
179
180 return TRUE;
181 }
182
183 // ----------------------------------------------------------------------------
184 // seeking
185 // ----------------------------------------------------------------------------
186
187 bool wxFFile::Seek(long ofs, wxSeekMode mode)
188 {
189 wxCHECK_MSG( IsOpened(), FALSE, wxT("can't seek on closed file") );
190
191 int origin;
192 switch ( mode )
193 {
194 default:
195 wxFAIL_MSG(wxT("unknown seek mode"));
196 // still fall through
197
198 case wxFromStart:
199 origin = SEEK_SET;
200 break;
201
202 case wxFromCurrent:
203 origin = SEEK_CUR;
204 break;
205
206 case wxFromEnd:
207 origin = SEEK_END;
208 break;
209 }
210
211 if ( fseek(m_fp, ofs, origin) != 0 )
212 {
213 wxLogSysError(_("Seek error on file '%s'"), m_name.c_str());
214
215 return FALSE;
216 }
217
218 return TRUE;
219 }
220
221 size_t wxFFile::Tell() const
222 {
223 long rc = ftell(m_fp);
224 if ( rc == -1 )
225 {
226 wxLogSysError(_("Can't find current position in file '%s'"),
227 m_name.c_str());
228 }
229
230 return (size_t)rc;
231 }
232
233 size_t wxFFile::Length() const
234 {
235 wxFFile& self = *(wxFFile *)this; // const_cast
236
237 size_t posOld = Tell();
238 if ( posOld != (size_t)-1 )
239 {
240 if ( self.SeekEnd() )
241 {
242 size_t len = Tell();
243
244 (void)self.Seek(posOld);
245
246 return len;
247 }
248 }
249
250 return (size_t)-1;
251 }
252
253 #endif // wxUSE_FILE