From 61b027446e3ec6ffa2e49d25fd72f22e3cb0a880 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 26 May 1998 15:05:56 +0000 Subject: [PATCH] added write_append mode to wxFile, implemented eof() for Unix git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@31 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/file.h | 8 +++++-- src/common/file.cpp | 54 +++++++++++++++++++++++++-------------------- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/include/wx/file.h b/include/wx/file.h index ece4086f95..8f0f734bed 100644 --- a/include/wx/file.h +++ b/include/wx/file.h @@ -50,7 +50,7 @@ public: // ------------------- // opening mode - enum OpenMode { read, write, read_write }; + enum OpenMode { read, write, read_write, write_append }; // standard values for file descriptor enum { fd_invalid = -1, fd_stdin, fd_stdout, fd_stderr }; // seek type @@ -72,8 +72,12 @@ public: // open/close bool Create(const char *szFileName, bool bOverwrite = FALSE); bool Open(const char *szFileName, OpenMode mode = read); + inline bool Close(); // Close is a NOP if not opened + + // assign an existing file descriptor and get it back from wxFile object void Attach(int fd) { Close(); m_fd = fd; } - inline void Close(); // Close is a NOP if not opened + void Detach() { m_fd = fd_invalid; } + int fd() const { return m_fd; } // read/write (unbuffered) // returns number of bytes read or ofsInvalid on error diff --git a/src/common/file.cpp b/src/common/file.cpp index 6c6ae4acc5..9011987266 100644 --- a/src/common/file.cpp +++ b/src/common/file.cpp @@ -120,8 +120,7 @@ bool wxFile::Create(const char *szFileName, bool bOverwrite) { // if bOverwrite we create a new file or truncate the existing one, // otherwise we only create the new file and fail if it already exists - int fd = bOverwrite ? creat(szFileName, 0) - : open(szFileName, O_CREAT | O_EXCL); + int fd = open(szFileName, O_CREAT | (bOverwrite ? O_TRUNC : O_EXCL)); if ( fd == -1 ) { wxLogSysError("can't create file '%s'", szFileName); @@ -144,7 +143,11 @@ bool wxFile::Open(const char *szFileName, OpenMode mode) break; case write: - flags |= O_WRONLY | O_CREAT; + flags |= O_WRONLY | O_CREAT | O_TRUNC; + break; + + case write_append: + flags |= O_WRONLY | O_APPEND; break; case read_write: @@ -165,14 +168,19 @@ bool wxFile::Open(const char *szFileName, OpenMode mode) } // close -void wxFile::Close() +bool wxFile::Close() { if ( IsOpened() ) { - if ( close(m_fd) == -1 ) + if ( close(m_fd) == -1 ) { wxLogSysError("can't close file descriptor %d", m_fd); - - m_fd = fd_invalid; + m_fd = fd_invalid; + return FALSE; + } + else + m_fd = fd_invalid; } + + return TRUE; } // ---------------------------------------------------------------------------- @@ -211,7 +219,7 @@ bool wxFile::Write(const void *pBuf, uint nCount) bool wxFile::Flush() { if ( IsOpened() ) { - // ## fsync() is not ANSI (BSDish) + // @@@ fsync() is not ANSI (BSDish) // if ( fsync(m_fd) == -1 ) { // TODO if (TRUE) { wxLogSysError("can't flush file descriptor %d", m_fd); @@ -282,7 +290,7 @@ off_t wxFile::Length() const #else int iRc = tell(m_fd); if ( iRc != -1 ) { - // # have to use const_cast :-( + // @ have to use const_cast :-( int iLen = ((wxFile *)this)->SeekEnd(); if ( iLen != -1 ) { // restore old position @@ -310,21 +318,19 @@ bool wxFile::Eof() const { wxASSERT( IsOpened() ); - // TODO: no eof in GnuWin32 - -#ifdef __LINUX__ - - int iRc = Tell() == Length(); - -#else - -#if defined(__GNUWIN32__) - int iRc = -1; -#else - int iRc = eof(m_fd); -#endif - -#endif + int iRc; + + #if defined(__UNIX__) || defined(__GNUWIN32__) + // @@ this doesn't work, of course, on unseekable file descriptors + off_t ofsCur = Tell(), + ofsMax = Length(); + if ( ofsCur == ofsInvalid || ofsMax == ofsInvalid ) + iRc = -1; + else + iRc = ofsCur == ofsMax; + #else // Windows and "native" compiler + iRc = eof(m_fd); + #endif // Windows/Unix switch ( iRc ) { case 1: -- 2.45.2