]>
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__)
34 #define WIN32_LEAN_AND_MEAN
56 #elif defined(__WXMSW__) && defined(__WXWINCE__)
57 #include "wx/msw/missing.h"
58 #elif (defined(__OS2__))
60 #elif (defined(__UNIX__) || defined(__GNUWIN32__))
65 #include "wx/msw/wrapwin.h"
67 #elif defined(__DOS__)
68 #if defined(__WATCOMC__)
70 #elif defined(__DJGPP__)
75 #error "Please specify the header with file functions declarations."
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
95 // Windows compilers don't have these constants
99 F_OK
= 0, // test for existence
100 X_OK
= 1, // execute permission
106 // there is no distinction between text and binary files under Unix, so define
107 // O_BINARY as 0 if the system headers don't do it already
108 #if defined(__UNIX__) && !defined(O_BINARY)
120 // some broken compilers don't have 3rd argument in open() and creat()
122 #define ACCESS(access)
124 #else // normal compiler
125 #define ACCESS(access) , (access)
130 #include "wx/string.h"
133 #endif // !WX_PRECOMP
135 #include "wx/filename.h"
137 #include "wx/filefn.h"
140 #include "wx/msw/mslu.h"
144 #include "wx/msw/private.h"
148 // ============================================================================
149 // implementation of wxFile
150 // ============================================================================
152 // ----------------------------------------------------------------------------
154 // ----------------------------------------------------------------------------
156 bool wxFile::Exists(const wxChar
*name
)
158 return wxFileExists(name
);
161 bool wxFile::Access(const wxChar
*name
, OpenMode mode
)
168 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
184 return wxAccess(name
, how
) == 0;
187 // ----------------------------------------------------------------------------
189 // ----------------------------------------------------------------------------
192 wxFile::wxFile(const wxChar
*szFileName
, OpenMode mode
)
197 Open(szFileName
, mode
);
200 // create the file, fail if it already exists and bOverwrite
201 bool wxFile::Create(const wxChar
*szFileName
, bool bOverwrite
, int accessMode
)
203 // if bOverwrite we create a new file or truncate the existing one,
204 // otherwise we only create the new file and fail if it already exists
205 #if defined(__WXMAC__) && !defined(__UNIX__) && !wxUSE_UNICODE
206 // Dominic Mazzoni [dmazzoni+@cs.cmu.edu] reports that open is still broken on the mac, so we replace
207 // int fd = open( szFileName , O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
208 int fd
= creat( szFileName
, accessMode
);
210 int fd
= wxOpen( szFileName
,
211 O_BINARY
| O_WRONLY
| O_CREAT
|
212 (bOverwrite
? O_TRUNC
: O_EXCL
)
213 ACCESS(accessMode
) );
217 wxLogSysError(_("can't create file '%s'"), szFileName
);
228 bool wxFile::Open(const wxChar
*szFileName
, OpenMode mode
, int accessMode
)
230 int flags
= O_BINARY
;
239 if ( wxFile::Exists(szFileName
) )
241 flags
|= O_WRONLY
| O_APPEND
;
244 //else: fall through as write_append is the same as write if the
245 // file doesn't exist
248 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
252 flags
|= O_WRONLY
| O_CREAT
| O_EXCL
;
261 // only read/write bits for "all" are supported by this function under
262 // Windows, and VC++ 8 returns EINVAL if any other bits are used in
263 // accessMode, so clear them as they have at best no effect anyhow
264 accessMode
&= wxS_IRUSR
| wxS_IWUSR
;
265 #endif // __WINDOWS__
267 int fd
= wxOpen( szFileName
, flags
ACCESS(accessMode
));
271 wxLogSysError(_("can't open file '%s'"), szFileName
);
284 if (wxClose(m_fd
) == -1)
286 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
297 // ----------------------------------------------------------------------------
299 // ----------------------------------------------------------------------------
302 ssize_t
wxFile::Read(void *pBuf
, size_t nCount
)
304 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
306 ssize_t iRc
= wxRead(m_fd
, pBuf
, nCount
);
310 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
311 return wxInvalidOffset
;
318 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
320 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
322 ssize_t iRc
= wxWrite(m_fd
, pBuf
, nCount
);
326 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
337 #if defined(__VISUALC__) || defined(HAVE_FSYNC)
338 // fsync() only works on disk files and returns errors for pipes, don't
340 if ( IsOpened() && GetKind() == wxFILE_KIND_DISK
)
342 if ( wxFsync(m_fd
) == -1 )
344 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
353 // ----------------------------------------------------------------------------
355 // ----------------------------------------------------------------------------
358 wxFileOffset
wxFile::Seek(wxFileOffset ofs
, wxSeekMode mode
)
360 wxASSERT_MSG( IsOpened(), _T("can't seek on closed file") );
361 wxCHECK_MSG( ofs
!= wxInvalidOffset
|| mode
!= wxFromStart
,
363 _T("invalid absolute file offset") );
368 wxFAIL_MSG(_("unknown seek origin"));
383 wxFileOffset iRc
= wxSeek(m_fd
, ofs
, origin
);
384 if ( iRc
== wxInvalidOffset
)
386 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
392 // get current file offset
393 wxFileOffset
wxFile::Tell() const
395 wxASSERT( IsOpened() );
397 wxFileOffset iRc
= wxTell(m_fd
);
398 if ( iRc
== wxInvalidOffset
)
400 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
406 // get current file length
407 wxFileOffset
wxFile::Length() const
409 wxASSERT( IsOpened() );
411 wxFileOffset iRc
= Tell();
412 if ( iRc
!= wxInvalidOffset
) {
413 // have to use const_cast :-(
414 wxFileOffset iLen
= ((wxFile
*)this)->SeekEnd();
415 if ( iLen
!= wxInvalidOffset
) {
416 // restore old position
417 if ( ((wxFile
*)this)->Seek(iRc
) == wxInvalidOffset
) {
419 iLen
= wxInvalidOffset
;
426 if ( iRc
== wxInvalidOffset
)
428 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
434 // is end of file reached?
435 bool wxFile::Eof() const
437 wxASSERT( IsOpened() );
441 #if defined(__DOS__) || defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
442 // @@ this doesn't work, of course, on unseekable file descriptors
443 wxFileOffset ofsCur
= Tell(),
445 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
446 iRc
= wxInvalidOffset
;
448 iRc
= ofsCur
== ofsMax
;
449 #else // Windows and "native" compiler
451 #endif // Windows/Unix
457 else if ( iRc
== wxInvalidOffset
)
458 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd
);
460 wxFAIL_MSG(_("invalid eof() return value."));
465 // ============================================================================
466 // implementation of wxTempFile
467 // ============================================================================
469 // ----------------------------------------------------------------------------
471 // ----------------------------------------------------------------------------
473 wxTempFile::wxTempFile(const wxString
& strName
)
478 bool wxTempFile::Open(const wxString
& strName
)
480 // we must have an absolute filename because otherwise CreateTempFileName()
481 // would create the temp file in $TMP (i.e. the system standard location
482 // for the temp files) which might be on another volume/drive/mount and
483 // wxRename()ing it later to m_strName from Commit() would then fail
485 // with the absolute filename, the temp file is created in the same
486 // directory as this one which ensures that wxRename() may work later
487 wxFileName
fn(strName
);
488 if ( !fn
.IsAbsolute() )
490 fn
.Normalize(wxPATH_NORM_ABSOLUTE
);
493 m_strName
= fn
.GetFullPath();
495 m_strTemp
= wxFileName::CreateTempFileName(m_strName
, &m_file
);
497 if ( m_strTemp
.empty() )
499 // CreateTempFileName() failed
504 // the temp file should have the same permissions as the original one
508 if ( stat( (const char*) m_strName
.fn_str(), &st
) == 0 )
514 // file probably didn't exist, just give it the default mode _using_
515 // user's umask (new files creation should respect umask)
516 mode_t mask
= umask(0777);
521 if ( chmod( (const char*) m_strTemp
.fn_str(), mode
) == -1 )
524 wxLogSysError(_("Failed to set temporary file permissions"));
532 // ----------------------------------------------------------------------------
534 // ----------------------------------------------------------------------------
536 wxTempFile::~wxTempFile()
542 bool wxTempFile::Commit()
546 if ( wxFile::Exists(m_strName
) && wxRemove(m_strName
) != 0 ) {
547 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
551 if ( !wxRenameFile(m_strTemp
, m_strName
) ) {
552 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
559 void wxTempFile::Discard()
562 if ( wxRemove(m_strTemp
) != 0 )
563 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());