2024-03-26 10:32:56 -03:00
|
|
|
|
#ifndef XNA_CSHARP_STREAM_HPP
|
|
|
|
|
#define XNA_CSHARP_STREAM_HPP
|
|
|
|
|
|
|
|
|
|
#include "../types.hpp"
|
|
|
|
|
#include "../enums.hpp"
|
|
|
|
|
#include "../xnaerror.hpp"
|
2024-03-26 15:56:17 -03:00
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <filesystem>
|
2024-03-26 10:32:56 -03:00
|
|
|
|
|
|
|
|
|
namespace xna {
|
|
|
|
|
class Stream {
|
|
|
|
|
public:
|
2024-05-01 19:09:43 -03:00
|
|
|
|
virtual ~Stream(){}
|
2024-03-26 10:32:56 -03:00
|
|
|
|
virtual Int Length() = 0;
|
2024-03-26 17:22:00 -03:00
|
|
|
|
virtual Long Position() = 0;
|
2024-03-26 10:32:56 -03:00
|
|
|
|
virtual void Close() = 0;
|
2024-05-09 09:13:26 -03:00
|
|
|
|
virtual bool IsClosed() = 0;
|
2024-03-26 10:32:56 -03:00
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class MemoryStream : public Stream {
|
|
|
|
|
public:
|
|
|
|
|
constexpr MemoryStream(Int capacity) :
|
|
|
|
|
_buffer(static_cast<size_t>(capacity)),
|
|
|
|
|
_length(capacity > 0 ? capacity : 0){}
|
|
|
|
|
|
|
|
|
|
virtual constexpr Int Length() override {
|
|
|
|
|
if (_closed)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return _length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual constexpr Long Position() override {
|
|
|
|
|
if (_closed)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return _position;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual constexpr void Close() override {
|
|
|
|
|
_closed = true;
|
|
|
|
|
_buffer = std::vector<Byte>();
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-09 09:13:26 -03:00
|
|
|
|
virtual constexpr bool IsClosed() override {
|
|
|
|
|
return _closed;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 19:09:43 -03:00
|
|
|
|
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;
|
2024-03-26 10:32:56 -03:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
Int _position{ 0 };
|
|
|
|
|
Int _origin{ 0 };
|
|
|
|
|
Int _length{ 0 };
|
|
|
|
|
std::vector<Byte> _buffer;
|
|
|
|
|
bool _closed{ false };
|
|
|
|
|
};
|
2024-03-26 15:56:17 -03:00
|
|
|
|
|
|
|
|
|
class FileStream : public Stream {
|
|
|
|
|
public:
|
2024-05-09 09:13:26 -03:00
|
|
|
|
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<74>rio, um novo arquivo dever<65> 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<65> ser truncado
|
|
|
|
|
//para que seu tamanho seja zero bytes.
|
|
|
|
|
//Tentativa de ler um arquivo truncado retornar<61> 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){
|
2024-03-26 15:56:17 -03:00
|
|
|
|
int flags = std::fstream::in
|
|
|
|
|
| std::fstream::out
|
2024-03-26 17:22:00 -03:00
|
|
|
|
| std::fstream::binary;
|
|
|
|
|
//| std::fstream::ate;
|
2024-03-26 15:56:17 -03:00
|
|
|
|
|
|
|
|
|
const auto exists = std::filesystem::exists(path);
|
|
|
|
|
|
|
|
|
|
if (!exists)
|
|
|
|
|
flags |= std::fstream::trunc;
|
|
|
|
|
|
|
|
|
|
_fstream.open(path.c_str(), flags);
|
|
|
|
|
|
2024-05-09 09:13:26 -03:00
|
|
|
|
if (!_fstream.good())
|
2024-03-26 15:56:17 -03:00
|
|
|
|
_closed = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~FileStream() {
|
|
|
|
|
Close();
|
2024-03-26 17:22:00 -03:00
|
|
|
|
}
|
2024-03-26 15:56:17 -03:00
|
|
|
|
|
2024-03-26 17:22:00 -03:00
|
|
|
|
virtual Int Length() override {
|
2024-03-26 15:56:17 -03:00
|
|
|
|
if (_closed)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2024-03-26 17:22:00 -03:00
|
|
|
|
const auto end = endOfFile();
|
|
|
|
|
return end;
|
2024-03-26 15:56:17 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual Long Position() override {
|
|
|
|
|
if (_closed)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return static_cast<Long>(_fstream.tellg());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void Close() override {
|
|
|
|
|
_closed = true;
|
|
|
|
|
|
|
|
|
|
if(_fstream.is_open())
|
|
|
|
|
_fstream.close();
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-09 09:13:26 -03:00
|
|
|
|
virtual constexpr bool IsClosed() override {
|
|
|
|
|
return _closed;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 19:09:43 -03:00
|
|
|
|
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;
|
2024-03-26 15:56:17 -03:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
std::fstream _fstream;
|
2024-05-08 20:42:15 -03:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::streampos _filesize{ 0 };
|
2024-03-26 15:56:17 -03:00
|
|
|
|
bool _closed{ false };
|
2024-05-09 09:13:26 -03:00
|
|
|
|
bool _truncated{ false };
|
2024-03-26 17:22:00 -03:00
|
|
|
|
|
|
|
|
|
Int endOfFile() {
|
|
|
|
|
if (_closed)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
const auto pos = _fstream.tellg();
|
|
|
|
|
_fstream.seekg(0, std::ios_base::end);
|
|
|
|
|
|
|
|
|
|
const auto end = _fstream.tellg();
|
|
|
|
|
_fstream.seekg(pos);
|
|
|
|
|
|
|
|
|
|
return static_cast<Int>(end);
|
|
|
|
|
}
|
2024-03-26 15:56:17 -03:00
|
|
|
|
};
|
2024-03-26 10:32:56 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|