]>
git.saurik.com Git - wxWidgets.git/blob - src/common/file.cpp
680f9dfb4e502567392d41ce7d6bab53ced5c3d2
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"
120 #endif // !WX_PRECOMP
122 #include "wx/filename.h"
124 #include "wx/filefn.h"
126 // there is no distinction between text and binary files under Unix, so define
127 // O_BINARY as 0 if the system headers don't do it already
128 #if defined(__UNIX__) && !defined(O_BINARY)
133 #include "wx/msw/mslu.h"
137 #include "wx/msw/private.h"
144 // ============================================================================
145 // implementation of wxFile
146 // ============================================================================
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
152 bool wxFile::Exists(const wxString
& name
)
154 return wxFileExists(name
);
157 bool wxFile::Access(const wxString
& name
, OpenMode mode
)
164 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
180 return wxAccess(name
, how
) == 0;
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
188 wxFile::wxFile(const wxString
& fileName
, OpenMode mode
)
193 Open(fileName
, mode
);
196 // create the file, fail if it already exists and bOverwrite
197 bool wxFile::Create(const wxString
& fileName
, bool bOverwrite
, int accessMode
)
199 // if bOverwrite we create a new file or truncate the existing one,
200 // otherwise we only create the new file and fail if it already exists
201 int fd
= wxOpen( fileName
,
202 O_BINARY
| O_WRONLY
| O_CREAT
|
203 (bOverwrite
? O_TRUNC
: O_EXCL
)
204 ACCESS(accessMode
) );
207 wxLogSysError(_("can't create file '%s'"), fileName
);
216 bool wxFile::Open(const wxString
& fileName
, OpenMode mode
, int accessMode
)
218 int flags
= O_BINARY
;
227 if ( wxFile::Exists(fileName
) )
229 flags
|= O_WRONLY
| O_APPEND
;
232 //else: fall through as write_append is the same as write if the
233 // file doesn't exist
236 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
240 flags
|= O_WRONLY
| O_CREAT
| O_EXCL
;
249 // only read/write bits for "all" are supported by this function under
250 // Windows, and VC++ 8 returns EINVAL if any other bits are used in
251 // accessMode, so clear them as they have at best no effect anyhow
252 accessMode
&= wxS_IRUSR
| wxS_IWUSR
;
253 #endif // __WINDOWS__
255 int fd
= wxOpen( fileName
, flags
ACCESS(accessMode
));
259 wxLogSysError(_("can't open file '%s'"), fileName
);
271 if (wxClose(m_fd
) == -1)
273 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
284 // ----------------------------------------------------------------------------
286 // ----------------------------------------------------------------------------
289 ssize_t
wxFile::Read(void *pBuf
, size_t nCount
)
291 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
293 ssize_t iRc
= wxRead(m_fd
, pBuf
, nCount
);
297 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
298 return wxInvalidOffset
;
305 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
307 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
309 ssize_t iRc
= wxWrite(m_fd
, pBuf
, nCount
);
313 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
321 bool wxFile::Write(const wxString
& s
, const wxMBConv
& conv
)
323 const wxWX2MBbuf buf
= s
.mb_str(conv
);
327 const size_t size
= strlen(buf
); // FIXME: use buf.length() when available
328 return Write(buf
, size
) == size
;
335 // fsync() only works on disk files and returns errors for pipes, don't
337 if ( IsOpened() && GetKind() == wxFILE_KIND_DISK
)
339 if ( wxFsync(m_fd
) == -1 )
341 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
350 // ----------------------------------------------------------------------------
352 // ----------------------------------------------------------------------------
355 wxFileOffset
wxFile::Seek(wxFileOffset ofs
, wxSeekMode mode
)
357 wxASSERT_MSG( IsOpened(), _T("can't seek on closed file") );
358 wxCHECK_MSG( ofs
!= wxInvalidOffset
|| mode
!= wxFromStart
,
360 _T("invalid absolute file offset") );
365 wxFAIL_MSG(_T("unknown seek origin"));
380 wxFileOffset iRc
= wxSeek(m_fd
, ofs
, origin
);
381 if ( iRc
== wxInvalidOffset
)
383 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
389 // get current file offset
390 wxFileOffset
wxFile::Tell() const
392 wxASSERT( IsOpened() );
394 wxFileOffset iRc
= wxTell(m_fd
);
395 if ( iRc
== wxInvalidOffset
)
397 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
403 // get current file length
404 wxFileOffset
wxFile::Length() const
406 wxASSERT( IsOpened() );
408 wxFileOffset iRc
= Tell();
409 if ( iRc
!= wxInvalidOffset
) {
410 // have to use const_cast :-(
411 wxFileOffset iLen
= ((wxFile
*)this)->SeekEnd();
412 if ( iLen
!= wxInvalidOffset
) {
413 // restore old position
414 if ( ((wxFile
*)this)->Seek(iRc
) == wxInvalidOffset
) {
416 iLen
= wxInvalidOffset
;
423 if ( iRc
== wxInvalidOffset
)
425 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
431 // is end of file reached?
432 bool wxFile::Eof() const
434 wxASSERT( IsOpened() );
438 #if defined(__DOS__) || defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
439 // @@ this doesn't work, of course, on unseekable file descriptors
440 wxFileOffset ofsCur
= Tell(),
442 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
443 iRc
= wxInvalidOffset
;
445 iRc
= ofsCur
== ofsMax
;
446 #else // Windows and "native" compiler
448 #endif // Windows/Unix
454 else if ( iRc
== wxInvalidOffset
)
455 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd
);
457 wxFAIL_MSG(_T("invalid eof() return value."));
462 // ============================================================================
463 // implementation of wxTempFile
464 // ============================================================================
466 // ----------------------------------------------------------------------------
468 // ----------------------------------------------------------------------------
470 wxTempFile::wxTempFile(const wxString
& strName
)
475 bool wxTempFile::Open(const wxString
& strName
)
477 // we must have an absolute filename because otherwise CreateTempFileName()
478 // would create the temp file in $TMP (i.e. the system standard location
479 // for the temp files) which might be on another volume/drive/mount and
480 // wxRename()ing it later to m_strName from Commit() would then fail
482 // with the absolute filename, the temp file is created in the same
483 // directory as this one which ensures that wxRename() may work later
484 wxFileName
fn(strName
);
485 if ( !fn
.IsAbsolute() )
487 fn
.Normalize(wxPATH_NORM_ABSOLUTE
);
490 m_strName
= fn
.GetFullPath();
492 m_strTemp
= wxFileName::CreateTempFileName(m_strName
, &m_file
);
494 if ( m_strTemp
.empty() )
496 // CreateTempFileName() failed
501 // the temp file should have the same permissions as the original one
505 if ( stat( (const char*) m_strName
.fn_str(), &st
) == 0 )
511 // file probably didn't exist, just give it the default mode _using_
512 // user's umask (new files creation should respect umask)
513 mode_t mask
= umask(0777);
518 if ( chmod( (const char*) m_strTemp
.fn_str(), mode
) == -1 )
521 wxLogSysError(_("Failed to set temporary file permissions"));
529 // ----------------------------------------------------------------------------
531 // ----------------------------------------------------------------------------
533 wxTempFile::~wxTempFile()
539 bool wxTempFile::Commit()
543 if ( wxFile::Exists(m_strName
) && wxRemove(m_strName
) != 0 ) {
544 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
548 if ( !wxRenameFile(m_strTemp
, m_strName
) ) {
549 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
556 void wxTempFile::Discard()
559 if ( wxRemove(m_strTemp
) != 0 )
560 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());