]>
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__)
63 #if defined(__WATCOMC__)
65 #elif defined(__DJGPP__)
70 #error "Please specify the header with file functions declarations."
72 #elif (defined(__WXPM__))
76 #elif (defined(__WXSTUBS__))
77 // Have to ifdef this for different environments
79 #elif (defined(__WXMAC__))
81 int access( const char *path
, int mode
) { return 0 ; }
83 int _access( const char *path
, int mode
) { return 0 ; }
85 char* mktemp( char * path
) { return path
;}
91 #error "Please specify the header with file functions declarations."
94 #include <stdio.h> // SEEK_xxx constants
95 #include <fcntl.h> // O_RDONLY &c
98 #include <sys/types.h> // needed for stat
99 #include <sys/stat.h> // stat
100 #elif ( defined(__MWERKS__) && defined(__WXMSW__) )
101 #include <sys/types.h> // needed for stat
102 #include <sys/stat.h> // stat
105 #if defined(__BORLANDC__) || defined(_MSC_VER)
110 // there is no distinction between text and binary files under Unix, so define
111 // O_BINARY as 0 if the system headers don't do it already
112 #if defined(__UNIX__) && !defined(O_BINARY)
124 // some broken compilers don't have 3rd argument in open() and creat()
126 #define ACCESS(access)
128 #else // normal compiler
129 #define ACCESS(access) , (access)
134 #include "wx/string.h"
137 #endif // !WX_PRECOMP
139 #include "wx/filename.h"
143 #include "wx/msw/mslu.h"
146 // ============================================================================
147 // implementation of wxFile
148 // ============================================================================
150 // ----------------------------------------------------------------------------
152 // ----------------------------------------------------------------------------
153 bool wxFile::Exists(const wxChar
*name
)
156 #if wxUSE_UNICODE && wxMBFILES
157 wxCharBuffer fname
= wxConvFile
.cWC2MB(name
);
159 return !wxAccess(fname
, 0) &&
160 !wxStat(wxMBSTRINGCAST fname
, &st
) &&
161 (st
.st_mode
& S_IFREG
);
164 return !wxAccess(name
, 0) &&
165 !wxStat(name
, &st
) &&
166 (st
.st_mode
& S_IFREG
);
170 bool wxFile::Access(const wxChar
*name
, OpenMode mode
)
184 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
187 return wxAccess(wxFNCONV(name
), how
) == 0;
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
195 wxFile::wxFile(const wxChar
*szFileName
, OpenMode mode
)
200 Open(szFileName
, mode
);
203 // create the file, fail if it already exists and bOverwrite
204 bool wxFile::Create(const wxChar
*szFileName
, bool bOverwrite
, int accessMode
)
206 // if bOverwrite we create a new file or truncate the existing one,
207 // otherwise we only create the new file and fail if it already exists
208 #if defined(__WXMAC__) && !defined(__UNIX__)
209 // Dominic Mazzoni [dmazzoni+@cs.cmu.edu] reports that open is still broken on the mac, so we replace
210 // int fd = open(wxUnix2MacFilename( szFileName ), O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
211 int fd
= creat( szFileName
, accessMode
);
213 int fd
= wxOpen(wxFNCONV(szFileName
),
214 O_BINARY
| O_WRONLY
| O_CREAT
|
215 (bOverwrite
? O_TRUNC
: O_EXCL
)
219 wxLogSysError(_("can't create file '%s'"), szFileName
);
229 bool wxFile::Open(const wxChar
*szFileName
, OpenMode mode
, int accessMode
)
231 int flags
= O_BINARY
;
239 if ( wxFile::Exists(szFileName
) )
241 flags
|= O_WRONLY
| O_APPEND
;
244 //else: fall through as write_append is the same as write if the
245 // file doesn't exist
248 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
252 flags
|= O_WRONLY
| O_CREAT
| O_EXCL
;
260 int fd
= wxOpen(wxFNCONV(szFileName
), flags
ACCESS(accessMode
));
262 wxLogSysError(_("can't open file '%s'"), szFileName
);
275 if ( close(m_fd
) == -1 ) {
276 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
287 // ----------------------------------------------------------------------------
289 // ----------------------------------------------------------------------------
292 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
294 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
297 int iRc
= ::read(m_fd
, (char*) pBuf
, nCount
);
299 int iRc
= ::read(m_fd
, pBuf
, nCount
);
302 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
303 return wxInvalidOffset
;
310 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
312 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
315 #if __MSL__ >= 0x6000
316 int iRc
= ::write(m_fd
, (void*) pBuf
, nCount
);
318 int iRc
= ::write(m_fd
, (const char*) pBuf
, nCount
);
321 int iRc
= ::write(m_fd
, pBuf
, nCount
);
324 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
336 #if defined(__VISUALC__) || wxHAVE_FSYNC
337 if ( wxFsync(m_fd
) == -1 )
339 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
350 // ----------------------------------------------------------------------------
352 // ----------------------------------------------------------------------------
355 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
357 wxASSERT( IsOpened() );
362 wxFAIL_MSG(_("unknown seek origin"));
377 int iRc
= lseek(m_fd
, ofs
, origin
);
379 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
380 return wxInvalidOffset
;
387 off_t
wxFile::Tell() const
389 wxASSERT( IsOpened() );
391 int iRc
= wxTell(m_fd
);
393 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
394 return wxInvalidOffset
;
400 // get current file length
401 off_t
wxFile::Length() const
403 wxASSERT( IsOpened() );
406 int iRc
= _filelength(m_fd
);
408 int iRc
= wxTell(m_fd
);
410 // @ have to use const_cast :-(
411 int iLen
= ((wxFile
*)this)->SeekEnd();
413 // restore old position
414 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
425 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
426 return wxInvalidOffset
;
432 // is end of file reached?
433 bool wxFile::Eof() const
435 wxASSERT( IsOpened() );
439 #if defined(__DOS__) || defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
440 // @@ this doesn't work, of course, on unseekable file descriptors
441 off_t ofsCur
= Tell(),
443 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
446 iRc
= ofsCur
== ofsMax
;
447 #else // Windows and "native" compiler
449 #endif // Windows/Unix
459 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd
);
463 wxFAIL_MSG(_("invalid eof() return value."));
469 // ============================================================================
470 // implementation of wxTempFile
471 // ============================================================================
473 // ----------------------------------------------------------------------------
475 // ----------------------------------------------------------------------------
477 wxTempFile::wxTempFile(const wxString
& strName
)
482 bool wxTempFile::Open(const wxString
& strName
)
484 // we must have an absolute filename because otherwise CreateTempFileName()
485 // would create the temp file in $TMP (i.e. the system standard location
486 // for the temp files) which might be on another volume/drive/mount and
487 // wxRename()ing it later to m_strName from Commit() would then fail
489 // with the absolute filename, the temp file is created in the same
490 // directory as this one which ensures that wxRename() may work later
491 wxFileName
fn(strName
);
492 if ( !fn
.IsAbsolute() )
494 fn
.Normalize(wxPATH_NORM_ABSOLUTE
);
497 m_strName
= fn
.GetFullPath();
499 m_strTemp
= wxFileName::CreateTempFileName(m_strName
, &m_file
);
501 if ( m_strTemp
.empty() )
503 // CreateTempFileName() failed
508 // the temp file should have the same permissions as the original one
512 if ( stat(m_strName
.fn_str(), &st
) == 0 )
518 // file probably didn't exist, just give it the default mode _using_
519 // user's umask (new files creation should respect umask)
520 mode_t mask
= umask(0777);
525 if ( chmod(m_strTemp
.mb_str(), mode
) == -1 )
527 wxLogSysError(_("Failed to set temporary file permissions"));
534 // ----------------------------------------------------------------------------
536 // ----------------------------------------------------------------------------
538 wxTempFile::~wxTempFile()
544 bool wxTempFile::Commit()
548 if ( wxFile::Exists(m_strName
) && wxRemove(m_strName
) != 0 ) {
549 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
553 if ( wxRename(m_strTemp
, m_strName
) != 0 ) {
554 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
561 void wxTempFile::Discard()
564 if ( wxRemove(m_strTemp
) != 0 )
565 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());