64 lines
2.0 KiB
Plaintext
64 lines
2.0 KiB
Plaintext
|
/***
|
||
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
||
|
|
||
|
File: IngestableReadBase.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_ = reinterpret_cast<AuUInt8 *>(array);
|
||
|
this->internalInLength_ = Z;
|
||
|
}
|
||
|
|
||
|
void IngestableReadBase::SetPointer(void *pointer, AuUInt32 length)
|
||
|
{
|
||
|
this->internalInBuffer_ = reinterpret_cast<AuUInt8 *>(pointer);
|
||
|
this->internalInLength_ = length;
|
||
|
}
|
||
|
|
||
|
// 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 amount)
|
||
|
{
|
||
|
if (inAlreadyAvailable > this->internalInLength_)
|
||
|
{
|
||
|
SysPanic("Invalid Buffer Position");
|
||
|
}
|
||
|
|
||
|
AuUInt32 currentOffset {};
|
||
|
if (in && inAlreadyAvailable)
|
||
|
{
|
||
|
currentOffset = reinterpret_cast<const AuUInt8 *>(in) - this->internalInBuffer_;
|
||
|
}
|
||
|
|
||
|
currentOffset += inAlreadyAvailable;
|
||
|
|
||
|
AuUInt32 currentLength = this->internalInLength_ - currentOffset;
|
||
|
if (currentLength > this->internalInLength_)
|
||
|
{
|
||
|
SysPanic("Invalid Buffer Position");
|
||
|
}
|
||
|
|
||
|
auto nextStreamRead = this->internalInBuffer_ + currentOffset;
|
||
|
auto nextStreamSegment = this->internalInBuffer_ + inAlreadyAvailable;
|
||
|
|
||
|
AuUInt read = AuMin(amount, currentLength);
|
||
|
if (reader->Read(AuMemoryViewStreamWrite(this->internalInBuffer_ + currentOffset, this->internalInBuffer_ + read, read)) != IO::EStreamError::eErrorNone)
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
in = (T *)(this->internalInBuffer_ + currentOffset);
|
||
|
inAlreadyAvailable += read;
|
||
|
|
||
|
return read;
|
||
|
}
|
||
|
}
|