2022-12-12 23:50:05 +00:00
|
|
|
/***
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-06-05 13:55:40 +00:00
|
|
|
template<typename T, AuUInt Z>
|
|
|
|
void IngestableReadBase::SetOutArray(T(&array)[Z])
|
|
|
|
{
|
|
|
|
this->internalOutBuffer_ = AuReinterpretCast<AuUInt8 *>(array);
|
|
|
|
this->internalOutLength_ = Z;
|
|
|
|
}
|
|
|
|
|
2022-12-12 23:50:05 +00:00
|
|
|
void IngestableReadBase::SetPointer(void *pHead, AuUInt32 dwLength)
|
|
|
|
{
|
|
|
|
this->internalInBuffer_ = AuReinterpretCast<AuUInt8 *>(pHead);
|
|
|
|
this->internalInLength_ = dwLength;
|
2023-06-10 19:03:29 +00:00
|
|
|
this->bIsSpecialNotZlibLike = true;
|
2022-12-12 23:50:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
2023-06-10 19:03:29 +00:00
|
|
|
AuUInt32 dwAmount,
|
|
|
|
BaseStream *pThat)
|
2022-12-12 23:50:05 +00:00
|
|
|
{
|
2023-06-10 19:03:29 +00:00
|
|
|
|
|
|
|
if (!this->bIsSpecialNotZlibLike)
|
|
|
|
{
|
|
|
|
if (auto pBuffer = pThat->GetWeakBuffer())
|
|
|
|
{
|
2023-06-10 20:47:37 +00:00
|
|
|
auto view = pBuffer->GetLinearReadableForNoMore(dwAmount);
|
2023-06-10 19:03:29 +00:00
|
|
|
in = (T *)view.ptr;
|
|
|
|
inAlreadyAvailable = view.length;
|
|
|
|
pBuffer->readPtr += view.length;
|
|
|
|
return view.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-12 23:50:05 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|