]>
git.saurik.com Git - wxWidgets.git/blob - src/common/file.cpp
45ab8ade8cff914a09e1f0602eaa2f6bc3ec17a5
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 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
27 #if defined(__WXMSW__) && !defined(__GNUWIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
30 #define WIN32_LEAN_AND_MEAN
52 #elif defined(__WXMSW__) && defined(__WXWINCE__)
53 #include "wx/msw/missing.h"
54 #elif (defined(__OS2__))
56 #elif (defined(__UNIX__) || defined(__GNUWIN32__))
61 #include "wx/msw/wrapwin.h"
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(__WXSTUBS__))
74 // Have to ifdef this for different environments
76 #elif (defined(__WXMAC__))
78 int access( const char *path
, int mode
) { return 0 ; }
80 int _access( const char *path
, int mode
) { return 0 ; }
82 char* mktemp( char * path
) { return path
;}
86 #error "Please specify the header with file functions declarations."
89 #include <stdio.h> // SEEK_xxx constants
91 // Windows compilers don't have these constants
95 F_OK
= 0, // test for existence
96 X_OK
= 1, // execute permission
106 // some broken compilers don't have 3rd argument in open() and creat()
108 #define ACCESS(access)
110 #else // normal compiler
111 #define ACCESS(access) , (access)
116 #include "wx/string.h"
119 #endif // !WX_PRECOMP
121 #include "wx/filename.h"
123 #include "wx/filefn.h"
125 // there is no distinction between text and binary files under Unix, so define
126 // O_BINARY as 0 if the system headers don't do it already
127 #if defined(__UNIX__) && !defined(O_BINARY)
132 #include "wx/msw/mslu.h"
136 #include "wx/msw/private.h"
143 // ============================================================================
144 // implementation of wxFile
145 // ============================================================================
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
151 bool wxFile::Exists(const wxString
& name
)
153 return wxFileExists(name
);
156 bool wxFile::Access(const wxString
& name
, OpenMode mode
)
163 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
179 return wxAccess(name
, how
) == 0;
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
187 wxFile::wxFile(const wxString
& fileName
, OpenMode mode
)
192 Open(fileName
, mode
);
195 // create the file, fail if it already exists and bOverwrite
196 bool wxFile::Create(const wxString
& fileName
, bool bOverwrite
, int accessMode
)
198 // if bOverwrite we create a new file or truncate the existing one,
199 // otherwise we only create the new file and fail if it already exists
200 #if defined(__WXMAC__) && !defined(__UNIX__) && !wxUSE_UNICODE
201 // Dominic Mazzoni [dmazzoni+@cs.cmu.edu] reports that open is still broken on the mac, so we replace
202 // int fd = open( fileName , O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
203 int fd
= creat( fileName
, accessMode
);
205 int fd
= wxOpen( fileName
,
206 O_BINARY
| O_WRONLY
| O_CREAT
|
207 (bOverwrite
? O_TRUNC
: O_EXCL
)
208 ACCESS(accessMode
) );
212 wxLogSysError(_("can't create file '%s'"), fileName
);
221 bool wxFile::Open(const wxString
& fileName
, OpenMode mode
, int accessMode
)
223 int flags
= O_BINARY
;
232 if ( wxFile::Exists(fileName
) )
234 flags
|= O_WRONLY
| O_APPEND
;
237 //else: fall through as write_append is the same as write if the
238 // file doesn't exist
241 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
245 flags
|= O_WRONLY
| O_CREAT
| O_EXCL
;
254 // only read/write bits for "all" are supported by this function under
255 // Windows, and VC++ 8 returns EINVAL if any other bits are used in
256 // accessMode, so clear them as they have at best no effect anyhow
257 accessMode
&= wxS_IRUSR
| wxS_IWUSR
;
258 #endif // __WINDOWS__
260 int fd
= wxOpen( fileName
, flags
ACCESS(accessMode
));
264 wxLogSysError(_("can't open file '%s'"), fileName
);
276 if (wxClose(m_fd
) == -1)
278 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
289 // ----------------------------------------------------------------------------
291 // ----------------------------------------------------------------------------
294 ssize_t
wxFile::Read(void *pBuf
, size_t nCount
)
296 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
298 ssize_t iRc
= wxRead(m_fd
, pBuf
, nCount
);
302 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
303 return wxInvalidOffset
;
310 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
312 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
314 ssize_t iRc
= wxWrite(m_fd
, pBuf
, nCount
);
318 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
326 bool wxFile::Write(const wxString
& s
, const wxMBConv
& conv
)
328 const wxWX2MBbuf buf
= s
.mb_str(conv
);
332 const size_t size
= strlen(buf
); // FIXME: use buf.length() when available
333 return Write(buf
, size
) == size
;
340 // fsync() only works on disk files and returns errors for pipes, don't
342 if ( IsOpened() && GetKind() == wxFILE_KIND_DISK
)
344 if ( wxFsync(m_fd
) == -1 )
346 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
355 // ----------------------------------------------------------------------------
357 // ----------------------------------------------------------------------------
360 wxFileOffset
wxFile::Seek(wxFileOffset ofs
, wxSeekMode mode
)
362 wxASSERT_MSG( IsOpened(), _T("can't seek on closed file") );
363 wxCHECK_MSG( ofs
!= wxInvalidOffset
|| mode
!= wxFromStart
,
365 _T("invalid absolute file offset") );
370 wxFAIL_MSG(_T("unknown seek origin"));
385 wxFileOffset iRc
= wxSeek(m_fd
, ofs
, origin
);
386 if ( iRc
== wxInvalidOffset
)
388 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
394 // get current file offset
395 wxFileOffset
wxFile::Tell() const
397 wxASSERT( IsOpened() );
399 wxFileOffset iRc
= wxTell(m_fd
);
400 if ( iRc
== wxInvalidOffset
)
402 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
408 // get current file length
409 wxFileOffset
wxFile::Length() const
411 wxASSERT( IsOpened() );
413 wxFileOffset iRc
= Tell();
414 if ( iRc
!= wxInvalidOffset
) {
415 // have to use const_cast :-(
416 wxFileOffset iLen
= ((wxFile
*)this)->SeekEnd();
417 if ( iLen
!= wxInvalidOffset
) {
418 // restore old position
419 if ( ((wxFile
*)this)->Seek(iRc
) == wxInvalidOffset
) {
421 iLen
= wxInvalidOffset
;
428 if ( iRc
== wxInvalidOffset
)
430 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
436 // is end of file reached?
437 bool wxFile::Eof() const
439 wxASSERT( IsOpened() );
443 #if defined(__DOS__) || defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
444 // @@ this doesn't work, of course, on unseekable file descriptors
445 wxFileOffset ofsCur
= Tell(),
447 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
448 iRc
= wxInvalidOffset
;
450 iRc
= ofsCur
== ofsMax
;
451 #else // Windows and "native" compiler
453 #endif // Windows/Unix
459 else if ( iRc
== wxInvalidOffset
)
460 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd
);
462 wxFAIL_MSG(_T("invalid eof() return value."));
467 // ============================================================================
468 // implementation of wxTempFile
469 // ============================================================================
471 // ----------------------------------------------------------------------------
473 // ----------------------------------------------------------------------------
475 wxTempFile::wxTempFile(const wxString
& strName
)
480 bool wxTempFile::Open(const wxString
& strName
)
482 // we must have an absolute filename because otherwise CreateTempFileName()
483 // would create the temp file in $TMP (i.e. the system standard location
484 // for the temp files) which might be on another volume/drive/mount and
485 // wxRename()ing it later to m_strName from Commit() would then fail
487 // with the absolute filename, the temp file is created in the same
488 // directory as this one which ensures that wxRename() may work later
489 wxFileName
fn(strName
);
490 if ( !fn
.IsAbsolute() )
492 fn
.Normalize(wxPATH_NORM_ABSOLUTE
);
495 m_strName
= fn
.GetFullPath();
497 m_strTemp
= wxFileName::CreateTempFileName(m_strName
, &m_file
);
499 if ( m_strTemp
.empty() )
501 // CreateTempFileName() failed
506 // the temp file should have the same permissions as the original one
510 if ( stat( (const char*) m_strName
.fn_str(), &st
) == 0 )
516 // file probably didn't exist, just give it the default mode _using_
517 // user's umask (new files creation should respect umask)
518 mode_t mask
= umask(0777);
523 if ( chmod( (const char*) m_strTemp
.fn_str(), mode
) == -1 )
526 wxLogSysError(_("Failed to set temporary file permissions"));
534 // ----------------------------------------------------------------------------
536 // ----------------------------------------------------------------------------
538 wxTempFile::~wxTempFile()
544 bool wxTempFile::Commit()
548 if ( wxFile::Exists(m_strName
) && wxRemove(m_strName
) != 0 ) {
549 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
553 if ( !wxRenameFile(m_strTemp
, m_strName
) ) {
554 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
561 void wxTempFile::Discard()
564 if ( wxRemove(m_strTemp
) != 0 )
565 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());