]>
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 // ----------------------------------------------------------------------------
17 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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__))
64 #include "wx/msw/wrapwin.h"
66 #elif defined(__DOS__)
67 #if defined(__WATCOMC__)
69 #elif defined(__DJGPP__)
74 #error "Please specify the header with file functions declarations."
76 #elif (defined(__WXPM__))
78 #elif (defined(__WXSTUBS__))
79 // Have to ifdef this for different environments
81 #elif (defined(__WXMAC__))
83 int access( const char *path
, int mode
) { return 0 ; }
85 int _access( const char *path
, int mode
) { return 0 ; }
87 char* mktemp( char * path
) { return path
;}
91 #error "Please specify the header with file functions declarations."
94 #include <stdio.h> // SEEK_xxx constants
97 #include <fcntl.h> // O_RDONLY &c
102 #elif !defined(__MWERKS__)
103 #include <sys/types.h> // needed for stat
104 #include <sys/stat.h> // stat
105 #elif defined(__MWERKS__) && ( defined(__WXMSW__) || defined(__MACH__) )
106 #include <sys/types.h> // needed for stat
107 #include <sys/stat.h> // stat
110 // Windows compilers don't have these constants
114 F_OK
= 0, // test for existence
115 X_OK
= 1, // execute permission
121 // there is no distinction between text and binary files under Unix, so define
122 // O_BINARY as 0 if the system headers don't do it already
123 #if defined(__UNIX__) && !defined(O_BINARY)
135 // some broken compilers don't have 3rd argument in open() and creat()
137 #define ACCESS(access)
139 #else // normal compiler
140 #define ACCESS(access) , (access)
145 #include "wx/string.h"
148 #endif // !WX_PRECOMP
150 #include "wx/filename.h"
152 #include "wx/filefn.h"
155 #include "wx/msw/mslu.h"
159 #include "wx/msw/private.h"
162 // ============================================================================
163 // implementation of wxFile
164 // ============================================================================
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
170 bool wxFile::Exists(const wxChar
*name
)
172 return wxFileExists(name
);
175 bool wxFile::Access(const wxChar
*name
, OpenMode mode
)
182 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
199 // FIXME: use CreateFile with 0 access to query the file
202 return wxAccess(name
, how
) == 0;
206 // ----------------------------------------------------------------------------
208 // ----------------------------------------------------------------------------
211 wxFile::wxFile(const wxChar
*szFileName
, OpenMode mode
)
216 Open(szFileName
, mode
);
219 // create the file, fail if it already exists and bOverwrite
220 bool wxFile::Create(const wxChar
*szFileName
, bool bOverwrite
, int accessMode
)
222 // if bOverwrite we create a new file or truncate the existing one,
223 // otherwise we only create the new file and fail if it already exists
224 #if defined(__WXMAC__) && !defined(__UNIX__) && !wxUSE_UNICODE
225 // Dominic Mazzoni [dmazzoni+@cs.cmu.edu] reports that open is still broken on the mac, so we replace
226 // int fd = open(wxUnix2MacFilename( szFileName ), O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
227 int fd
= creat( szFileName
, accessMode
);
230 HANDLE fileHandle
= ::CreateFile(szFileName
, GENERIC_WRITE
, 0, NULL
,
231 bOverwrite
? CREATE_ALWAYS
: CREATE_NEW
, FILE_ATTRIBUTE_NORMAL
,
234 if (fileHandle
== INVALID_HANDLE_VALUE
)
235 fd
= (int) fileHandle
;
239 int fd
= wxOpen( szFileName
,
240 O_BINARY
| O_WRONLY
| O_CREAT
|
241 (bOverwrite
? O_TRUNC
: O_EXCL
)
242 ACCESS(accessMode
) );
247 wxLogSysError(_("can't create file '%s'"), szFileName
);
258 bool wxFile::Open(const wxChar
*szFileName
, OpenMode mode
, int accessMode
)
263 DWORD disposition
= 0;
265 int flags
= O_BINARY
;
270 access
= GENERIC_READ
;
271 shareMode
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
272 disposition
= OPEN_EXISTING
;
276 if ( wxFile::Exists(szFileName
) )
278 access
= GENERIC_READ
|GENERIC_WRITE
;
279 shareMode
= FILE_SHARE_READ
;
283 //else: fall through as write_append is the same as write if the
284 // file doesn't exist
287 access
= GENERIC_WRITE
;
289 disposition
= TRUNCATE_EXISTING
;
293 access
= GENERIC_WRITE
;
295 disposition
= TRUNCATE_EXISTING
;
299 access
= GENERIC_READ
|GENERIC_WRITE
;
306 HANDLE fileHandle
= ::CreateFile(szFileName
, access
, shareMode
, NULL
,
307 disposition
, FILE_ATTRIBUTE_NORMAL
, 0);
308 if (fileHandle
== INVALID_HANDLE_VALUE
)
311 fd
= (int) fileHandle
;
313 int flags
= O_BINARY
;
322 if ( wxFile::Exists(szFileName
) )
324 flags
|= O_WRONLY
| O_APPEND
;
327 //else: fall through as write_append is the same as write if the
328 // file doesn't exist
331 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
335 flags
|= O_WRONLY
| O_CREAT
| O_EXCL
;
343 int fd
= wxOpen( szFileName
, flags
ACCESS(accessMode
));
347 wxLogSysError(_("can't open file '%s'"), szFileName
);
361 if (!CloseHandle((HANDLE
) m_fd
))
363 if ( close(m_fd
) == -1 )
366 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
377 // ----------------------------------------------------------------------------
379 // ----------------------------------------------------------------------------
382 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
384 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
389 if (ReadFile((HANDLE
) m_fd
, pBuf
, (DWORD
) nCount
, & bytesRead
, NULL
))
393 #elif defined(__MWERKS__)
394 int iRc
= ::read(m_fd
, (char*) pBuf
, nCount
);
396 int iRc
= ::read(m_fd
, pBuf
, nCount
);
399 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
400 return wxInvalidOffset
;
407 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
409 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
414 if (WriteFile((HANDLE
) m_fd
, pBuf
, (DWORD
) nCount
, & bytesRead
, NULL
))
418 #elif defined(__MWERKS__)
419 #if __MSL__ >= 0x6000
420 int iRc
= ::write(m_fd
, (void*) pBuf
, nCount
);
422 int iRc
= ::write(m_fd
, (const char*) pBuf
, nCount
);
425 int iRc
= ::write(m_fd
, pBuf
, nCount
);
428 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
442 #elif defined(__VISUALC__) || wxHAVE_FSYNC
443 if ( wxFsync(m_fd
) == -1 )
445 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
456 // ----------------------------------------------------------------------------
458 // ----------------------------------------------------------------------------
461 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
463 wxASSERT( IsOpened() );
469 wxFAIL_MSG(_("unknown seek origin"));
476 origin
= FILE_CURRENT
;
484 DWORD res
= SetFilePointer((HANDLE
) m_fd
, ofs
, 0, origin
) ;
485 if (res
== 0xFFFFFFFF && GetLastError() != NO_ERROR
)
487 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
488 return wxInvalidOffset
;
496 wxFAIL_MSG(_("unknown seek origin"));
511 int iRc
= lseek(m_fd
, ofs
, origin
);
513 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
514 return wxInvalidOffset
;
522 off_t
wxFile::Tell() const
524 wxASSERT( IsOpened() );
527 DWORD res
= SetFilePointer((HANDLE
) m_fd
, 0, 0, FILE_CURRENT
) ;
528 if (res
== 0xFFFFFFFF && GetLastError() != NO_ERROR
)
530 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
531 return wxInvalidOffset
;
536 int iRc
= wxTell(m_fd
);
538 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
539 return wxInvalidOffset
;
546 // get current file length
547 off_t
wxFile::Length() const
549 wxASSERT( IsOpened() );
552 DWORD off0
= SetFilePointer((HANDLE
) m_fd
, 0, 0, FILE_CURRENT
);
553 DWORD off1
= SetFilePointer((HANDLE
) m_fd
, 0, 0, FILE_END
);
557 SetFilePointer((HANDLE
) m_fd
, off0
, 0, FILE_BEGIN
);
561 int iRc
= _filelength(m_fd
);
563 int iRc
= wxTell(m_fd
);
565 // @ have to use const_cast :-(
566 int iLen
= ((wxFile
*)this)->SeekEnd();
568 // restore old position
569 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
580 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
581 return wxInvalidOffset
;
588 // is end of file reached?
589 bool wxFile::Eof() const
591 wxASSERT( IsOpened() );
594 DWORD off0
= SetFilePointer((HANDLE
) m_fd
, 0, 0, FILE_CURRENT
);
595 DWORD off1
= SetFilePointer((HANDLE
) m_fd
, 0, 0, FILE_END
);
600 SetFilePointer((HANDLE
) m_fd
, off0
, 0, FILE_BEGIN
);
606 #if defined(__DOS__) || defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
607 // @@ this doesn't work, of course, on unseekable file descriptors
608 off_t ofsCur
= Tell(),
610 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
613 iRc
= ofsCur
== ofsMax
;
614 #else // Windows and "native" compiler
616 #endif // Windows/Unix
626 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd
);
630 wxFAIL_MSG(_("invalid eof() return value."));
637 // ============================================================================
638 // implementation of wxTempFile
639 // ============================================================================
641 // ----------------------------------------------------------------------------
643 // ----------------------------------------------------------------------------
645 wxTempFile::wxTempFile(const wxString
& strName
)
650 bool wxTempFile::Open(const wxString
& strName
)
652 // we must have an absolute filename because otherwise CreateTempFileName()
653 // would create the temp file in $TMP (i.e. the system standard location
654 // for the temp files) which might be on another volume/drive/mount and
655 // wxRename()ing it later to m_strName from Commit() would then fail
657 // with the absolute filename, the temp file is created in the same
658 // directory as this one which ensures that wxRename() may work later
659 wxFileName
fn(strName
);
660 if ( !fn
.IsAbsolute() )
662 fn
.Normalize(wxPATH_NORM_ABSOLUTE
);
665 m_strName
= fn
.GetFullPath();
667 m_strTemp
= wxFileName::CreateTempFileName(m_strName
, &m_file
);
669 if ( m_strTemp
.empty() )
671 // CreateTempFileName() failed
676 // the temp file should have the same permissions as the original one
680 if ( stat( (const char*) m_strName
.fn_str(), &st
) == 0 )
686 // file probably didn't exist, just give it the default mode _using_
687 // user's umask (new files creation should respect umask)
688 mode_t mask
= umask(0777);
693 if ( chmod( (const char*) m_strTemp
.fn_str(), mode
) == -1 )
695 wxLogSysError(_("Failed to set temporary file permissions"));
702 // ----------------------------------------------------------------------------
704 // ----------------------------------------------------------------------------
706 wxTempFile::~wxTempFile()
712 bool wxTempFile::Commit()
716 if ( wxFile::Exists(m_strName
) && wxRemove(m_strName
) != 0 ) {
717 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
721 if ( !wxRenameFile(m_strTemp
, m_strName
) ) {
722 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
729 void wxTempFile::Discard()
732 if ( wxRemove(m_strTemp
) != 0 )
733 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());