From e66ade1b8496d9086a6e9189e5a33cb3a6e73e22 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 30 Mar 2018 00:11:31 +0200 Subject: [PATCH] Factor out reusable part of wxLZMAInputStream into wxLZMAData No changes yet, this is a pure refactoring in preparation for adding wxLZMAOutputStream. --- include/wx/lzmastream.h | 44 +++++++++++++++++++++++++-------- src/common/lzmastream.cpp | 52 ++++++++++++++++++++------------------- 2 files changed, 61 insertions(+), 35 deletions(-) diff --git a/include/wx/lzmastream.h b/include/wx/lzmastream.h index 3c2a77808f..604a2543a2 100644 --- a/include/wx/lzmastream.h +++ b/include/wx/lzmastream.h @@ -17,18 +17,48 @@ #include "wx/stream.h" #include "wx/versioninfo.h" +namespace wxPrivate +{ + +// Private wrapper for lzma_stream struct. struct wxLZMAStream; +// Common part of input and output LZMA streams: this is just an implementation +// detail and is not part of the public API. +class WXDLLIMPEXP_BASE wxLZMAData +{ +protected: + wxLZMAData(); + ~wxLZMAData(); + + wxLZMAStream* m_stream; + wxUint8* m_streamBuf; + wxFileOffset m_pos; + + wxDECLARE_NO_COPY_CLASS(wxLZMAData); +}; + +} // namespace wxPrivate + // ---------------------------------------------------------------------------- // Filter for decompressing data compressed using LZMA // ---------------------------------------------------------------------------- -class WXDLLIMPEXP_BASE wxLZMAInputStream : public wxFilterInputStream +class WXDLLIMPEXP_BASE wxLZMAInputStream : public wxFilterInputStream, + private wxPrivate::wxLZMAData { public: - explicit wxLZMAInputStream(wxInputStream& stream); - explicit wxLZMAInputStream(wxInputStream* stream); - virtual ~wxLZMAInputStream(); + explicit wxLZMAInputStream(wxInputStream& stream) + : wxFilterInputStream(stream) + { + Init(); + } + + explicit wxLZMAInputStream(wxInputStream* stream) + : wxFilterInputStream(stream) + { + Init(); + } char Peek() wxOVERRIDE { return wxInputStream::Peek(); } wxFileOffset GetLength() const wxOVERRIDE { return wxInputStream::GetLength(); } @@ -39,12 +69,6 @@ protected: private: void Init(); - - wxLZMAStream* m_stream; - wxUint8* m_inbuf; - wxFileOffset m_pos; - - wxDECLARE_NO_COPY_CLASS(wxLZMAInputStream); }; WXDLLIMPEXP_BASE wxVersionInfo wxGetLibLZMAVersionInfo(); diff --git a/src/common/lzmastream.cpp b/src/common/lzmastream.cpp index b8aae286f1..e07460b7df 100644 --- a/src/common/lzmastream.cpp +++ b/src/common/lzmastream.cpp @@ -33,11 +33,14 @@ #include +namespace wxPrivate +{ + // ---------------------------------------------------------------------------- // Constants // ---------------------------------------------------------------------------- -static const size_t wxLZMA_BUF_SIZE = 4096; +const size_t wxLZMA_BUF_SIZE = 4096; // ---------------------------------------------------------------------------- // Private helpers @@ -58,6 +61,10 @@ struct wxLZMAStream : lzma_stream } }; +} // namespace wxPrivate + +using namespace wxPrivate; + // ============================================================================ // implementation // ============================================================================ @@ -80,28 +87,29 @@ wxVersionInfo wxGetLibLZMAVersionInfo() ); } +// ---------------------------------------------------------------------------- +// wxLZMAData: common helpers for compression and decompression +// ---------------------------------------------------------------------------- + +wxLZMAData::wxLZMAData() +{ + m_stream = new wxLZMAStream; + m_streamBuf = new wxUint8[wxLZMA_BUF_SIZE]; + m_pos = 0; +} + +wxLZMAData::~wxLZMAData() +{ + delete [] m_streamBuf; + delete m_stream; +} + // ---------------------------------------------------------------------------- // wxLZMAInputStream: decompression // ---------------------------------------------------------------------------- -wxLZMAInputStream::wxLZMAInputStream(wxInputStream& stream) - : wxFilterInputStream(stream) -{ - Init(); -} - -wxLZMAInputStream::wxLZMAInputStream(wxInputStream* stream) - : wxFilterInputStream(stream) -{ - Init(); -} - void wxLZMAInputStream::Init() { - m_stream = new wxLZMAStream; - m_inbuf = new wxUint8[wxLZMA_BUF_SIZE]; - m_pos = 0; - // We don't specify any memory usage limit nor any flags, not even // LZMA_CONCATENATED recommended by liblzma documentation, because we don't // foresee the need to support concatenated compressed files for now. @@ -126,12 +134,6 @@ void wxLZMAInputStream::Init() m_lasterror = wxSTREAM_READ_ERROR; } -wxLZMAInputStream::~wxLZMAInputStream() -{ - delete [] m_inbuf; - delete m_stream; -} - size_t wxLZMAInputStream::OnSysRead(void* outbuf, size_t size) { m_stream->next_out = static_cast(outbuf); @@ -145,8 +147,8 @@ size_t wxLZMAInputStream::OnSysRead(void* outbuf, size_t size) // Get more input data if needed. if ( !m_stream->avail_in ) { - m_parent_i_stream->Read(m_inbuf, wxLZMA_BUF_SIZE); - m_stream->next_in = m_inbuf; + m_parent_i_stream->Read(m_streamBuf, wxLZMA_BUF_SIZE); + m_stream->next_in = m_streamBuf; m_stream->avail_in = m_parent_i_stream->LastRead(); if ( !m_stream->avail_in )