1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00

Implementações em Stream

This commit is contained in:
Danilo 2024-05-29 09:42:18 -03:00
parent 6151589755
commit cdf3ea02fe
7 changed files with 185 additions and 165 deletions

View File

@ -12,29 +12,29 @@ namespace xna {
void LzxDecoderStream::Close()
{
}
Long LzxDecoderStream::Seek(Long offset, SeekOrigin const& origin, xna_error_ptr_arg)
Long LzxDecoderStream::Seek(Long offset, SeekOrigin const& origin)
{
return Long();
}
Int LzxDecoderStream::Read(Byte* buffer, Int bufferLength, Int offset, Int count, xna_error_ptr_arg)
Int LzxDecoderStream::Read(Byte* buffer, Int bufferLength, Int offset, Int count)
{
return decompressedStream->Read(buffer, bufferLength, offset, count, err);
return decompressedStream->Read(buffer, bufferLength, offset, count);
}
Int LzxDecoderStream::Read(std::vector<Byte>& buffer, Int offset, Int count, xna_error_ptr_arg)
Int LzxDecoderStream::Read(std::vector<Byte>& buffer, Int offset, Int count)
{
return decompressedStream->Read(buffer, offset, count, err);
return decompressedStream->Read(buffer, offset, count);
}
Int LzxDecoderStream::ReadByte(xna_error_ptr_arg)
Int LzxDecoderStream::ReadByte()
{
return Int();
}
void LzxDecoderStream::Write(Byte const* buffer, Int bufferLength, Int offset, Int count, xna_error_ptr_arg)
void LzxDecoderStream::Write(Byte const* buffer, Int bufferLength, Int offset, Int count)
{
}
void LzxDecoderStream::Write(std::vector<Byte> const& buffer, Int offset, Int count, xna_error_ptr_arg)
void LzxDecoderStream::Write(std::vector<Byte> const& buffer, Int offset, Int count)
{
}
void LzxDecoderStream::WriteByte(Byte value, xna_error_ptr_arg)
void LzxDecoderStream::WriteByte(Byte value)
{
}
}

View File

@ -2,7 +2,7 @@
#include "csharp/buffer.hpp"
namespace xna {
Long MemoryStream::Seek(Long offset, SeekOrigin const& origin, xna_error_ptr_arg) {
Long MemoryStream::Seek(Long offset, SeekOrigin const& origin) {
Long p = 0;
switch (origin)
@ -11,7 +11,6 @@ namespace xna {
p = _origin + offset;
if (p < _origin) {
xna_error_apply(err, XnaErrorCode::OVERFLOW_OPERATION);
return -1;
}
break;
@ -19,7 +18,6 @@ namespace xna {
p = _position + offset;
if (p < _origin) {
xna_error_apply(err, XnaErrorCode::OVERFLOW_OPERATION);
return -1;
}
break;
@ -27,12 +25,10 @@ namespace xna {
p = _length + offset;
if (p < _origin) {
xna_error_apply(err, XnaErrorCode::OVERFLOW_OPERATION);
return -1;
}
break;
default:
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
return -1;
}
@ -42,9 +38,8 @@ namespace xna {
return _position;
}
Int MemoryStream::Read(Byte* buffer, Int bufferLength, Int offset, Int count, xna_error_ptr_arg) {
if (buffer == nullptr || offset < 0 || count < 0 || bufferLength - offset < count) {
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
Int MemoryStream::Read(Byte* buffer, Int bufferLength, Int offset, Int count) {
if (buffer == nullptr || offset < 0 || count < 0 || bufferLength - offset < count) {
return -1;
}
@ -68,11 +63,11 @@ namespace xna {
return off;
}
Int MemoryStream::Read(std::vector<Byte>& buffer, Int offset, Int count, xna_error_ptr_arg) {
return Read(buffer.data(), static_cast<Int>(buffer.size()), offset, count, err);
Int MemoryStream::Read(std::vector<Byte>& buffer, Int offset, Int count) {
return Read(buffer.data(), static_cast<Int>(buffer.size()), offset, count);
}
Int MemoryStream::ReadByte(xna_error_ptr_arg) {
Int MemoryStream::ReadByte() {
if (!_closed)
return 0;
@ -82,9 +77,8 @@ namespace xna {
return _buffer[_position++];
}
void MemoryStream::Write(Byte const* buffer, Int bufferLength, Int offset, Int count, xna_error_ptr_arg){
if (buffer == nullptr || offset < 0 || count < 0 || bufferLength - offset < count) {
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
void MemoryStream::Write(Byte const* buffer, Int bufferLength, Int offset, Int count){
if (buffer == nullptr || offset < 0 || count < 0 || bufferLength - offset < count) {
return;
}
@ -94,7 +88,6 @@ namespace xna {
auto i = _position + count;
if (i < 0 || i > _length) {
xna_error_apply(err, XnaErrorCode::OVERFLOW_OPERATION);
return;
}
@ -109,23 +102,110 @@ namespace xna {
_position = i;
}
void MemoryStream::Write(std::vector<Byte> const& buffer, Int offset, Int count, xna_error_ptr_arg){
Write(buffer.data(), static_cast<Int>(buffer.size()), offset, count, err);
void MemoryStream::Write(std::vector<Byte> const& buffer, Int offset, Int count){
Write(buffer.data(), static_cast<Int>(buffer.size()), offset, count);
}
void MemoryStream::WriteByte(Byte value, xna_error_ptr_arg) {
void MemoryStream::WriteByte(Byte value) {
if (_closed)
return;
if (_position >= _length) {
xna_error_apply(err, XnaErrorCode::OVERFLOW_OPERATION);
return;
}
_buffer[_position++] = value;
}
Long FileStream::Seek(Long offset, SeekOrigin const& origin, xna_error_ptr_arg){
FileStream::FileStream(String const& path, FileMode fileMode) {
int flags = std::fstream::in
| std::fstream::out
| std::fstream::binary;
const auto exists = std::filesystem::exists(path);
switch (fileMode)
{
//Especifica se deve abrir um arquivo existente.
case FileMode::Open:
if (!exists) {
_closed = true;
return;
}
break;
//Especifica que se deve abrir um arquivo, se existir;
// caso contrário, um novo arquivo deverá ser criado.
case FileMode::OpenOrCreate:
case FileMode::Create:
if (!exists)
flags |= std::fstream::trunc;
break;
//Especifica que o sistema operacional deve criar um novo arquivo.
//Se o arquivo já existir, não abre o arquivo.
case FileMode::CreateNew:
if (!exists)
flags |= std::fstream::trunc;
else
return;
break;
//Abre o arquivo, se existir, e busca o final do arquivo ou cria um novo arquivo.
case FileMode::Append:
if (!exists)
flags |= std::fstream::trunc;
else
flags |= std::fstream::app;
break;
//Especifica que se deve abrir um arquivo existente.
//Quando o arquivo for aberto, ele deverá ser truncado
//para que seu tamanho seja zero bytes.
//Tentativa de ler um arquivo truncado retornará 0;
case FileMode::Truncate:
flags |= std::fstream::trunc;
_truncated = true;
break;
default:
break;
}
_fstream.open(path.c_str(), flags);
if (!_fstream.good())
_closed = true;
}
FileStream::FileStream(String const& path) {
int flags = std::fstream::in
| std::fstream::out
| std::fstream::binary;
//| std::fstream::ate;
const auto exists = std::filesystem::exists(path);
if (!exists)
flags |= std::fstream::trunc;
_fstream.open(path.c_str(), flags);
if (!_fstream.good())
_closed = true;
}
Int FileStream::Length() {
if (_closed)
return 0;
const auto end = endOfFile();
return end;
}
Long FileStream::Position() {
if (_closed)
return 0;
return static_cast<Long>(_fstream.tellg());
}
Long FileStream::Seek(Long offset, SeekOrigin const& origin){
if (_closed)
return 0;
@ -143,7 +223,6 @@ namespace xna {
seek = std::ios_base::end;
break;
default:
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
return -1;
}
@ -151,7 +230,6 @@ namespace xna {
const auto state = _fstream.rdstate();
if (state != std::fstream::goodbit) {
xna_error_apply(err, XnaErrorCode::OVERFLOW_OPERATION);
return -1;
}
@ -159,9 +237,8 @@ namespace xna {
return pos;
}
Int FileStream::Read(Byte* buffer, Int bufferLength, Int offset, Int count, xna_error_ptr_arg){
if (buffer == nullptr || offset < 0 || count < 0 || bufferLength - offset < count) {
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
Int FileStream::Read(Byte* buffer, Int bufferLength, Int offset, Int count){
if (buffer == nullptr || offset < 0 || count < 0 || bufferLength - offset < count) {
return -1;
}
@ -172,18 +249,17 @@ namespace xna {
_fstream.read(_buff + offset, count);
if (_fstream.rdstate() != std::fstream::goodbit) {
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
return -1;
}
return static_cast<Int>(_fstream.gcount());
}
Int FileStream::Read(std::vector<Byte>& buffer, Int offset, Int count, xna_error_ptr_arg){
return Read(buffer.data(), static_cast<Int>(buffer.size()), offset, count, err);
Int FileStream::Read(std::vector<Byte>& buffer, Int offset, Int count){
return Read(buffer.data(), static_cast<Int>(buffer.size()), offset, count);
}
Int FileStream::ReadByte(xna_error_ptr_arg){
Int FileStream::ReadByte(){
if (_closed || _truncated)
return 0;
@ -192,7 +268,6 @@ namespace xna {
_fstream.read(&c, 1);
if (_fstream.rdstate() != std::fstream::goodbit) {
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
return -1;
}
@ -201,9 +276,8 @@ namespace xna {
return result;
}
void FileStream::Write(Byte const* buffer, Int bufferLength, Int offset, Int count, xna_error_ptr_arg) {
void FileStream::Write(Byte const* buffer, Int bufferLength, Int offset, Int count) {
if (buffer == nullptr || offset < 0 || count < 0 || bufferLength - offset < count) {
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
return;
}
@ -215,15 +289,15 @@ namespace xna {
_fstream.write(_buff + offset, count);
if (_fstream.rdstate() != std::fstream::goodbit) {
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
return;
}
}
void FileStream::Write(std::vector<Byte> const& buffer, Int offset, Int count, xna_error_ptr_arg) {
Write(buffer.data(), static_cast<Int>(buffer.size()), offset, count, err);
void FileStream::Write(std::vector<Byte> const& buffer, Int offset, Int count) {
Write(buffer.data(), static_cast<Int>(buffer.size()), offset, count);
}
void FileStream::WriteByte(Byte value, xna_error_ptr_arg) {
void FileStream::WriteByte(Byte value) {
if (_closed)
return;
@ -232,7 +306,7 @@ namespace xna {
_fstream.write(&c, 1);
if (_fstream.rdstate() != std::fstream::goodbit) {
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
return;
}
}
}

View File

@ -75,13 +75,13 @@ namespace xna {
Int Length() override;
Long Position() override;
void Close() override;
Long Seek(Long offset, SeekOrigin const& origin, xna_error_nullarg) override;
Int Read(Byte* buffer, Int bufferLength, Int offset, Int count, xna_error_nullarg) override;
Int Read(std::vector<Byte>& buffer, Int offset, Int count, xna_error_nullarg) override;
Int ReadByte(xna_error_nullarg) override;
void Write(Byte const* buffer, Int bufferLength, Int offset, Int count, xna_error_nullarg) override;
void Write(std::vector<Byte> const& buffer, Int offset, Int count, xna_error_nullarg) override;
void WriteByte(Byte value, xna_error_nullarg) override;
Long Seek(Long offset, SeekOrigin const& origin) override;
Int Read(Byte* buffer, Int bufferLength, Int offset, Int count) override;
Int Read(std::vector<Byte>& buffer, Int offset, Int count) override;
Int ReadByte() override;
void Write(Byte const* buffer, Int bufferLength, Int offset, Int count) override;
void Write(std::vector<Byte> const& buffer, Int offset, Int count) override;
void WriteByte(Byte value) override;
virtual constexpr bool IsClosed() override { return false; }
};
}

View File

@ -5,7 +5,7 @@
#include "../default.hpp"
namespace xna {
//A simplified port of the BinaryReader class.
//A simplified port of the System.IO.BinaryReader class.
class BinaryReader {
public:
BinaryReader(sptr<Stream> const& input) {
@ -81,7 +81,7 @@ namespace xna {
//bool m2BytesPerChar{ false };
};
//A simplified port of the BinaryWriter class.
//A simplified port of the System.IO.BinaryWriter class.
class BinaryWriter {
public:
BinaryWriter(sptr<Stream> const& stream) {

View File

@ -5,13 +5,18 @@
#include <vector>
namespace xna {
//A simplified port of the System.Buffer class.
class Buffer {
public:
//Copies from one primitive array to another primitive array without
// respecting types.
template <typename T>
static void BlockCopy(T const* src, rsize_t srcOffset, T* dst, rsize_t dstOffset, rsize_t byteCount) {
memmove_s(dst + dstOffset, byteCount, src + srcOffset, byteCount);
}
//Copies from one primitive array to another primitive array without
// respecting types.
template <typename TSOURCE, typename TDEST>
static void BlockCopy(TSOURCE const* src, rsize_t srcOffset, TDEST* dst, rsize_t dstOffset, rsize_t byteCount) {
memmove_s(dst + dstOffset, byteCount, src + srcOffset, byteCount);

View File

@ -6,8 +6,11 @@
#include <any>
namespace xna {
//A simplified port of the System.IServiceProvider
//Defines a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.
class IServiceProvider {
public:
//Gets the service object of the specified type.
virtual std::any GetService(Type& serviceType) = 0;
};
}

View File

@ -8,22 +8,41 @@
#include <filesystem>
namespace xna {
//A simplified port of the System.IO.Stream.
//Provides a generic view of a sequence of bytes. This is an abstract class.
class Stream {
public:
virtual ~Stream(){}
//Gets the length in bytes of the stream.
virtual Int Length() = 0;
virtual Long Position() = 0;
//Gets the position within the current stream.
virtual Long Position() = 0;
//Closes the current stream and releases any resources
virtual void Close() = 0;
virtual bool IsClosed() = 0;
virtual Long Seek(Long offset, SeekOrigin const& origin, xna_error_nullarg) = 0;
virtual Int Read(Byte* buffer, Int bufferLength, Int offset, Int count, xna_error_nullarg) = 0;
virtual Int Read(std::vector<Byte>& buffer, Int offset, Int count, xna_error_nullarg) = 0;
virtual Int ReadByte(xna_error_nullarg) = 0;
virtual void Write(Byte const* buffer, Int bufferLength, Int offset, Int count, xna_error_nullarg) = 0;
virtual void Write(std::vector<Byte> const& buffer, Int offset, Int count, xna_error_nullarg) = 0;
virtual void WriteByte(Byte value, xna_error_nullarg) = 0;
//Sets the position within the current stream.
virtual Long Seek(Long offset, SeekOrigin const& origin) = 0;
//
//Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
//
virtual Int Read(Byte* buffer, Int bufferLength, Int offset, Int count) = 0;
virtual Int Read(std::vector<Byte>& buffer, Int offset, Int count) = 0;
//Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.
virtual Int ReadByte() = 0;
//
//When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
//
virtual void Write(Byte const* buffer, Int bufferLength, Int offset, Int count) = 0;
virtual void Write(std::vector<Byte> const& buffer, Int offset, Int count) = 0;
//Writes a byte to the current position in the stream and advances the position within the stream by one byte.
virtual void WriteByte(Byte value) = 0;
};
//A simplified port of the System.IO.MemoryStream.
class MemoryStream : public Stream {
public:
constexpr MemoryStream(Int capacity) :
@ -53,13 +72,13 @@ namespace xna {
return _closed;
}
virtual Long Seek(Long offset, SeekOrigin const& origin, xna_error_nullarg) override;
virtual Int Read(Byte* buffer, Int bufferLength, Int offset, Int count, xna_error_nullarg) override;
virtual Int Read(std::vector<Byte>& buffer, Int offset, Int count, xna_error_nullarg) override;
virtual Int ReadByte(xna_error_nullarg) override;
virtual void Write(Byte const* buffer, Int bufferLength, Int offset, Int count, xna_error_nullarg) override;
virtual void Write(std::vector<Byte> const& buffer, Int offset, Int count, xna_error_nullarg) override;
virtual void WriteByte(Byte value, xna_error_nullarg) override;
virtual Long Seek(Long offset, SeekOrigin const& origin) override;
virtual Int Read(Byte* buffer, Int bufferLength, Int offset, Int count) override;
virtual Int Read(std::vector<Byte>& buffer, Int offset, Int count) override;
virtual Int ReadByte() override;
virtual void Write(Byte const* buffer, Int bufferLength, Int offset, Int count) override;
virtual void Write(std::vector<Byte> const& buffer, Int offset, Int count) override;
virtual void WriteByte(Byte value) override;
public:
Int _position{ 0 };
@ -69,118 +88,37 @@ namespace xna {
bool _closed{ false };
};
//A simplified port of the System.IO.FileStream.
class FileStream : public Stream {
public:
FileStream(String const& path, FileMode fileMode) {
int flags = std::fstream::in
| std::fstream::out
| std::fstream::binary;
const auto exists = std::filesystem::exists(path);
switch (fileMode)
{
//Especifica se deve abrir um arquivo existente.
case FileMode::Open:
if (!exists) {
_closed = true;
return;
}
break;
//Especifica que se deve abrir um arquivo, se existir;
// caso contrário, um novo arquivo deverá ser criado.
case FileMode::OpenOrCreate:
case FileMode::Create:
if (!exists)
flags |= std::fstream::trunc;
break;
//Especifica que o sistema operacional deve criar um novo arquivo.
//Se o arquivo já existir, não abre o arquivo.
case FileMode::CreateNew:
if (!exists)
flags |= std::fstream::trunc;
else
return;
break;
//Abre o arquivo, se existir, e busca o final do arquivo ou cria um novo arquivo.
case FileMode::Append:
if (!exists)
flags |= std::fstream::trunc;
else
flags |= std::fstream::app;
break;
//Especifica que se deve abrir um arquivo existente.
//Quando o arquivo for aberto, ele deverá ser truncado
//para que seu tamanho seja zero bytes.
//Tentativa de ler um arquivo truncado retornará 0;
case FileMode::Truncate:
flags |= std::fstream::trunc;
_truncated = true;
break;
default:
break;
}
_fstream.open(path.c_str(), flags);
if (!_fstream.good())
_closed = true;
}
FileStream(String const& path){
int flags = std::fstream::in
| std::fstream::out
| std::fstream::binary;
//| std::fstream::ate;
const auto exists = std::filesystem::exists(path);
if (!exists)
flags |= std::fstream::trunc;
_fstream.open(path.c_str(), flags);
if (!_fstream.good())
_closed = true;
}
FileStream(String const& path, FileMode fileMode);
FileStream(String const& path);
~FileStream() {
Close();
}
virtual Int Length() override {
if (_closed)
return 0;
virtual Int Length() override;
virtual Long Position() override;
const auto end = endOfFile();
return end;
}
virtual Long Position() override {
if (_closed)
return 0;
return static_cast<Long>(_fstream.tellg());
}
virtual void Close() override {
inline virtual void Close() override {
_closed = true;
if(_fstream.is_open())
_fstream.close();
}
virtual constexpr bool IsClosed() override {
inline virtual constexpr bool IsClosed() override {
return _closed;
}
virtual Long Seek(Long offset, SeekOrigin const& origin, xna_error_nullarg) override;
virtual Int Read(Byte* buffer, Int bufferLength, Int offset, Int count, xna_error_nullarg) override;
virtual Int Read(std::vector<Byte>& buffer, Int offset, Int count, xna_error_nullarg) override;
virtual Int ReadByte(xna_error_nullarg) override;
virtual void Write(Byte const* buffer, Int bufferLength, Int offset, Int count, xna_error_nullarg) override;
virtual void Write(std::vector<Byte> const& buffer, Int offset, Int count, xna_error_nullarg) override;
virtual void WriteByte(Byte value, xna_error_nullarg) override;
virtual Long Seek(Long offset, SeekOrigin const& origin) override;
virtual Int Read(Byte* buffer, Int bufferLength, Int offset, Int count) override;
virtual Int Read(std::vector<Byte>& buffer, Int offset, Int count) override;
virtual Int ReadByte() override;
virtual void Write(Byte const* buffer, Int bufferLength, Int offset, Int count) override;
virtual void Write(std::vector<Byte> const& buffer, Int offset, Int count) override;
virtual void WriteByte(Byte value) override;
public:
std::fstream _fstream;