]>
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(__WXWINE__)
35 #define WIN32_LEAN_AND_MEAN
57 #include <windows.h> // for GetTempFileName
58 #elif (defined(__UNIX__) || defined(__GNUWIN32__))
63 #elif (defined(__WXPM__))
67 #elif (defined(__WXSTUBS__))
68 // Have to ifdef this for different environments
70 #elif (defined(__WXMAC__))
72 int access( const char *path
, int mode
) { return 0 ; }
74 int _access( const char *path
, int mode
) { return 0 ; }
76 char* mktemp( char * path
) { return path
;}
82 #error "Please specify the header with file functions declarations."
85 #include <stdio.h> // SEEK_xxx constants
86 #include <fcntl.h> // O_RDONLY &c
89 #include <sys/types.h> // needed for stat
90 #include <sys/stat.h> // stat
93 #if defined(__BORLANDC__) || defined(_MSC_VER)
98 // there is no distinction between text and binary files under Unix, so define
99 // O_BINARY as 0 if the system headers don't do it already
100 #if defined(__UNIX__) && !defined(O_BINARY)
112 // some broken compilers don't have 3rd argument in open() and creat()
114 #define ACCESS(access)
116 #else // normal compiler
117 #define ACCESS(access) , (access)
121 #include "wx/string.h"
126 // ============================================================================
127 // implementation of wxFile
128 // ============================================================================
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
133 bool wxFile::Exists(const wxChar
*name
)
136 #if wxUSE_UNICODE && wxMBFILES
137 wxCharBuffer fname
= wxConvFile
.cWC2MB(name
);
140 return !access(wxUnix2MacFilename( name
) , 0) && !stat(wxUnix2MacFilename( name
), &st
) && (st
.st_mode
& S_IFREG
);
142 return !wxAccess(fname
, 0) &&
143 !wxStat(wxMBSTRINGCAST fname
, &st
) &&
144 (st
.st_mode
& S_IFREG
);
148 return !access(wxUnix2MacFilename( name
) , 0) && !stat(wxUnix2MacFilename( name
), &st
) && (st
.st_mode
& S_IFREG
);
150 return !wxAccess(name
, 0) &&
151 !wxStat(name
, &st
) &&
152 (st
.st_mode
& S_IFREG
);
157 bool wxFile::Access(const wxChar
*name
, OpenMode mode
)
171 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
174 return wxAccess(wxFNCONV(name
), how
) == 0;
177 // ----------------------------------------------------------------------------
179 // ----------------------------------------------------------------------------
182 wxFile::wxFile(const wxChar
*szFileName
, OpenMode mode
)
187 Open(szFileName
, mode
);
190 // create the file, fail if it already exists and bOverwrite
191 bool wxFile::Create(const wxChar
*szFileName
, bool bOverwrite
, int accessMode
)
193 // if bOverwrite we create a new file or truncate the existing one,
194 // otherwise we only create the new file and fail if it already exists
196 // Dominic Mazzoni [dmazzoni+@cs.cmu.edu] reports that open is still broken on the mac, so we replace
197 // int fd = open(wxUnix2MacFilename( szFileName ), O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
198 int fd
= creat(wxUnix2MacFilename( szFileName
), accessMode
);
200 int fd
= wxOpen(wxFNCONV(szFileName
),
201 O_BINARY
| O_WRONLY
| O_CREAT
|
202 (bOverwrite
? O_TRUNC
: O_EXCL
)
206 wxLogSysError(_("can't create file '%s'"), szFileName
);
216 bool wxFile::Open(const wxChar
*szFileName
, OpenMode mode
, int accessMode
)
218 int flags
= O_BINARY
;
226 if ( wxFile::Exists(szFileName
) )
228 flags
|= O_WRONLY
| O_APPEND
;
231 //else: fall through as write_append is the same as write if the
232 // file doesn't exist
235 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
244 int fd
= open(wxUnix2MacFilename( szFileName
), flags
, access
);
246 int fd
= wxOpen(wxFNCONV(szFileName
), flags
ACCESS(accessMode
));
249 wxLogSysError(_("can't open file '%s'"), szFileName
);
262 if ( close(m_fd
) == -1 ) {
263 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
274 // ----------------------------------------------------------------------------
276 // ----------------------------------------------------------------------------
279 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
281 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
284 int iRc
= ::read(m_fd
, (char*) pBuf
, nCount
);
286 int iRc
= ::read(m_fd
, pBuf
, nCount
);
289 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
290 return wxInvalidOffset
;
297 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
299 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
302 #if __MSL__ >= 0x6000
303 int iRc
= ::write(m_fd
, (void*) pBuf
, nCount
);
305 int iRc
= ::write(m_fd
, (const char*) pBuf
, nCount
);
308 int iRc
= ::write(m_fd
, pBuf
, nCount
);
311 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
323 #if defined(__VISUALC__) || wxHAVE_FSYNC
324 if ( wxFsync(m_fd
) == -1 )
326 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
337 // ----------------------------------------------------------------------------
339 // ----------------------------------------------------------------------------
342 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
344 wxASSERT( IsOpened() );
349 wxFAIL_MSG(_("unknown seek origin"));
364 int iRc
= lseek(m_fd
, ofs
, origin
);
366 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
367 return wxInvalidOffset
;
374 off_t
wxFile::Tell() const
376 wxASSERT( IsOpened() );
378 int iRc
= wxTell(m_fd
);
380 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
381 return wxInvalidOffset
;
387 // get current file length
388 off_t
wxFile::Length() const
390 wxASSERT( IsOpened() );
393 int iRc
= _filelength(m_fd
);
395 int iRc
= wxTell(m_fd
);
397 // @ have to use const_cast :-(
398 int iLen
= ((wxFile
*)this)->SeekEnd();
400 // restore old position
401 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
412 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
413 return wxInvalidOffset
;
419 // is end of file reached?
420 bool wxFile::Eof() const
422 wxASSERT( IsOpened() );
426 #if defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
427 // @@ this doesn't work, of course, on unseekable file descriptors
428 off_t ofsCur
= Tell(),
430 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
433 iRc
= ofsCur
== ofsMax
;
434 #else // Windows and "native" compiler
436 #endif // Windows/Unix
446 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd
);
450 wxFAIL_MSG(_("invalid eof() return value."));
456 // ============================================================================
457 // implementation of wxTempFile
458 // ============================================================================
460 // ----------------------------------------------------------------------------
462 // ----------------------------------------------------------------------------
463 wxTempFile::wxTempFile(const wxString
& strName
)
468 bool wxTempFile::Open(const wxString
& strName
)
472 // we want to create the file in the same directory as strName because
473 // otherwise rename() in Commit() might not work (if the files are on
474 // different partitions for example). Unfortunately, the only standard
475 // (POSIX) temp file creation function tmpnam() can't do it.
476 #if defined(__UNIX__) || defined(__WXSTUBS__)|| defined( __WXMAC__ )
477 static const wxChar
*szMktempSuffix
= wxT("XXXXXX");
478 m_strTemp
<< strName
<< szMktempSuffix
;
479 // can use the cast because length doesn't change
480 mktemp(wxMBSTRINGCAST m_strTemp
.mb_str());
481 #elif defined(__WXPM__)
482 // for now just create a file
483 // future enhancements can be to set some extended attributes for file systems
484 // OS/2 supports that have them (HPFS, FAT32) and security (HPFS386)
485 static const wxChar
*szMktempSuffix
= wxT("XXX");
486 m_strTemp
<< strName
<< szMktempSuffix
;
487 ::DosCreateDir(m_strTemp
.GetWriteBuf(MAX_PATH
), NULL
);
490 wxSplitPath(strName
, &strPath
, NULL
, NULL
);
491 if ( strPath
.IsEmpty() )
492 strPath
= wxT('.'); // GetTempFileName will fail if we give it empty string
494 if ( !GetTempFileName(strPath
, wxT("wx_"),0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
496 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
497 if ( !GetTempFileName((BYTE
) (DWORD
)(const wxChar
*) strPath
, wxT("wx_"),0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
499 wxLogLastError(wxT("GetTempFileName"));
500 m_strTemp
.UngetWriteBuf();
501 #endif // Windows/Unix
503 int access
= wxS_DEFAULT
;
505 // create the file with the same mode as the original one under Unix
506 mode_t umaskOld
= 0; // just to suppress compiler warning
510 if ( stat(strName
.fn_str(), &st
) == 0 )
512 // this assumes that only lower bits of st_mode contain the access
513 // rights, but it's true for at least all Unices which have S_IXXXX()
514 // macros, so should not be less portable than using (not POSIX)
516 access
= st
.st_mode
& 0777;
518 // we want to create the file with exactly the same access rights as
519 // the original one, so disable the user's umask for the moment
525 // file probably didn't exist, just create with default mode _using_
526 // user's umask (new files creation should respet umask)
527 changedUmask
= FALSE
;
531 bool ok
= m_file
.Open(m_strTemp
, wxFile::write
, access
);
536 // restore umask now that the file is created
537 (void)umask(umaskOld
);
544 // ----------------------------------------------------------------------------
546 // ----------------------------------------------------------------------------
548 wxTempFile::~wxTempFile()
554 bool wxTempFile::Commit()
559 if ( wxFile::Exists(m_strName
) && wxRemove(m_strName
) != 0 ) {
560 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
564 if ( wxRename(m_strTemp
, m_strName
) != 0 ) {
565 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
569 if ( wxFile::Exists(m_strName
) && remove(wxUnix2MacFilename( m_strName
)) != 0 ) {
570 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
574 if ( rename(wxUnix2MacFilename( m_strTemp
), wxUnix2MacFilename( m_strName
)) != 0 ) {
575 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
583 void wxTempFile::Discard()
587 if ( wxRemove(m_strTemp
) != 0 )
588 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());
590 if ( remove( wxUnix2MacFilename(m_strTemp
.fn_str())) != 0 )
591 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());