]> git.saurik.com Git - wxWidgets.git/blame - src/common/file.cpp
dynamic event types actually work with these changes, also corrected more
[wxWidgets.git] / src / common / file.cpp
CommitLineData
c801d85f
KB
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>
246037e2 10// Licence: wxWindows license
c801d85f
KB
11/////////////////////////////////////////////////////////////////////////////
12
13// ----------------------------------------------------------------------------
14// headers
15// ----------------------------------------------------------------------------
16
17#ifdef __GNUG__
3f4a0c5b 18 #pragma implementation "file.h"
c801d85f
KB
19#endif
20
21// For compilers that support precompilation, includes "wx.h".
22#include "wx/wxprec.h"
c801d85f
KB
23
24#ifdef __BORLANDC__
ce4169a4 25 #pragma hdrstop
c801d85f
KB
26#endif
27
ce4169a4
RR
28#if wxUSE_FILE
29
c801d85f 30// standard
5ea105e0 31#if defined(__WXMSW__) && !defined(__GNUWIN32__) && !defined(__WXWINE__)
c801d85f 32 #include <io.h>
30a5be97 33
a3ef5bf5 34#ifndef __SALFORDC__
49d5d881
VZ
35 #define WIN32_LEAN_AND_MEAN
36 #define NOSERVICE
37 #define NOIME
38 #define NOATOM
39 #define NOGDI
40 #define NOGDICAPMASKS
41 #define NOMETAFILE
42 #define NOMINMAX
43 #define NOMSG
44 #define NOOPENFILE
45 #define NORASTEROPS
46 #define NOSCROLL
47 #define NOSOUND
48 #define NOSYSMETRICS
49 #define NOTEXTMETRIC
50 #define NOWH
51 #define NOCOMM
52 #define NOKANJI
53 #define NOCRYPT
54 #define NOMCX
a3ef5bf5
JS
55#endif
56
49d5d881 57 #include <windows.h> // for GetTempFileName
c801d85f 58#elif (defined(__UNIX__) || defined(__GNUWIN32__))
3f4a0c5b
VZ
59 #include <unistd.h>
60 #ifdef __GNUWIN32__
61 #include <windows.h>
62 #endif
c2ff79b1
DW
63#elif (defined(__WXPM__))
64 #include <io.h>
c2ff79b1
DW
65 #define W_OK 2
66 #define R_OK 4
34138703 67#elif (defined(__WXSTUBS__))
3f4a0c5b
VZ
68 // Have to ifdef this for different environments
69 #include <io.h>
17dff81c 70#elif (defined(__WXMAC__))
3f4a0c5b
VZ
71 int access( const char *path, int mode ) { return 0 ; }
72 char* mktemp( char * path ) { return path ;}
73 #include <unistd.h>
74 #include <unix.h>
75 #define W_OK 2
76 #define R_OK 4
c801d85f 77#else
3f4a0c5b 78 #error "Please specify the header with file functions declarations."
c801d85f
KB
79#endif //Win/UNIX
80
81#include <stdio.h> // SEEK_xxx constants
82#include <fcntl.h> // O_RDONLY &c
a3ef5bf5 83
469e1e5c 84#ifndef __MWERKS__
3f4a0c5b
VZ
85 #include <sys/types.h> // needed for stat
86 #include <sys/stat.h> // stat
469e1e5c 87#endif
c801d85f 88
3f4a0c5b 89#if defined(__BORLANDC__) || defined(_MSC_VER)
49d5d881
VZ
90 #define W_OK 2
91 #define R_OK 4
34138703
JS
92#endif
93
b12915c1
VZ
94// there is no distinction between text and binary files under Unix, so define
95// O_BINARY as 0 if the system headers don't do it already
96#if defined(__UNIX__) && !defined(O_BINARY)
49d5d881 97 #define O_BINARY (0)
246037e2 98#endif //__UNIX__
c801d85f 99
a3ef5bf5 100#ifdef __SALFORDC__
3f4a0c5b 101 #include <unix.h>
a3ef5bf5
JS
102#endif
103
81d66cf3 104#ifndef MAX_PATH
3f4a0c5b 105 #define MAX_PATH 512
81d66cf3 106#endif
c801d85f 107
49d5d881
VZ
108// some broken compilers don't have 3rd argument in open() and creat()
109#ifdef __SALFORDC__
110 #define ACCESS(access)
111 #define stat _stat
112#else // normal compiler
113 #define ACCESS(access) , (access)
114#endif // Salford C
115
f6bcfd97
BP
116// wxWindows
117#include "wx/string.h"
118#include "wx/intl.h"
119#include "wx/file.h"
120#include "wx/log.h"
121
c801d85f
KB
122// ============================================================================
123// implementation of wxFile
124// ============================================================================
125
126// ----------------------------------------------------------------------------
127// static functions
128// ----------------------------------------------------------------------------
50920146 129bool wxFile::Exists(const wxChar *name)
246037e2 130{
f6bcfd97 131 wxStructStat st;
50920146 132#if wxUSE_UNICODE && wxMBFILES
dcf924a3 133 wxCharBuffer fname = wxConvFile.cWC2MB(name);
a3ef5bf5 134
7c74e7fe
SC
135#ifdef __WXMAC__
136 return !access(wxUnix2MacFilename( name ) , 0) && !stat(wxUnix2MacFilename( name ), &st) && (st.st_mode & S_IFREG);
137#else
f6bcfd97
BP
138 return !wxAccess(fname, 0) &&
139 !wxStat(wxMBSTRINGCAST fname, &st) &&
50920146 140 (st.st_mode & S_IFREG);
7c74e7fe
SC
141#endif
142#else
143#ifdef __WXMAC__
144 return !access(wxUnix2MacFilename( name ) , 0) && !stat(wxUnix2MacFilename( name ), &st) && (st.st_mode & S_IFREG);
50920146 145#else
f6bcfd97
BP
146 return !wxAccess(name, 0) &&
147 !wxStat(name, &st) &&
49d5d881 148 (st.st_mode & S_IFREG);
50920146 149#endif
7c74e7fe 150#endif
d1427b70
VZ
151}
152
50920146 153bool wxFile::Access(const wxChar *name, OpenMode mode)
d1427b70 154{
49d5d881 155 int how = 0;
d1427b70 156
49d5d881
VZ
157 switch ( mode ) {
158 case read:
159 how = R_OK;
160 break;
d1427b70 161
49d5d881
VZ
162 case write:
163 how = W_OK;
164 break;
d1427b70 165
49d5d881 166 default:
223d09f6 167 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
49d5d881 168 }
d1427b70 169
f6bcfd97 170 return wxAccess(wxFNCONV(name), how) == 0;
c801d85f
KB
171}
172
173// ----------------------------------------------------------------------------
174// opening/closing
175// ----------------------------------------------------------------------------
176
177// ctors
50920146 178wxFile::wxFile(const wxChar *szFileName, OpenMode mode)
c801d85f 179{
49d5d881
VZ
180 m_fd = fd_invalid;
181 m_error = FALSE;
c801d85f 182
49d5d881 183 Open(szFileName, mode);
c801d85f
KB
184}
185
c801d85f 186// create the file, fail if it already exists and bOverwrite
50920146 187bool wxFile::Create(const wxChar *szFileName, bool bOverwrite, int accessMode)
c801d85f 188{
49d5d881
VZ
189 // if bOverwrite we create a new file or truncate the existing one,
190 // otherwise we only create the new file and fail if it already exists
7c74e7fe 191#ifdef __WXMAC__
312cd9e9 192 int fd = open(wxUnix2MacFilename( szFileName ), O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
7c74e7fe 193#else
f6bcfd97
BP
194 int fd = wxOpen(wxFNCONV(szFileName),
195 O_BINARY | O_WRONLY | O_CREAT |
196 (bOverwrite ? O_TRUNC : O_EXCL)
197 ACCESS(accessMode));
7c74e7fe 198#endif
49d5d881
VZ
199 if ( fd == -1 ) {
200 wxLogSysError(_("can't create file '%s'"), szFileName);
201 return FALSE;
202 }
203 else {
204 Attach(fd);
205 return TRUE;
206 }
c801d85f
KB
207}
208
209// open the file
50920146 210bool wxFile::Open(const wxChar *szFileName, OpenMode mode, int accessMode)
c801d85f 211{
49d5d881 212 int flags = O_BINARY;
c801d85f 213
49d5d881
VZ
214 switch ( mode ) {
215 case read:
216 flags |= O_RDONLY;
217 break;
c801d85f 218
f6bcfd97
BP
219 case write_append:
220 if ( wxFile::Exists(szFileName) )
221 {
222 flags |= O_WRONLY | O_APPEND;
223 break;
224 }
225 //else: fall through as write_append is the same as write if the
226 // file doesn't exist
227
49d5d881
VZ
228 case write:
229 flags |= O_WRONLY | O_CREAT | O_TRUNC;
230 break;
61b02744 231
49d5d881
VZ
232 case read_write:
233 flags |= O_RDWR;
234 break;
235 }
c801d85f 236
7c74e7fe 237#ifdef __WXMAC__
312cd9e9 238 int fd = open(wxUnix2MacFilename( szFileName ), flags, access);
7c74e7fe 239#else
f6bcfd97 240 int fd = wxOpen(wxFNCONV(szFileName), flags ACCESS(accessMode));
7c74e7fe 241#endif
49d5d881
VZ
242 if ( fd == -1 ) {
243 wxLogSysError(_("can't open file '%s'"), szFileName);
244 return FALSE;
245 }
246 else {
247 Attach(fd);
248 return TRUE;
249 }
c801d85f
KB
250}
251
252// close
61b02744 253bool wxFile::Close()
c801d85f 254{
49d5d881
VZ
255 if ( IsOpened() ) {
256 if ( close(m_fd) == -1 ) {
257 wxLogSysError(_("can't close file descriptor %d"), m_fd);
258 m_fd = fd_invalid;
259 return FALSE;
260 }
261 else
262 m_fd = fd_invalid;
61b02744 263 }
61b02744 264
49d5d881 265 return TRUE;
c801d85f
KB
266}
267
268// ----------------------------------------------------------------------------
269// read/write
270// ----------------------------------------------------------------------------
271
272// read
273off_t wxFile::Read(void *pBuf, off_t nCount)
274{
49d5d881 275 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
c801d85f 276
469e1e5c 277#ifdef __MWERKS__
49d5d881 278 int iRc = ::read(m_fd, (char*) pBuf, nCount);
469e1e5c 279#else
49d5d881 280 int iRc = ::read(m_fd, pBuf, nCount);
469e1e5c 281#endif
49d5d881
VZ
282 if ( iRc == -1 ) {
283 wxLogSysError(_("can't read from file descriptor %d"), m_fd);
284 return wxInvalidOffset;
285 }
286 else
287 return (size_t)iRc;
c801d85f
KB
288}
289
290// write
c86f1403 291size_t wxFile::Write(const void *pBuf, size_t nCount)
c801d85f 292{
49d5d881 293 wxCHECK( (pBuf != NULL) && IsOpened(), 0 );
c801d85f 294
469e1e5c 295#ifdef __MWERKS__
49d5d881 296 int iRc = ::write(m_fd, (const char*) pBuf, nCount);
469e1e5c 297#else
49d5d881 298 int iRc = ::write(m_fd, pBuf, nCount);
469e1e5c 299#endif
49d5d881
VZ
300 if ( iRc == -1 ) {
301 wxLogSysError(_("can't write to file descriptor %d"), m_fd);
302 m_error = TRUE;
303 return 0;
304 }
305 else
306 return iRc;
c801d85f
KB
307}
308
309// flush
310bool wxFile::Flush()
311{
49d5d881
VZ
312 if ( IsOpened() ) {
313#if defined(__VISUALC__) || wxHAVE_FSYNC
f6bcfd97 314 if ( wxFsync(m_fd) == -1 )
09914df7
VZ
315 {
316 wxLogSysError(_("can't flush file descriptor %d"), m_fd);
317 return FALSE;
318 }
49d5d881 319#else // no fsync
09914df7 320 // just do nothing
49d5d881
VZ
321#endif // fsync
322 }
c801d85f 323
49d5d881 324 return TRUE;
c801d85f
KB
325}
326
327// ----------------------------------------------------------------------------
328// seek
329// ----------------------------------------------------------------------------
330
331// seek
79c3e0e1 332off_t wxFile::Seek(off_t ofs, wxSeekMode mode)
c801d85f 333{
49d5d881
VZ
334 wxASSERT( IsOpened() );
335
a1b82138 336 int origin;
49d5d881 337 switch ( mode ) {
a1b82138
VZ
338 default:
339 wxFAIL_MSG(_("unknown seek origin"));
340
49d5d881 341 case wxFromStart:
a1b82138 342 origin = SEEK_SET;
49d5d881
VZ
343 break;
344
345 case wxFromCurrent:
a1b82138 346 origin = SEEK_CUR;
49d5d881
VZ
347 break;
348
349 case wxFromEnd:
a1b82138 350 origin = SEEK_END;
49d5d881 351 break;
49d5d881
VZ
352 }
353
a1b82138 354 int iRc = lseek(m_fd, ofs, origin);
49d5d881
VZ
355 if ( iRc == -1 ) {
356 wxLogSysError(_("can't seek on file descriptor %d"), m_fd);
357 return wxInvalidOffset;
358 }
359 else
360 return (off_t)iRc;
c801d85f
KB
361}
362
363// get current off_t
364off_t wxFile::Tell() const
365{
49d5d881
VZ
366 wxASSERT( IsOpened() );
367
f6bcfd97 368 int iRc = wxTell(m_fd);
49d5d881
VZ
369 if ( iRc == -1 ) {
370 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd);
371 return wxInvalidOffset;
372 }
373 else
374 return (off_t)iRc;
c801d85f
KB
375}
376
377// get current file length
378off_t wxFile::Length() const
379{
49d5d881 380 wxASSERT( IsOpened() );
c801d85f 381
49d5d881 382#ifdef __VISUALC__
c801d85f 383 int iRc = _filelength(m_fd);
49d5d881 384#else // !VC++
f6bcfd97 385 int iRc = wxTell(m_fd);
c801d85f 386 if ( iRc != -1 ) {
49d5d881
VZ
387 // @ have to use const_cast :-(
388 int iLen = ((wxFile *)this)->SeekEnd();
389 if ( iLen != -1 ) {
390 // restore old position
391 if ( ((wxFile *)this)->Seek(iRc) == -1 ) {
392 // error
393 iLen = -1;
394 }
c801d85f 395 }
c801d85f 396
49d5d881
VZ
397 iRc = iLen;
398 }
399#endif // VC++
400
401 if ( iRc == -1 ) {
402 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd);
403 return wxInvalidOffset;
c801d85f 404 }
49d5d881
VZ
405 else
406 return (off_t)iRc;
c801d85f
KB
407}
408
409// is end of file reached?
410bool wxFile::Eof() const
411{
49d5d881 412 wxASSERT( IsOpened() );
c801d85f 413
49d5d881 414 int iRc;
61b02744 415
49d5d881 416#if defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
61b02744
VZ
417 // @@ this doesn't work, of course, on unseekable file descriptors
418 off_t ofsCur = Tell(),
49d5d881 419 ofsMax = Length();
1678ad78 420 if ( ofsCur == wxInvalidOffset || ofsMax == wxInvalidOffset )
49d5d881 421 iRc = -1;
61b02744 422 else
49d5d881
VZ
423 iRc = ofsCur == ofsMax;
424#else // Windows and "native" compiler
61b02744 425 iRc = eof(m_fd);
49d5d881 426#endif // Windows/Unix
c801d85f 427
49d5d881
VZ
428 switch ( iRc ) {
429 case 1:
430 break;
c801d85f 431
49d5d881
VZ
432 case 0:
433 return FALSE;
c801d85f 434
49d5d881 435 case -1:
8e3f1261 436 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd);
49d5d881 437 break;
c801d85f 438
49d5d881
VZ
439 default:
440 wxFAIL_MSG(_("invalid eof() return value."));
441 }
c801d85f 442
49d5d881 443 return TRUE;
c801d85f
KB
444}
445
446// ============================================================================
447// implementation of wxTempFile
448// ============================================================================
449
450// ----------------------------------------------------------------------------
451// construction
452// ----------------------------------------------------------------------------
453wxTempFile::wxTempFile(const wxString& strName)
454{
49d5d881 455 Open(strName);
c801d85f
KB
456}
457
458bool wxTempFile::Open(const wxString& strName)
459{
49d5d881 460 m_strName = strName;
246037e2 461
49d5d881
VZ
462 // we want to create the file in the same directory as strName because
463 // otherwise rename() in Commit() might not work (if the files are on
464 // different partitions for example). Unfortunately, the only standard
465 // (POSIX) temp file creation function tmpnam() can't do it.
466#if defined(__UNIX__) || defined(__WXSTUBS__)|| defined( __WXMAC__ )
223d09f6 467 static const wxChar *szMktempSuffix = wxT("XXXXXX");
b59650be 468 m_strTemp << strName << szMktempSuffix;
e90c1d2a
VZ
469 // can use the cast because length doesn't change
470 mktemp(wxMBSTRINGCAST m_strTemp.mb_str());
c2ff79b1
DW
471#elif defined(__WXPM__)
472 // for now just create a file
473 // future enhancements can be to set some extended attributes for file systems
474 // OS/2 supports that have them (HPFS, FAT32) and security (HPFS386)
223d09f6 475 static const wxChar *szMktempSuffix = wxT("XXX");
c2ff79b1 476 m_strTemp << strName << szMktempSuffix;
f6bcfd97 477 ::DosCreateDir(m_strTemp.GetWriteBuf(MAX_PATH), NULL);
49d5d881 478#else // Windows
30a5be97
VZ
479 wxString strPath;
480 wxSplitPath(strName, &strPath, NULL, NULL);
481 if ( strPath.IsEmpty() )
223d09f6 482 strPath = wxT('.'); // GetTempFileName will fail if we give it empty string
81d66cf3 483#ifdef __WIN32__
223d09f6 484 if ( !GetTempFileName(strPath, wxT("wx_"),0, m_strTemp.GetWriteBuf(MAX_PATH)) )
81d66cf3 485#else
49d5d881 486 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
223d09f6 487 if ( !GetTempFileName((BYTE) (DWORD)(const wxChar*) strPath, wxT("wx_"),0, m_strTemp.GetWriteBuf(MAX_PATH)) )
81d66cf3 488#endif
223d09f6 489 wxLogLastError(wxT("GetTempFileName"));
30a5be97 490 m_strTemp.UngetWriteBuf();
49d5d881
VZ
491#endif // Windows/Unix
492
493 int access = wxS_DEFAULT;
494#ifdef __UNIX__
495 // create the file with the same mode as the original one under Unix
f46f1bc6 496 mode_t umaskOld = 0; // just to suppress compiler warning
fe99e285
VZ
497 bool changedUmask;
498
f6bcfd97 499 wxStructStat st;
50920146 500 if ( stat(strName.fn_str(), &st) == 0 )
49d5d881
VZ
501 {
502 // this assumes that only lower bits of st_mode contain the access
503 // rights, but it's true for at least all Unices which have S_IXXXX()
504 // macros, so should not be less portable than using (not POSIX)
505 // S_IFREG &c
506 access = st.st_mode & 0777;
fe99e285
VZ
507
508 // we want to create the file with exactly the same access rights as
509 // the original one, so disable the user's umask for the moment
510 umaskOld = umask(0);
511 changedUmask = TRUE;
49d5d881
VZ
512 }
513 else
514 {
fe99e285
VZ
515 // file probably didn't exist, just create with default mode _using_
516 // user's umask (new files creation should respet umask)
cedda7e6 517 changedUmask = FALSE;
49d5d881 518 }
49d5d881
VZ
519#endif // Unix
520
cedda7e6 521 bool ok = m_file.Open(m_strTemp, wxFile::write, access);
49d5d881
VZ
522
523#ifdef __UNIX__
fe99e285
VZ
524 if ( changedUmask )
525 {
526 // restore umask now that the file is created
527 (void)umask(umaskOld);
528 }
49d5d881 529#endif // Unix
246037e2 530
49d5d881 531 return ok;
c801d85f
KB
532}
533
534// ----------------------------------------------------------------------------
535// destruction
536// ----------------------------------------------------------------------------
537
538wxTempFile::~wxTempFile()
539{
49d5d881
VZ
540 if ( IsOpened() )
541 Discard();
c801d85f
KB
542}
543
544bool wxTempFile::Commit()
545{
49d5d881 546 m_file.Close();
c801d85f 547
7c74e7fe 548#ifndef __WXMAC__
f6bcfd97 549 if ( wxFile::Exists(m_strName) && wxRemove(m_strName) != 0 ) {
49d5d881
VZ
550 wxLogSysError(_("can't remove file '%s'"), m_strName.c_str());
551 return FALSE;
552 }
c801d85f 553
f6bcfd97 554 if ( wxRename(m_strTemp, m_strName) != 0 ) {
49d5d881
VZ
555 wxLogSysError(_("can't commit changes to file '%s'"), m_strName.c_str());
556 return FALSE;
557 }
7c74e7fe
SC
558#else
559 if ( wxFile::Exists(m_strName) && remove(wxUnix2MacFilename( m_strName )) != 0 ) {
560 wxLogSysError(_("can't remove file '%s'"), m_strName.c_str());
561 return FALSE;
562 }
563
564 if ( rename(wxUnix2MacFilename( m_strTemp ), wxUnix2MacFilename( m_strName )) != 0 ) {
565 wxLogSysError(_("can't commit changes to file '%s'"), m_strName.c_str());
566 return FALSE;
567 }
568#endif
c801d85f 569
49d5d881 570 return TRUE;
c801d85f
KB
571}
572
573void wxTempFile::Discard()
574{
49d5d881 575 m_file.Close();
7c74e7fe 576#ifndef __WXMAC__
f6bcfd97 577 if ( wxRemove(m_strTemp) != 0 )
49d5d881 578 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp.c_str());
7c74e7fe
SC
579#else
580 if ( remove( wxUnix2MacFilename(m_strTemp.fn_str())) != 0 )
581 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp.c_str());
582#endif
c801d85f 583}
ce4169a4 584
cc985fac
PA
585#endif
586