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:
parent
6151589755
commit
cdf3ea02fe
@ -12,29 +12,29 @@ namespace xna {
|
|||||||
void LzxDecoderStream::Close()
|
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();
|
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();
|
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)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,7 +2,7 @@
|
|||||||
#include "csharp/buffer.hpp"
|
#include "csharp/buffer.hpp"
|
||||||
|
|
||||||
namespace xna {
|
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;
|
Long p = 0;
|
||||||
|
|
||||||
switch (origin)
|
switch (origin)
|
||||||
@ -11,7 +11,6 @@ namespace xna {
|
|||||||
p = _origin + offset;
|
p = _origin + offset;
|
||||||
|
|
||||||
if (p < _origin) {
|
if (p < _origin) {
|
||||||
xna_error_apply(err, XnaErrorCode::OVERFLOW_OPERATION);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -19,7 +18,6 @@ namespace xna {
|
|||||||
p = _position + offset;
|
p = _position + offset;
|
||||||
|
|
||||||
if (p < _origin) {
|
if (p < _origin) {
|
||||||
xna_error_apply(err, XnaErrorCode::OVERFLOW_OPERATION);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -27,12 +25,10 @@ namespace xna {
|
|||||||
p = _length + offset;
|
p = _length + offset;
|
||||||
|
|
||||||
if (p < _origin) {
|
if (p < _origin) {
|
||||||
xna_error_apply(err, XnaErrorCode::OVERFLOW_OPERATION);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,9 +38,8 @@ namespace xna {
|
|||||||
return _position;
|
return _position;
|
||||||
}
|
}
|
||||||
|
|
||||||
Int MemoryStream::Read(Byte* buffer, Int bufferLength, Int offset, Int count, xna_error_ptr_arg) {
|
Int MemoryStream::Read(Byte* buffer, Int bufferLength, Int offset, Int count) {
|
||||||
if (buffer == nullptr || offset < 0 || count < 0 || bufferLength - offset < count) {
|
if (buffer == nullptr || offset < 0 || count < 0 || bufferLength - offset < count) {
|
||||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,11 +63,11 @@ namespace xna {
|
|||||||
return off;
|
return off;
|
||||||
}
|
}
|
||||||
|
|
||||||
Int MemoryStream::Read(std::vector<Byte>& buffer, Int offset, Int count, xna_error_ptr_arg) {
|
Int MemoryStream::Read(std::vector<Byte>& buffer, Int offset, Int count) {
|
||||||
return Read(buffer.data(), static_cast<Int>(buffer.size()), offset, count, err);
|
return Read(buffer.data(), static_cast<Int>(buffer.size()), offset, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
Int MemoryStream::ReadByte(xna_error_ptr_arg) {
|
Int MemoryStream::ReadByte() {
|
||||||
if (!_closed)
|
if (!_closed)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@ -82,9 +77,8 @@ namespace xna {
|
|||||||
return _buffer[_position++];
|
return _buffer[_position++];
|
||||||
}
|
}
|
||||||
|
|
||||||
void MemoryStream::Write(Byte const* buffer, Int bufferLength, Int offset, Int count, xna_error_ptr_arg){
|
void MemoryStream::Write(Byte const* buffer, Int bufferLength, Int offset, Int count){
|
||||||
if (buffer == nullptr || offset < 0 || count < 0 || bufferLength - offset < count) {
|
if (buffer == nullptr || offset < 0 || count < 0 || bufferLength - offset < count) {
|
||||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,7 +88,6 @@ namespace xna {
|
|||||||
auto i = _position + count;
|
auto i = _position + count;
|
||||||
|
|
||||||
if (i < 0 || i > _length) {
|
if (i < 0 || i > _length) {
|
||||||
xna_error_apply(err, XnaErrorCode::OVERFLOW_OPERATION);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,23 +102,110 @@ namespace xna {
|
|||||||
_position = i;
|
_position = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MemoryStream::Write(std::vector<Byte> const& buffer, Int offset, Int count, xna_error_ptr_arg){
|
void MemoryStream::Write(std::vector<Byte> const& buffer, Int offset, Int count){
|
||||||
Write(buffer.data(), static_cast<Int>(buffer.size()), offset, count, err);
|
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)
|
if (_closed)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (_position >= _length) {
|
if (_position >= _length) {
|
||||||
xna_error_apply(err, XnaErrorCode::OVERFLOW_OPERATION);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_buffer[_position++] = value;
|
_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)
|
if (_closed)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@ -143,7 +223,6 @@ namespace xna {
|
|||||||
seek = std::ios_base::end;
|
seek = std::ios_base::end;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,7 +230,6 @@ namespace xna {
|
|||||||
const auto state = _fstream.rdstate();
|
const auto state = _fstream.rdstate();
|
||||||
|
|
||||||
if (state != std::fstream::goodbit) {
|
if (state != std::fstream::goodbit) {
|
||||||
xna_error_apply(err, XnaErrorCode::OVERFLOW_OPERATION);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,9 +237,8 @@ namespace xna {
|
|||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
Int FileStream::Read(Byte* buffer, Int bufferLength, Int offset, Int count, xna_error_ptr_arg){
|
Int FileStream::Read(Byte* buffer, Int bufferLength, Int offset, Int count){
|
||||||
if (buffer == nullptr || offset < 0 || count < 0 || bufferLength - offset < count) {
|
if (buffer == nullptr || offset < 0 || count < 0 || bufferLength - offset < count) {
|
||||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -172,18 +249,17 @@ namespace xna {
|
|||||||
_fstream.read(_buff + offset, count);
|
_fstream.read(_buff + offset, count);
|
||||||
|
|
||||||
if (_fstream.rdstate() != std::fstream::goodbit) {
|
if (_fstream.rdstate() != std::fstream::goodbit) {
|
||||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return static_cast<Int>(_fstream.gcount());
|
return static_cast<Int>(_fstream.gcount());
|
||||||
}
|
}
|
||||||
|
|
||||||
Int FileStream::Read(std::vector<Byte>& buffer, Int offset, Int count, xna_error_ptr_arg){
|
Int FileStream::Read(std::vector<Byte>& buffer, Int offset, Int count){
|
||||||
return Read(buffer.data(), static_cast<Int>(buffer.size()), offset, count, err);
|
return Read(buffer.data(), static_cast<Int>(buffer.size()), offset, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
Int FileStream::ReadByte(xna_error_ptr_arg){
|
Int FileStream::ReadByte(){
|
||||||
if (_closed || _truncated)
|
if (_closed || _truncated)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@ -192,7 +268,6 @@ namespace xna {
|
|||||||
_fstream.read(&c, 1);
|
_fstream.read(&c, 1);
|
||||||
|
|
||||||
if (_fstream.rdstate() != std::fstream::goodbit) {
|
if (_fstream.rdstate() != std::fstream::goodbit) {
|
||||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -201,9 +276,8 @@ namespace xna {
|
|||||||
return result;
|
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) {
|
if (buffer == nullptr || offset < 0 || count < 0 || bufferLength - offset < count) {
|
||||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -215,15 +289,15 @@ namespace xna {
|
|||||||
_fstream.write(_buff + offset, count);
|
_fstream.write(_buff + offset, count);
|
||||||
|
|
||||||
if (_fstream.rdstate() != std::fstream::goodbit) {
|
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) {
|
void FileStream::Write(std::vector<Byte> const& buffer, Int offset, Int count) {
|
||||||
Write(buffer.data(), static_cast<Int>(buffer.size()), offset, count, err);
|
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)
|
if (_closed)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -232,7 +306,7 @@ namespace xna {
|
|||||||
_fstream.write(&c, 1);
|
_fstream.write(&c, 1);
|
||||||
|
|
||||||
if (_fstream.rdstate() != std::fstream::goodbit) {
|
if (_fstream.rdstate() != std::fstream::goodbit) {
|
||||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -75,13 +75,13 @@ namespace xna {
|
|||||||
Int Length() override;
|
Int Length() override;
|
||||||
Long Position() override;
|
Long Position() override;
|
||||||
void Close() override;
|
void Close() override;
|
||||||
Long Seek(Long offset, SeekOrigin const& origin, xna_error_nullarg) override;
|
Long Seek(Long offset, SeekOrigin const& origin) override;
|
||||||
Int Read(Byte* buffer, Int bufferLength, Int offset, Int count, xna_error_nullarg) override;
|
Int Read(Byte* buffer, Int bufferLength, Int offset, Int count) override;
|
||||||
Int Read(std::vector<Byte>& buffer, Int offset, Int count, xna_error_nullarg) override;
|
Int Read(std::vector<Byte>& buffer, Int offset, Int count) override;
|
||||||
Int ReadByte(xna_error_nullarg) override;
|
Int ReadByte() override;
|
||||||
void Write(Byte const* buffer, Int bufferLength, Int offset, Int count, xna_error_nullarg) override;
|
void Write(Byte const* buffer, Int bufferLength, Int offset, Int count) override;
|
||||||
void Write(std::vector<Byte> const& buffer, Int offset, Int count, xna_error_nullarg) override;
|
void Write(std::vector<Byte> const& buffer, Int offset, Int count) override;
|
||||||
void WriteByte(Byte value, xna_error_nullarg) override;
|
void WriteByte(Byte value) override;
|
||||||
virtual constexpr bool IsClosed() override { return false; }
|
virtual constexpr bool IsClosed() override { return false; }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
#include "../default.hpp"
|
#include "../default.hpp"
|
||||||
|
|
||||||
namespace xna {
|
namespace xna {
|
||||||
//A simplified port of the BinaryReader class.
|
//A simplified port of the System.IO.BinaryReader class.
|
||||||
class BinaryReader {
|
class BinaryReader {
|
||||||
public:
|
public:
|
||||||
BinaryReader(sptr<Stream> const& input) {
|
BinaryReader(sptr<Stream> const& input) {
|
||||||
@ -81,7 +81,7 @@ namespace xna {
|
|||||||
//bool m2BytesPerChar{ false };
|
//bool m2BytesPerChar{ false };
|
||||||
};
|
};
|
||||||
|
|
||||||
//A simplified port of the BinaryWriter class.
|
//A simplified port of the System.IO.BinaryWriter class.
|
||||||
class BinaryWriter {
|
class BinaryWriter {
|
||||||
public:
|
public:
|
||||||
BinaryWriter(sptr<Stream> const& stream) {
|
BinaryWriter(sptr<Stream> const& stream) {
|
||||||
|
@ -5,13 +5,18 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace xna {
|
namespace xna {
|
||||||
|
//A simplified port of the System.Buffer class.
|
||||||
class Buffer {
|
class Buffer {
|
||||||
public:
|
public:
|
||||||
|
//Copies from one primitive array to another primitive array without
|
||||||
|
// respecting types.
|
||||||
template <typename T>
|
template <typename T>
|
||||||
static void BlockCopy(T const* src, rsize_t srcOffset, T* dst, rsize_t dstOffset, rsize_t byteCount) {
|
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);
|
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>
|
template <typename TSOURCE, typename TDEST>
|
||||||
static void BlockCopy(TSOURCE const* src, rsize_t srcOffset, TDEST* dst, rsize_t dstOffset, rsize_t byteCount) {
|
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);
|
memmove_s(dst + dstOffset, byteCount, src + srcOffset, byteCount);
|
||||||
|
@ -6,8 +6,11 @@
|
|||||||
#include <any>
|
#include <any>
|
||||||
|
|
||||||
namespace xna {
|
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 {
|
class IServiceProvider {
|
||||||
public:
|
public:
|
||||||
|
//Gets the service object of the specified type.
|
||||||
virtual std::any GetService(Type& serviceType) = 0;
|
virtual std::any GetService(Type& serviceType) = 0;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -8,22 +8,41 @@
|
|||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
|
||||||
namespace xna {
|
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 {
|
class Stream {
|
||||||
public:
|
public:
|
||||||
virtual ~Stream(){}
|
virtual ~Stream(){}
|
||||||
|
//Gets the length in bytes of the stream.
|
||||||
virtual Int Length() = 0;
|
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 void Close() = 0;
|
||||||
virtual bool IsClosed() = 0;
|
virtual bool IsClosed() = 0;
|
||||||
virtual Long Seek(Long offset, SeekOrigin const& origin, xna_error_nullarg) = 0;
|
//Sets the position within the current stream.
|
||||||
virtual Int Read(Byte* buffer, Int bufferLength, Int offset, Int count, xna_error_nullarg) = 0;
|
virtual Long Seek(Long offset, SeekOrigin const& origin) = 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;
|
//Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
|
||||||
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;
|
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 {
|
class MemoryStream : public Stream {
|
||||||
public:
|
public:
|
||||||
constexpr MemoryStream(Int capacity) :
|
constexpr MemoryStream(Int capacity) :
|
||||||
@ -53,13 +72,13 @@ namespace xna {
|
|||||||
return _closed;
|
return _closed;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual Long Seek(Long offset, SeekOrigin const& origin, xna_error_nullarg) override;
|
virtual Long Seek(Long offset, SeekOrigin const& origin) override;
|
||||||
virtual Int Read(Byte* buffer, Int bufferLength, Int offset, Int count, xna_error_nullarg) override;
|
virtual Int Read(Byte* buffer, Int bufferLength, Int offset, Int count) override;
|
||||||
virtual Int Read(std::vector<Byte>& buffer, Int offset, Int count, xna_error_nullarg) override;
|
virtual Int Read(std::vector<Byte>& buffer, Int offset, Int count) override;
|
||||||
virtual Int ReadByte(xna_error_nullarg) override;
|
virtual Int ReadByte() override;
|
||||||
virtual void Write(Byte const* buffer, Int bufferLength, Int offset, Int count, xna_error_nullarg) 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, xna_error_nullarg) override;
|
virtual void Write(std::vector<Byte> const& buffer, Int offset, Int count) override;
|
||||||
virtual void WriteByte(Byte value, xna_error_nullarg) override;
|
virtual void WriteByte(Byte value) override;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Int _position{ 0 };
|
Int _position{ 0 };
|
||||||
@ -69,118 +88,37 @@ namespace xna {
|
|||||||
bool _closed{ false };
|
bool _closed{ false };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//A simplified port of the System.IO.FileStream.
|
||||||
class FileStream : public Stream {
|
class FileStream : public Stream {
|
||||||
public:
|
public:
|
||||||
FileStream(String const& path, FileMode fileMode) {
|
FileStream(String const& path, FileMode fileMode);
|
||||||
int flags = std::fstream::in
|
FileStream(String const& path);
|
||||||
| 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() {
|
~FileStream() {
|
||||||
Close();
|
Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual Int Length() override {
|
virtual Int Length() override;
|
||||||
if (_closed)
|
virtual Long Position() override;
|
||||||
return 0;
|
|
||||||
|
|
||||||
const auto end = endOfFile();
|
inline virtual void Close() override {
|
||||||
return end;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual Long Position() override {
|
|
||||||
if (_closed)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
return static_cast<Long>(_fstream.tellg());
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void Close() override {
|
|
||||||
_closed = true;
|
_closed = true;
|
||||||
|
|
||||||
if(_fstream.is_open())
|
if(_fstream.is_open())
|
||||||
_fstream.close();
|
_fstream.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual constexpr bool IsClosed() override {
|
inline virtual constexpr bool IsClosed() override {
|
||||||
return _closed;
|
return _closed;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual Long Seek(Long offset, SeekOrigin const& origin, xna_error_nullarg) override;
|
virtual Long Seek(Long offset, SeekOrigin const& origin) override;
|
||||||
virtual Int Read(Byte* buffer, Int bufferLength, Int offset, Int count, xna_error_nullarg) override;
|
virtual Int Read(Byte* buffer, Int bufferLength, Int offset, Int count) override;
|
||||||
virtual Int Read(std::vector<Byte>& buffer, Int offset, Int count, xna_error_nullarg) override;
|
virtual Int Read(std::vector<Byte>& buffer, Int offset, Int count) override;
|
||||||
virtual Int ReadByte(xna_error_nullarg) override;
|
virtual Int ReadByte() override;
|
||||||
virtual void Write(Byte const* buffer, Int bufferLength, Int offset, Int count, xna_error_nullarg) 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, xna_error_nullarg) override;
|
virtual void Write(std::vector<Byte> const& buffer, Int offset, Int count) override;
|
||||||
virtual void WriteByte(Byte value, xna_error_nullarg) override;
|
virtual void WriteByte(Byte value) override;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
std::fstream _fstream;
|
std::fstream _fstream;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user