]>
git.saurik.com Git - wxWidgets.git/blob - src/common/file.cpp
6c6ae4acc553a189c3bfd92bc01dcaa30e43fa5f
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"
30 #if defined(__WINDOWS__) && !defined(__GNUWIN32__)
32 #elif (defined(__UNIX__) || defined(__GNUWIN32__))
35 #error "Please specify the header with file functions declarations."
38 #include <stdio.h> // SEEK_xxx constants
39 #include <fcntl.h> // O_RDONLY &c
40 #include <sys/types.h> // needed for stat
41 #include <sys/stat.h> // stat
43 // Microsoft compiler loves underscores, feed them to it
52 #define access _access
59 #define O_RDONLY _O_RDONLY
60 #define O_WRONLY _O_WRONLY
61 #define O_RDWR _O_RDWR
62 #define O_EXCL _O_EXCL
63 #define O_CREAT _O_CREAT
64 #define O_BINARY _O_BINARY
66 #define S_IFDIR _S_IFDIR
67 #define S_IFREG _S_IFREG
69 #define S_IREAD _S_IREAD
70 #define S_IWRITE _S_IWRITE
72 #define tell(fd) lseek(fd, 0, SEEK_CUR)
75 // there is no distinction between text and binary files under Unix
81 #include <wx/string.h>
87 // ============================================================================
88 // implementation of wxFile
89 // ============================================================================
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
94 bool wxFile::Exists(const char *sz
)
97 return !access(sz
, 0) && !stat(sz
, &st
) && (st
.st_mode
& S_IFREG
);
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
105 wxFile::wxFile(const char *szFileName
, OpenMode mode
)
109 Open(szFileName
, mode
);
118 // create the file, fail if it already exists and bOverwrite
119 bool wxFile::Create(const char *szFileName
, bool bOverwrite
)
121 // if bOverwrite we create a new file or truncate the existing one,
122 // otherwise we only create the new file and fail if it already exists
123 int fd
= bOverwrite
? creat(szFileName
, 0)
124 : open(szFileName
, O_CREAT
| O_EXCL
);
127 wxLogSysError("can't create file '%s'", szFileName
);
137 bool wxFile::Open(const char *szFileName
, OpenMode mode
)
139 int flags
= O_BINARY
;
147 flags
|= O_WRONLY
| O_CREAT
;
155 int fd
= open(szFileName
, flags
, S_IREAD
| S_IWRITE
);
158 wxLogSysError("can't open file '%s'", szFileName
);
171 if ( close(m_fd
) == -1 )
172 wxLogSysError("can't close file descriptor %d", m_fd
);
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
183 off_t
wxFile::Read(void *pBuf
, off_t nCount
)
185 wxCHECK_RET( (pBuf
!= NULL
) && IsOpened(), 0 );
187 int iRc
= ::read(m_fd
, pBuf
, nCount
);
189 wxLogSysError("can't read from file descriptor %d", m_fd
);
197 bool wxFile::Write(const void *pBuf
, uint nCount
)
199 wxCHECK_RET( (pBuf
!= NULL
) && IsOpened(), 0 );
201 int iRc
= ::write(m_fd
, pBuf
, nCount
);
203 wxLogSysError("can't write to file descriptor %d", m_fd
);
214 // ## fsync() is not ANSI (BSDish)
215 // if ( fsync(m_fd) == -1 ) { // TODO
217 wxLogSysError("can't flush file descriptor %d", m_fd
);
225 // ----------------------------------------------------------------------------
227 // ----------------------------------------------------------------------------
230 off_t
wxFile::Seek(off_t ofs
, SeekMode mode
)
232 wxASSERT( IsOpened() );
249 wxFAIL_MSG("unknown seek origin");
252 int iRc
= lseek(m_fd
, ofs
, flag
);
254 wxLogSysError("can't seek on file descriptor %d", m_fd
);
262 off_t
wxFile::Tell() const
264 wxASSERT( IsOpened() );
266 int iRc
= tell(m_fd
);
268 wxLogSysError("can't get seek position on file descriptor %d", m_fd
);
275 // get current file length
276 off_t
wxFile::Length() const
278 wxASSERT( IsOpened() );
281 int iRc
= _filelength(m_fd
);
283 int iRc
= tell(m_fd
);
285 // # have to use const_cast :-(
286 int iLen
= ((wxFile
*)this)->SeekEnd();
288 // restore old position
289 if ( ((wxFile
*)this)->Seek(iRc
) == -1 ) {
301 wxLogSysError("can't find length of file on file descriptor %d", m_fd
);
308 // is end of file reached?
309 bool wxFile::Eof() const
311 wxASSERT( IsOpened() );
313 // TODO: no eof in GnuWin32
317 int iRc
= Tell() == Length();
321 #if defined(__GNUWIN32__)
337 wxLogSysError("can't determine if the end of file is reached on "
338 "descriptor %d", m_fd
);
342 wxFAIL_MSG("invalid eof() return value.");
348 // ============================================================================
349 // implementation of wxTempFile
350 // ============================================================================
352 // ----------------------------------------------------------------------------
354 // ----------------------------------------------------------------------------
355 wxTempFile::wxTempFile(const wxString
& strName
)
360 bool wxTempFile::Open(const wxString
& strName
)
363 m_strTemp
= tmpnam(NULL
);
364 return m_file
.Open(m_strTemp
, wxFile::write
);
367 // ----------------------------------------------------------------------------
369 // ----------------------------------------------------------------------------
371 wxTempFile::~wxTempFile()
377 bool wxTempFile::Commit()
381 if ( wxFile::Exists(m_strName
) && remove(m_strName
) != 0 ) {
382 wxLogSysError("can't remove file '%s'", m_strName
.c_str());
386 if ( rename(m_strTemp
, m_strName
) != 0 ) {
387 wxLogSysError("can't commit changes to file '%s'", m_strName
.c_str());
394 void wxTempFile::Discard()
397 if ( remove(m_strTemp
) != 0 )
398 wxLogSysError("can't remove temporary file '%s'", m_strTemp
.c_str());