]>
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__) && !defined(__WXMICROWIN__)
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
);
139 return !wxAccess(fname
, 0) &&
140 !wxStat(wxMBSTRINGCAST fname
, &st
) &&
141 (st
.st_mode
& S_IFREG
);
144 return !wxAccess(name
, 0) &&
145 !wxStat(name
, &st
) &&
146 (st
.st_mode
& S_IFREG
);
150 bool wxFile::Access(const wxChar
*name
, OpenMode mode
)
164 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
167 return wxAccess(wxFNCONV(name
), how
) == 0;
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
175 wxFile::wxFile(const wxChar
*szFileName
, OpenMode mode
)
180 Open(szFileName
, mode
);
183 // create the file, fail if it already exists and bOverwrite
184 bool wxFile::Create(const wxChar
*szFileName
, bool bOverwrite
, int accessMode
)
186 // if bOverwrite we create a new file or truncate the existing one,
187 // otherwise we only create the new file and fail if it already exists
188 #if defined(__WXMAC__) && !defined(__UNIX__)
189 // Dominic Mazzoni [dmazzoni+@cs.cmu.edu] reports that open is still broken on the mac, so we replace
190 // int fd = open(wxUnix2MacFilename( szFileName ), O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
191 int fd
= creat( szFileName
, accessMode
);
193 int fd
= wxOpen(wxFNCONV(szFileName
),
194 O_BINARY
| O_WRONLY
| O_CREAT
|
195 (bOverwrite
? O_TRUNC
: O_EXCL
)
199 wxLogSysError(_("can't create file '%s'"), szFileName
);
209 bool wxFile::Open(const wxChar
*szFileName
, OpenMode mode
, int accessMode
)
211 int flags
= O_BINARY
;
219 if ( wxFile::Exists(szFileName
) )
221 flags
|= O_WRONLY
| O_APPEND
;
224 //else: fall through as write_append is the same as write if the
225 // file doesn't exist
228 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
236 int fd
= wxOpen(wxFNCONV(szFileName
), flags
ACCESS(accessMode
));
238 wxLogSysError(_("can't open file '%s'"), szFileName
);
251 if ( close(m_fd
) == -1 ) {
252 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
263 // ----------------------------------------------------------------------------
265 // ----------------------------------------------------------------------------
268 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
270 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
273 int iRc
= ::read(m_fd
, (char*) pBuf
, nCount
);
275 int iRc
= ::read(m_fd
, pBuf
, nCount
);
278 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
279 return wxInvalidOffset
;
286 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
288 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
291 #if __MSL__ >= 0x6000
292 int iRc
= ::write(m_fd
, (void*) pBuf
, nCount
);
294 int iRc
= ::write(m_fd
, (const char*) pBuf
, nCount
);
297 int iRc
= ::write(m_fd
, pBuf
, nCount
);
300 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
312 #if defined(__VISUALC__) || wxHAVE_FSYNC
313 if ( wxFsync(m_fd
) == -1 )
315 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
326 // ----------------------------------------------------------------------------
328 // ----------------------------------------------------------------------------
331 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
333 wxASSERT( IsOpened() );
338 wxFAIL_MSG(_("unknown seek origin"));
353 int iRc
= lseek(m_fd
, ofs
, origin
);
355 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
356 return wxInvalidOffset
;
363 off_t
wxFile::Tell() const
365 wxASSERT( IsOpened() );
367 int iRc
= wxTell(m_fd
);
369 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
370 return wxInvalidOffset
;
376 // get current file length
377 off_t
wxFile::Length() const
379 wxASSERT( IsOpened() );
382 int iRc
= _filelength(m_fd
);
384 int iRc
= wxTell(m_fd
);
386 // @ have to use const_cast :-(
387 int iLen
= ((wxFile
*)this)->SeekEnd();
389 // restore old position
390 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
401 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
402 return wxInvalidOffset
;
408 // is end of file reached?
409 bool wxFile::Eof() const
411 wxASSERT( IsOpened() );
415 #if defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
416 // @@ this doesn't work, of course, on unseekable file descriptors
417 off_t ofsCur
= Tell(),
419 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
422 iRc
= ofsCur
== ofsMax
;
423 #else // Windows and "native" compiler
425 #endif // Windows/Unix
435 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd
);
439 wxFAIL_MSG(_("invalid eof() return value."));
445 // ============================================================================
446 // implementation of wxTempFile
447 // ============================================================================
449 // ----------------------------------------------------------------------------
451 // ----------------------------------------------------------------------------
452 wxTempFile::wxTempFile(const wxString
& strName
)
457 bool wxTempFile::Open(const wxString
& strName
)
461 // we want to create the file in the same directory as strName because
462 // otherwise rename() in Commit() might not work (if the files are on
463 // different partitions for example). Unfortunately, the only standard
464 // (POSIX) temp file creation function tmpnam() can't do it.
465 #if defined(__UNIX__) || defined(__WXSTUBS__)|| defined( __WXMAC__ )
466 static const wxChar
*szMktempSuffix
= wxT("XXXXXX");
467 m_strTemp
<< strName
<< szMktempSuffix
;
468 // can use the cast because length doesn't change
469 mktemp(wxMBSTRINGCAST m_strTemp
.mb_str());
470 #elif defined(__WXPM__)
471 // for now just create a file
472 // future enhancements can be to set some extended attributes for file systems
473 // OS/2 supports that have them (HPFS, FAT32) and security (HPFS386)
474 static const wxChar
*szMktempSuffix
= wxT("XXX");
475 m_strTemp
<< strName
<< szMktempSuffix
;
476 ::DosCreateDir(m_strTemp
.GetWriteBuf(MAX_PATH
), NULL
);
479 wxSplitPath(strName
, &strPath
, NULL
, NULL
);
480 if ( strPath
.IsEmpty() )
481 strPath
= wxT('.'); // GetTempFileName will fail if we give it empty string
483 if ( !GetTempFileName(strPath
, wxT("wx_"),0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
485 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
486 if ( !GetTempFileName((BYTE
) (DWORD
)(const wxChar
*) strPath
, wxT("wx_"),0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
488 wxLogLastError(wxT("GetTempFileName"));
489 m_strTemp
.UngetWriteBuf();
490 #endif // Windows/Unix
492 int access
= wxS_DEFAULT
;
494 // create the file with the same mode as the original one under Unix
495 mode_t umaskOld
= 0; // just to suppress compiler warning
499 if ( stat(strName
.fn_str(), &st
) == 0 )
501 // this assumes that only lower bits of st_mode contain the access
502 // rights, but it's true for at least all Unices which have S_IXXXX()
503 // macros, so should not be less portable than using (not POSIX)
505 access
= st
.st_mode
& 0777;
507 // we want to create the file with exactly the same access rights as
508 // the original one, so disable the user's umask for the moment
514 // file probably didn't exist, just create with default mode _using_
515 // user's umask (new files creation should respet umask)
516 changedUmask
= FALSE
;
520 bool ok
= m_file
.Open(m_strTemp
, wxFile::write
, access
);
525 // restore umask now that the file is created
526 (void)umask(umaskOld
);
533 // ----------------------------------------------------------------------------
535 // ----------------------------------------------------------------------------
537 wxTempFile::~wxTempFile()
543 bool wxTempFile::Commit()
547 if ( wxFile::Exists(m_strName
) && wxRemove(m_strName
) != 0 ) {
548 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
552 if ( wxRename(m_strTemp
, m_strName
) != 0 ) {
553 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
560 void wxTempFile::Discard()
563 if ( wxRemove(m_strTemp
) != 0 )
564 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());