]>
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
91 #elif ( defined(__MWERKS__) && defined(__WXMSW__) )
92 #include <sys/types.h> // needed for stat
93 #include <sys/stat.h> // stat
96 #if defined(__BORLANDC__) || defined(_MSC_VER)
101 // there is no distinction between text and binary files under Unix, so define
102 // O_BINARY as 0 if the system headers don't do it already
103 #if defined(__UNIX__) && !defined(O_BINARY)
115 // some broken compilers don't have 3rd argument in open() and creat()
117 #define ACCESS(access)
119 #else // normal compiler
120 #define ACCESS(access) , (access)
124 #include "wx/string.h"
129 // ============================================================================
130 // implementation of wxFile
131 // ============================================================================
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
136 bool wxFile::Exists(const wxChar
*name
)
139 #if wxUSE_UNICODE && wxMBFILES
140 wxCharBuffer fname
= wxConvFile
.cWC2MB(name
);
142 return !wxAccess(fname
, 0) &&
143 !wxStat(wxMBSTRINGCAST fname
, &st
) &&
144 (st
.st_mode
& S_IFREG
);
147 return !wxAccess(name
, 0) &&
148 !wxStat(name
, &st
) &&
149 (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
191 #if defined(__WXMAC__) && !defined(__UNIX__)
192 // Dominic Mazzoni [dmazzoni+@cs.cmu.edu] reports that open is still broken on the mac, so we replace
193 // int fd = open(wxUnix2MacFilename( szFileName ), O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
194 int fd
= creat( szFileName
, accessMode
);
196 int fd
= wxOpen(wxFNCONV(szFileName
),
197 O_BINARY
| O_WRONLY
| O_CREAT
|
198 (bOverwrite
? O_TRUNC
: O_EXCL
)
202 wxLogSysError(_("can't create file '%s'"), szFileName
);
212 bool wxFile::Open(const wxChar
*szFileName
, OpenMode mode
, int accessMode
)
214 int flags
= O_BINARY
;
222 if ( wxFile::Exists(szFileName
) )
224 flags
|= O_WRONLY
| O_APPEND
;
227 //else: fall through as write_append is the same as write if the
228 // file doesn't exist
231 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
239 int fd
= wxOpen(wxFNCONV(szFileName
), flags
ACCESS(accessMode
));
241 wxLogSysError(_("can't open file '%s'"), szFileName
);
254 if ( close(m_fd
) == -1 ) {
255 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
266 // ----------------------------------------------------------------------------
268 // ----------------------------------------------------------------------------
271 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
273 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
276 int iRc
= ::read(m_fd
, (char*) pBuf
, nCount
);
278 int iRc
= ::read(m_fd
, pBuf
, nCount
);
281 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
282 return wxInvalidOffset
;
289 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
291 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
294 #if __MSL__ >= 0x6000
295 int iRc
= ::write(m_fd
, (void*) pBuf
, nCount
);
297 int iRc
= ::write(m_fd
, (const char*) pBuf
, nCount
);
300 int iRc
= ::write(m_fd
, pBuf
, nCount
);
303 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
315 #if defined(__VISUALC__) || wxHAVE_FSYNC
316 if ( wxFsync(m_fd
) == -1 )
318 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
329 // ----------------------------------------------------------------------------
331 // ----------------------------------------------------------------------------
334 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
336 wxASSERT( IsOpened() );
341 wxFAIL_MSG(_("unknown seek origin"));
356 int iRc
= lseek(m_fd
, ofs
, origin
);
358 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
359 return wxInvalidOffset
;
366 off_t
wxFile::Tell() const
368 wxASSERT( IsOpened() );
370 int iRc
= wxTell(m_fd
);
372 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
373 return wxInvalidOffset
;
379 // get current file length
380 off_t
wxFile::Length() const
382 wxASSERT( IsOpened() );
385 int iRc
= _filelength(m_fd
);
387 int iRc
= wxTell(m_fd
);
389 // @ have to use const_cast :-(
390 int iLen
= ((wxFile
*)this)->SeekEnd();
392 // restore old position
393 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
404 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
405 return wxInvalidOffset
;
411 // is end of file reached?
412 bool wxFile::Eof() const
414 wxASSERT( IsOpened() );
418 #if defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
419 // @@ this doesn't work, of course, on unseekable file descriptors
420 off_t ofsCur
= Tell(),
422 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
425 iRc
= ofsCur
== ofsMax
;
426 #else // Windows and "native" compiler
428 #endif // Windows/Unix
438 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd
);
442 wxFAIL_MSG(_("invalid eof() return value."));
448 // ============================================================================
449 // implementation of wxTempFile
450 // ============================================================================
452 // ----------------------------------------------------------------------------
454 // ----------------------------------------------------------------------------
455 wxTempFile::wxTempFile(const wxString
& strName
)
460 bool wxTempFile::Open(const wxString
& strName
)
464 // we want to create the file in the same directory as strName because
465 // otherwise rename() in Commit() might not work (if the files are on
466 // different partitions for example). Unfortunately, the only standard
467 // (POSIX) temp file creation function tmpnam() can't do it.
468 #if defined(__UNIX__) || defined(__WXSTUBS__)|| defined( __WXMAC__ )
469 static const wxChar
*szMktempSuffix
= wxT("XXXXXX");
470 m_strTemp
<< strName
<< szMktempSuffix
;
471 // can use the cast because length doesn't change
472 mktemp(wxMBSTRINGCAST m_strTemp
.mb_str());
473 #elif defined(__WXPM__)
474 // for now just create a file
475 // future enhancements can be to set some extended attributes for file systems
476 // OS/2 supports that have them (HPFS, FAT32) and security (HPFS386)
477 static const wxChar
*szMktempSuffix
= wxT("XXX");
478 m_strTemp
<< strName
<< szMktempSuffix
;
479 // Temporarily remove - MN
481 ::DosCreateDir(m_strTemp
.GetWriteBuf(MAX_PATH
), NULL
);
485 wxSplitPath(strName
, &strPath
, NULL
, NULL
);
486 if ( strPath
.IsEmpty() )
487 strPath
= wxT('.'); // GetTempFileName will fail if we give it empty string
489 if ( !GetTempFileName(strPath
, wxT("wx_"),0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
491 // Not sure why MSVC++ 1.5 header defines first param as BYTE - bug?
492 if ( !GetTempFileName((BYTE
) (DWORD
)(const wxChar
*) strPath
, wxT("wx_"),0, m_strTemp
.GetWriteBuf(MAX_PATH
)) )
494 wxLogLastError(wxT("GetTempFileName"));
495 m_strTemp
.UngetWriteBuf();
496 #endif // Windows/Unix
498 int access
= wxS_DEFAULT
;
500 // create the file with the same mode as the original one under Unix
501 mode_t umaskOld
= 0; // just to suppress compiler warning
505 if ( stat(strName
.fn_str(), &st
) == 0 )
507 // this assumes that only lower bits of st_mode contain the access
508 // rights, but it's true for at least all Unices which have S_IXXXX()
509 // macros, so should not be less portable than using (not POSIX)
511 access
= st
.st_mode
& 0777;
513 // we want to create the file with exactly the same access rights as
514 // the original one, so disable the user's umask for the moment
520 // file probably didn't exist, just create with default mode _using_
521 // user's umask (new files creation should respet umask)
522 changedUmask
= FALSE
;
526 bool ok
= m_file
.Open(m_strTemp
, wxFile::write
, access
);
531 // restore umask now that the file is created
532 (void)umask(umaskOld
);
539 // ----------------------------------------------------------------------------
541 // ----------------------------------------------------------------------------
543 wxTempFile::~wxTempFile()
549 bool wxTempFile::Commit()
553 if ( wxFile::Exists(m_strName
) && wxRemove(m_strName
) != 0 ) {
554 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
558 if ( wxRename(m_strTemp
, m_strName
) != 0 ) {
559 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
566 void wxTempFile::Discard()
569 if ( wxRemove(m_strTemp
) != 0 )
570 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());