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

Correções em Stream

This commit is contained in:
Danilo 2024-11-08 15:26:57 -03:00
parent 360154e88a
commit 5a3f0e7335
5 changed files with 122 additions and 91 deletions

View File

@ -80,9 +80,7 @@ namespace xna {
std::vector<Byte> charBytes;
std::vector<Char> singleChar;
std::vector<Byte> buffer;
std::vector<Char> charBuffer;
//bool m2BytesPerChar{ false };
std::vector<Char> charBuffer;
};
//A simplified port of the System.IO.BinaryWriter class.
@ -143,9 +141,8 @@ namespace xna {
}
//Writes a 32-bit integer in a compressed format.
void Write7BitEncodedInt(Int value);
void Write7BitEncodedInt(Int value);
//void Write7BitEncodedInt64(Long value);
protected:
sptr<Stream> OutStream = nullptr;

View File

@ -8,17 +8,15 @@ 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.
//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) {
static inline 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.
//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) {
static inline 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

@ -1,69 +1,112 @@
#ifndef XNA_CSHARP_STREAM_HPP
#define XNA_CSHARP_STREAM_HPP
#include "../default.hpp"
#include <fstream>
#include <string>
#include <vector>
#include <cstdint>
namespace xna {
// Provides seek reference points.
// To seek to the end of a stream, call stream.Seek(0, SeekOrigin.End).
enum class SeekOrigin {
Begin,
Current,
End,
};
// Contains constants for specifying how the OS should open a file.
// These will control whether you overwrite a file, open an existing
// file, or some combination thereof.
//
// To append to a file, use Append (which maps to OpenOrCreate then we seek
// to the end of the file). To truncate a file or create it if it doesn't
// exist, use Create.
enum class FileMode {
// Creates a new file. An exception is raised if the file already exists.
CreateNew,
// Creates a new file. If the file already exists, it is overwritten.
Create,
// Opens an existing file. An exception is raised if the file does not exist.
Open,
// Opens the file if it exists. Otherwise, creates a new file.
OpenOrCreate,
// Opens an existing file. Once opened, the file is truncated so that its
// size is zero bytes. The calling process must open the file with at least
// WRITE access. An exception is raised if the file does not exist.
Truncate,
// Opens the file if it exists and seeks to the end. Otherwise,
// creates a new file.
Append,
};
//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(){}
//When overridden in a derived class, gets a value indicating whether the current stream supports reading.
virtual bool CanRead() { return true; }
//When overridden in a derived class, gets a value indicating whether the current stream supports writing.
virtual bool CanWrite() { return true; }
//When overridden in a derived class, gets a value indicating whether the current stream supports seeking.
virtual bool CanSeek() { return true; }
//Gets a value that determines whether the current stream can time out.
virtual bool CanTimeout() { return false; }
//Gets the length in bytes of the stream.
virtual Int Length() = 0;
//Gets the position within the current stream.
virtual Long Position() = 0;
virtual int64_t Length() = 0;
//Gets or sets the position within the current stream.
virtual int64_t Position() = 0;
//Gets or sets the position within the current stream.
virtual void Position(int64_t value) { Seek(value, SeekOrigin::Begin); }
//Closes the current stream and releases any resources
virtual void Close() = 0;
//Sets the position within the current stream.
virtual Long Seek(Long offset, SeekOrigin const& origin) = 0;
virtual int64_t Seek(int64_t 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 int32_t Read(uint8_t* buffer, int32_t bufferLength, int32_t offset, int32_t count) = 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(std::vector<Byte>& buffer, Int offset, Int count) = 0;
virtual int32_t Read(std::vector<uint8_t>& buffer, int32_t offset, int32_t 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;
virtual int32_t ReadByte() = 0;
//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(uint8_t const* buffer, int32_t bufferLength, int32_t offset, int32_t count) = 0;
//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(std::vector<Byte> const& buffer, Int offset, Int count) = 0;
virtual void Write(std::vector<uint8_t> const& buffer, int32_t offset, int32_t 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;
virtual void WriteByte(uint8_t value) = 0;
};
//A simplified port of the System.IO.MemoryStream.
class MemoryStream : public Stream {
public:
constexpr MemoryStream(std::vector<Byte> const& bytes):
constexpr MemoryStream(std::vector<uint8_t> const& bytes):
_buffer(bytes),
_length(static_cast<Int>(bytes.size())){}
_length(static_cast<int64_t>(bytes.size())){}
~MemoryStream() override {
Close();
}
constexpr MemoryStream(Int capacity) :
constexpr MemoryStream(int32_t capacity) :
_buffer(static_cast<size_t>(capacity)),
_length(capacity > 0 ? capacity : 0){}
virtual constexpr Int Length() override {
virtual constexpr int64_t Length() override {
if (_closed)
return 0;
return _length;
}
virtual constexpr Long Position() override {
virtual constexpr int64_t Position() override {
if (_closed)
return 0;
@ -72,39 +115,39 @@ namespace xna {
virtual constexpr void Close() override {
_closed = true;
_buffer = std::vector<Byte>();
_buffer = std::vector<uint8_t>();
}
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;
virtual int64_t Seek(int64_t offset, SeekOrigin const& origin) override;
virtual int32_t Read(uint8_t* buffer, int32_t bufferLength, int32_t offset, int32_t count) override;
virtual int32_t Read(std::vector<uint8_t>& buffer, int32_t offset, int32_t count) override;
virtual int32_t ReadByte() override;
virtual void Write(uint8_t const* buffer, int32_t bufferLength, int32_t offset, int32_t count) override;
virtual void Write(std::vector<uint8_t> const& buffer, int32_t offset, int32_t count) override;
virtual void WriteByte(uint8_t value) override;
public:
std::vector<Byte> _buffer;
std::vector<uint8_t> _buffer;
private:
Int _position{ 0 };
Int _origin{ 0 };
Int _length{ 0 };
int32_t _position{ 0 };
int32_t _origin{ 0 };
int64_t _length{ 0 };
bool _closed{ false };
};
//A simplified port of the System.IO.FileStream.
class FileStream : public Stream {
public:
FileStream(String const& path, FileMode fileMode);
FileStream(String const& path);
FileStream(std::string const& path, FileMode fileMode);
FileStream(std::string const& path);
~FileStream() override {
Close();
}
virtual Int Length() override;
virtual Long Position() override;
virtual int64_t Length() override;
virtual int64_t Position() override;
inline virtual void Close() override {
_closed = true;
@ -113,13 +156,13 @@ namespace xna {
_fstream.close();
}
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;
virtual int64_t Seek(int64_t offset, SeekOrigin const& origin) override;
virtual int32_t Read(uint8_t* buffer, int32_t bufferLength, int32_t offset, int32_t count) override;
virtual int32_t Read(std::vector<uint8_t>& buffer, int32_t offset, int32_t count) override;
virtual int32_t ReadByte() override;
virtual void Write(uint8_t const* buffer, int32_t bufferLength, int32_t offset, int32_t count) override;
virtual void Write(std::vector<uint8_t> const& buffer, int32_t offset, int32_t count) override;
virtual void WriteByte(uint8_t value) override;
public:
std::fstream _fstream;
@ -129,7 +172,7 @@ namespace xna {
bool _closed{ false };
bool _truncated{ false };
Int endOfFile() {
int64_t endOfFile() {
if (_closed)
return 0;
@ -139,7 +182,7 @@ namespace xna {
const auto end = _fstream.tellg();
_fstream.seekg(pos);
return static_cast<Int>(end);
return static_cast<int64_t>(end);
}
};
}

View File

@ -152,16 +152,7 @@ namespace xna {
Texture3D,
TextureCube,
Void
};
enum class FileMode {
CreateNew,
Create,
Append,
Open,
OpenOrCreate,
Truncate
};
};
enum class FillMode
{

View File

@ -1,9 +1,11 @@
#include "xna/csharp/stream.hpp"
#include "xna/csharp/buffer.hpp"
#include "xna/exception.hpp"
#include <filesystem>
namespace xna {
Long MemoryStream::Seek(Long offset, SeekOrigin const& origin) {
Long p = 0;
int64_t MemoryStream::Seek(int64_t offset, SeekOrigin const& origin) {
int64_t p = 0;
switch (origin)
{
@ -32,11 +34,11 @@ namespace xna {
return -1;
}
_position = static_cast<Int>(p);
_position = static_cast<int32_t>(p);
return _position;
}
Int MemoryStream::Read(Byte* buffer, Int bufferLength, Int offset, Int count) {
int32_t MemoryStream::Read(uint8_t* buffer, int32_t bufferLength, int32_t offset, int32_t count) {
if (buffer == nullptr || offset < 0 || count < 0 || bufferLength - offset < count) {
return -1;
}
@ -53,7 +55,7 @@ namespace xna {
buffer[offset + byteCount] = _buffer[_position + static_cast<size_t>(byteCount)];
}
else {
Buffer::BlockCopy<Byte>(_buffer.data(), _position, buffer, offset, off);
Buffer::BlockCopy<uint8_t>(_buffer.data(), _position, buffer, offset, off);
}
_position += off;
@ -61,11 +63,11 @@ namespace xna {
return off;
}
Int MemoryStream::Read(std::vector<Byte>& buffer, Int offset, Int count) {
return Read(buffer.data(), static_cast<Int>(buffer.size()), offset, count);
int32_t MemoryStream::Read(std::vector<uint8_t>& buffer, int32_t offset, int32_t count) {
return Read(buffer.data(), static_cast<int32_t>(buffer.size()), offset, count);
}
Int MemoryStream::ReadByte() {
int32_t MemoryStream::ReadByte() {
if (!_closed)
return 0;
@ -75,7 +77,7 @@ namespace xna {
return _buffer[_position++];
}
void MemoryStream::Write(Byte const* buffer, Int bufferLength, Int offset, Int count){
void MemoryStream::Write(uint8_t const* buffer, int32_t bufferLength, int32_t offset, int32_t count){
if (buffer == nullptr || offset < 0 || count < 0 || bufferLength - offset < count) {
return;
}
@ -100,11 +102,11 @@ namespace xna {
_position = i;
}
void MemoryStream::Write(std::vector<Byte> const& buffer, Int offset, Int count){
Write(buffer.data(), static_cast<Int>(buffer.size()), offset, count);
void MemoryStream::Write(std::vector<uint8_t> const& buffer, int32_t offset, int32_t count){
Write(buffer.data(), static_cast<int32_t>(buffer.size()), offset, count);
}
void MemoryStream::WriteByte(Byte value) {
void MemoryStream::WriteByte(uint8_t value) {
if (_closed)
return;
@ -115,7 +117,7 @@ namespace xna {
_buffer[_position++] = value;
}
FileStream::FileStream(String const& path, FileMode fileMode) {
FileStream::FileStream(std::string const& path, FileMode fileMode) {
int flags = std::fstream::in
| std::fstream::out
| std::fstream::binary;
@ -171,7 +173,7 @@ namespace xna {
Exception::Throw("Failed to open file: " + path);
}
FileStream::FileStream(String const& path) {
FileStream::FileStream(std::string const& path) {
int flags = std::fstream::in
| std::fstream::out
| std::fstream::binary;
@ -187,7 +189,7 @@ namespace xna {
Exception::Throw("Failed to open file: " + path);
}
Int FileStream::Length() {
int64_t FileStream::Length() {
if (_closed)
return 0;
@ -195,14 +197,14 @@ namespace xna {
return end;
}
Long FileStream::Position() {
int64_t FileStream::Position() {
if (_closed)
return 0;
return static_cast<Long>(_fstream.tellg());
return static_cast<int64_t>(_fstream.tellg());
}
Long FileStream::Seek(Long offset, SeekOrigin const& origin){
int64_t FileStream::Seek(int64_t offset, SeekOrigin const& origin){
if (_closed)
return 0;
@ -230,11 +232,11 @@ namespace xna {
return -1;
}
const auto pos = static_cast<Long>(_fstream.tellg());
const auto pos = static_cast<int64_t>(_fstream.tellg());
return pos;
}
Int FileStream::Read(Byte* buffer, Int bufferLength, Int offset, Int count){
int32_t FileStream::Read(uint8_t* buffer, int32_t bufferLength, int32_t offset, int32_t count){
if (buffer == nullptr || offset < 0 || count < 0 || bufferLength - offset < count) {
return -1;
}
@ -249,14 +251,14 @@ namespace xna {
return -1;
}
return static_cast<Int>(_fstream.gcount());
return static_cast<int32_t>(_fstream.gcount());
}
Int FileStream::Read(std::vector<Byte>& buffer, Int offset, Int count){
return Read(buffer.data(), static_cast<Int>(buffer.size()), offset, count);
int32_t FileStream::Read(std::vector<uint8_t>& buffer, int32_t offset, int32_t count){
return Read(buffer.data(), static_cast<int32_t>(buffer.size()), offset, count);
}
Int FileStream::ReadByte(){
int32_t FileStream::ReadByte(){
if (_closed || _truncated)
return 0;
@ -268,12 +270,12 @@ namespace xna {
return -1;
}
const auto result = static_cast<Int>(c);
const auto result = static_cast<int32_t>(c);
return result;
}
void FileStream::Write(Byte const* buffer, Int bufferLength, Int offset, Int count) {
void FileStream::Write(uint8_t const* buffer, int32_t bufferLength, int32_t offset, int32_t count) {
if (buffer == nullptr || offset < 0 || count < 0 || bufferLength - offset < count) {
return;
}
@ -290,11 +292,11 @@ namespace xna {
}
}
void FileStream::Write(std::vector<Byte> const& buffer, Int offset, Int count) {
Write(buffer.data(), static_cast<Int>(buffer.size()), offset, count);
void FileStream::Write(std::vector<uint8_t> const& buffer, int32_t offset, int32_t count) {
Write(buffer.data(), static_cast<int32_t>(buffer.size()), offset, count);
}
void FileStream::WriteByte(Byte value) {
void FileStream::WriteByte(uint8_t value) {
if (_closed)
return;