]>
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(__WXSTUBS__))
64 // Have to ifdef this for different environments
66 #elif (defined(__WXMAC__))
67 int access( const char *path
, int mode
) { return 0 ; }
68 char* mktemp( char * path
) { return path
;}
74 #error "Please specify the header with file functions declarations."
77 #include <stdio.h> // SEEK_xxx constants
78 #include <fcntl.h> // O_RDONLY &c
81 #include <sys/types.h> // needed for stat
82 #include <sys/stat.h> // stat
85 // Microsoft compiler loves underscores, feed them to it
94 #define access _access
102 #define O_RDONLY _O_RDONLY
103 #define O_WRONLY _O_WRONLY
104 #define O_RDWR _O_RDWR
105 #define O_EXCL _O_EXCL
106 #define O_CREAT _O_CREAT
107 #define O_BINARY _O_BINARY
109 #define S_IFDIR _S_IFDIR
110 #define S_IFREG _S_IFREG
112 #define tell(fd) lseek(fd, 0, SEEK_CUR)
115 #if defined(__BORLANDC__) || defined(_MSC_VER)
120 // there is no distinction between text and binary files under Unix
130 #include <wx/string.h>
140 char gwxMacFileName
[ MAX_PATH
] ;
141 char gwxMacFileName2
[ MAX_PATH
] ;
142 char gwxMacFileName3
[ MAX_PATH
] ;
145 // some broken compilers don't have 3rd argument in open() and creat()
147 #define ACCESS(access)
149 #else // normal compiler
150 #define ACCESS(access) , (access)
153 // ============================================================================
154 // implementation of wxFile
155 // ============================================================================
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
160 bool wxFile::Exists(const wxChar
*name
)
163 #if wxUSE_UNICODE && wxMBFILES
164 wxCharBuffer fname
= wxConvFile
.cWC2MB(name
);
166 return !access(fname
, 0) &&
167 !stat(MBSTRINGCAST fname
, &st
) &&
168 (st
.st_mode
& S_IFREG
);
170 return !access(name
, 0) &&
171 !stat((wxChar
*) name
, &st
) &&
172 (st
.st_mode
& S_IFREG
);
176 bool wxFile::Access(const wxChar
*name
, OpenMode mode
)
190 wxFAIL_MSG(_T("bad wxFile::Access mode parameter."));
193 return access(wxFNCONV(name
), how
) == 0;
196 // ----------------------------------------------------------------------------
198 // ----------------------------------------------------------------------------
201 wxFile::wxFile(const wxChar
*szFileName
, OpenMode mode
)
206 Open(szFileName
, mode
);
215 // create the file, fail if it already exists and bOverwrite
216 bool wxFile::Create(const wxChar
*szFileName
, bool bOverwrite
, int accessMode
)
218 // if bOverwrite we create a new file or truncate the existing one,
219 // otherwise we only create the new file and fail if it already exists
220 int fd
= open(wxFNCONV(szFileName
),
221 O_WRONLY
| O_CREAT
| (bOverwrite
? O_TRUNC
: O_EXCL
)
225 wxLogSysError(_("can't create file '%s'"), szFileName
);
235 bool wxFile::Open(const wxChar
*szFileName
, OpenMode mode
, int accessMode
)
237 int flags
= O_BINARY
;
245 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
249 flags
|= O_WRONLY
| O_APPEND
;
257 int fd
= open(wxFNCONV(szFileName
), flags
ACCESS(accessMode
));
260 wxLogSysError(_("can't open file '%s'"), szFileName
);
273 if ( close(m_fd
) == -1 ) {
274 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
285 // ----------------------------------------------------------------------------
287 // ----------------------------------------------------------------------------
290 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
292 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
295 int iRc
= ::read(m_fd
, (char*) pBuf
, nCount
);
297 int iRc
= ::read(m_fd
, pBuf
, nCount
);
300 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
301 return wxInvalidOffset
;
308 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
310 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
313 int iRc
= ::write(m_fd
, (const char*) pBuf
, nCount
);
315 int iRc
= ::write(m_fd
, pBuf
, nCount
);
318 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
330 #if defined(__VISUALC__) || wxHAVE_FSYNC
331 if ( fsync(m_fd
) == -1 )
333 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
344 // ----------------------------------------------------------------------------
346 // ----------------------------------------------------------------------------
349 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
351 wxASSERT( IsOpened() );
368 wxFAIL_MSG(_("unknown seek origin"));
371 int iRc
= lseek(m_fd
, ofs
, flag
);
373 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
374 return wxInvalidOffset
;
381 off_t
wxFile::Tell() const
383 wxASSERT( IsOpened() );
385 int iRc
= tell(m_fd
);
387 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
388 return wxInvalidOffset
;
394 // get current file length
395 off_t
wxFile::Length() const
397 wxASSERT( IsOpened() );
400 int iRc
= _filelength(m_fd
);
402 int iRc
= tell(m_fd
);
404 // @ have to use const_cast :-(
405 int iLen
= ((wxFile
*)this)->SeekEnd();
407 // restore old position
408 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
419 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
420 return wxInvalidOffset
;
426 // is end of file reached?
427 bool wxFile::Eof() const
429 wxASSERT( IsOpened() );
433 #if defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
434 // @@ this doesn't work, of course, on unseekable file descriptors
435 off_t ofsCur
= Tell(),
437 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
440 iRc
= ofsCur
== ofsMax
;
441 #else // Windows and "native" compiler
443 #endif // Windows/Unix
453 wxLogSysError(_("can't determine if the end of file is reached on \
454 descriptor %d"), m_fd
);
458 wxFAIL_MSG(_("invalid eof() return value."));
464 // ============================================================================
465 // implementation of wxTempFile
466 // ============================================================================
468 // ----------------------------------------------------------------------------
470 // ----------------------------------------------------------------------------
471 wxTempFile::wxTempFile(const wxString
& strName
)
476 bool wxTempFile::Open(const wxString
& strName
)
480 // we want to create the file in the same directory as strName because
481 // otherwise rename() in Commit() might not work (if the files are on
482 // different partitions for example). Unfortunately, the only standard
483 // (POSIX) temp file creation function tmpnam() can't do it.
484 #if defined(__UNIX__) || defined(__WXSTUBS__)|| defined( __WXMAC__ )
485 static const wxChar
*szMktempSuffix
= _T("XXXXXX");
486 m_strTemp
<< strName
<< szMktempSuffix
;
487 mktemp(MBSTRINGCAST m_strTemp
.mb_str()); // will do because length doesn't change
490 wxSplitPath(strName
, &strPath
, NULL
, NULL
);
491 if ( strPath
.IsEmpty() )
492 strPath
= _T('.'); // GetTempFileName will fail if we give it empty string
494 if ( !GetTempFileName(strPath
, _T("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
) (const wxChar
*) strPath
, _T("wx_"),0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
499 wxLogLastError(_T("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()
558 if ( wxFile::Exists(m_strName
) && remove(m_strName
.fn_str()) != 0 ) {
559 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
563 if ( rename(m_strTemp
.fn_str(), m_strName
.fn_str()) != 0 ) {
564 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
571 void wxTempFile::Discard()
574 if ( remove(m_strTemp
.fn_str()) != 0 )
575 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());