Skip to content

Commit

Permalink
OrcLib: ByteStream: add Clone() method to select Streams
Browse files Browse the repository at this point in the history
  • Loading branch information
jgautier-anssi authored and fabienfl-orc committed Feb 10, 2021
1 parent c52b401 commit 518f4b4
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/OrcLib/ByteStream.h
Expand Up @@ -69,10 +69,11 @@ class ORCLIB_API ByteStream
STDMETHOD_(ULONG64, GetSize)() PURE;
STDMETHOD(SetSize)(ULONG64) PURE;

STDMETHOD(Clone)(std::shared_ptr<ByteStream>& clone) { return E_NOTIMPL; }

STDMETHOD(Close)() PURE;

static std::shared_ptr<ByteStream> GetStream(const OutputSpec& output);

static std::shared_ptr<ByteStream> GetHashStream(const std::shared_ptr<ByteStream>& aStream);

static HRESULT Get_IInStream(const std::shared_ptr<ByteStream>& aStream, ::IInStream** pInStream);
Expand Down
18 changes: 18 additions & 0 deletions src/OrcLib/FileStream.cpp
Expand Up @@ -16,6 +16,24 @@

using namespace Orc;

STDMETHODIMP Orc::FileStream::Clone(std::shared_ptr<ByteStream>& clone)
{
auto new_stream = std::make_shared<Orc::FileStream>();
if (auto hr = new_stream->Duplicate(*this); FAILED(hr))
return hr;

ULONG64 ullCurPos = 0LLU;
if (auto hr = SetFilePointer(0LL, FILE_CURRENT, &ullCurPos); FAILED(hr))
return hr;

ULONG64 ullDupCurPos = 0LLU;
if (auto hr = new_stream->SetFilePointer(ullCurPos, FILE_BEGIN, &ullDupCurPos); FAILED(hr))
return hr;

clone = new_stream;
return S_OK;
}

/*
CFileStream::Close
Expand Down
1 change: 1 addition & 0 deletions src/OrcLib/FileStream.h
Expand Up @@ -96,6 +96,7 @@ class ORCLIB_API FileStream : public ByteStream

const std::wstring& Path() const { return m_strPath; }

STDMETHOD(Clone)(std::shared_ptr<ByteStream>& clone);
STDMETHOD(Close)();

protected:
Expand Down
19 changes: 19 additions & 0 deletions src/OrcLib/MemoryStream.cpp
Expand Up @@ -31,6 +31,25 @@ static const size_t MEMORY_STREAM_THRESHOLD_INCREMENT = (1024 * 1024 * 16);

static const size_t MEMORY_STREAM_RESERVE_MAX = (1024 * 1024 * 200);

STDMETHODIMP Orc::MemoryStream::Clone(std::shared_ptr<ByteStream>& clone)
{
auto new_stream = std::make_shared<MemoryStream>();

if (auto hr = new_stream->Duplicate(*this); FAILED(hr))
return hr;

ULONG64 ullCurPos = 0LLU;
if (auto hr = SetFilePointer(0LL, FILE_CURRENT, &ullCurPos); FAILED(hr))
return hr;

ULONG64 ullDupCurPos = 0LLU;
if (auto hr = new_stream->SetFilePointer(ullCurPos, FILE_BEGIN, &ullDupCurPos); FAILED(hr))
return hr;

clone = new_stream;
return S_OK;
}

HRESULT MemoryStream::Close()
{
return S_OK;
Expand Down
1 change: 1 addition & 0 deletions src/OrcLib/MemoryStream.h
Expand Up @@ -73,6 +73,7 @@ class ORCLIB_API MemoryStream : public ByteStream
STDMETHOD_(ULONG64, GetSize)();
STDMETHOD(SetSize)(ULONG64 ullSize);

STDMETHOD(Clone)(std::shared_ptr<ByteStream>& clone);
STDMETHOD(Close)();

HRESULT Duplicate(const MemoryStream& other);
Expand Down
35 changes: 29 additions & 6 deletions src/OrcLib/ResourceStream.cpp
Expand Up @@ -74,6 +74,7 @@ HRESULT ResourceStream::OpenForReadOnly(__in const HINSTANCE hInstance, __in WOR
}
{
ScopedLock sl(m_cs);
m_hInstance = hInstance;
std::swap(m_hFileResource, hFileResource);
std::swap(m_hResource, hResource);
}
Expand All @@ -84,12 +85,9 @@ HRESULT ResourceStream::OpenForReadOnly(__in const HINSTANCE hInstance, __in WOR
HRESULT ResourceStream::OpenForReadOnly(__in const HINSTANCE hInstance, __in HRSRC hRes)
{
HGLOBAL hFileResource = NULL;
HRSRC hResource = NULL;

// First find and load the required resource
hResource = hRes;

if ((hFileResource = LoadResource(hInstance, hResource)) == NULL)
if ((hFileResource = LoadResource(hInstance, hRes)) == NULL)
{
HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
Log::Debug("Failed LoadResource [{}]", SystemError(hr));
Expand All @@ -107,16 +105,17 @@ HRESULT ResourceStream::OpenForReadOnly(__in const HINSTANCE hInstance, __in HRS

DWORD dwSize = 0L;

if ((dwSize = SizeofResource(hInstance, hResource)) == 0)
if ((dwSize = SizeofResource(hInstance, hRes)) == 0)
{
HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
Log::Debug("Failed SizeofResource [{}]", SystemError(hr));
return hr;
}
{
ScopedLock sl(m_cs);
m_hInstance = hInstance;
std::swap(m_hFileResource, hFileResource);
std::swap(m_hResource, hResource);
std::swap(m_hResource, hRes);
}

return MemoryStream::OpenForReadOnly(lpFile, dwSize);
Expand Down Expand Up @@ -158,6 +157,30 @@ HRESULT Orc::ResourceStream::OpenForReadOnly(const std::wstring& strResourceSpec
return S_OK;
}

STDMETHODIMP Orc::ResourceStream::Clone(std::shared_ptr<ByteStream>& clone)
{
auto new_stream = std::make_shared<ResourceStream>();

if (IsOpen() == S_FALSE)
{
clone = new_stream;
return S_OK;
}

if (auto hr = new_stream->OpenForReadOnly(m_hInstance, m_hResource); FAILED(hr))
return hr;

ULONG64 ullCurPos = 0LLU;
if (auto hr = SetFilePointer(0LL, FILE_CURRENT, &ullCurPos); FAILED(hr))
return hr;

ULONG64 ullDupCurPos = 0LLU;
if (auto hr = new_stream->SetFilePointer(ullCurPos, FILE_BEGIN, &ullDupCurPos); FAILED(hr))
return hr;
clone = new_stream;
return S_OK;
}

HRESULT ResourceStream::Close()
{
HGLOBAL hFileResource = NULL;
Expand Down
2 changes: 2 additions & 0 deletions src/OrcLib/ResourceStream.h
Expand Up @@ -42,10 +42,12 @@ class ORCLIB_API ResourceStream : public MemoryStream
STDMETHOD(OpenForReadOnly)(__in const HINSTANCE hInstance, __in HRSRC hRes);
STDMETHOD(OpenForReadOnly)(__in const std::wstring& strResourceSpec);

STDMETHOD(Clone)(std::shared_ptr<ByteStream>& clone);
STDMETHOD(Close)();

protected:
CriticalSection m_cs;
HINSTANCE m_hInstance = NULL;
HGLOBAL m_hFileResource = NULL;
HRSRC m_hResource = NULL;
};
Expand Down

0 comments on commit 518f4b4

Please sign in to comment.