AuroraRuntime/Source/IO/IOAdapterCompression.cpp

103 lines
3.1 KiB
C++
Raw Normal View History

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