]> git.saurik.com Git - wxWidgets.git/blob - src/common/file.cpp
Added wxAcceleratorTable, wxFrame::SetAcceleratorTable and additions to process it...
[wxWidgets.git] / src / common / file.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: file.cpp
3 // Purpose: wxFile - encapsulates low-level "file descriptor"
4 // wxTempFile
5 // Author: Vadim Zeitlin
6 // Modified by:
7 // Created: 29/01/98
8 // RCS-ID: $Id$
9 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
12
13 // ----------------------------------------------------------------------------
14 // headers
15 // ----------------------------------------------------------------------------
16
17 #ifdef __GNUG__
18 #pragma implementation "file.h"
19 #endif
20
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
23 #include "wx/defs.h"
24
25 #ifdef __BORLANDC__
26 #pragma hdrstop
27 #endif
28
29 // standard
30 #if defined(__WXMSW__) && !defined(__GNUWIN32__)
31 #include <io.h>
32
33 #define WIN32_LEAN_AND_MEAN
34 #define NOSERVICE
35 #define NOIME
36 #define NOATOM
37 #define NOGDI
38 #define NOGDICAPMASKS
39 #define NOMETAFILE
40 #define NOMINMAX
41 #define NOMSG
42 #define NOOPENFILE
43 #define NORASTEROPS
44 #define NOSCROLL
45 #define NOSOUND
46 #define NOSYSMETRICS
47 #define NOTEXTMETRIC
48 #define NOWH
49 #define NOCOMM
50 #define NOKANJI
51 #define NOCRYPT
52 #define NOMCX
53 #include <windows.h> // for GetTempFileName
54 #elif (defined(__UNIX__) || defined(__GNUWIN32__))
55 #include <unistd.h>
56 #else
57 #error "Please specify the header with file functions declarations."
58 #endif //Win/UNIX
59
60 #include <stdio.h> // SEEK_xxx constants
61 #include <fcntl.h> // O_RDONLY &c
62 #include <sys/types.h> // needed for stat
63 #include <sys/stat.h> // stat
64
65 // Microsoft compiler loves underscores, feed them to it
66 #ifdef _MSC_VER
67 // functions
68 #define open _open
69 #define close _close
70 #define read _read
71 #define write _write
72 #define lseek _lseek
73 #define fsync _commit
74 #define access _access
75 #define eof _eof
76
77 // types
78 #define stat _stat
79
80 // constants
81 #define O_RDONLY _O_RDONLY
82 #define O_WRONLY _O_WRONLY
83 #define O_RDWR _O_RDWR
84 #define O_EXCL _O_EXCL
85 #define O_CREAT _O_CREAT
86 #define O_BINARY _O_BINARY
87
88 #define S_IFDIR _S_IFDIR
89 #define S_IFREG _S_IFREG
90 #else
91 #define tell(fd) lseek(fd, 0, SEEK_CUR)
92 #endif //_MSC_VER
93
94 // there is no distinction between text and binary files under Unix
95 #ifdef __UNIX__
96 #define O_BINARY (0)
97 #endif //__UNIX__
98
99 // wxWindows
100 #include <wx/string.h>
101 #include <wx/intl.h>
102 #include <wx/file.h>
103 #include <wx/log.h>
104
105 #ifndef MAX_PATH
106 #define MAX_PATH 512
107 #endif
108
109 // ============================================================================
110 // implementation of wxFile
111 // ============================================================================
112
113 // ----------------------------------------------------------------------------
114 // static functions
115 // ----------------------------------------------------------------------------
116 bool wxFile::Exists(const char *sz)
117 {
118 struct stat st;
119 return !access(sz, 0) && !stat(sz, &st) && (st.st_mode & S_IFREG);
120 }
121
122 // ----------------------------------------------------------------------------
123 // opening/closing
124 // ----------------------------------------------------------------------------
125
126 // ctors
127 wxFile::wxFile(const char *szFileName, OpenMode mode)
128 {
129 m_fd = fd_invalid;
130 m_error = FALSE;
131
132 Open(szFileName, mode);
133 }
134
135 // dtor
136 wxFile::~wxFile()
137 {
138 Close();
139 }
140
141 // create the file, fail if it already exists and bOverwrite
142 bool wxFile::Create(const char *szFileName, bool bOverwrite, int access)
143 {
144 // if bOverwrite we create a new file or truncate the existing one,
145 // otherwise we only create the new file and fail if it already exists
146 int fd = open(szFileName, O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
147
148 if ( fd == -1 ) {
149 wxLogSysError("can't create file '%s'", szFileName);
150 return FALSE;
151 }
152 else {
153 Attach(fd);
154 return TRUE;
155 }
156 }
157
158 // open the file
159 bool wxFile::Open(const char *szFileName, OpenMode mode, int access)
160 {
161 int flags = O_BINARY;
162
163 switch ( mode ) {
164 case read:
165 flags |= O_RDONLY;
166 break;
167
168 case write:
169 flags |= O_WRONLY | O_CREAT | O_TRUNC;
170 break;
171
172 case write_append:
173 flags |= O_WRONLY | O_APPEND;
174 break;
175
176 case read_write:
177 flags |= O_RDWR;
178 break;
179 }
180
181 int fd = open(szFileName, flags, access);
182
183 if ( fd == -1 ) {
184 wxLogSysError("can't open file '%s'", szFileName);
185 return FALSE;
186 }
187 else {
188 Attach(fd);
189 return TRUE;
190 }
191 }
192
193 // close
194 bool wxFile::Close()
195 {
196 if ( IsOpened() ) {
197 if ( close(m_fd) == -1 ) {
198 wxLogSysError("can't close file descriptor %d", m_fd);
199 m_fd = fd_invalid;
200 return FALSE;
201 }
202 else
203 m_fd = fd_invalid;
204 }
205
206 return TRUE;
207 }
208
209 // ----------------------------------------------------------------------------
210 // read/write
211 // ----------------------------------------------------------------------------
212
213 // read
214 off_t wxFile::Read(void *pBuf, off_t nCount)
215 {
216 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
217
218 int iRc = ::read(m_fd, pBuf, nCount);
219 if ( iRc == -1 ) {
220 wxLogSysError("can't read from file descriptor %d", m_fd);
221 return wxInvalidOffset;
222 }
223 else
224 return (uint)iRc;
225 }
226
227 // write
228 uint wxFile::Write(const void *pBuf, uint nCount)
229 {
230 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
231
232 int iRc = ::write(m_fd, pBuf, nCount);
233 if ( iRc == -1 ) {
234 wxLogSysError("can't write to file descriptor %d", m_fd);
235 m_error = TRUE;
236 return 0;
237 }
238 else
239 return iRc;
240 }
241
242 // flush
243 bool wxFile::Flush()
244 {
245 if ( IsOpened() ) {
246 // @@@ fsync() is not ANSI (BSDish)
247 // if ( fsync(m_fd) == -1 ) { // TODO
248 if (TRUE) {
249 wxLogSysError("can't flush file descriptor %d", m_fd);
250 return FALSE;
251 }
252 }
253
254 return TRUE;
255 }
256
257 // ----------------------------------------------------------------------------
258 // seek
259 // ----------------------------------------------------------------------------
260
261 // seek
262 off_t wxFile::Seek(off_t ofs, wxSeekMode mode)
263 {
264 wxASSERT( IsOpened() );
265
266 int flag = -1;
267 switch ( mode ) {
268 case wxFromStart:
269 flag = SEEK_SET;
270 break;
271
272 case wxFromCurrent:
273 flag = SEEK_CUR;
274 break;
275
276 case wxFromEnd:
277 flag = SEEK_END;
278 break;
279
280 default:
281 wxFAIL_MSG("unknown seek origin");
282 }
283
284 int iRc = lseek(m_fd, ofs, flag);
285 if ( iRc == -1 ) {
286 wxLogSysError("can't seek on file descriptor %d", m_fd);
287 return wxInvalidOffset;
288 }
289 else
290 return (off_t)iRc;
291 }
292
293 // get current off_t
294 off_t wxFile::Tell() const
295 {
296 wxASSERT( IsOpened() );
297
298 int iRc = tell(m_fd);
299 if ( iRc == -1 ) {
300 wxLogSysError("can't get seek position on file descriptor %d", m_fd);
301 return wxInvalidOffset;
302 }
303 else
304 return (off_t)iRc;
305 }
306
307 // get current file length
308 off_t wxFile::Length() const
309 {
310 wxASSERT( IsOpened() );
311
312 #ifdef _MSC_VER
313 int iRc = _filelength(m_fd);
314 #else
315 int iRc = tell(m_fd);
316 if ( iRc != -1 ) {
317 // @ have to use const_cast :-(
318 int iLen = ((wxFile *)this)->SeekEnd();
319 if ( iLen != -1 ) {
320 // restore old position
321 if ( ((wxFile *)this)->Seek(iRc) == -1 ) {
322 // error
323 iLen = -1;
324 }
325 }
326
327 iRc = iLen;
328 }
329
330 #endif //_MSC_VER
331
332 if ( iRc == -1 ) {
333 wxLogSysError("can't find length of file on file descriptor %d", m_fd);
334 return wxInvalidOffset;
335 }
336 else
337 return (off_t)iRc;
338 }
339
340 // is end of file reached?
341 bool wxFile::Eof() const
342 {
343 wxASSERT( IsOpened() );
344
345 int iRc;
346
347 #if defined(__UNIX__) || defined(__GNUWIN32__)
348 // @@ this doesn't work, of course, on unseekable file descriptors
349 off_t ofsCur = Tell(),
350 ofsMax = Length();
351 if ( ofsCur == wxInvalidOffset || ofsMax == wxInvalidOffset )
352 iRc = -1;
353 else
354 iRc = ofsCur == ofsMax;
355 #else // Windows and "native" compiler
356 iRc = eof(m_fd);
357 #endif // Windows/Unix
358
359 switch ( iRc ) {
360 case 1:
361 break;
362
363 case 0:
364 return FALSE;
365
366 case -1:
367 wxLogSysError("can't determine if the end of file is reached on "
368 "descriptor %d", m_fd);
369 break;
370
371 default:
372 wxFAIL_MSG("invalid eof() return value.");
373 }
374
375 return TRUE;
376 }
377
378 // ============================================================================
379 // implementation of wxTempFile
380 // ============================================================================
381
382 // ----------------------------------------------------------------------------
383 // construction
384 // ----------------------------------------------------------------------------
385 wxTempFile::wxTempFile(const wxString& strName)
386 {
387 Open(strName);
388 }
389
390 bool wxTempFile::Open(const wxString& strName)
391 {
392 m_strName = strName;
393
394 // we want to create the file in the same directory as strName because
395 // otherwise rename() in Commit() might not work (if the files are on
396 // different partitions for example). Unfortunately, the only standard
397 // (POSIX) temp file creation function tmpnam() can't do it.
398 #ifdef __UNIX__
399 static const char *szMktempSuffix = "XXXXXX";
400 m_strTemp << strName << szMktempSuffix;
401 mktemp((char *)m_strTemp.c_str()); // will do because length doesn't change
402 #else // Windows
403 wxString strPath;
404 wxSplitPath(strName, &strPath, NULL, NULL);
405 if ( strPath.IsEmpty() )
406 strPath = '.'; // GetTempFileName will fail if we give it empty string
407 #ifdef __WIN32__
408 if ( !GetTempFileName(strPath, "wx_",0, m_strTemp.GetWriteBuf(MAX_PATH)) )
409 #else
410 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
411 if ( !GetTempFileName((BYTE) (const char*) strPath, "wx_",0, m_strTemp.GetWriteBuf(MAX_PATH)) )
412 #endif
413 wxLogLastError("GetTempFileName");
414 m_strTemp.UngetWriteBuf();
415 #endif // Windows/Unix
416
417 return m_file.Open(m_strTemp, wxFile::write);
418 }
419
420 // ----------------------------------------------------------------------------
421 // destruction
422 // ----------------------------------------------------------------------------
423
424 wxTempFile::~wxTempFile()
425 {
426 if ( IsOpened() )
427 Discard();
428 }
429
430 bool wxTempFile::Commit()
431 {
432 m_file.Close();
433
434 if ( wxFile::Exists(m_strName) && remove(m_strName) != 0 ) {
435 wxLogSysError("can't remove file '%s'", m_strName.c_str());
436 return FALSE;
437 }
438
439 if ( rename(m_strTemp, m_strName) != 0 ) {
440 wxLogSysError("can't commit changes to file '%s'", m_strName.c_str());
441 return FALSE;
442 }
443
444 return TRUE;
445 }
446
447 void wxTempFile::Discard()
448 {
449 m_file.Close();
450 if ( remove(m_strTemp) != 0 )
451 wxLogSysError("can't remove temporary file '%s'", m_strTemp.c_str());
452 }