diff --git a/framework/content/lzx/decoderstream.cpp b/framework/content/lzx/decoderstream.cpp index 610eb2c..c75e2f0 100644 --- a/framework/content/lzx/decoderstream.cpp +++ b/framework/content/lzx/decoderstream.cpp @@ -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& buffer, Int offset, Int count, xna_error_ptr_arg) + Int LzxDecoderStream::Read(std::vector& 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 const& buffer, Int offset, Int count, xna_error_ptr_arg) + void LzxDecoderStream::Write(std::vector const& buffer, Int offset, Int count) { } - void LzxDecoderStream::WriteByte(Byte value, xna_error_ptr_arg) + void LzxDecoderStream::WriteByte(Byte value) { } } \ No newline at end of file diff --git a/framework/csharp/stream.cpp b/framework/csharp/stream.cpp index 3e414b6..a850f46 100644 --- a/framework/csharp/stream.cpp +++ b/framework/csharp/stream.cpp @@ -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& buffer, Int offset, Int count, xna_error_ptr_arg) { - return Read(buffer.data(), static_cast(buffer.size()), offset, count, err); + Int MemoryStream::Read(std::vector& buffer, Int offset, Int count) { + return Read(buffer.data(), static_cast(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 const& buffer, Int offset, Int count, xna_error_ptr_arg){ - Write(buffer.data(), static_cast(buffer.size()), offset, count, err); + void MemoryStream::Write(std::vector const& buffer, Int offset, Int count){ + Write(buffer.data(), static_cast(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(_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(_fstream.gcount()); } - Int FileStream::Read(std::vector& buffer, Int offset, Int count, xna_error_ptr_arg){ - return Read(buffer.data(), static_cast(buffer.size()), offset, count, err); + Int FileStream::Read(std::vector& buffer, Int offset, Int count){ + return Read(buffer.data(), static_cast(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 const& buffer, Int offset, Int count, xna_error_ptr_arg) { - Write(buffer.data(), static_cast(buffer.size()), offset, count, err); + void FileStream::Write(std::vector const& buffer, Int offset, Int count) { + Write(buffer.data(), static_cast(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; } } } \ No newline at end of file diff --git a/inc/content/lzx/decoderstream.hpp b/inc/content/lzx/decoderstream.hpp index 882a5bf..1c5acb1 100644 --- a/inc/content/lzx/decoderstream.hpp +++ b/inc/content/lzx/decoderstream.hpp @@ -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& 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 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& 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 const& buffer, Int offset, Int count) override; + void WriteByte(Byte value) override; virtual constexpr bool IsClosed() override { return false; } }; } diff --git a/inc/csharp/binary.hpp b/inc/csharp/binary.hpp index b5509d2..08a7d3c 100644 --- a/inc/csharp/binary.hpp +++ b/inc/csharp/binary.hpp @@ -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 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 const& stream) { diff --git a/inc/csharp/buffer.hpp b/inc/csharp/buffer.hpp index 9810c1f..b691720 100644 --- a/inc/csharp/buffer.hpp +++ b/inc/csharp/buffer.hpp @@ -5,13 +5,18 @@ #include 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 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 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); diff --git a/inc/csharp/service.hpp b/inc/csharp/service.hpp index b7a9cb8..4858611 100644 --- a/inc/csharp/service.hpp +++ b/inc/csharp/service.hpp @@ -6,8 +6,11 @@ #include 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; }; } diff --git a/inc/csharp/stream.hpp b/inc/csharp/stream.hpp index d41b8df..d335383 100644 --- a/inc/csharp/stream.hpp +++ b/inc/csharp/stream.hpp @@ -8,22 +8,41 @@ #include 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& 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 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& 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 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& 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 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& 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 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(_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& 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 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& 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 const& buffer, Int offset, Int count) override; + virtual void WriteByte(Byte value) override; public: std::fstream _fstream;