]>
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 license
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__))
62 #elif defined(__DOS__)
63 #if defined(__WATCOMC__)
65 #elif defined(__DJGPP__)
70 #error "Please specify the header with file functions declarations."
72 #elif (defined(__WXPM__))
74 #elif (defined(__WXSTUBS__))
75 // Have to ifdef this for different environments
77 #elif (defined(__WXMAC__))
79 int access( const char *path
, int mode
) { return 0 ; }
81 int _access( const char *path
, int mode
) { return 0 ; }
83 char* mktemp( char * path
) { return path
;}
87 #error "Please specify the header with file functions declarations."
90 #include <stdio.h> // SEEK_xxx constants
91 #include <fcntl.h> // O_RDONLY &c
93 #if !defined(__MWERKS__) || defined(__WXMSW__)
94 #include <sys/types.h> // needed for stat
95 #include <sys/stat.h> // stat
98 // Windows compilers don't have these constants
102 F_OK
= 0, // test for existence
103 X_OK
= 1, // execute permission
109 // there is no distinction between text and binary files under Unix, so define
110 // O_BINARY as 0 if the system headers don't do it already
111 #if defined(__UNIX__) && !defined(O_BINARY)
123 // some broken compilers don't have 3rd argument in open() and creat()
125 #define ACCESS(access)
127 #else // normal compiler
128 #define ACCESS(access) , (access)
133 #include "wx/string.h"
136 #endif // !WX_PRECOMP
138 #include "wx/filename.h"
140 #include "wx/filefn.h"
143 #include "wx/msw/mslu.h"
146 // ============================================================================
147 // implementation of wxFile
148 // ============================================================================
150 // ----------------------------------------------------------------------------
152 // ----------------------------------------------------------------------------
154 bool wxFile::Exists(const wxChar
*name
)
156 return wxFileExists(name
);
159 bool wxFile::Access(const wxChar
*name
, OpenMode mode
)
166 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
182 return wxAccess(name
, how
) == 0;
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
190 wxFile::wxFile(const wxChar
*szFileName
, OpenMode mode
)
195 Open(szFileName
, mode
);
198 // create the file, fail if it already exists and bOverwrite
199 bool wxFile::Create(const wxChar
*szFileName
, bool bOverwrite
, int accessMode
)
201 // if bOverwrite we create a new file or truncate the existing one,
202 // otherwise we only create the new file and fail if it already exists
203 #if defined(__WXMAC__) && !defined(__UNIX__)
204 // Dominic Mazzoni [dmazzoni+@cs.cmu.edu] reports that open is still broken on the mac, so we replace
205 // int fd = open(wxUnix2MacFilename( szFileName ), O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
206 int fd
= creat( szFileName
, accessMode
);
208 int fd
= wxOpen( szFileName
,
209 O_BINARY
| O_WRONLY
| O_CREAT
|
210 (bOverwrite
? O_TRUNC
: O_EXCL
)
211 ACCESS(accessMode
) );
215 wxLogSysError(_("can't create file '%s'"), szFileName
);
226 bool wxFile::Open(const wxChar
*szFileName
, OpenMode mode
, int accessMode
)
228 int flags
= O_BINARY
;
237 if ( wxFile::Exists(szFileName
) )
239 flags
|= O_WRONLY
| O_APPEND
;
242 //else: fall through as write_append is the same as write if the
243 // file doesn't exist
246 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
250 flags
|= O_WRONLY
| O_CREAT
| O_EXCL
;
258 int fd
= wxOpen( szFileName
, flags
ACCESS(accessMode
));
261 wxLogSysError(_("can't open file '%s'"), szFileName
);
274 if ( close(m_fd
) == -1 ) {
275 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
286 // ----------------------------------------------------------------------------
288 // ----------------------------------------------------------------------------
291 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
293 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
296 int iRc
= ::read(m_fd
, (char*) pBuf
, nCount
);
298 int iRc
= ::read(m_fd
, pBuf
, nCount
);
301 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
302 return wxInvalidOffset
;
309 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
311 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
314 #if __MSL__ >= 0x6000
315 int iRc
= ::write(m_fd
, (void*) pBuf
, nCount
);
317 int iRc
= ::write(m_fd
, (const char*) pBuf
, nCount
);
320 int iRc
= ::write(m_fd
, pBuf
, nCount
);
323 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
335 #if defined(__VISUALC__) || wxHAVE_FSYNC
336 if ( wxFsync(m_fd
) == -1 )
338 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
349 // ----------------------------------------------------------------------------
351 // ----------------------------------------------------------------------------
354 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
356 wxASSERT( IsOpened() );
361 wxFAIL_MSG(_("unknown seek origin"));
376 int iRc
= lseek(m_fd
, ofs
, origin
);
378 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
379 return wxInvalidOffset
;
386 off_t
wxFile::Tell() const
388 wxASSERT( IsOpened() );
390 int iRc
= wxTell(m_fd
);
392 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
393 return wxInvalidOffset
;
399 // get current file length
400 off_t
wxFile::Length() const
402 wxASSERT( IsOpened() );
405 int iRc
= _filelength(m_fd
);
407 int iRc
= wxTell(m_fd
);
409 // @ have to use const_cast :-(
410 int iLen
= ((wxFile
*)this)->SeekEnd();
412 // restore old position
413 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
424 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
425 return wxInvalidOffset
;
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 off_t ofsCur
= Tell(),
442 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
445 iRc
= ofsCur
== ofsMax
;
446 #else // Windows and "native" compiler
448 #endif // Windows/Unix
458 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd
);
462 wxFAIL_MSG(_("invalid eof() return value."));
468 // ============================================================================
469 // implementation of wxTempFile
470 // ============================================================================
472 // ----------------------------------------------------------------------------
474 // ----------------------------------------------------------------------------
476 wxTempFile::wxTempFile(const wxString
& strName
)
481 bool wxTempFile::Open(const wxString
& strName
)
483 // we must have an absolute filename because otherwise CreateTempFileName()
484 // would create the temp file in $TMP (i.e. the system standard location
485 // for the temp files) which might be on another volume/drive/mount and
486 // wxRename()ing it later to m_strName from Commit() would then fail
488 // with the absolute filename, the temp file is created in the same
489 // directory as this one which ensures that wxRename() may work later
490 wxFileName
fn(strName
);
491 if ( !fn
.IsAbsolute() )
493 fn
.Normalize(wxPATH_NORM_ABSOLUTE
);
496 m_strName
= fn
.GetFullPath();
498 m_strTemp
= wxFileName::CreateTempFileName(m_strName
, &m_file
);
500 if ( m_strTemp
.empty() )
502 // CreateTempFileName() failed
507 // the temp file should have the same permissions as the original one
511 if ( stat( (const char*) m_strName
.fn_str(), &st
) == 0 )
517 // file probably didn't exist, just give it the default mode _using_
518 // user's umask (new files creation should respect umask)
519 mode_t mask
= umask(0777);
524 if ( chmod( (const char*) m_strTemp
.fn_str(), mode
) == -1 )
526 wxLogSysError(_("Failed to set temporary file permissions"));
533 // ----------------------------------------------------------------------------
535 // ----------------------------------------------------------------------------
537 wxTempFile::~wxTempFile()
543 bool wxTempFile::Commit()
547 if ( wxFile::Exists(m_strName
) && wxRemove(m_strName
) != 0 ) {
548 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
552 if ( wxRename(m_strTemp
, m_strName
) != 0 ) {
553 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
560 void wxTempFile::Discard()
563 if ( wxRemove(m_strTemp
) != 0 )
564 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());