]>
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 #elif (defined(__UNIX__) || defined(__GNUWIN32__))
62 #elif (defined(__WXPM__))
66 #elif (defined(__WXSTUBS__))
67 // Have to ifdef this for different environments
69 #elif (defined(__WXMAC__))
71 int access( const char *path
, int mode
) { return 0 ; }
73 int _access( const char *path
, int mode
) { return 0 ; }
75 char* mktemp( char * path
) { return path
;}
81 #error "Please specify the header with file functions declarations."
84 #include <stdio.h> // SEEK_xxx constants
85 #include <fcntl.h> // O_RDONLY &c
88 #include <sys/types.h> // needed for stat
89 #include <sys/stat.h> // stat
90 #elif ( defined(__MWERKS__) && defined(__WXMSW__) )
91 #include <sys/types.h> // needed for stat
92 #include <sys/stat.h> // stat
95 #if defined(__BORLANDC__) || defined(_MSC_VER)
100 // there is no distinction between text and binary files under Unix, so define
101 // O_BINARY as 0 if the system headers don't do it already
102 #if defined(__UNIX__) && !defined(O_BINARY)
114 // some broken compilers don't have 3rd argument in open() and creat()
116 #define ACCESS(access)
118 #else // normal compiler
119 #define ACCESS(access) , (access)
124 #include "wx/string.h"
127 #endif // !WX_PRECOMP
129 #include "wx/filename.h"
132 // ============================================================================
133 // implementation of wxFile
134 // ============================================================================
136 // ----------------------------------------------------------------------------
138 // ----------------------------------------------------------------------------
139 bool wxFile::Exists(const wxChar
*name
)
142 #if wxUSE_UNICODE && wxMBFILES
143 wxCharBuffer fname
= wxConvFile
.cWC2MB(name
);
145 return !wxAccess(fname
, 0) &&
146 !wxStat(wxMBSTRINGCAST fname
, &st
) &&
147 (st
.st_mode
& S_IFREG
);
150 return !wxAccess(name
, 0) &&
151 !wxStat(name
, &st
) &&
152 (st
.st_mode
& S_IFREG
);
156 bool wxFile::Access(const wxChar
*name
, OpenMode mode
)
170 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
173 return wxAccess(wxFNCONV(name
), how
) == 0;
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
181 wxFile::wxFile(const wxChar
*szFileName
, OpenMode mode
)
186 Open(szFileName
, mode
);
189 // create the file, fail if it already exists and bOverwrite
190 bool wxFile::Create(const wxChar
*szFileName
, bool bOverwrite
, int accessMode
)
192 // if bOverwrite we create a new file or truncate the existing one,
193 // otherwise we only create the new file and fail if it already exists
194 #if defined(__WXMAC__) && !defined(__UNIX__)
195 // Dominic Mazzoni [dmazzoni+@cs.cmu.edu] reports that open is still broken on the mac, so we replace
196 // int fd = open(wxUnix2MacFilename( szFileName ), O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
197 int fd
= creat( szFileName
, accessMode
);
199 int fd
= wxOpen(wxFNCONV(szFileName
),
200 O_BINARY
| O_WRONLY
| O_CREAT
|
201 (bOverwrite
? O_TRUNC
: O_EXCL
)
205 wxLogSysError(_("can't create file '%s'"), szFileName
);
215 bool wxFile::Open(const wxChar
*szFileName
, OpenMode mode
, int accessMode
)
217 int flags
= O_BINARY
;
225 if ( wxFile::Exists(szFileName
) )
227 flags
|= O_WRONLY
| O_APPEND
;
230 //else: fall through as write_append is the same as write if the
231 // file doesn't exist
234 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
238 flags
|= O_WRONLY
| O_CREAT
| O_EXCL
;
246 int fd
= wxOpen(wxFNCONV(szFileName
), flags
ACCESS(accessMode
));
248 wxLogSysError(_("can't open file '%s'"), szFileName
);
261 if ( close(m_fd
) == -1 ) {
262 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
273 // ----------------------------------------------------------------------------
275 // ----------------------------------------------------------------------------
278 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
280 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
283 int iRc
= ::read(m_fd
, (char*) pBuf
, nCount
);
285 int iRc
= ::read(m_fd
, pBuf
, nCount
);
288 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
289 return wxInvalidOffset
;
296 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
298 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
301 #if __MSL__ >= 0x6000
302 int iRc
= ::write(m_fd
, (void*) pBuf
, nCount
);
304 int iRc
= ::write(m_fd
, (const char*) pBuf
, nCount
);
307 int iRc
= ::write(m_fd
, pBuf
, nCount
);
310 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
322 #if defined(__VISUALC__) || wxHAVE_FSYNC
323 if ( wxFsync(m_fd
) == -1 )
325 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
336 // ----------------------------------------------------------------------------
338 // ----------------------------------------------------------------------------
341 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
343 wxASSERT( IsOpened() );
348 wxFAIL_MSG(_("unknown seek origin"));
363 int iRc
= lseek(m_fd
, ofs
, origin
);
365 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
366 return wxInvalidOffset
;
373 off_t
wxFile::Tell() const
375 wxASSERT( IsOpened() );
377 int iRc
= wxTell(m_fd
);
379 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
380 return wxInvalidOffset
;
386 // get current file length
387 off_t
wxFile::Length() const
389 wxASSERT( IsOpened() );
392 int iRc
= _filelength(m_fd
);
394 int iRc
= wxTell(m_fd
);
396 // @ have to use const_cast :-(
397 int iLen
= ((wxFile
*)this)->SeekEnd();
399 // restore old position
400 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
411 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
412 return wxInvalidOffset
;
418 // is end of file reached?
419 bool wxFile::Eof() const
421 wxASSERT( IsOpened() );
425 #if defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
426 // @@ this doesn't work, of course, on unseekable file descriptors
427 off_t ofsCur
= Tell(),
429 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
432 iRc
= ofsCur
== ofsMax
;
433 #else // Windows and "native" compiler
435 #endif // Windows/Unix
445 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd
);
449 wxFAIL_MSG(_("invalid eof() return value."));
455 // ============================================================================
456 // implementation of wxTempFile
457 // ============================================================================
459 // ----------------------------------------------------------------------------
461 // ----------------------------------------------------------------------------
462 wxTempFile::wxTempFile(const wxString
& strName
)
467 bool wxTempFile::Open(const wxString
& strName
)
471 m_strTemp
= wxFileName::CreateTempFileName(strName
);
473 if ( m_strTemp
.empty() )
475 // CreateTempFileName() failed
479 // actually open the file now (it must already exist)
480 if ( !m_file
.Open(m_strTemp
, wxFile::write
) )
482 // opening existing file failed?
487 // the temp file should have the same permissions as the original one
491 if ( stat(strName
.fn_str(), &st
) == 0 )
497 // file probably didn't exist, just give it the default mode _using_
498 // user's umask (new files creation should respect umask)
499 mode_t mask
= umask(0777);
504 if ( chmod(m_strTemp
.mb_str(), mode
) == -1 )
506 wxLogSysError(_("Failed to set temporary file permissions"));
513 // ----------------------------------------------------------------------------
515 // ----------------------------------------------------------------------------
517 wxTempFile::~wxTempFile()
523 bool wxTempFile::Commit()
527 if ( wxFile::Exists(m_strName
) && wxRemove(m_strName
) != 0 ) {
528 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
532 if ( wxRename(m_strTemp
, m_strName
) != 0 ) {
533 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
540 void wxTempFile::Discard()
543 if ( wxRemove(m_strTemp
) != 0 )
544 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());