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