]>
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__))
71 int access( const char *path
, int mode
) { return 0 ; }
72 char* mktemp( char * path
) { return path
;}
78 #error "Please specify the header with file functions declarations."
81 #include <stdio.h> // SEEK_xxx constants
82 #include <fcntl.h> // O_RDONLY &c
85 #include <sys/types.h> // needed for stat
86 #include <sys/stat.h> // stat
89 #if defined(__BORLANDC__) || defined(_MSC_VER)
94 // there is no distinction between text and binary files under Unix, so define
95 // O_BINARY as 0 if the system headers don't do it already
96 #if defined(__UNIX__) && !defined(O_BINARY)
108 // some broken compilers don't have 3rd argument in open() and creat()
110 #define ACCESS(access)
112 #else // normal compiler
113 #define ACCESS(access) , (access)
117 #include "wx/string.h"
122 // ============================================================================
123 // implementation of wxFile
124 // ============================================================================
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
129 bool wxFile::Exists(const wxChar
*name
)
132 #if wxUSE_UNICODE && wxMBFILES
133 wxCharBuffer fname
= wxConvFile
.cWC2MB(name
);
136 return !access(wxUnix2MacFilename( name
) , 0) && !stat(wxUnix2MacFilename( name
), &st
) && (st
.st_mode
& S_IFREG
);
138 return !wxAccess(fname
, 0) &&
139 !wxStat(wxMBSTRINGCAST fname
, &st
) &&
140 (st
.st_mode
& S_IFREG
);
144 return !access(wxUnix2MacFilename( name
) , 0) && !stat(wxUnix2MacFilename( name
), &st
) && (st
.st_mode
& S_IFREG
);
146 return !wxAccess(name
, 0) &&
147 !wxStat(name
, &st
) &&
148 (st
.st_mode
& S_IFREG
);
153 bool wxFile::Access(const wxChar
*name
, OpenMode mode
)
167 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
170 return wxAccess(wxFNCONV(name
), how
) == 0;
173 // ----------------------------------------------------------------------------
175 // ----------------------------------------------------------------------------
178 wxFile::wxFile(const wxChar
*szFileName
, OpenMode mode
)
183 Open(szFileName
, mode
);
186 // create the file, fail if it already exists and bOverwrite
187 bool wxFile::Create(const wxChar
*szFileName
, bool bOverwrite
, int accessMode
)
189 // if bOverwrite we create a new file or truncate the existing one,
190 // otherwise we only create the new file and fail if it already exists
192 int fd
= open(wxUnix2MacFilename( szFileName
), O_CREAT
| (bOverwrite
? O_TRUNC
: O_EXCL
), access
);
194 int fd
= wxOpen(wxFNCONV(szFileName
),
195 O_BINARY
| O_WRONLY
| O_CREAT
|
196 (bOverwrite
? O_TRUNC
: O_EXCL
)
200 wxLogSysError(_("can't create file '%s'"), szFileName
);
210 bool wxFile::Open(const wxChar
*szFileName
, OpenMode mode
, int accessMode
)
212 int flags
= O_BINARY
;
220 if ( wxFile::Exists(szFileName
) )
222 flags
|= O_WRONLY
| O_APPEND
;
225 //else: fall through as write_append is the same as write if the
226 // file doesn't exist
229 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
238 int fd
= open(wxUnix2MacFilename( szFileName
), flags
, access
);
240 int fd
= wxOpen(wxFNCONV(szFileName
), flags
ACCESS(accessMode
));
243 wxLogSysError(_("can't open file '%s'"), szFileName
);
256 if ( close(m_fd
) == -1 ) {
257 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
273 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
275 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
278 int iRc
= ::read(m_fd
, (char*) pBuf
, nCount
);
280 int iRc
= ::read(m_fd
, pBuf
, nCount
);
283 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
284 return wxInvalidOffset
;
291 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
293 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
296 int iRc
= ::write(m_fd
, (const char*) pBuf
, nCount
);
298 int iRc
= ::write(m_fd
, pBuf
, nCount
);
301 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
313 #if defined(__VISUALC__) || wxHAVE_FSYNC
314 if ( wxFsync(m_fd
) == -1 )
316 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
327 // ----------------------------------------------------------------------------
329 // ----------------------------------------------------------------------------
332 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
334 wxASSERT( IsOpened() );
339 wxFAIL_MSG(_("unknown seek origin"));
354 int iRc
= lseek(m_fd
, ofs
, origin
);
356 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
357 return wxInvalidOffset
;
364 off_t
wxFile::Tell() const
366 wxASSERT( IsOpened() );
368 int iRc
= wxTell(m_fd
);
370 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
371 return wxInvalidOffset
;
377 // get current file length
378 off_t
wxFile::Length() const
380 wxASSERT( IsOpened() );
383 int iRc
= _filelength(m_fd
);
385 int iRc
= wxTell(m_fd
);
387 // @ have to use const_cast :-(
388 int iLen
= ((wxFile
*)this)->SeekEnd();
390 // restore old position
391 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
402 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
403 return wxInvalidOffset
;
409 // is end of file reached?
410 bool wxFile::Eof() const
412 wxASSERT( IsOpened() );
416 #if defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
417 // @@ this doesn't work, of course, on unseekable file descriptors
418 off_t ofsCur
= Tell(),
420 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
423 iRc
= ofsCur
== ofsMax
;
424 #else // Windows and "native" compiler
426 #endif // Windows/Unix
436 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd
);
440 wxFAIL_MSG(_("invalid eof() return value."));
446 // ============================================================================
447 // implementation of wxTempFile
448 // ============================================================================
450 // ----------------------------------------------------------------------------
452 // ----------------------------------------------------------------------------
453 wxTempFile::wxTempFile(const wxString
& strName
)
458 bool wxTempFile::Open(const wxString
& strName
)
462 // we want to create the file in the same directory as strName because
463 // otherwise rename() in Commit() might not work (if the files are on
464 // different partitions for example). Unfortunately, the only standard
465 // (POSIX) temp file creation function tmpnam() can't do it.
466 #if defined(__UNIX__) || defined(__WXSTUBS__)|| defined( __WXMAC__ )
467 static const wxChar
*szMktempSuffix
= wxT("XXXXXX");
468 m_strTemp
<< strName
<< szMktempSuffix
;
469 // can use the cast because length doesn't change
470 mktemp(wxMBSTRINGCAST m_strTemp
.mb_str());
471 #elif defined(__WXPM__)
472 // for now just create a file
473 // future enhancements can be to set some extended attributes for file systems
474 // OS/2 supports that have them (HPFS, FAT32) and security (HPFS386)
475 static const wxChar
*szMktempSuffix
= wxT("XXX");
476 m_strTemp
<< strName
<< szMktempSuffix
;
477 ::DosCreateDir(m_strTemp
.GetWriteBuf(MAX_PATH
), NULL
);
480 wxSplitPath(strName
, &strPath
, NULL
, NULL
);
481 if ( strPath
.IsEmpty() )
482 strPath
= wxT('.'); // GetTempFileName will fail if we give it empty string
484 if ( !GetTempFileName(strPath
, wxT("wx_"),0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
486 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
487 if ( !GetTempFileName((BYTE
) (DWORD
)(const wxChar
*) strPath
, wxT("wx_"),0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
489 wxLogLastError(wxT("GetTempFileName"));
490 m_strTemp
.UngetWriteBuf();
491 #endif // Windows/Unix
493 int access
= wxS_DEFAULT
;
495 // create the file with the same mode as the original one under Unix
496 mode_t umaskOld
= 0; // just to suppress compiler warning
500 if ( stat(strName
.fn_str(), &st
) == 0 )
502 // this assumes that only lower bits of st_mode contain the access
503 // rights, but it's true for at least all Unices which have S_IXXXX()
504 // macros, so should not be less portable than using (not POSIX)
506 access
= st
.st_mode
& 0777;
508 // we want to create the file with exactly the same access rights as
509 // the original one, so disable the user's umask for the moment
515 // file probably didn't exist, just create with default mode _using_
516 // user's umask (new files creation should respet umask)
517 changedUmask
= FALSE
;
521 bool ok
= m_file
.Open(m_strTemp
, wxFile::write
, access
);
526 // restore umask now that the file is created
527 (void)umask(umaskOld
);
534 // ----------------------------------------------------------------------------
536 // ----------------------------------------------------------------------------
538 wxTempFile::~wxTempFile()
544 bool wxTempFile::Commit()
549 if ( wxFile::Exists(m_strName
) && wxRemove(m_strName
) != 0 ) {
550 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
554 if ( wxRename(m_strTemp
, m_strName
) != 0 ) {
555 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
559 if ( wxFile::Exists(m_strName
) && remove(wxUnix2MacFilename( m_strName
)) != 0 ) {
560 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
564 if ( rename(wxUnix2MacFilename( m_strTemp
), wxUnix2MacFilename( m_strName
)) != 0 ) {
565 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
573 void wxTempFile::Discard()
577 if ( wxRemove(m_strTemp
) != 0 )
578 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());
580 if ( remove( wxUnix2MacFilename(m_strTemp
.fn_str())) != 0 )
581 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());