]>
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(__DOS__) && defined(__WATCOMC__)
64 #elif (defined(__WXPM__))
68 #elif (defined(__WXSTUBS__))
69 // Have to ifdef this for different environments
71 #elif (defined(__WXMAC__))
73 int access( const char *path
, int mode
) { return 0 ; }
75 int _access( const char *path
, int mode
) { return 0 ; }
77 char* mktemp( char * path
) { return path
;}
83 #error "Please specify the header with file functions declarations."
86 #include <stdio.h> // SEEK_xxx constants
87 #include <fcntl.h> // O_RDONLY &c
90 #include <sys/types.h> // needed for stat
91 #include <sys/stat.h> // stat
92 #elif ( defined(__MWERKS__) && defined(__WXMSW__) )
93 #include <sys/types.h> // needed for stat
94 #include <sys/stat.h> // stat
97 #if defined(__BORLANDC__) || defined(_MSC_VER)
102 // there is no distinction between text and binary files under Unix, so define
103 // O_BINARY as 0 if the system headers don't do it already
104 #if defined(__UNIX__) && !defined(O_BINARY)
116 // some broken compilers don't have 3rd argument in open() and creat()
118 #define ACCESS(access)
120 #else // normal compiler
121 #define ACCESS(access) , (access)
126 #include "wx/string.h"
129 #endif // !WX_PRECOMP
131 #include "wx/filename.h"
134 // ============================================================================
135 // implementation of wxFile
136 // ============================================================================
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
141 bool wxFile::Exists(const wxChar
*name
)
144 #if wxUSE_UNICODE && wxMBFILES
145 wxCharBuffer fname
= wxConvFile
.cWC2MB(name
);
147 return !wxAccess(fname
, 0) &&
148 !wxStat(wxMBSTRINGCAST fname
, &st
) &&
149 (st
.st_mode
& S_IFREG
);
152 return !wxAccess(name
, 0) &&
153 !wxStat(name
, &st
) &&
154 (st
.st_mode
& S_IFREG
);
158 bool wxFile::Access(const wxChar
*name
, OpenMode mode
)
172 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
175 return wxAccess(wxFNCONV(name
), how
) == 0;
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
183 wxFile::wxFile(const wxChar
*szFileName
, OpenMode mode
)
188 Open(szFileName
, mode
);
191 // create the file, fail if it already exists and bOverwrite
192 bool wxFile::Create(const wxChar
*szFileName
, bool bOverwrite
, int accessMode
)
194 // if bOverwrite we create a new file or truncate the existing one,
195 // otherwise we only create the new file and fail if it already exists
196 #if defined(__WXMAC__) && !defined(__UNIX__)
197 // Dominic Mazzoni [dmazzoni+@cs.cmu.edu] reports that open is still broken on the mac, so we replace
198 // int fd = open(wxUnix2MacFilename( szFileName ), O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
199 int fd
= creat( szFileName
, accessMode
);
201 int fd
= wxOpen(wxFNCONV(szFileName
),
202 O_BINARY
| O_WRONLY
| O_CREAT
|
203 (bOverwrite
? O_TRUNC
: O_EXCL
)
207 wxLogSysError(_("can't create file '%s'"), szFileName
);
217 bool wxFile::Open(const wxChar
*szFileName
, OpenMode mode
, int accessMode
)
219 int flags
= O_BINARY
;
227 if ( wxFile::Exists(szFileName
) )
229 flags
|= O_WRONLY
| O_APPEND
;
232 //else: fall through as write_append is the same as write if the
233 // file doesn't exist
236 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
240 flags
|= O_WRONLY
| O_CREAT
| O_EXCL
;
248 int fd
= wxOpen(wxFNCONV(szFileName
), flags
ACCESS(accessMode
));
250 wxLogSysError(_("can't open file '%s'"), szFileName
);
263 if ( close(m_fd
) == -1 ) {
264 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
275 // ----------------------------------------------------------------------------
277 // ----------------------------------------------------------------------------
280 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
282 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
285 int iRc
= ::read(m_fd
, (char*) pBuf
, nCount
);
287 int iRc
= ::read(m_fd
, pBuf
, nCount
);
290 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
291 return wxInvalidOffset
;
298 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
300 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
303 #if __MSL__ >= 0x6000
304 int iRc
= ::write(m_fd
, (void*) pBuf
, nCount
);
306 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 ( wxFsync(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
= wxTell(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
= wxTell(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 descriptor %d"), m_fd
);
451 wxFAIL_MSG(_("invalid eof() return value."));
457 // ============================================================================
458 // implementation of wxTempFile
459 // ============================================================================
461 // ----------------------------------------------------------------------------
463 // ----------------------------------------------------------------------------
464 wxTempFile::wxTempFile(const wxString
& strName
)
469 bool wxTempFile::Open(const wxString
& strName
)
473 m_strTemp
= wxFileName::CreateTempFileName(strName
);
475 if ( m_strTemp
.empty() )
477 // CreateTempFileName() failed
481 // actually open the file now (it must already exist)
482 if ( !m_file
.Open(m_strTemp
, wxFile::write
) )
484 // opening existing file failed?
489 // the temp file should have the same permissions as the original one
493 if ( stat(strName
.fn_str(), &st
) == 0 )
499 // file probably didn't exist, just give it the default mode _using_
500 // user's umask (new files creation should respect umask)
501 mode_t mask
= umask(0777);
506 if ( chmod(m_strTemp
.mb_str(), mode
) == -1 )
508 wxLogSysError(_("Failed to set temporary file permissions"));
515 // ----------------------------------------------------------------------------
517 // ----------------------------------------------------------------------------
519 wxTempFile::~wxTempFile()
525 bool wxTempFile::Commit()
529 if ( wxFile::Exists(m_strName
) && wxRemove(m_strName
) != 0 ) {
530 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
534 if ( wxRename(m_strTemp
, m_strName
) != 0 ) {
535 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
542 void wxTempFile::Discard()
545 if ( wxRemove(m_strTemp
) != 0 )
546 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());