AuroraRuntime/Source/IO/Adapters/AuIOAdapterCompression.cpp

110 lines
3.3 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuIOAdapterCompression.cpp
Date: 2022-6-6
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include <Aurora/IO/IOExperimental.hpp>
#include "AuIOAdapterCompression.hpp"
namespace Aurora::IO::Adapters
{
CompressionStreamReader::CompressionStreamReader(const AuSPtr<Compression::ICompressionStream> &pCompressionStream) :
pCompressionStream(pCompressionStream)
{
}
CompressionSeekingReader::CompressionSeekingReader(const AuSPtr<Compression::ICompressionStream> &pCompressionStream)
: pCompressionStream(pCompressionStream)
{
}
EStreamError CompressionStreamReader::IsOpen()
{
return this->bErrored_ ? EStreamError::eErrorEndOfStream : EStreamError::eErrorNone;
}
EStreamError CompressionStreamReader::Read(const Memory::MemoryViewStreamWrite &parameters)
{
if (!this->pCompressionStream)
{
return EStreamError::eErrorStreamNotOpen;
}
auto pair = this->pCompressionStream->ReadEx(parameters, true);
if (pair == AuStreamReadWrittenPair_t{})
{
this->bErrored_ = true;
return EStreamError::eErrorEndOfStream;
}
parameters.outVariable = pair.second;
return EStreamError::eErrorNone;
}
void CompressionStreamReader::Close()
{
}
EStreamError CompressionSeekingReader::IsOpen()
{
return this->bErrored_ ?
EStreamError::eErrorEndOfStream :
EStreamError::eErrorNone;
}
EStreamError CompressionSeekingReader::ArbitraryRead(AuUInt offset, const Memory::MemoryViewStreamWrite &parameters)
{
if (this->uOffset != offset)
{
if (this->uOffset > offset)
{
if (!this->pCompressionStream->GoBackByProcessedN(this->uOffset - offset))
{
SysPushErrorIO("Negative compression seek out of bounds");
return EStreamError::eErrorStreamInterrupted;
}
}
else
{
if (!this->pCompressionStream->GoForwardByProcessedN(offset - this->uOffset))
{
SysPushErrorIO("Positive compression seek out of bounds");
return EStreamError::eErrorStreamInterrupted;
}
}
this->uOffset = offset;
}
auto pair = this->pCompressionStream->ReadEx(parameters, true);
if (pair == AuStreamReadWrittenPair_t {})
{
this->bErrored_ = true;
return EStreamError::eErrorEndOfStream;
}
this->uOffset += pair.second;
parameters.outVariable = pair.second;
return EStreamError::eErrorNone;
}
void CompressionSeekingReader::Close()
{
}
AUKN_SYM AuSPtr<IStreamReader> NewCompressionReadAdapter(const AuSPtr<Compression::ICompressionStream> &pCompresionStream)
{
return AuMakeShared<CompressionStreamReader>(pCompresionStream);
}
AUKN_SYM AuSPtr<ISeekingReader> NewCompressionSeekingAdapter(const AuSPtr<Compression::ICompressionStream> &pCompresionStream)
{
return AuMakeShared<CompressionSeekingReader>(pCompresionStream);
}
}