]>
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__))
68 #elif (defined(__WXSTUBS__))
69 // Have to ifdef this for different environments
71 #elif (defined(__WXMAC__))
72 int access( const char *path
, int mode
) { return 0 ; }
73 char* mktemp( char * path
) { return path
;}
79 #error "Please specify the header with file functions declarations."
82 #include <stdio.h> // SEEK_xxx constants
83 #include <fcntl.h> // O_RDONLY &c
86 #include <sys/types.h> // needed for stat
87 #include <sys/stat.h> // stat
90 // Microsoft compiler loves underscores, feed them to it
99 #define access _access
107 #define O_RDONLY _O_RDONLY
108 #define O_WRONLY _O_WRONLY
109 #define O_RDWR _O_RDWR
110 #define O_EXCL _O_EXCL
111 #define O_CREAT _O_CREAT
112 #define O_BINARY _O_BINARY
114 #define S_IFDIR _S_IFDIR
115 #define S_IFREG _S_IFREG
117 #define tell(fd) lseek(fd, 0, SEEK_CUR)
120 #if defined(__BORLANDC__) || defined(_MSC_VER)
125 // there is no distinction between text and binary files under Unix
135 #include "wx/string.h"
144 // some broken compilers don't have 3rd argument in open() and creat()
146 #define ACCESS(access)
148 #else // normal compiler
149 #define ACCESS(access) , (access)
152 // ============================================================================
153 // implementation of wxFile
154 // ============================================================================
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
159 bool wxFile::Exists(const wxChar
*name
)
162 #if wxUSE_UNICODE && wxMBFILES
163 wxCharBuffer fname
= wxConvFile
.cWC2MB(name
);
166 return !access(wxUnix2MacFilename( name
) , 0) && !stat(wxUnix2MacFilename( name
), &st
) && (st
.st_mode
& S_IFREG
);
168 return !access(fname
, 0) &&
169 !stat(wxMBSTRINGCAST fname
, &st
) &&
170 (st
.st_mode
& S_IFREG
);
174 return !access(wxUnix2MacFilename( name
) , 0) && !stat(wxUnix2MacFilename( name
), &st
) && (st
.st_mode
& S_IFREG
);
176 return !access(name
, 0) &&
177 !stat((wxChar
*) name
, &st
) &&
178 (st
.st_mode
& S_IFREG
);
183 bool wxFile::Access(const wxChar
*name
, OpenMode mode
)
197 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
200 return access(wxFNCONV(name
), how
) == 0;
203 // ----------------------------------------------------------------------------
205 // ----------------------------------------------------------------------------
208 wxFile::wxFile(const wxChar
*szFileName
, OpenMode mode
)
213 Open(szFileName
, mode
);
216 // create the file, fail if it already exists and bOverwrite
217 bool wxFile::Create(const wxChar
*szFileName
, bool bOverwrite
, int accessMode
)
219 // if bOverwrite we create a new file or truncate the existing one,
220 // otherwise we only create the new file and fail if it already exists
222 int fd
= open(wxUnix2MacFilename( szFileName
), O_CREAT
| (bOverwrite
? O_TRUNC
: O_EXCL
), access
);
224 int fd
= open(wxFNCONV(szFileName
),
225 O_WRONLY
| O_CREAT
| (bOverwrite
? O_TRUNC
: O_EXCL
)
229 wxLogSysError(_("can't create file '%s'"), szFileName
);
239 bool wxFile::Open(const wxChar
*szFileName
, OpenMode mode
, int accessMode
)
241 int flags
= O_BINARY
;
249 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
253 flags
|= O_WRONLY
| O_APPEND
;
262 int fd
= open(wxUnix2MacFilename( szFileName
), flags
, access
);
264 int fd
= open(wxFNCONV(szFileName
), flags
ACCESS(accessMode
));
267 wxLogSysError(_("can't open file '%s'"), szFileName
);
280 if ( close(m_fd
) == -1 ) {
281 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
292 // ----------------------------------------------------------------------------
294 // ----------------------------------------------------------------------------
297 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
299 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
302 int iRc
= ::read(m_fd
, (char*) pBuf
, nCount
);
304 int iRc
= ::read(m_fd
, pBuf
, nCount
);
307 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
308 return wxInvalidOffset
;
315 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
317 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
320 int iRc
= ::write(m_fd
, (const char*) pBuf
, nCount
);
322 int iRc
= ::write(m_fd
, pBuf
, nCount
);
325 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
337 #if defined(__VISUALC__) || wxHAVE_FSYNC
338 if ( fsync(m_fd
) == -1 )
340 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
351 // ----------------------------------------------------------------------------
353 // ----------------------------------------------------------------------------
356 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
358 wxASSERT( IsOpened() );
363 wxFAIL_MSG(_("unknown seek origin"));
378 int iRc
= lseek(m_fd
, ofs
, origin
);
380 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
381 return wxInvalidOffset
;
388 off_t
wxFile::Tell() const
390 wxASSERT( IsOpened() );
392 int iRc
= tell(m_fd
);
394 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
395 return wxInvalidOffset
;
401 // get current file length
402 off_t
wxFile::Length() const
404 wxASSERT( IsOpened() );
407 int iRc
= _filelength(m_fd
);
409 int iRc
= tell(m_fd
);
411 // @ have to use const_cast :-(
412 int iLen
= ((wxFile
*)this)->SeekEnd();
414 // restore old position
415 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
426 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
427 return wxInvalidOffset
;
433 // is end of file reached?
434 bool wxFile::Eof() const
436 wxASSERT( IsOpened() );
440 #if defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
441 // @@ this doesn't work, of course, on unseekable file descriptors
442 off_t ofsCur
= Tell(),
444 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
447 iRc
= ofsCur
== ofsMax
;
448 #else // Windows and "native" compiler
450 #endif // Windows/Unix
460 wxLogSysError(_("can't determine if the end of file is reached on \
461 descriptor %d"), m_fd
);
465 wxFAIL_MSG(_("invalid eof() return value."));
471 // ============================================================================
472 // implementation of wxTempFile
473 // ============================================================================
475 // ----------------------------------------------------------------------------
477 // ----------------------------------------------------------------------------
478 wxTempFile::wxTempFile(const wxString
& strName
)
483 bool wxTempFile::Open(const wxString
& strName
)
487 // we want to create the file in the same directory as strName because
488 // otherwise rename() in Commit() might not work (if the files are on
489 // different partitions for example). Unfortunately, the only standard
490 // (POSIX) temp file creation function tmpnam() can't do it.
491 #if defined(__UNIX__) || defined(__WXSTUBS__)|| defined( __WXMAC__ )
492 static const wxChar
*szMktempSuffix
= wxT("XXXXXX");
493 m_strTemp
<< strName
<< szMktempSuffix
;
494 // can use the cast because length doesn't change
495 mktemp(wxMBSTRINGCAST m_strTemp
.mb_str());
496 #elif defined(__WXPM__)
497 // for now just create a file
498 // future enhancements can be to set some extended attributes for file systems
499 // OS/2 supports that have them (HPFS, FAT32) and security (HPFS386)
500 static const wxChar
*szMktempSuffix
= wxT("XXX");
501 m_strTemp
<< strName
<< szMktempSuffix
;
502 mkdir(m_strTemp
.GetWriteBuf(MAX_PATH
));
505 wxSplitPath(strName
, &strPath
, NULL
, NULL
);
506 if ( strPath
.IsEmpty() )
507 strPath
= wxT('.'); // GetTempFileName will fail if we give it empty string
509 if ( !GetTempFileName(strPath
, wxT("wx_"),0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
511 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
512 if ( !GetTempFileName((BYTE
) (DWORD
)(const wxChar
*) strPath
, wxT("wx_"),0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
514 wxLogLastError(wxT("GetTempFileName"));
515 m_strTemp
.UngetWriteBuf();
516 #endif // Windows/Unix
518 int access
= wxS_DEFAULT
;
520 // create the file with the same mode as the original one under Unix
521 mode_t umaskOld
= 0; // just to suppress compiler warning
525 if ( stat(strName
.fn_str(), &st
) == 0 )
527 // this assumes that only lower bits of st_mode contain the access
528 // rights, but it's true for at least all Unices which have S_IXXXX()
529 // macros, so should not be less portable than using (not POSIX)
531 access
= st
.st_mode
& 0777;
533 // we want to create the file with exactly the same access rights as
534 // the original one, so disable the user's umask for the moment
540 // file probably didn't exist, just create with default mode _using_
541 // user's umask (new files creation should respet umask)
542 changedUmask
= FALSE
;
546 bool ok
= m_file
.Open(m_strTemp
, wxFile::write
, access
);
551 // restore umask now that the file is created
552 (void)umask(umaskOld
);
559 // ----------------------------------------------------------------------------
561 // ----------------------------------------------------------------------------
563 wxTempFile::~wxTempFile()
569 bool wxTempFile::Commit()
574 if ( wxFile::Exists(m_strName
) && remove(m_strName
.fn_str()) != 0 ) {
575 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
579 if ( rename(m_strTemp
.fn_str(), m_strName
.fn_str()) != 0 ) {
580 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
584 if ( wxFile::Exists(m_strName
) && remove(wxUnix2MacFilename( m_strName
)) != 0 ) {
585 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
589 if ( rename(wxUnix2MacFilename( m_strTemp
), wxUnix2MacFilename( m_strName
)) != 0 ) {
590 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
598 void wxTempFile::Discard()
602 if ( remove(m_strTemp
.fn_str()) != 0 )
603 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());
605 if ( remove( wxUnix2MacFilename(m_strTemp
.fn_str())) != 0 )
606 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());