From 717296957c08c76473a25e4946e84e19f9d27cbf Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Tue, 18 Apr 2017 15:22:09 -0400 Subject: Fold the CFGAS_Stream subclasses in CFGAS_Stream MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This CL moves ReadData and WriteData into CFGAS_Stream and predicates their usage on m_isWriteStream. This then removes the two subclasses of CFGAS_Stream and just passes the correct flag through the constructor. Change-Id: I72ea88f333c8bdaf5b323b2832479231cfd2c0b8 Reviewed-on: https://pdfium-review.googlesource.com/4154 Commit-Queue: dsinclair Reviewed-by: Tom Sepez Reviewed-by: Nicolás Peña --- xfa/fgas/crt/ifgas_stream.cpp | 152 +++++++++++++++++------------------------- 1 file changed, 60 insertions(+), 92 deletions(-) (limited to 'xfa/fgas/crt/ifgas_stream.cpp') diff --git a/xfa/fgas/crt/ifgas_stream.cpp b/xfa/fgas/crt/ifgas_stream.cpp index 3f5069e4bb..06ab46551d 100644 --- a/xfa/fgas/crt/ifgas_stream.cpp +++ b/xfa/fgas/crt/ifgas_stream.cpp @@ -25,6 +25,9 @@ namespace { class CFGAS_Stream : public IFGAS_Stream { public: + template + friend CFX_RetainPtr pdfium::MakeRetain(Args&&... args); + // IFGAS_Stream FX_FILESIZE GetLength() const override { return m_pStream->GetSize(); } FX_FILESIZE GetPosition() override { return m_iPosition; } @@ -38,15 +41,13 @@ class CFGAS_Stream : public IFGAS_Stream { uint16_t GetCodePage() const override { return m_wCodePage; } void SetCodePage(uint16_t wCodePage) override; - protected: + private: CFGAS_Stream(const CFX_RetainPtr& stream, bool isWriteSteam); ~CFGAS_Stream() override; - virtual FX_STRSIZE ReadData(uint8_t* pBuffer, FX_STRSIZE iBufferSize) { - return 0; - } - virtual void WriteData(const CFX_WideStringC& str) {} + FX_STRSIZE ReadData(uint8_t* pBuffer, FX_STRSIZE iBufferSize); + void WriteData(const CFX_WideStringC& str); uint16_t m_wCodePage; FX_STRSIZE m_wBOMLength; @@ -55,34 +56,6 @@ class CFGAS_Stream : public IFGAS_Stream { CFX_RetainPtr m_pStream; }; -class CFGAS_FileReadStreamImp : public CFGAS_Stream { - public: - template - friend CFX_RetainPtr pdfium::MakeRetain(Args&&... args); - - // CFGAS_Stream: - FX_STRSIZE ReadData(uint8_t* pBuffer, FX_STRSIZE iBufferSize) override; - - private: - explicit CFGAS_FileReadStreamImp( - const CFX_RetainPtr& pFileRead); - ~CFGAS_FileReadStreamImp() override {} -}; - -class CFGAS_FileWriteStreamImp : public CFGAS_Stream { - public: - template - friend CFX_RetainPtr pdfium::MakeRetain(Args&&... args); - - // CFGAS_Stream: - void WriteData(const CFX_WideStringC& str) override; - - private: - explicit CFGAS_FileWriteStreamImp( - const CFX_RetainPtr& pFileWrite); - ~CFGAS_FileWriteStreamImp() override {} -}; - class CFGAS_WideStringReadStream : public IFGAS_Stream { public: template @@ -232,9 +205,20 @@ void SwapByteOrder(wchar_t* pStr, FX_STRSIZE iLength) { #define BOM_UTF16_LE 0xFFFE0000 #endif // _FX_ENDIAN_ == _FX_LITTLE_ENDIAN_ -CFGAS_FileReadStreamImp::CFGAS_FileReadStreamImp( - const CFX_RetainPtr& pFileRead) - : CFGAS_Stream(pFileRead, false) { +CFGAS_Stream::CFGAS_Stream(const CFX_RetainPtr& stream, + bool isWriteStream) + : m_wCodePage(FX_CODEPAGE_DefANSI), + m_wBOMLength(0), + m_IsWriteStream(isWriteStream), + m_iPosition(0), + m_pStream(stream) { + ASSERT(m_pStream); + + if (isWriteStream) { + m_iPosition = m_pStream->GetSize(); + return; + } + FX_FILESIZE iPosition = GetPosition(); Seek(FX_STREAMSEEK_Begin, 0); @@ -263,60 +247,6 @@ CFGAS_FileReadStreamImp::CFGAS_FileReadStreamImp( std::max(static_cast(m_wBOMLength), iPosition)); } -FX_STRSIZE CFGAS_FileReadStreamImp::ReadData(uint8_t* pBuffer, - FX_STRSIZE iBufferSize) { - ASSERT(pBuffer && iBufferSize > 0); - - iBufferSize = std::min( - iBufferSize, static_cast(m_pStream->GetSize() - m_iPosition)); - if (iBufferSize <= 0) - return 0; - - if (m_pStream->ReadBlock(pBuffer, m_iPosition, iBufferSize)) { - pdfium::base::CheckedNumeric new_pos = m_iPosition; - new_pos += iBufferSize; - if (!new_pos.IsValid()) - return 0; - - m_iPosition = new_pos.ValueOrDie(); - return iBufferSize; - } - return 0; -} - -CFGAS_FileWriteStreamImp::CFGAS_FileWriteStreamImp( - const CFX_RetainPtr& pFileWrite) - : CFGAS_Stream(pFileWrite, true) { - m_iPosition = m_pStream->GetSize(); -} - -void CFGAS_FileWriteStreamImp::WriteData(const CFX_WideStringC& str) { - if (str.GetLength() == 0) - return; - - if (!m_pStream->WriteBlock(str.c_str(), m_iPosition, - str.GetLength() * sizeof(wchar_t))) - return; - - pdfium::base::CheckedNumeric new_pos = m_iPosition; - new_pos += str.GetLength() * sizeof(wchar_t); - // TODO(dsinclair): Not sure what to do if we over flow .... - if (!new_pos.IsValid()) - return; - - m_iPosition = new_pos.ValueOrDie(); -} - -CFGAS_Stream::CFGAS_Stream(const CFX_RetainPtr& stream, - bool isWriteStream) - : m_wCodePage(FX_CODEPAGE_DefANSI), - m_wBOMLength(0), - m_IsWriteStream(isWriteStream), - m_iPosition(0), - m_pStream(stream) { - ASSERT(m_pStream); -} - CFGAS_Stream::~CFGAS_Stream() {} void CFGAS_Stream::Seek(FX_STREAMSEEK eSeek, FX_FILESIZE iOffset) { @@ -338,6 +268,29 @@ void CFGAS_Stream::SetCodePage(uint16_t wCodePage) { m_wCodePage = wCodePage; } +FX_STRSIZE CFGAS_Stream::ReadData(uint8_t* pBuffer, FX_STRSIZE iBufferSize) { + ASSERT(pBuffer && iBufferSize > 0); + + if (m_IsWriteStream) + return -1; + + iBufferSize = std::min( + iBufferSize, static_cast(m_pStream->GetSize() - m_iPosition)); + if (iBufferSize <= 0) + return 0; + + if (m_pStream->ReadBlock(pBuffer, m_iPosition, iBufferSize)) { + pdfium::base::CheckedNumeric new_pos = m_iPosition; + new_pos += iBufferSize; + if (!new_pos.IsValid()) + return 0; + + m_iPosition = new_pos.ValueOrDie(); + return iBufferSize; + } + return 0; +} + FX_STRSIZE CFGAS_Stream::ReadString(wchar_t* pStr, FX_STRSIZE iMaxLength, bool* bEOS) { @@ -387,6 +340,21 @@ FX_STRSIZE CFGAS_Stream::ReadString(wchar_t* pStr, return iMaxLength; } +void CFGAS_Stream::WriteData(const CFX_WideStringC& str) { + if (!m_IsWriteStream || str.GetLength() == 0) + return; + if (m_pStream->WriteBlock(str.c_str(), m_iPosition, + str.GetLength() * sizeof(wchar_t))) { + pdfium::base::CheckedNumeric new_pos = m_iPosition; + new_pos += str.GetLength() * sizeof(wchar_t); + // TODO(dsinclair): Not sure what to do if we over flow .... + if (!new_pos.IsValid()) + return; + + m_iPosition = new_pos.ValueOrDie(); + } +} + void CFGAS_Stream::WriteString(const CFX_WideStringC& str) { if (!m_IsWriteStream) return; @@ -441,7 +409,7 @@ CFX_RetainPtr IFGAS_Stream::CreateReadStream( if (!pFileRead) return nullptr; - return pdfium::MakeRetain(pFileRead); + return pdfium::MakeRetain(pFileRead, false); } // static @@ -450,7 +418,7 @@ CFX_RetainPtr IFGAS_Stream::CreateWriteStream( if (!pFileWrite) return nullptr; - return pdfium::MakeRetain(pFileWrite); + return pdfium::MakeRetain(pFileWrite, true); } // static -- cgit v1.2.3