AuroraRuntime/Source/Compression/AuIngestableReadBase.inl

90 lines
2.9 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuIngestableReadBase.inl
Date: 2022-2-14
Author: Reece
***/
#pragma once
namespace Aurora::Compression
{
template<typename T, AuUInt Z>
void IngestableReadBase::SetArray(T(&array)[Z])
{
this->internalInBuffer_ = AuReinterpretCast<AuUInt8 *>(array);
this->internalInLength_ = Z;
}
template<typename T, AuUInt Z>
void IngestableReadBase::SetOutArray(T(&array)[Z])
{
this->internalOutBuffer_ = AuReinterpretCast<AuUInt8 *>(array);
this->internalOutLength_ = Z;
}
void IngestableReadBase::SetPointer(void *pHead, AuUInt32 dwLength)
{
this->internalInBuffer_ = AuReinterpretCast<AuUInt8 *>(pHead);
this->internalInLength_ = dwLength;
this->bIsSpecialNotZlibLike = true;
}
// Given a zlib-like interface input paremeters, a stream source, and a buffer...
//
// ...reads
template<typename T, typename Z>
AuUInt32 IngestableReadBase::IngestForInPointer(const AuSPtr<IO::IStreamReader> &reader,
T *&in,
Z &inAlreadyAvailable,
AuUInt32 dwAmount,
BaseStream *pThat)
{
if (!this->bIsSpecialNotZlibLike)
{
if (auto pBuffer = pThat->GetWeakBuffer())
{
auto view = pBuffer->GetLinearReadableForNoMore(dwAmount);
in = (T *)view.ptr;
inAlreadyAvailable = view.length;
pBuffer->readPtr += view.length;
return view.length;
}
}
if (inAlreadyAvailable > this->internalInLength_)
{
SysPanic("Invalid Buffer Position");
}
AuUInt32 dwCurrentOffset {};
if (in && inAlreadyAvailable)
{
dwCurrentOffset = AuReinterpretCast<const AuUInt8 *>(in) - this->internalInBuffer_;
}
dwCurrentOffset += inAlreadyAvailable;
AuUInt32 dwCurrentLength = this->internalInLength_ - dwCurrentOffset;
if (dwCurrentLength > this->internalInLength_)
{
SysPanic("Invalid Buffer Position");
}
auto nextStreamRead = this->internalInBuffer_ + dwCurrentOffset;
auto nextStreamSegment = this->internalInBuffer_ + inAlreadyAvailable;
AuUInt uRead = AuMin(dwAmount, dwCurrentLength);
if (reader->Read(AuMemoryViewStreamWrite(this->internalInBuffer_ + dwCurrentOffset,
uRead)) != IO::EStreamError::eErrorNone)
{
return 0;
}
in = (T *)(this->internalInBuffer_ + dwCurrentOffset);
inAlreadyAvailable += uRead;
return uRead;
}
}