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

147 lines
4.4 KiB
C++
Raw Normal View History

2024-03-26 10:32:56 -03:00
#ifndef XNA_CSHARP_STREAM_HPP
#define XNA_CSHARP_STREAM_HPP
2024-07-13 22:50:52 -03:00
#include "../default.hpp"
2024-03-26 10:32:56 -03:00
namespace xna {
2024-07-13 22:50:52 -03:00
enum class SeekOrigin {
Begin,
Current,
End,
};
2024-05-29 09:42:18 -03:00
//A simplified port of the System.IO.Stream.
//Provides a generic view of a sequence of bytes. This is an abstract class.
2024-03-26 10:32:56 -03:00
class Stream {
public:
2024-05-01 19:09:43 -03:00
virtual ~Stream(){}
2024-05-29 09:42:18 -03:00
//Gets the length in bytes of the stream.
2024-03-26 10:32:56 -03:00
virtual Int Length() = 0;
2024-05-29 09:42:18 -03:00
//Gets the position within the current stream.
virtual Long Position() = 0;
//Closes the current stream and releases any resources
2024-03-26 10:32:56 -03:00
virtual void Close() = 0;
2024-05-29 09:42:18 -03:00
//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;
2024-07-13 22:50:52 -03:00
//Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
2024-05-29 09:42:18 -03:00
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;
2024-07-13 22:50:52 -03:00
//Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
2024-05-29 09:42:18 -03:00
virtual void Write(Byte const* buffer, Int bufferLength, Int offset, Int count) = 0;
2024-07-13 22:50:52 -03:00
//Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
2024-05-29 09:42:18 -03:00
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;
2024-03-26 10:32:56 -03:00
};
2024-05-29 09:42:18 -03:00
//A simplified port of the System.IO.MemoryStream.
2024-03-26 10:32:56 -03:00
class MemoryStream : public Stream {
public:
2024-05-29 15:58:37 -03:00
constexpr MemoryStream(std::vector<Byte> const& bytes):
_buffer(bytes),
_length(static_cast<Int>(bytes.size())){}
2024-05-29 15:58:37 -03:00
2024-07-13 22:50:52 -03:00
~MemoryStream() override {
Close();
}
2024-03-26 10:32:56 -03:00
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-07-13 22:50:52 -03:00
}
2024-05-09 09:13:26 -03:00
2024-05-29 09:42:18 -03:00
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;
2024-06-04 14:37:28 -03:00
2024-03-26 10:32:56 -03:00
public:
2024-05-29 15:58:37 -03:00
std::vector<Byte> _buffer;
2024-07-13 22:50:52 -03:00
2024-05-29 15:58:37 -03:00
private:
2024-03-26 10:32:56 -03:00
Int _position{ 0 };
Int _origin{ 0 };
Int _length{ 0 };
bool _closed{ false };
};
2024-03-26 15:56:17 -03:00
2024-05-29 09:42:18 -03:00
//A simplified port of the System.IO.FileStream.
2024-03-26 15:56:17 -03:00
class FileStream : public Stream {
public:
2024-05-29 09:42:18 -03:00
FileStream(String const& path, FileMode fileMode);
FileStream(String const& path);
2024-03-26 15:56:17 -03:00
2024-07-13 22:50:52 -03:00
~FileStream() override {
2024-03-26 15:56:17 -03:00
Close();
2024-03-26 17:22:00 -03:00
}
2024-03-26 15:56:17 -03:00
2024-05-29 09:42:18 -03:00
virtual Int Length() override;
virtual Long Position() override;
2024-03-26 15:56:17 -03:00
2024-05-29 09:42:18 -03:00
inline virtual void Close() override {
2024-03-26 15:56:17 -03:00
_closed = true;
if(_fstream.is_open())
_fstream.close();
2024-07-13 22:50:52 -03:00
}
2024-05-09 09:13:26 -03:00
2024-05-29 09:42:18 -03:00
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;
2024-06-04 14:37:28 -03:00
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