]>
git.saurik.com Git - wxWidgets.git/blob - src/common/file.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFile - encapsulates low-level "file descriptor"
5 // Author: Vadim Zeitlin
9 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
18 #pragma implementation "file.h"
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
31 #if defined(__WXMSW__) && !defined(__GNUWIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
35 #define WIN32_LEAN_AND_MEAN
57 #elif defined(__WXMSW__) && defined(__WXWINCE__)
58 // TODO: what to include?
59 #elif (defined(__UNIX__) || defined(__GNUWIN32__))
65 #elif defined(__DOS__)
66 #if defined(__WATCOMC__)
68 #elif defined(__DJGPP__)
73 #error "Please specify the header with file functions declarations."
75 #elif (defined(__WXPM__))
77 #elif (defined(__WXSTUBS__))
78 // Have to ifdef this for different environments
80 #elif (defined(__WXMAC__))
82 int access( const char *path
, int mode
) { return 0 ; }
84 int _access( const char *path
, int mode
) { return 0 ; }
86 char* mktemp( char * path
) { return path
;}
90 #error "Please specify the header with file functions declarations."
93 #include <stdio.h> // SEEK_xxx constants
96 #include <fcntl.h> // O_RDONLY &c
101 #elif !defined(__MWERKS__)
102 #include <sys/types.h> // needed for stat
103 #include <sys/stat.h> // stat
104 #elif defined(__MWERKS__) && ( defined(__WXMSW__) || defined(__MACH__) )
105 #include <sys/types.h> // needed for stat
106 #include <sys/stat.h> // stat
109 // Windows compilers don't have these constants
113 F_OK
= 0, // test for existence
114 X_OK
= 1, // execute permission
120 // there is no distinction between text and binary files under Unix, so define
121 // O_BINARY as 0 if the system headers don't do it already
122 #if defined(__UNIX__) && !defined(O_BINARY)
134 // some broken compilers don't have 3rd argument in open() and creat()
136 #define ACCESS(access)
138 #else // normal compiler
139 #define ACCESS(access) , (access)
144 #include "wx/string.h"
147 #endif // !WX_PRECOMP
149 #include "wx/filename.h"
151 #include "wx/filefn.h"
154 #include "wx/msw/mslu.h"
158 #include "wx/msw/private.h"
161 // ============================================================================
162 // implementation of wxFile
163 // ============================================================================
165 // ----------------------------------------------------------------------------
167 // ----------------------------------------------------------------------------
169 bool wxFile::Exists(const wxChar
*name
)
171 return wxFileExists(name
);
174 bool wxFile::Access(const wxChar
*name
, OpenMode mode
)
181 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
198 // FIXME: use CreateFile with 0 access to query the file
201 return wxAccess(name
, how
) == 0;
205 // ----------------------------------------------------------------------------
207 // ----------------------------------------------------------------------------
210 wxFile::wxFile(const wxChar
*szFileName
, OpenMode mode
)
215 Open(szFileName
, mode
);
218 // create the file, fail if it already exists and bOverwrite
219 bool wxFile::Create(const wxChar
*szFileName
, bool bOverwrite
, int accessMode
)
221 // if bOverwrite we create a new file or truncate the existing one,
222 // otherwise we only create the new file and fail if it already exists
223 #if defined(__WXMAC__) && !defined(__UNIX__) && !wxUSE_UNICODE
224 // Dominic Mazzoni [dmazzoni+@cs.cmu.edu] reports that open is still broken on the mac, so we replace
225 // int fd = open(wxUnix2MacFilename( szFileName ), O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
226 int fd
= creat( szFileName
, accessMode
);
229 HANDLE fileHandle
= ::CreateFile(szFileName
, GENERIC_WRITE
, 0, NULL
,
230 bOverwrite
? CREATE_ALWAYS
: CREATE_NEW
, FILE_ATTRIBUTE_NORMAL
,
233 if (fileHandle
== INVALID_HANDLE_VALUE
)
234 fd
= (int) fileHandle
;
238 int fd
= wxOpen( szFileName
,
239 O_BINARY
| O_WRONLY
| O_CREAT
|
240 (bOverwrite
? O_TRUNC
: O_EXCL
)
241 ACCESS(accessMode
) );
246 wxLogSysError(_("can't create file '%s'"), szFileName
);
257 bool wxFile::Open(const wxChar
*szFileName
, OpenMode mode
, int accessMode
)
262 DWORD disposition
= 0;
264 int flags
= O_BINARY
;
269 access
= GENERIC_READ
;
270 shareMode
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
271 disposition
= OPEN_EXISTING
;
275 if ( wxFile::Exists(szFileName
) )
277 access
= GENERIC_READ
|GENERIC_WRITE
;
278 shareMode
= FILE_SHARE_READ
;
282 //else: fall through as write_append is the same as write if the
283 // file doesn't exist
286 access
= GENERIC_WRITE
;
288 disposition
= TRUNCATE_EXISTING
;
292 access
= GENERIC_WRITE
;
294 disposition
= TRUNCATE_EXISTING
;
298 access
= GENERIC_READ
|GENERIC_WRITE
;
305 HANDLE fileHandle
= ::CreateFile(szFileName
, access
, shareMode
, NULL
,
306 disposition
, FILE_ATTRIBUTE_NORMAL
, 0);
307 if (fileHandle
== INVALID_HANDLE_VALUE
)
310 fd
= (int) fileHandle
;
312 int flags
= O_BINARY
;
321 if ( wxFile::Exists(szFileName
) )
323 flags
|= O_WRONLY
| O_APPEND
;
326 //else: fall through as write_append is the same as write if the
327 // file doesn't exist
330 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
334 flags
|= O_WRONLY
| O_CREAT
| O_EXCL
;
342 int fd
= wxOpen( szFileName
, flags
ACCESS(accessMode
));
346 wxLogSysError(_("can't open file '%s'"), szFileName
);
360 if (!CloseHandle((HANDLE
) m_fd
))
362 if ( close(m_fd
) == -1 )
365 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
376 // ----------------------------------------------------------------------------
378 // ----------------------------------------------------------------------------
381 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
383 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
388 if (ReadFile((HANDLE
) m_fd
, pBuf
, (DWORD
) nCount
, & bytesRead
, NULL
))
392 #elif defined(__MWERKS__)
393 int iRc
= ::read(m_fd
, (char*) pBuf
, nCount
);
395 int iRc
= ::read(m_fd
, pBuf
, nCount
);
398 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
399 return wxInvalidOffset
;
406 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
408 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
413 if (WriteFile((HANDLE
) m_fd
, pBuf
, (DWORD
) nCount
, & bytesRead
, NULL
))
417 #elif defined(__MWERKS__)
418 #if __MSL__ >= 0x6000
419 int iRc
= ::write(m_fd
, (void*) pBuf
, nCount
);
421 int iRc
= ::write(m_fd
, (const char*) pBuf
, nCount
);
424 int iRc
= ::write(m_fd
, pBuf
, nCount
);
427 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
441 #elif defined(__VISUALC__) || wxHAVE_FSYNC
442 if ( wxFsync(m_fd
) == -1 )
444 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
455 // ----------------------------------------------------------------------------
457 // ----------------------------------------------------------------------------
460 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
462 wxASSERT( IsOpened() );
468 wxFAIL_MSG(_("unknown seek origin"));
475 origin
= FILE_CURRENT
;
483 DWORD res
= SetFilePointer((HANDLE
) m_fd
, ofs
, 0, origin
) ;
484 if (res
== 0xFFFFFFFF && GetLastError() != NO_ERROR
)
486 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
487 return wxInvalidOffset
;
495 wxFAIL_MSG(_("unknown seek origin"));
510 int iRc
= lseek(m_fd
, ofs
, origin
);
512 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
513 return wxInvalidOffset
;
521 off_t
wxFile::Tell() const
523 wxASSERT( IsOpened() );
526 DWORD res
= SetFilePointer((HANDLE
) m_fd
, 0, 0, FILE_CURRENT
) ;
527 if (res
== 0xFFFFFFFF && GetLastError() != NO_ERROR
)
529 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
530 return wxInvalidOffset
;
535 int iRc
= wxTell(m_fd
);
537 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
538 return wxInvalidOffset
;
545 // get current file length
546 off_t
wxFile::Length() const
548 wxASSERT( IsOpened() );
551 DWORD off0
= SetFilePointer((HANDLE
) m_fd
, 0, 0, FILE_CURRENT
);
552 DWORD off1
= SetFilePointer((HANDLE
) m_fd
, 0, 0, FILE_END
);
556 SetFilePointer((HANDLE
) m_fd
, off0
, 0, FILE_BEGIN
);
560 int iRc
= _filelength(m_fd
);
562 int iRc
= wxTell(m_fd
);
564 // @ have to use const_cast :-(
565 int iLen
= ((wxFile
*)this)->SeekEnd();
567 // restore old position
568 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
579 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
580 return wxInvalidOffset
;
587 // is end of file reached?
588 bool wxFile::Eof() const
590 wxASSERT( IsOpened() );
593 DWORD off0
= SetFilePointer((HANDLE
) m_fd
, 0, 0, FILE_CURRENT
);
594 DWORD off1
= SetFilePointer((HANDLE
) m_fd
, 0, 0, FILE_END
);
599 SetFilePointer((HANDLE
) m_fd
, off0
, 0, FILE_BEGIN
);
605 #if defined(__DOS__) || defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
606 // @@ this doesn't work, of course, on unseekable file descriptors
607 off_t ofsCur
= Tell(),
609 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
612 iRc
= ofsCur
== ofsMax
;
613 #else // Windows and "native" compiler
615 #endif // Windows/Unix
625 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd
);
629 wxFAIL_MSG(_("invalid eof() return value."));
636 // ============================================================================
637 // implementation of wxTempFile
638 // ============================================================================
640 // ----------------------------------------------------------------------------
642 // ----------------------------------------------------------------------------
644 wxTempFile::wxTempFile(const wxString
& strName
)
649 bool wxTempFile::Open(const wxString
& strName
)
651 // we must have an absolute filename because otherwise CreateTempFileName()
652 // would create the temp file in $TMP (i.e. the system standard location
653 // for the temp files) which might be on another volume/drive/mount and
654 // wxRename()ing it later to m_strName from Commit() would then fail
656 // with the absolute filename, the temp file is created in the same
657 // directory as this one which ensures that wxRename() may work later
658 wxFileName
fn(strName
);
659 if ( !fn
.IsAbsolute() )
661 fn
.Normalize(wxPATH_NORM_ABSOLUTE
);
664 m_strName
= fn
.GetFullPath();
666 m_strTemp
= wxFileName::CreateTempFileName(m_strName
, &m_file
);
668 if ( m_strTemp
.empty() )
670 // CreateTempFileName() failed
675 // the temp file should have the same permissions as the original one
679 if ( stat( (const char*) m_strName
.fn_str(), &st
) == 0 )
685 // file probably didn't exist, just give it the default mode _using_
686 // user's umask (new files creation should respect umask)
687 mode_t mask
= umask(0777);
692 if ( chmod( (const char*) m_strTemp
.fn_str(), mode
) == -1 )
694 wxLogSysError(_("Failed to set temporary file permissions"));
701 // ----------------------------------------------------------------------------
703 // ----------------------------------------------------------------------------
705 wxTempFile::~wxTempFile()
711 bool wxTempFile::Commit()
715 if ( wxFile::Exists(m_strName
) && wxRemove(m_strName
) != 0 ) {
716 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
720 if ( wxRenameFile(m_strTemp
, m_strName
) != 0 ) {
721 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
728 void wxTempFile::Discard()
731 if ( wxRemove(m_strTemp
) != 0 )
732 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());