From 8f0ff178513f59471bfce85cf6d7a78f4f4818ab Mon Sep 17 00:00:00 2001 From: Ryan Norton Date: Tue, 23 Nov 2004 14:26:10 +0000 Subject: [PATCH] [ 1070686 ] wxOutputStream::Close() git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@30731 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/latex/wx/outptstr.tex | 13 +++++++++++++ include/wx/archive.h | 1 - include/wx/stream.h | 2 ++ include/wx/wfstream.h | 2 ++ include/wx/zstream.h | 3 ++- src/common/stream.cpp | 7 +++++++ src/common/zstream.cpp | 20 ++++++++++---------- src/msw/utilsexc.cpp | 8 +++++--- tests/streams/bstream.h | 5 ++++- 9 files changed, 45 insertions(+), 16 deletions(-) diff --git a/docs/latex/wx/outptstr.tex b/docs/latex/wx/outptstr.tex index 9cd305c6b9..1c61a7c606 100644 --- a/docs/latex/wx/outptstr.tex +++ b/docs/latex/wx/outptstr.tex @@ -33,6 +33,19 @@ Creates a dummy wxOutputStream object. Destructor. +\membersection{wxOutputStream::Close}\label{wxoutputstreamclose} + +\func{bool}{Close}{\void} + +Closes the stream, returning {\tt false} if an error occurs. The +stream is closed implicitly in the destructor if Close() is not +called explicitly. + +If this stream wraps another stream or some other resource such +as a file, then the underlying resource is closed too if it is owned +by this stream, or left open otherwise. + + \membersection{wxOutputStream::LastWrite}\label{wxoutputstreamlastwrite} \constfunc{size\_t}{LastWrite}{\void} diff --git a/include/wx/archive.h b/include/wx/archive.h index c821da920d..5bb4880f8f 100644 --- a/include/wx/archive.h +++ b/include/wx/archive.h @@ -149,7 +149,6 @@ public: virtual bool CopyArchiveMetaData(wxArchiveInputStream& stream) = 0; virtual bool CloseEntry() = 0; - virtual bool Close() = 0; protected: wxArchiveOutputStream(wxOutputStream& stream, wxMBConv& conv); diff --git a/include/wx/stream.h b/include/wx/stream.h index dc574b2200..5799895fe0 100644 --- a/include/wx/stream.h +++ b/include/wx/stream.h @@ -257,6 +257,7 @@ public: virtual size_t LastWrite() const { return wxStreamBase::m_lastcount; } virtual void Sync(); + virtual bool Close() { return true; } wxOutputStream& operator<<(wxInputStream& out) { return Write(out); } wxOutputStream& operator<<( __wxOutputManip func) { return func(*this); } @@ -515,6 +516,7 @@ public: wxFileOffset TellO() const; void Sync(); + bool Close(); wxFileOffset GetLength() const; diff --git a/include/wx/wfstream.h b/include/wx/wfstream.h index 514d795fff..b26ec29f5b 100644 --- a/include/wx/wfstream.h +++ b/include/wx/wfstream.h @@ -67,6 +67,7 @@ class WXDLLIMPEXP_BASE wxFileOutputStream: public wxOutputStream { // { return wxOutputStream::Write(buffer, size); } void Sync(); + bool Close() { return m_file_destroy ? m_file->Close() : true; } wxFileOffset GetLength() const; bool Ok() const { return m_file->IsOpened(); } @@ -136,6 +137,7 @@ class WXDLLIMPEXP_BASE wxFFileOutputStream: public wxOutputStream { // { return wxOutputStream::Write(buffer, size); } void Sync(); + bool Close() { return m_file_destroy ? m_file->Close() : true; } wxFileOffset GetLength() const; bool Ok() const { return m_file->IsOpened(); } diff --git a/include/wx/zstream.h b/include/wx/zstream.h index 1723d93702..16d43212e6 100644 --- a/include/wx/zstream.h +++ b/include/wx/zstream.h @@ -69,9 +69,10 @@ class WXDLLIMPEXP_BASE wxZlibInputStream: public wxFilterInputStream { class WXDLLIMPEXP_BASE wxZlibOutputStream: public wxFilterOutputStream { public: wxZlibOutputStream(wxOutputStream& stream, int level = -1, int flags = wxZLIB_ZLIB); - virtual ~wxZlibOutputStream(); + virtual ~wxZlibOutputStream() { Close(); } void Sync() { DoFlush(false); } + bool Close(); wxFileOffset GetLength() const { return m_pos; } static bool CanHandleGZip(); diff --git a/src/common/stream.cpp b/src/common/stream.cpp index b2154a2d59..62a8e98e7d 100644 --- a/src/common/stream.cpp +++ b/src/common/stream.cpp @@ -1185,6 +1185,13 @@ wxBufferedOutputStream::~wxBufferedOutputStream() delete m_o_streambuf; } +bool wxBufferedOutputStream::Close() +{ + Sync(); + return IsOk(); +} + + wxOutputStream& wxBufferedOutputStream::Write(const void *buffer, size_t size) { m_lastcount = 0; diff --git a/src/common/zstream.cpp b/src/common/zstream.cpp index 7309afc292..7afa0548ff 100644 --- a/src/common/zstream.cpp +++ b/src/common/zstream.cpp @@ -238,20 +238,20 @@ wxZlibOutputStream::wxZlibOutputStream(wxOutputStream& stream, m_lasterror = wxSTREAM_WRITE_ERROR; } -wxZlibOutputStream::~wxZlibOutputStream() -{ - if (m_deflate && m_z_buffer) - DoFlush(true); - deflateEnd(m_deflate); - delete m_deflate; +bool wxZlibOutputStream::Close() + { + DoFlush(true); + deflateEnd(m_deflate); + delete m_deflate; - delete[] m_z_buffer; -} + m_deflate = NULL; + delete[] m_z_buffer; + m_z_buffer = NULL; + return IsOk(); + } void wxZlibOutputStream::DoFlush(bool final) { - wxASSERT_MSG(m_deflate && m_z_buffer, wxT("Deflate stream not open")); - if (!m_deflate || !m_z_buffer) m_lasterror = wxSTREAM_WRITE_ERROR; if (!IsOk()) diff --git a/src/msw/utilsexc.cpp b/src/msw/utilsexc.cpp index ecdb0f9792..8899b5e0f1 100644 --- a/src/msw/utilsexc.cpp +++ b/src/msw/utilsexc.cpp @@ -179,7 +179,8 @@ class wxPipeOutputStream: public wxOutputStream { public: wxPipeOutputStream(HANDLE hOutput); - virtual ~wxPipeOutputStream(); + virtual ~wxPipeOutputStream() { Close(); } + bool Close(); protected: size_t OnSysWrite(const void *buffer, size_t len); @@ -444,11 +445,12 @@ wxPipeOutputStream::wxPipeOutputStream(HANDLE hOutput) } } -wxPipeOutputStream::~wxPipeOutputStream() +bool wxPipeOutputStream::Close() { - ::CloseHandle(m_hOutput); + return ::CloseHandle(m_hOutput) != 0; } + size_t wxPipeOutputStream::OnSysWrite(const void *buffer, size_t len) { m_lasterror = wxSTREAM_NO_ERROR; diff --git a/tests/streams/bstream.h b/tests/streams/bstream.h index a73b495384..fc2d6fb237 100644 --- a/tests/streams/bstream.h +++ b/tests/streams/bstream.h @@ -400,9 +400,12 @@ protected: DoDeleteInStream(); } void DeleteOutStream() - { + { if (m_pCurrentOut == NULL) return; + + CPPUNIT_ASSERT(m_pCurrentOut->Close()); + delete m_pCurrentOut; m_pCurrentOut = NULL; // Incase something extra needs to be done. -- 2.45.2