]>
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__)
35 #define WIN32_LEAN_AND_MEAN
57 #elif (defined(__UNIX__) || defined(__GNUWIN32__))
63 #elif defined(__DOS__)
64 #if defined(__WATCOMC__)
66 #elif defined(__DJGPP__)
71 #error "Please specify the header with file functions declarations."
73 #elif (defined(__WXPM__))
75 #elif (defined(__WXSTUBS__))
76 // Have to ifdef this for different environments
78 #elif (defined(__WXMAC__))
80 int access( const char *path
, int mode
) { return 0 ; }
82 int _access( const char *path
, int mode
) { return 0 ; }
84 char* mktemp( char * path
) { return path
;}
88 #error "Please specify the header with file functions declarations."
91 #include <stdio.h> // SEEK_xxx constants
92 #include <fcntl.h> // O_RDONLY &c
95 #include <sys/types.h> // needed for stat
96 #include <sys/stat.h> // stat
97 #elif defined(__MWERKS__) && ( defined(__WXMSW__) || defined(__MACH__) )
98 #include <sys/types.h> // needed for stat
99 #include <sys/stat.h> // stat
102 // Windows compilers don't have these constants
106 F_OK
= 0, // test for existence
107 X_OK
= 1, // execute permission
113 // there is no distinction between text and binary files under Unix, so define
114 // O_BINARY as 0 if the system headers don't do it already
115 #if defined(__UNIX__) && !defined(O_BINARY)
127 // some broken compilers don't have 3rd argument in open() and creat()
129 #define ACCESS(access)
131 #else // normal compiler
132 #define ACCESS(access) , (access)
137 #include "wx/string.h"
140 #endif // !WX_PRECOMP
142 #include "wx/filename.h"
144 #include "wx/filefn.h"
147 #include "wx/msw/mslu.h"
150 // ============================================================================
151 // implementation of wxFile
152 // ============================================================================
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
158 bool wxFile::Exists(const wxChar
*name
)
160 return wxFileExists(name
);
163 bool wxFile::Access(const wxChar
*name
, OpenMode mode
)
170 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
186 return wxAccess(name
, how
) == 0;
189 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
194 wxFile::wxFile(const wxChar
*szFileName
, OpenMode mode
)
199 Open(szFileName
, mode
);
202 // create the file, fail if it already exists and bOverwrite
203 bool wxFile::Create(const wxChar
*szFileName
, bool bOverwrite
, int accessMode
)
205 // if bOverwrite we create a new file or truncate the existing one,
206 // otherwise we only create the new file and fail if it already exists
207 #if defined(__WXMAC__) && !defined(__UNIX__) && !wxUSE_UNICODE
208 // Dominic Mazzoni [dmazzoni+@cs.cmu.edu] reports that open is still broken on the mac, so we replace
209 // int fd = open(wxUnix2MacFilename( szFileName ), O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
210 int fd
= creat( szFileName
, accessMode
);
212 int fd
= wxOpen( szFileName
,
213 O_BINARY
| O_WRONLY
| O_CREAT
|
214 (bOverwrite
? O_TRUNC
: O_EXCL
)
215 ACCESS(accessMode
) );
219 wxLogSysError(_("can't create file '%s'"), szFileName
);
230 bool wxFile::Open(const wxChar
*szFileName
, OpenMode mode
, int accessMode
)
232 int flags
= O_BINARY
;
241 if ( wxFile::Exists(szFileName
) )
243 flags
|= O_WRONLY
| O_APPEND
;
246 //else: fall through as write_append is the same as write if the
247 // file doesn't exist
250 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
254 flags
|= O_WRONLY
| O_CREAT
| O_EXCL
;
262 int fd
= wxOpen( szFileName
, flags
ACCESS(accessMode
));
265 wxLogSysError(_("can't open file '%s'"), szFileName
);
278 if ( close(m_fd
) == -1 ) {
279 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
290 // ----------------------------------------------------------------------------
292 // ----------------------------------------------------------------------------
295 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
297 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
300 int iRc
= ::read(m_fd
, (char*) pBuf
, nCount
);
302 int iRc
= ::read(m_fd
, pBuf
, nCount
);
305 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
306 return wxInvalidOffset
;
313 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
315 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
318 #if __MSL__ >= 0x6000
319 int iRc
= ::write(m_fd
, (void*) pBuf
, nCount
);
321 int iRc
= ::write(m_fd
, (const char*) pBuf
, nCount
);
324 int iRc
= ::write(m_fd
, pBuf
, nCount
);
327 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
339 #if defined(__VISUALC__) || wxHAVE_FSYNC
340 if ( wxFsync(m_fd
) == -1 )
342 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
353 // ----------------------------------------------------------------------------
355 // ----------------------------------------------------------------------------
358 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
360 wxASSERT( IsOpened() );
365 wxFAIL_MSG(_("unknown seek origin"));
380 int iRc
= lseek(m_fd
, ofs
, origin
);
382 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
383 return wxInvalidOffset
;
390 off_t
wxFile::Tell() const
392 wxASSERT( IsOpened() );
394 int iRc
= wxTell(m_fd
);
396 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
397 return wxInvalidOffset
;
403 // get current file length
404 off_t
wxFile::Length() const
406 wxASSERT( IsOpened() );
409 int iRc
= _filelength(m_fd
);
411 int iRc
= wxTell(m_fd
);
413 // @ have to use const_cast :-(
414 int iLen
= ((wxFile
*)this)->SeekEnd();
416 // restore old position
417 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
428 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
429 return wxInvalidOffset
;
435 // is end of file reached?
436 bool wxFile::Eof() const
438 wxASSERT( IsOpened() );
442 #if defined(__DOS__) || defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
443 // @@ this doesn't work, of course, on unseekable file descriptors
444 off_t ofsCur
= Tell(),
446 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
449 iRc
= ofsCur
== ofsMax
;
450 #else // Windows and "native" compiler
452 #endif // Windows/Unix
462 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd
);
466 wxFAIL_MSG(_("invalid eof() return value."));
472 // ============================================================================
473 // implementation of wxTempFile
474 // ============================================================================
476 // ----------------------------------------------------------------------------
478 // ----------------------------------------------------------------------------
480 wxTempFile::wxTempFile(const wxString
& strName
)
485 bool wxTempFile::Open(const wxString
& strName
)
487 // we must have an absolute filename because otherwise CreateTempFileName()
488 // would create the temp file in $TMP (i.e. the system standard location
489 // for the temp files) which might be on another volume/drive/mount and
490 // wxRename()ing it later to m_strName from Commit() would then fail
492 // with the absolute filename, the temp file is created in the same
493 // directory as this one which ensures that wxRename() may work later
494 wxFileName
fn(strName
);
495 if ( !fn
.IsAbsolute() )
497 fn
.Normalize(wxPATH_NORM_ABSOLUTE
);
500 m_strName
= fn
.GetFullPath();
502 m_strTemp
= wxFileName::CreateTempFileName(m_strName
, &m_file
);
504 if ( m_strTemp
.empty() )
506 // CreateTempFileName() failed
511 // the temp file should have the same permissions as the original one
515 if ( stat( (const char*) m_strName
.fn_str(), &st
) == 0 )
521 // file probably didn't exist, just give it the default mode _using_
522 // user's umask (new files creation should respect umask)
523 mode_t mask
= umask(0777);
528 if ( chmod( (const char*) m_strTemp
.fn_str(), mode
) == -1 )
530 wxLogSysError(_("Failed to set temporary file permissions"));
537 // ----------------------------------------------------------------------------
539 // ----------------------------------------------------------------------------
541 wxTempFile::~wxTempFile()
547 bool wxTempFile::Commit()
551 if ( wxFile::Exists(m_strName
) && wxRemove(m_strName
) != 0 ) {
552 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
556 if ( wxRename(m_strTemp
, m_strName
) != 0 ) {
557 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
564 void wxTempFile::Discard()
567 if ( wxRemove(m_strTemp
) != 0 )
568 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());