]>
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 return !wxAccess(name
, 0) &&
157 !wxStat(name
, &st
) &&
158 (st
.st_mode
& S_IFREG
);
161 bool wxFile::Access(const wxChar
*name
, OpenMode mode
)
175 wxFAIL_MSG(wxT("bad wxFile::Access mode parameter."));
178 return wxAccess( name
, how
) == 0;
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
186 wxFile::wxFile(const wxChar
*szFileName
, OpenMode mode
)
191 Open(szFileName
, mode
);
194 // create the file, fail if it already exists and bOverwrite
195 bool wxFile::Create(const wxChar
*szFileName
, bool bOverwrite
, int accessMode
)
197 // if bOverwrite we create a new file or truncate the existing one,
198 // otherwise we only create the new file and fail if it already exists
199 #if defined(__WXMAC__) && !defined(__UNIX__)
200 // Dominic Mazzoni [dmazzoni+@cs.cmu.edu] reports that open is still broken on the mac, so we replace
201 // int fd = open(wxUnix2MacFilename( szFileName ), O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL), access);
202 int fd
= creat( szFileName
, accessMode
);
204 int fd
= wxOpen( szFileName
,
205 O_BINARY
| O_WRONLY
| O_CREAT
|
206 (bOverwrite
? O_TRUNC
: O_EXCL
)
207 ACCESS(accessMode
) );
211 wxLogSysError(_("can't create file '%s'"), szFileName
);
222 bool wxFile::Open(const wxChar
*szFileName
, OpenMode mode
, int accessMode
)
224 int flags
= O_BINARY
;
233 if ( wxFile::Exists(szFileName
) )
235 flags
|= O_WRONLY
| O_APPEND
;
238 //else: fall through as write_append is the same as write if the
239 // file doesn't exist
242 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
246 flags
|= O_WRONLY
| O_CREAT
| O_EXCL
;
254 int fd
= wxOpen( szFileName
, flags
ACCESS(accessMode
));
257 wxLogSysError(_("can't open file '%s'"), szFileName
);
270 if ( close(m_fd
) == -1 ) {
271 wxLogSysError(_("can't close file descriptor %d"), m_fd
);
282 // ----------------------------------------------------------------------------
284 // ----------------------------------------------------------------------------
287 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
289 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
292 int iRc
= ::read(m_fd
, (char*) pBuf
, nCount
);
294 int iRc
= ::read(m_fd
, pBuf
, nCount
);
297 wxLogSysError(_("can't read from file descriptor %d"), m_fd
);
298 return wxInvalidOffset
;
305 size_t wxFile::Write(const void *pBuf
, size_t nCount
)
307 wxCHECK( (pBuf
!= NULL
) && IsOpened(), 0 );
310 #if __MSL__ >= 0x6000
311 int iRc
= ::write(m_fd
, (void*) pBuf
, nCount
);
313 int iRc
= ::write(m_fd
, (const char*) pBuf
, nCount
);
316 int iRc
= ::write(m_fd
, pBuf
, nCount
);
319 wxLogSysError(_("can't write to file descriptor %d"), m_fd
);
331 #if defined(__VISUALC__) || wxHAVE_FSYNC
332 if ( wxFsync(m_fd
) == -1 )
334 wxLogSysError(_("can't flush file descriptor %d"), m_fd
);
345 // ----------------------------------------------------------------------------
347 // ----------------------------------------------------------------------------
350 off_t
wxFile::Seek(off_t ofs
, wxSeekMode mode
)
352 wxASSERT( IsOpened() );
357 wxFAIL_MSG(_("unknown seek origin"));
372 int iRc
= lseek(m_fd
, ofs
, origin
);
374 wxLogSysError(_("can't seek on file descriptor %d"), m_fd
);
375 return wxInvalidOffset
;
382 off_t
wxFile::Tell() const
384 wxASSERT( IsOpened() );
386 int iRc
= wxTell(m_fd
);
388 wxLogSysError(_("can't get seek position on file descriptor %d"), m_fd
);
389 return wxInvalidOffset
;
395 // get current file length
396 off_t
wxFile::Length() const
398 wxASSERT( IsOpened() );
401 int iRc
= _filelength(m_fd
);
403 int iRc
= wxTell(m_fd
);
405 // @ have to use const_cast :-(
406 int iLen
= ((wxFile
*)this)->SeekEnd();
408 // restore old position
409 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
420 wxLogSysError(_("can't find length of file on file descriptor %d"), m_fd
);
421 return wxInvalidOffset
;
427 // is end of file reached?
428 bool wxFile::Eof() const
430 wxASSERT( IsOpened() );
434 #if defined(__DOS__) || defined(__UNIX__) || defined(__GNUWIN32__) || defined( __MWERKS__ ) || defined(__SALFORDC__)
435 // @@ this doesn't work, of course, on unseekable file descriptors
436 off_t ofsCur
= Tell(),
438 if ( ofsCur
== wxInvalidOffset
|| ofsMax
== wxInvalidOffset
)
441 iRc
= ofsCur
== ofsMax
;
442 #else // Windows and "native" compiler
444 #endif // Windows/Unix
454 wxLogSysError(_("can't determine if the end of file is reached on descriptor %d"), m_fd
);
458 wxFAIL_MSG(_("invalid eof() return value."));
464 // ============================================================================
465 // implementation of wxTempFile
466 // ============================================================================
468 // ----------------------------------------------------------------------------
470 // ----------------------------------------------------------------------------
472 wxTempFile::wxTempFile(const wxString
& strName
)
477 bool wxTempFile::Open(const wxString
& strName
)
479 // we must have an absolute filename because otherwise CreateTempFileName()
480 // would create the temp file in $TMP (i.e. the system standard location
481 // for the temp files) which might be on another volume/drive/mount and
482 // wxRename()ing it later to m_strName from Commit() would then fail
484 // with the absolute filename, the temp file is created in the same
485 // directory as this one which ensures that wxRename() may work later
486 wxFileName
fn(strName
);
487 if ( !fn
.IsAbsolute() )
489 fn
.Normalize(wxPATH_NORM_ABSOLUTE
);
492 m_strName
= fn
.GetFullPath();
494 m_strTemp
= wxFileName::CreateTempFileName(m_strName
, &m_file
);
496 if ( m_strTemp
.empty() )
498 // CreateTempFileName() failed
503 // the temp file should have the same permissions as the original one
507 if ( stat( (const char*) m_strName
.fn_str(), &st
) == 0 )
513 // file probably didn't exist, just give it the default mode _using_
514 // user's umask (new files creation should respect umask)
515 mode_t mask
= umask(0777);
520 if ( chmod( (const char*) m_strTemp
.fn_str(), mode
) == -1 )
522 wxLogSysError(_("Failed to set temporary file permissions"));
529 // ----------------------------------------------------------------------------
531 // ----------------------------------------------------------------------------
533 wxTempFile::~wxTempFile()
539 bool wxTempFile::Commit()
543 if ( wxFile::Exists(m_strName
) && wxRemove(m_strName
) != 0 ) {
544 wxLogSysError(_("can't remove file '%s'"), m_strName
.c_str());
548 if ( wxRename(m_strTemp
, m_strName
) != 0 ) {
549 wxLogSysError(_("can't commit changes to file '%s'"), m_strName
.c_str());
556 void wxTempFile::Discard()
559 if ( wxRemove(m_strTemp
) != 0 )
560 wxLogSysError(_("can't remove temporary file '%s'"), m_strTemp
.c_str());