AuroraRuntime/Source/IO/Character/StringToProvider.cpp

86 lines
2.2 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: StringToProvider.cpp
Date: 2022-1-31
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "StringToProvider.hpp"
namespace Aurora::IO::Character
{
class CharacterProvider : public ICharacterProviderEx
{
public:
CharacterProvider(const AuString &in, AuUInt offset = 0);
CharacterProvider(const AuSPtr<AuString> &in, AuUInt offset = 0);
bool GetByte(AuUInt8 &val) override;
AuUInt GetPosition() override;
bool SetPosition(AuUInt offset) override;
protected:
AuUInt offset_ {};
AuSPtr<AuString> string_;
};
CharacterProvider::CharacterProvider(const AuString &in, AuUInt offset) : string_(AuMakeShared<AuString>(in)), offset_(offset)
{}
CharacterProvider::CharacterProvider(const AuSPtr<AuString> &in, AuUInt offset) : string_(in), offset_(offset)
{}
bool CharacterProvider::GetByte(AuUInt8 &val)
{
if (!this->string_)
{
return false;
}
auto offset = this->offset_++;
if (offset >= this->string_->size())
{
offset = this->string_->size();
return false;
}
val = this->string_->at(offset);
return true;
}
AuUInt CharacterProvider::GetPosition()
{
return this->offset_;
}
bool CharacterProvider::SetPosition(AuUInt offset)
{
if (!this->string_)
{
return false;
}
this->offset_ = offset;
return this->offset_ < this->string_->size();
}
AUKN_SYM ICharacterProviderEx *ProviderFromSharedStringNew(const AuSPtr<AuString> &str, AuUInt index)
{
return _new CharacterProvider(str, index);
}
AUKN_SYM void ProviderFromSharedStringRelease(ICharacterProviderEx *me)
{
AuSafeDelete<CharacterProvider *>(me);
}
AUKN_SYM ICharacterProviderEx *ProviderFromStringNew(const AuString &str, AuUInt index)
{
return _new CharacterProvider(str, index);
}
AUKN_SYM void ProviderFromStringRelease(ICharacterProviderEx *me)
{
AuSafeDelete<CharacterProvider *>(me);
}
}