]>
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 char* mktemp(char *path
) { return _mktemp(path
); }
65 #elif (defined(__WXPM__))
69 #elif (defined(__WXSTUBS__))
70 // Have to ifdef this for different environments
72 #elif (defined(__WXMAC__))
74 int access( const char *path
, int mode
) { return 0 ; }
76 int _access( const char *path
, int mode
) { return 0 ; }
78 char* mktemp( char * path
) { return path
;}
84 #error "Please specify the header with file functions declarations."
87 #include <stdio.h> // SEEK_xxx constants
88 #include <fcntl.h> // O_RDONLY &c
91 #include <sys/types.h> // needed for stat
92 #include <sys/stat.h> // stat
93 #elif ( defined(__MWERKS__) && defined(__WXMSW__) )
94 #include <sys/types.h> // needed for stat
95 #include <sys/stat.h> // stat
98 #if defined(__BORLANDC__) || defined(_MSC_VER)
103 // there is no distinction between text and binary files under Unix, so define
104 // O_BINARY as 0 if the system headers don't do it already
105 #if defined(__UNIX__) && !defined(O_BINARY)
117 // some broken compilers don't have 3rd argument in open() and creat()
119 #define ACCESS(access)
121 #else // normal compiler
122 #define ACCESS(access) , (access)
127 #include "wx/string.h"
130 #endif // !WX_PRECOMP
132 #include "wx/filename.h"
135 // ============================================================================
136 // implementation of wxFile
137 // ============================================================================
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
142 bool wxFile::Exists(const wxChar
*name
)
145 #if wxUSE_UNICODE && wxMBFILES
146 wxCharBuffer fname
= wxConvFile
.cWC2MB(name
);
148 return !wxAccess(fname
, 0) &&
149 !wxStat(wxMBSTRINGCAST fname
, &st
) &&
150 (st
.st_mode
& S_IFREG
);
153 return !wxAccess(name
, 0) &&
154 !wxStat(name
, &st
) &&
155 (st
.st_mode
& S_IFREG
);
159 bool wxFile::Access(const wxChar
*name
, OpenMode mode
)
173 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
176 return wxAccess(wxFNCONV(name
), how
) == 0;
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
184 wxFile::wxFile(const wxChar
*szFileName
, OpenMode mode
)
189 Open(szFileName
, mode
);
192 // create the file, fail if it already exists and bOverwrite
193 bool wxFile::Create(const wxChar
*szFileName
, bool bOverwrite
, int accessMode
)
195 // if bOverwrite we create a new file or truncate the existing one,
196 // otherwise we only create the new file and fail if it already exists
197 #if defined(__WXMAC__) && !defined(__UNIX__)
198 // Dominic Mazzoni [dmazzoni+@cs.cmu.edu] reports that open is still broken on the mac, so we replace
199 // int fd = open(wxUnix2MacFilename( szFileName ), O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
200 int fd
= creat( szFileName
, accessMode
);
202 int fd
= wxOpen(wxFNCONV(szFileName
),
203 O_BINARY
| O_WRONLY
| O_CREAT
|
204 (bOverwrite
? O_TRUNC
: O_EXCL
)
208 wxLogSysError(_("can't create file '%s'"), szFileName
);
218 bool wxFile::Open(const wxChar
*szFileName
, OpenMode mode
, int accessMode
)
220 int flags
= O_BINARY
;
228 if ( wxFile::Exists(szFileName
) )
230 flags
|= O_WRONLY
| O_APPEND
;
233 //else: fall through as write_append is the same as write if the
234 // file doesn't exist
237 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
241 flags
|= O_WRONLY
| O_CREAT
| O_EXCL
;
249 int fd
= wxOpen(wxFNCONV(szFileName
), flags
ACCESS(accessMode
));
251 wxLogSysError(_("can't open file '%s'"), szFileName
);
264 if ( close(m_fd
) == -1 ) {
265 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
276 // ----------------------------------------------------------------------------
278 // ----------------------------------------------------------------------------
281 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
283 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
286 int iRc
= ::read(m_fd
, (char*) pBuf
, nCount
);
288 int iRc
= ::read(m_fd
, pBuf
, nCount
);
291 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
292 return wxInvalidOffset
;
299 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
301 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
304 #if __MSL__ >= 0x6000
305 int iRc
= ::write(m_fd
, (void*) pBuf
, nCount
);
307 int iRc
= ::write(m_fd
, (const char*) pBuf
, nCount
);
310 int iRc
= ::write(m_fd
, pBuf
, nCount
);
313 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
325 #if defined(__VISUALC__) || wxHAVE_FSYNC
326 if ( wxFsync(m_fd
) == -1 )
328 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
339 // ----------------------------------------------------------------------------
341 // ----------------------------------------------------------------------------
344 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
346 wxASSERT( IsOpened() );
351 wxFAIL_MSG(_("unknown seek origin"));
366 int iRc
= lseek(m_fd
, ofs
, origin
);
368 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
369 return wxInvalidOffset
;
376 off_t
wxFile::Tell() const
378 wxASSERT( IsOpened() );
380 int iRc
= wxTell(m_fd
);
382 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
383 return wxInvalidOffset
;
389 // get current file length
390 off_t
wxFile::Length() const
392 wxASSERT( IsOpened() );
395 int iRc
= _filelength(m_fd
);
397 int iRc
= wxTell(m_fd
);
399 // @ have to use const_cast :-(
400 int iLen
= ((wxFile
*)this)->SeekEnd();
402 // restore old position
403 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
414 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
415 return wxInvalidOffset
;
421 // is end of file reached?
422 bool wxFile::Eof() const
424 wxASSERT( IsOpened() );
428 #if defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
429 // @@ this doesn't work, of course, on unseekable file descriptors
430 off_t ofsCur
= Tell(),
432 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
435 iRc
= ofsCur
== ofsMax
;
436 #else // Windows and "native" compiler
438 #endif // Windows/Unix
448 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd
);
452 wxFAIL_MSG(_("invalid eof() return value."));
458 // ============================================================================
459 // implementation of wxTempFile
460 // ============================================================================
462 // ----------------------------------------------------------------------------
464 // ----------------------------------------------------------------------------
465 wxTempFile::wxTempFile(const wxString
& strName
)
470 bool wxTempFile::Open(const wxString
& strName
)
474 m_strTemp
= wxFileName::CreateTempFileName(strName
);
476 if ( m_strTemp
.empty() )
478 // CreateTempFileName() failed
482 // actually open the file now (it must already exist)
483 if ( !m_file
.Open(m_strTemp
, wxFile::write
) )
485 // opening existing file failed?
490 // the temp file should have the same permissions as the original one
494 if ( stat(strName
.fn_str(), &st
) == 0 )
500 // file probably didn't exist, just give it the default mode _using_
501 // user's umask (new files creation should respect umask)
502 mode_t mask
= umask(0777);
507 if ( chmod(m_strTemp
.mb_str(), mode
) == -1 )
509 wxLogSysError(_("Failed to set temporary file permissions"));
516 // ----------------------------------------------------------------------------
518 // ----------------------------------------------------------------------------
520 wxTempFile::~wxTempFile()
526 bool wxTempFile::Commit()
530 if ( wxFile::Exists(m_strName
) && wxRemove(m_strName
) != 0 ) {
531 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
535 if ( wxRename(m_strTemp
, m_strName
) != 0 ) {
536 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
543 void wxTempFile::Discard()
546 if ( wxRemove(m_strTemp
) != 0 )
547 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());