2024-04-28 20:19:37 -03:00
|
|
|
#ifndef XNA_CSHARP_BINARY_HPP
|
|
|
|
#define XNA_CSHARP_BINARY_HPP
|
|
|
|
|
|
|
|
#include "stream.hpp"
|
|
|
|
#include "../default.hpp"
|
|
|
|
|
|
|
|
namespace xna {
|
2024-05-29 09:42:18 -03:00
|
|
|
//A simplified port of the System.IO.BinaryReader class.
|
2024-04-28 20:19:37 -03:00
|
|
|
class BinaryReader {
|
|
|
|
public:
|
2024-07-13 22:50:52 -03:00
|
|
|
BinaryReader(sptr<Stream> const& input);
|
2024-04-28 20:19:37 -03:00
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
//Returns the next available character and does not advance the byte or character position.
|
|
|
|
Int PeekChar();
|
|
|
|
//Reads bytes from the underlying stream and advances the current position of the stream.
|
2024-07-13 22:50:52 -03:00
|
|
|
virtual Int Read();
|
2024-05-28 21:16:26 -03:00
|
|
|
//Reads a Boolean value from the current stream and advances the current position of the stream by one byte.
|
2024-07-13 22:50:52 -03:00
|
|
|
virtual bool ReadBoolean();
|
2024-05-28 21:16:26 -03:00
|
|
|
//Reads the next byte from the current stream and advances the current position of the stream by one byte.
|
2024-07-13 22:50:52 -03:00
|
|
|
virtual Byte ReadByte();
|
2024-05-28 21:16:26 -03:00
|
|
|
//Reads a signed byte from this stream and advances the current position of the stream by one byte.
|
2024-07-13 22:50:52 -03:00
|
|
|
virtual Sbyte ReadSByte();
|
2024-05-28 21:16:26 -03:00
|
|
|
//Reads the next character from the current stream and advances the current position of the stream.
|
2024-07-13 22:50:52 -03:00
|
|
|
virtual Char ReadChar();
|
2024-05-28 21:16:26 -03:00
|
|
|
//Reads a 2-byte signed integer from the current stream and advances the current position of the stream by two bytes.
|
2024-07-13 22:50:52 -03:00
|
|
|
virtual Short ReadInt16();
|
2024-05-28 21:16:26 -03:00
|
|
|
//Reads a 2-byte unsigned integer from the current stream and advances the position of the stream by two bytes.
|
2024-07-13 22:50:52 -03:00
|
|
|
virtual Ushort ReadUInt16();
|
2024-05-28 21:16:26 -03:00
|
|
|
//Reads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes.
|
2024-07-13 22:50:52 -03:00
|
|
|
virtual Int ReadInt32();
|
2024-05-28 21:16:26 -03:00
|
|
|
//Reads a 4-byte unsigned integer from the current stream and advances the position of the stream by four bytes.
|
2024-07-13 22:50:52 -03:00
|
|
|
virtual Uint ReadUInt32();
|
2024-05-28 21:16:26 -03:00
|
|
|
//Reads a 8-byte signed integer from the current stream and advances the current position of the stream by eight bytes.
|
2024-07-13 22:50:52 -03:00
|
|
|
virtual Long ReadInt64();
|
2024-05-28 21:16:26 -03:00
|
|
|
//Reads a 8-byte unsigned integer from the current stream and advances the position of the stream by eight bytes.
|
2024-07-13 22:50:52 -03:00
|
|
|
virtual Ulong ReadUInt64();
|
2024-05-28 21:16:26 -03:00
|
|
|
//Reads a 4-byte floating point value from the current stream and advances the current position of the stream by four bytes.
|
2024-07-13 22:50:52 -03:00
|
|
|
virtual float ReadSingle();
|
2024-05-28 21:16:26 -03:00
|
|
|
//Reads an 8-byte floating point value from the current stream and advances the current position of the stream by eight bytes.
|
2024-07-13 22:50:52 -03:00
|
|
|
virtual double ReadDouble();
|
2024-05-28 21:16:26 -03:00
|
|
|
//Reads a string from the current stream.
|
2024-07-13 22:50:52 -03:00
|
|
|
virtual std::string ReadString();
|
2024-04-28 20:19:37 -03:00
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
//Reads chars from the underlying stream and advances the current position of the stream.
|
2024-07-13 22:50:52 -03:00
|
|
|
virtual Int Read(std::vector<Char>& buffer, size_t index, size_t count);
|
2024-05-28 21:16:26 -03:00
|
|
|
//Reads bytes from the underlying stream and advances the current position of the stream.
|
2024-07-13 22:50:52 -03:00
|
|
|
virtual Int Read(std::vector<Byte>& buffer, size_t index, size_t count);
|
2024-04-28 20:19:37 -03:00
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
// Reads the specified number of bytes from the current stream into a byte array
|
|
|
|
// and advances the current position by that number of bytes.
|
2024-07-13 22:50:52 -03:00
|
|
|
virtual std::vector<Byte> ReadBytes(size_t count);
|
2024-05-28 16:59:03 -03:00
|
|
|
|
|
|
|
// Reads a 32-bit integer in compressed format.
|
2024-05-28 21:16:26 -03:00
|
|
|
// This function may throw a std::format_error exception.
|
|
|
|
Int Read7BitEncodedInt();
|
2024-05-28 16:59:03 -03:00
|
|
|
|
|
|
|
// Reads a 64-bit integer in compressed format.
|
2024-05-28 21:16:26 -03:00
|
|
|
// This function may throw a std::format_error exception.
|
|
|
|
Long Read7BitEncodedInt64();
|
|
|
|
|
2024-07-13 22:50:52 -03:00
|
|
|
//Exposes access to the underlying stream of the BinaryReader.
|
|
|
|
virtual inline sptr<Stream> BaseStream() const {
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Closes the current reader and the underlying stream.
|
|
|
|
virtual inline void Close() {
|
|
|
|
stream = nullptr;
|
|
|
|
}
|
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
protected:
|
|
|
|
Int InternalReadOneChar();
|
|
|
|
void FillBuffer(Int numBytes);
|
|
|
|
Int InternalReadChars(Char* buffer, size_t bufferSize, size_t index, size_t count);
|
|
|
|
|
2024-04-28 20:19:37 -03:00
|
|
|
private:
|
|
|
|
static constexpr int maxCharBytesSize = 128;
|
|
|
|
static constexpr int bufferLength = 16;
|
2024-05-01 19:09:43 -03:00
|
|
|
sptr<Stream> stream = nullptr;
|
2024-04-28 20:19:37 -03:00
|
|
|
std::vector<Byte> charBytes;
|
|
|
|
std::vector<Char> singleChar;
|
|
|
|
std::vector<Byte> buffer;
|
2024-11-08 15:26:57 -03:00
|
|
|
std::vector<Char> charBuffer;
|
2024-07-13 22:50:52 -03:00
|
|
|
};
|
2024-04-28 20:19:37 -03:00
|
|
|
|
2024-05-29 09:42:18 -03:00
|
|
|
//A simplified port of the System.IO.BinaryWriter class.
|
2024-04-28 20:19:37 -03:00
|
|
|
class BinaryWriter {
|
|
|
|
public:
|
2024-07-13 22:50:52 -03:00
|
|
|
BinaryWriter(sptr<Stream> const& stream);
|
2024-07-06 12:20:54 -03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
BinaryWriter() = default;
|
|
|
|
|
2024-05-28 21:24:27 -03:00
|
|
|
//Sets the position within the current stream.
|
2024-05-28 21:20:08 -03:00
|
|
|
Long Seek(Int offset, SeekOrigin origin);
|
2024-04-28 20:19:37 -03:00
|
|
|
|
2024-05-28 21:24:27 -03:00
|
|
|
//
|
|
|
|
// Writes a value to the current stream.
|
|
|
|
//
|
|
|
|
|
2024-07-13 22:50:52 -03:00
|
|
|
//Writes a value to the current stream.
|
2024-07-06 12:20:54 -03:00
|
|
|
virtual void Write(bool value);
|
2024-07-13 22:50:52 -03:00
|
|
|
//Writes a value to the current stream.
|
2024-07-06 12:20:54 -03:00
|
|
|
virtual void Write(Byte value);
|
2024-07-13 22:50:52 -03:00
|
|
|
//Writes a value to the current stream.
|
2024-07-06 12:20:54 -03:00
|
|
|
virtual void Write(Sbyte value);
|
2024-07-13 22:50:52 -03:00
|
|
|
//Writes a value to the current stream.
|
2024-07-06 12:20:54 -03:00
|
|
|
virtual void Write(Byte const* buffer, Int bufferLength);
|
2024-07-13 22:50:52 -03:00
|
|
|
//Writes a value to the current stream.
|
2024-07-06 12:20:54 -03:00
|
|
|
virtual void Write(std::vector<Byte> const& buffer);
|
2024-07-13 22:50:52 -03:00
|
|
|
//Writes a value to the current stream.
|
2024-07-06 12:20:54 -03:00
|
|
|
virtual void Write(Byte const* buffer, Int bufferLength, Int index, Int count);
|
2024-07-13 22:50:52 -03:00
|
|
|
//Writes a value to the current stream.
|
2024-07-06 12:20:54 -03:00
|
|
|
virtual void Write(std::vector<Byte> const& buffer, Int index, Int count);
|
2024-07-13 22:50:52 -03:00
|
|
|
//Writes a value to the current stream.
|
2024-07-06 12:20:54 -03:00
|
|
|
virtual void Write(Char ch);
|
2024-07-13 22:50:52 -03:00
|
|
|
//Writes a value to the current stream.
|
2024-07-06 12:20:54 -03:00
|
|
|
virtual void Write(double value);
|
2024-07-13 22:50:52 -03:00
|
|
|
//Writes a value to the current stream.
|
2024-07-06 12:20:54 -03:00
|
|
|
virtual void Write(Short value);
|
2024-07-13 22:50:52 -03:00
|
|
|
//Writes a value to the current stream.
|
2024-07-06 12:20:54 -03:00
|
|
|
virtual void Write(Ushort value);
|
2024-07-13 22:50:52 -03:00
|
|
|
//Writes a value to the current stream.
|
2024-07-06 12:20:54 -03:00
|
|
|
virtual void Write(Int value);
|
2024-07-13 22:50:52 -03:00
|
|
|
//Writes a value to the current stream.
|
2024-07-06 12:20:54 -03:00
|
|
|
virtual void Write(Uint value);
|
2024-07-13 22:50:52 -03:00
|
|
|
//Writes a value to the current stream.
|
2024-07-06 12:20:54 -03:00
|
|
|
virtual void Write(Long value);
|
2024-07-13 22:50:52 -03:00
|
|
|
//Writes a value to the current stream.
|
2024-07-06 12:20:54 -03:00
|
|
|
virtual void Write(Ulong value);
|
2024-07-13 22:50:52 -03:00
|
|
|
//Writes a value to the current stream.
|
2024-07-06 12:20:54 -03:00
|
|
|
virtual void Write(float value);
|
2024-07-13 22:50:52 -03:00
|
|
|
//Writes a value to the current stream.
|
2024-07-06 12:20:54 -03:00
|
|
|
virtual void Write(std::string const& value);
|
2024-07-13 22:50:52 -03:00
|
|
|
//Writes a value to the current stream.
|
2024-07-06 12:20:54 -03:00
|
|
|
virtual void Write(const char* _string, size_t stringLength);
|
2024-04-28 20:19:37 -03:00
|
|
|
|
2024-07-13 22:50:52 -03:00
|
|
|
//Exposes access to the underlying stream of the BinaryWriter.
|
|
|
|
virtual inline sptr<Stream> BaseStream() const {
|
|
|
|
return OutStream;
|
|
|
|
}
|
|
|
|
|
2024-05-28 21:24:27 -03:00
|
|
|
//Writes a 32-bit integer in a compressed format.
|
2024-11-08 15:26:57 -03:00
|
|
|
void Write7BitEncodedInt(Int value);
|
2024-05-28 21:24:27 -03:00
|
|
|
|
2024-07-13 22:50:52 -03:00
|
|
|
protected:
|
|
|
|
sptr<Stream> OutStream = nullptr;
|
2024-04-28 20:19:37 -03:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<Byte> _buffer;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|