]>
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
);
209 // create the file, fail if it already exists and bOverwrite
210 bool wxFile::Create(const wxChar
*szFileName
, bool bOverwrite
, int accessMode
)
212 // if bOverwrite we create a new file or truncate the existing one,
213 // otherwise we only create the new file and fail if it already exists
214 int fd
= open(wxFNCONV(szFileName
),
215 O_WRONLY
| O_CREAT
| (bOverwrite
? O_TRUNC
: O_EXCL
)
219 wxLogSysError(_("can't create file '%s'"), szFileName
);
229 bool wxFile::Open(const wxChar
*szFileName
, OpenMode mode
, int accessMode
)
231 int flags
= O_BINARY
;
239 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
243 flags
|= O_WRONLY
| O_APPEND
;
251 int fd
= open(wxFNCONV(szFileName
), flags
ACCESS(accessMode
));
254 wxLogSysError(_("can't open file '%s'"), szFileName
);
267 if ( close(m_fd
) == -1 ) {
268 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
284 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
286 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
289 int iRc
= ::read(m_fd
, (char*) pBuf
, nCount
);
291 int iRc
= ::read(m_fd
, pBuf
, nCount
);
294 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
295 return wxInvalidOffset
;
302 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
304 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
307 int iRc
= ::write(m_fd
, (const char*) pBuf
, nCount
);
309 int iRc
= ::write(m_fd
, pBuf
, nCount
);
312 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
324 #if defined(__VISUALC__) || wxHAVE_FSYNC
325 if ( fsync(m_fd
) == -1 )
327 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
338 // ----------------------------------------------------------------------------
340 // ----------------------------------------------------------------------------
343 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
345 wxASSERT( IsOpened() );
350 wxFAIL_MSG(_("unknown seek origin"));
365 int iRc
= lseek(m_fd
, ofs
, origin
);
367 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
368 return wxInvalidOffset
;
375 off_t
wxFile::Tell() const
377 wxASSERT( IsOpened() );
379 int iRc
= tell(m_fd
);
381 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
382 return wxInvalidOffset
;
388 // get current file length
389 off_t
wxFile::Length() const
391 wxASSERT( IsOpened() );
394 int iRc
= _filelength(m_fd
);
396 int iRc
= tell(m_fd
);
398 // @ have to use const_cast :-(
399 int iLen
= ((wxFile
*)this)->SeekEnd();
401 // restore old position
402 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
413 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
414 return wxInvalidOffset
;
420 // is end of file reached?
421 bool wxFile::Eof() const
423 wxASSERT( IsOpened() );
427 #if defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
428 // @@ this doesn't work, of course, on unseekable file descriptors
429 off_t ofsCur
= Tell(),
431 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
434 iRc
= ofsCur
== ofsMax
;
435 #else // Windows and "native" compiler
437 #endif // Windows/Unix
447 wxLogSysError(_("can't determine if the end of file is reached on \
448 descriptor %d"), m_fd
);
452 wxFAIL_MSG(_("invalid eof() return value."));
458 // ============================================================================
459 // implementation of wxTempFile
460 // ============================================================================
462 // ----------------------------------------------------------------------------
464 // ----------------------------------------------------------------------------
465 wxTempFile::wxTempFile(const wxString
& strName
)
470 bool wxTempFile::Open(const wxString
& strName
)
474 // we want to create the file in the same directory as strName because
475 // otherwise rename() in Commit() might not work (if the files are on
476 // different partitions for example). Unfortunately, the only standard
477 // (POSIX) temp file creation function tmpnam() can't do it.
478 #if defined(__UNIX__) || defined(__WXSTUBS__)|| defined( __WXMAC__ )
479 static const wxChar
*szMktempSuffix
= _T("XXXXXX");
480 m_strTemp
<< strName
<< szMktempSuffix
;
481 mktemp(MBSTRINGCAST m_strTemp
.mb_str()); // will do because length doesn't change
484 wxSplitPath(strName
, &strPath
, NULL
, NULL
);
485 if ( strPath
.IsEmpty() )
486 strPath
= _T('.'); // GetTempFileName will fail if we give it empty string
488 if ( !GetTempFileName(strPath
, _T("wx_"),0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
490 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
491 if ( !GetTempFileName((BYTE
) (const wxChar
*) strPath
, _T("wx_"),0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
493 wxLogLastError(_T("GetTempFileName"));
494 m_strTemp
.UngetWriteBuf();
495 #endif // Windows/Unix
497 int access
= wxS_DEFAULT
;
499 // create the file with the same mode as the original one under Unix
500 mode_t umaskOld
= 0; // just to suppress compiler warning
504 if ( stat(strName
.fn_str(), &st
) == 0 )
506 // this assumes that only lower bits of st_mode contain the access
507 // rights, but it's true for at least all Unices which have S_IXXXX()
508 // macros, so should not be less portable than using (not POSIX)
510 access
= st
.st_mode
& 0777;
512 // we want to create the file with exactly the same access rights as
513 // the original one, so disable the user's umask for the moment
519 // file probably didn't exist, just create with default mode _using_
520 // user's umask (new files creation should respet umask)
521 changedUmask
= FALSE
;
525 bool ok
= m_file
.Open(m_strTemp
, wxFile::write
, access
);
530 // restore umask now that the file is created
531 (void)umask(umaskOld
);
538 // ----------------------------------------------------------------------------
540 // ----------------------------------------------------------------------------
542 wxTempFile::~wxTempFile()
548 bool wxTempFile::Commit()
552 if ( wxFile::Exists(m_strName
) && remove(m_strName
.fn_str()) != 0 ) {
553 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
557 if ( rename(m_strTemp
.fn_str(), m_strName
.fn_str()) != 0 ) {
558 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
565 void wxTempFile::Discard()
568 if ( remove(m_strTemp
.fn_str()) != 0 )
569 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());