mirror of
https://github.com/borgesdan/xn65
synced 2024-12-29 21:54:47 +01:00
Remove xna_errors em BinaryReader
This commit is contained in:
parent
e2c4c738f5
commit
d85649aef4
@ -2,109 +2,73 @@
|
||||
#include "csharp/buffer.hpp"
|
||||
|
||||
namespace xna {
|
||||
Int BinaryReader::PeekChar(xna_error_ptr_arg)
|
||||
Int BinaryReader::PeekChar()
|
||||
{
|
||||
if (!stream) {
|
||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const auto position = stream->Position();
|
||||
const auto num = Read(err);
|
||||
const auto num = Read();
|
||||
|
||||
if (xna_error_haserros(err))
|
||||
return -1;
|
||||
|
||||
stream->Seek(position, SeekOrigin::Begin, err);
|
||||
|
||||
if (xna_error_haserros(err))
|
||||
return -1;
|
||||
stream->Seek(position, SeekOrigin::Begin);
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
Int BinaryReader::Read(xna_error_ptr_arg)
|
||||
Int BinaryReader::Read()
|
||||
{
|
||||
if (!stream) {
|
||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const auto result = InternalReadOneChar();
|
||||
|
||||
return xna_error_haserros(err) ? -1 : result;
|
||||
return result;
|
||||
}
|
||||
|
||||
bool BinaryReader::ReadBoolean(xna_error_ptr_arg)
|
||||
bool BinaryReader::ReadBoolean()
|
||||
{
|
||||
FillBuffer(1);
|
||||
return xna_error_haserros(err) ? false : buffer[0] > 0;
|
||||
return buffer[0] > 0;
|
||||
}
|
||||
|
||||
Byte BinaryReader::ReadByte(xna_error_ptr_arg)
|
||||
Byte BinaryReader::ReadByte()
|
||||
{
|
||||
if (!stream) {
|
||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const auto num = stream->ReadByte();
|
||||
|
||||
if (num == -1)
|
||||
{
|
||||
xna_error_apply(err, XnaErrorCode::END_OF_FILE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return static_cast<Byte>(num);
|
||||
}
|
||||
|
||||
Sbyte BinaryReader::ReadSByte(xna_error_ptr_arg)
|
||||
Sbyte BinaryReader::ReadSByte()
|
||||
{
|
||||
FillBuffer(1);
|
||||
return xna_error_haserros(err) ? -1 : static_cast<Sbyte>(buffer[0]);
|
||||
return static_cast<Sbyte>(buffer[0]);
|
||||
}
|
||||
|
||||
Char BinaryReader::ReadChar(xna_error_ptr_arg)
|
||||
Char BinaryReader::ReadChar()
|
||||
{
|
||||
auto num = Read(err);
|
||||
auto num = Read();
|
||||
|
||||
if (xna_error_haserros(err) || num == -1)
|
||||
if (num == -1)
|
||||
return '\0';
|
||||
|
||||
return static_cast<Char>(num);
|
||||
}
|
||||
|
||||
Short BinaryReader::ReadInt16(xna_error_ptr_arg)
|
||||
Short BinaryReader::ReadInt16()
|
||||
{
|
||||
FillBuffer(2);
|
||||
|
||||
if (xna_error_haserros(err))
|
||||
return -1;
|
||||
FillBuffer(2);
|
||||
|
||||
return static_cast<Short>(
|
||||
static_cast<Int>(buffer[0])
|
||||
| static_cast<Int>(buffer[1]) << 8);
|
||||
}
|
||||
|
||||
Ushort BinaryReader::ReadUInt16(xna_error_ptr_arg)
|
||||
Ushort BinaryReader::ReadUInt16()
|
||||
{
|
||||
FillBuffer(2);
|
||||
|
||||
if (xna_error_haserros(err))
|
||||
return 0;
|
||||
FillBuffer(2);
|
||||
|
||||
return static_cast<Ushort>(
|
||||
static_cast<Uint>(buffer[0])
|
||||
| static_cast<Uint>(buffer[1]) << 8);
|
||||
}
|
||||
|
||||
Int BinaryReader::ReadInt32(xna_error_ptr_arg)
|
||||
Int BinaryReader::ReadInt32()
|
||||
{
|
||||
FillBuffer(4);
|
||||
|
||||
if (xna_error_haserros(err))
|
||||
return -1;
|
||||
FillBuffer(4);
|
||||
|
||||
return static_cast<Int>(buffer[0])
|
||||
| static_cast<Int>(buffer[1]) << 8
|
||||
@ -112,12 +76,9 @@ namespace xna {
|
||||
| static_cast<Int>(buffer[3]) << 24;
|
||||
}
|
||||
|
||||
Uint BinaryReader::ReadUInt32(xna_error_ptr_arg)
|
||||
Uint BinaryReader::ReadUInt32()
|
||||
{
|
||||
FillBuffer(4);
|
||||
|
||||
if (xna_error_haserros(err))
|
||||
return -1;
|
||||
FillBuffer(4);
|
||||
|
||||
return static_cast<Uint>(
|
||||
static_cast<Int>(buffer[0])
|
||||
@ -126,12 +87,9 @@ namespace xna {
|
||||
| static_cast<Int>(buffer[3]) << 24);
|
||||
}
|
||||
|
||||
Long BinaryReader::ReadInt64(xna_error_ptr_arg)
|
||||
Long BinaryReader::ReadInt64()
|
||||
{
|
||||
FillBuffer(8);
|
||||
|
||||
if (xna_error_haserros(err))
|
||||
return -1;
|
||||
FillBuffer(8);
|
||||
|
||||
const auto num1 = static_cast<Uint>(
|
||||
static_cast<Int>(buffer[4])
|
||||
@ -148,12 +106,9 @@ namespace xna {
|
||||
return static_cast<Long>(num1) << 32 | static_cast<Long>(num2);
|
||||
}
|
||||
|
||||
Ulong BinaryReader::ReadUInt64(xna_error_ptr_arg)
|
||||
Ulong BinaryReader::ReadUInt64()
|
||||
{
|
||||
FillBuffer(8);
|
||||
|
||||
if (xna_error_haserros(err))
|
||||
return 0;
|
||||
FillBuffer(8);
|
||||
|
||||
const auto num1 = static_cast<Uint>(
|
||||
static_cast<Int>(buffer[4])
|
||||
@ -170,12 +125,9 @@ namespace xna {
|
||||
return static_cast<Ulong>(num1) << 32 | static_cast<Ulong>(num2);
|
||||
}
|
||||
|
||||
float BinaryReader::ReadSingle(xna_error_ptr_arg)
|
||||
float BinaryReader::ReadSingle()
|
||||
{
|
||||
FillBuffer(4);
|
||||
|
||||
if (xna_error_haserros(err))
|
||||
return std::numeric_limits<float>::quiet_NaN();
|
||||
FillBuffer(4);
|
||||
|
||||
const auto num = static_cast<Uint>(
|
||||
static_cast<Int>(buffer[0])
|
||||
@ -186,12 +138,9 @@ namespace xna {
|
||||
return *(float*)#
|
||||
}
|
||||
|
||||
double BinaryReader::ReadDouble(xna_error_ptr_arg)
|
||||
double BinaryReader::ReadDouble()
|
||||
{
|
||||
FillBuffer(8);
|
||||
|
||||
if (xna_error_haserros(err))
|
||||
return std::numeric_limits<double>::quiet_NaN();
|
||||
FillBuffer(8);
|
||||
|
||||
const auto num1 = static_cast<Uint>(
|
||||
static_cast<Int>(buffer[4])
|
||||
@ -210,20 +159,15 @@ namespace xna {
|
||||
return *(double*)&num3;
|
||||
}
|
||||
|
||||
std::string BinaryReader::ReadString(xna_error_ptr_arg)
|
||||
std::string BinaryReader::ReadString()
|
||||
{
|
||||
static const auto empty = std::string();
|
||||
|
||||
if (!stream) {
|
||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return std::string();
|
||||
}
|
||||
static const auto empty = std::string();
|
||||
|
||||
Int num = 0;
|
||||
auto val1 = Read7BitEncodedInt();
|
||||
|
||||
if (val1 < 0) {
|
||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
//xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return empty;
|
||||
}
|
||||
|
||||
@ -242,7 +186,7 @@ namespace xna {
|
||||
const auto byteCount = stream->Read(charBytes, 0, val1 - num > 128 ? 128 : val1 - num);
|
||||
|
||||
if (byteCount == 0) {
|
||||
xna_error_apply(err, XnaErrorCode::END_OF_FILE);
|
||||
//xna_error_apply(err, XnaErrorCode::END_OF_FILE);
|
||||
return empty;
|
||||
}
|
||||
|
||||
@ -275,7 +219,8 @@ namespace xna {
|
||||
|
||||
while (num1 == 0)
|
||||
{
|
||||
auto byteCount = m2BytesPerChar ? 2 : 1;
|
||||
//auto byteCount = m2BytesPerChar ? 2 : 1;
|
||||
auto byteCount = 1;
|
||||
const auto num4 = stream->ReadByte();
|
||||
|
||||
charBytes[0] = static_cast<Byte>(num4);
|
||||
@ -310,8 +255,8 @@ namespace xna {
|
||||
|
||||
void BinaryReader::FillBuffer(Int numBytes)
|
||||
{
|
||||
if (!stream || !buffer.empty() && (numBytes < 0 || numBytes > buffer.size())) {
|
||||
throw std::runtime_error("Stream is null or the buffer is not valid.");
|
||||
if (numBytes < 0 || numBytes > buffer.size()) {
|
||||
throw std::out_of_range("numBytes");
|
||||
}
|
||||
|
||||
Int bytesRead = 0;
|
||||
@ -339,7 +284,7 @@ namespace xna {
|
||||
} while (bytesRead < numBytes);
|
||||
}
|
||||
|
||||
Int BinaryReader::InternalReadChars(Char* buffer, size_t bufferSize, size_t index, size_t count, xna_error_ptr_arg)
|
||||
Int BinaryReader::InternalReadChars(Char* buffer, size_t bufferSize, size_t index, size_t count)
|
||||
{
|
||||
auto charCount = count;
|
||||
|
||||
@ -352,8 +297,8 @@ namespace xna {
|
||||
if (count1 > 1)
|
||||
--count1;
|
||||
|
||||
if (m2BytesPerChar)
|
||||
count1 <<= 1;
|
||||
/*if (m2BytesPerChar)
|
||||
count1 <<= 1;*/
|
||||
|
||||
if (count1 > 128)
|
||||
count1 = 128;
|
||||
@ -363,19 +308,17 @@ namespace xna {
|
||||
|
||||
std::vector<Byte> numArray;
|
||||
|
||||
byteCount = stream->Read(charBytes, 0, static_cast<Int>(count1), err);
|
||||
byteCount = stream->Read(charBytes, 0, static_cast<Int>(count1));
|
||||
numArray = charBytes;
|
||||
|
||||
if (byteCount == 0)
|
||||
return static_cast<Int>(count - charCount);
|
||||
|
||||
if (position < 0 || byteCount < 0 || (position + byteCount) > numArray.size()) {
|
||||
xna_error_apply(err, XnaErrorCode::ARGUMENT_OUT_OF_RANGE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (index < 0 || charCount < 0 || (index + charCount) > bufferSize) {
|
||||
xna_error_apply(err, XnaErrorCode::ARGUMENT_OUT_OF_RANGE);
|
||||
if (index < 0 || charCount < 0 || (index + charCount) > bufferSize) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -397,256 +340,8 @@ namespace xna {
|
||||
return static_cast<Int>(count - charCount);
|
||||
}
|
||||
|
||||
Long BinaryWriter::Seek(Int offset, SeekOrigin origin, xna_error_ptr_arg)
|
||||
Int BinaryReader::Read7BitEncodedInt()
|
||||
{
|
||||
if (!_stream) {
|
||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return _stream->Seek(offset, origin);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(bool value, xna_error_ptr_arg)
|
||||
{
|
||||
if (!_stream) {
|
||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return;
|
||||
}
|
||||
|
||||
_buffer[0] = value ? (Byte)1 : (Byte)0;
|
||||
_stream->Write(_buffer, 0, 1);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Byte value, xna_error_ptr_arg)
|
||||
{
|
||||
if (!_stream) {
|
||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return;
|
||||
}
|
||||
|
||||
_stream->WriteByte(value);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Sbyte value, xna_error_ptr_arg)
|
||||
{
|
||||
if (!_stream) {
|
||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return;
|
||||
}
|
||||
|
||||
_stream->WriteByte(static_cast<Byte>(value));
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Byte const* buffer, Int bufferLength, xna_error_ptr_arg)
|
||||
{
|
||||
if (!_stream) {
|
||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return;
|
||||
}
|
||||
|
||||
_stream->Write(buffer, bufferLength, 0, bufferLength);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(std::vector<Byte> const& buffer, xna_error_ptr_arg)
|
||||
{
|
||||
if (!_stream) {
|
||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return;
|
||||
}
|
||||
|
||||
_stream->Write(buffer, 0, static_cast<Int>(buffer.size()));
|
||||
}
|
||||
void BinaryWriter::Write(Byte const* buffer, Int bufferLength, Int index, Int count, xna_error_ptr_arg)
|
||||
{
|
||||
if (!_stream) {
|
||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return;
|
||||
}
|
||||
|
||||
_stream->Write(buffer, bufferLength, index, count);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(std::vector<Byte> const& buffer, Int index, Int count, xna_error_ptr_arg)
|
||||
{
|
||||
if (!_stream) {
|
||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return;
|
||||
}
|
||||
|
||||
_stream->Write(buffer, index, count);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Char ch, xna_error_ptr_arg)
|
||||
{
|
||||
if (!_stream) {
|
||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return;
|
||||
}
|
||||
|
||||
_buffer[0] = static_cast<Byte>(ch);
|
||||
_stream->Write(_buffer, 0, 1);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(double value, xna_error_ptr_arg)
|
||||
{
|
||||
if (!_stream) {
|
||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return;
|
||||
}
|
||||
|
||||
Ulong num = (Ulong) * (Long*)&value;
|
||||
_buffer[0] = static_cast<Byte>(num);
|
||||
_buffer[1] = static_cast<Byte>(num >> 8);
|
||||
_buffer[2] = static_cast<Byte>(num >> 16);
|
||||
_buffer[3] = static_cast<Byte>(num >> 24);
|
||||
_buffer[4] = static_cast<Byte>(num >> 32);
|
||||
_buffer[5] = static_cast<Byte>(num >> 40);
|
||||
_buffer[6] = static_cast<Byte>(num >> 48);
|
||||
_buffer[7] = static_cast<Byte>(num >> 56);
|
||||
|
||||
_stream->Write(_buffer, 0, 8);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Short value, xna_error_ptr_arg)
|
||||
{
|
||||
if (!_stream) {
|
||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return;
|
||||
}
|
||||
|
||||
_buffer[0] = static_cast<Byte>(value);
|
||||
_buffer[1] = static_cast<Byte>((Uint)value >> 8);
|
||||
_stream->Write(_buffer, 0, 2);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Ushort value, xna_error_ptr_arg)
|
||||
{
|
||||
if (!_stream) {
|
||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return;
|
||||
}
|
||||
|
||||
_buffer[0] = static_cast<Byte>(value);
|
||||
_buffer[1] = static_cast<Byte>((Uint)value >> 8);
|
||||
_stream->Write(_buffer, 0, 2);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Int value, xna_error_ptr_arg)
|
||||
{
|
||||
if (!_stream) {
|
||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return;
|
||||
}
|
||||
|
||||
_buffer[0] = static_cast<Byte>(value);
|
||||
_buffer[1] = static_cast<Byte>(value >> 8);
|
||||
_buffer[2] = static_cast<Byte>(value >> 16);
|
||||
_buffer[3] = static_cast<Byte>(value >> 24);
|
||||
_stream->Write(_buffer, 0, 4);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Uint value, xna_error_ptr_arg)
|
||||
{
|
||||
if (!_stream) {
|
||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return;
|
||||
}
|
||||
|
||||
_buffer[0] = static_cast<Byte>(value);
|
||||
_buffer[1] = static_cast<Byte>(value >> 8);
|
||||
_buffer[2] = static_cast<Byte>(value >> 16);
|
||||
_buffer[3] = static_cast<Byte>(value >> 24);
|
||||
_stream->Write(_buffer, 0, 4);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Ulong value, xna_error_ptr_arg)
|
||||
{
|
||||
if (!_stream) {
|
||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return;
|
||||
}
|
||||
|
||||
_buffer[0] = static_cast<Byte>(value);
|
||||
_buffer[1] = static_cast<Byte>(value >> 8);
|
||||
_buffer[2] = static_cast<Byte>(value >> 16);
|
||||
_buffer[3] = static_cast<Byte>(value >> 24);
|
||||
_buffer[4] = static_cast<Byte>(value >> 32);
|
||||
_buffer[5] = static_cast<Byte>(value >> 40);
|
||||
_buffer[6] = static_cast<Byte>(value >> 48);
|
||||
_buffer[7] = static_cast<Byte>(value >> 56);
|
||||
_stream->Write(_buffer, 0, 8);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(float value, xna_error_ptr_arg)
|
||||
{
|
||||
if (!_stream) {
|
||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return;
|
||||
}
|
||||
|
||||
Uint num = *(Uint*)&value;
|
||||
_buffer[0] = static_cast<Byte>(num);
|
||||
_buffer[1] = static_cast<Byte>(num >> 8);
|
||||
_buffer[2] = static_cast<Byte>(num >> 16);
|
||||
_buffer[3] = static_cast<Byte>(num >> 24);
|
||||
_stream->Write(_buffer, 0, 4);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(std::string const& value, xna_error_ptr_arg)
|
||||
{
|
||||
if (!_stream) {
|
||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return;
|
||||
}
|
||||
|
||||
Write(value.c_str(), value.size());
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(const char* _string, size_t stringLength, xna_error_ptr_arg)
|
||||
{
|
||||
if (!_stream) {
|
||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return;
|
||||
}
|
||||
|
||||
Write7BitEncodedInt(static_cast<Int>(stringLength));
|
||||
const auto b = reinterpret_cast<const Byte*>(_string);
|
||||
_stream->Write(b, static_cast<Int>(stringLength), 0, static_cast<Int>(stringLength));
|
||||
}
|
||||
|
||||
void BinaryWriter::Write7BitEncodedInt(Int value)
|
||||
{
|
||||
Uint num;
|
||||
for (num = (Uint)value; num >= (Uint)128U; num >>= 7)
|
||||
Write((Byte)(num | (Uint)128U));
|
||||
|
||||
Write((Byte)num);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Long value, xna_error_ptr_arg)
|
||||
{
|
||||
if (!_stream) {
|
||||
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return;
|
||||
}
|
||||
|
||||
_buffer[0] = static_cast<Byte>(value);
|
||||
_buffer[1] = static_cast<Byte>(value >> 8);
|
||||
_buffer[2] = static_cast<Byte>(value >> 16);
|
||||
_buffer[3] = static_cast<Byte>(value >> 24);
|
||||
_buffer[4] = static_cast<Byte>(value >> 32);
|
||||
_buffer[5] = static_cast<Byte>(value >> 40);
|
||||
_buffer[6] = static_cast<Byte>(value >> 48);
|
||||
_buffer[7] = static_cast<Byte>(value >> 56);
|
||||
_stream->Write(_buffer, 0, 8);
|
||||
}
|
||||
|
||||
Int BinaryReader::Read7BitEncodedInt() noexcept(false)
|
||||
{
|
||||
if (!stream)
|
||||
return -1;
|
||||
|
||||
Uint result = 0;
|
||||
Byte byteReadJustNow;
|
||||
|
||||
@ -673,14 +368,14 @@ namespace xna {
|
||||
return static_cast<Int>(result);
|
||||
}
|
||||
|
||||
Long BinaryReader::Read7BitEncodedInt64() noexcept(false) {
|
||||
Long BinaryReader::Read7BitEncodedInt64() {
|
||||
Ulong result = 0;
|
||||
Byte byteReadJustNow;
|
||||
|
||||
constexpr Int MaxBytesWithoutOverflow = 9;
|
||||
|
||||
for (size_t shift = 0; shift < MaxBytesWithoutOverflow * 7; shift += 7)
|
||||
{
|
||||
{
|
||||
byteReadJustNow = ReadByte();
|
||||
result |= (static_cast<Ulong>(byteReadJustNow & 0x7Ful)) << shift;
|
||||
|
||||
@ -690,7 +385,7 @@ namespace xna {
|
||||
}
|
||||
|
||||
byteReadJustNow = ReadByte();
|
||||
|
||||
|
||||
if (byteReadJustNow > 0b1u)
|
||||
{
|
||||
throw std::format_error("Too many bytes in what should have been a 7-bit encoded integer.");
|
||||
@ -700,32 +395,33 @@ namespace xna {
|
||||
return static_cast<Long>(result);
|
||||
}
|
||||
|
||||
Int BinaryReader::Read(std::vector<Char>& buffer, size_t index, size_t count, xna_error_ptr_arg)
|
||||
Int BinaryReader::Read(std::vector<Char>& buffer, size_t index, size_t count)
|
||||
{
|
||||
return InternalReadChars(buffer.data(), buffer.size(), index, count, err);
|
||||
return InternalReadChars(buffer.data(), buffer.size(), index, count);
|
||||
}
|
||||
|
||||
Int BinaryReader::Read(std::vector<Byte>& buffer, size_t index, size_t count, xna_error_ptr_arg)
|
||||
Int BinaryReader::Read(std::vector<Byte>& buffer, size_t index, size_t count)
|
||||
{
|
||||
auto data = reinterpret_cast<Char*>(buffer.data());
|
||||
return InternalReadChars(data, buffer.size(), index, count, err);
|
||||
return InternalReadChars(data, buffer.size(), index, count);
|
||||
}
|
||||
std::vector<Byte> BinaryReader::ReadBytes(size_t count, xna_error_ptr_arg)
|
||||
|
||||
std::vector<Byte> BinaryReader::ReadBytes(size_t count)
|
||||
{
|
||||
std::vector<Byte> result(count);
|
||||
Int numRead = 0;
|
||||
|
||||
do {
|
||||
const auto n = stream->Read(result, static_cast<Int>(numRead), static_cast<Int>(count), err);
|
||||
|
||||
const auto n = stream->Read(result, static_cast<Int>(numRead), static_cast<Int>(count));
|
||||
|
||||
if (n == 0)
|
||||
break;
|
||||
|
||||
|
||||
numRead += n;
|
||||
count -= n;
|
||||
} while (count > 0);
|
||||
|
||||
if (numRead != result.size()) {
|
||||
if (numRead != result.size()) {
|
||||
std::vector<Byte> copy(numRead);
|
||||
Buffer::BlockCopy(result.data(), 0, copy.data(), 0, numRead);
|
||||
result = copy;
|
||||
@ -733,4 +429,156 @@ namespace xna {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//Binary Writer
|
||||
|
||||
Long BinaryWriter::Seek(Int offset, SeekOrigin origin, xna_error_ptr_arg)
|
||||
{
|
||||
return _stream->Seek(offset, origin);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(bool value, xna_error_ptr_arg)
|
||||
{
|
||||
_buffer[0] = value ? (Byte)1 : (Byte)0;
|
||||
_stream->Write(_buffer, 0, 1);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Byte value, xna_error_ptr_arg)
|
||||
{
|
||||
_stream->WriteByte(value);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Sbyte value, xna_error_ptr_arg)
|
||||
{
|
||||
_stream->WriteByte(static_cast<Byte>(value));
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Byte const* buffer, Int bufferLength, xna_error_ptr_arg)
|
||||
{
|
||||
_stream->Write(buffer, bufferLength, 0, bufferLength);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(std::vector<Byte> const& buffer, xna_error_ptr_arg)
|
||||
{
|
||||
_stream->Write(buffer, 0, static_cast<Int>(buffer.size()));
|
||||
}
|
||||
void BinaryWriter::Write(Byte const* buffer, Int bufferLength, Int index, Int count, xna_error_ptr_arg)
|
||||
{
|
||||
_stream->Write(buffer, bufferLength, index, count);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(std::vector<Byte> const& buffer, Int index, Int count, xna_error_ptr_arg)
|
||||
{
|
||||
_stream->Write(buffer, index, count);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Char ch, xna_error_ptr_arg)
|
||||
{
|
||||
_buffer[0] = static_cast<Byte>(ch);
|
||||
_stream->Write(_buffer, 0, 1);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(double value, xna_error_ptr_arg)
|
||||
{
|
||||
Ulong num = (Ulong) * (Long*)&value;
|
||||
_buffer[0] = static_cast<Byte>(num);
|
||||
_buffer[1] = static_cast<Byte>(num >> 8);
|
||||
_buffer[2] = static_cast<Byte>(num >> 16);
|
||||
_buffer[3] = static_cast<Byte>(num >> 24);
|
||||
_buffer[4] = static_cast<Byte>(num >> 32);
|
||||
_buffer[5] = static_cast<Byte>(num >> 40);
|
||||
_buffer[6] = static_cast<Byte>(num >> 48);
|
||||
_buffer[7] = static_cast<Byte>(num >> 56);
|
||||
|
||||
_stream->Write(_buffer, 0, 8);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Short value, xna_error_ptr_arg)
|
||||
{
|
||||
_buffer[0] = static_cast<Byte>(value);
|
||||
_buffer[1] = static_cast<Byte>((Uint)value >> 8);
|
||||
_stream->Write(_buffer, 0, 2);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Ushort value, xna_error_ptr_arg)
|
||||
{
|
||||
_buffer[0] = static_cast<Byte>(value);
|
||||
_buffer[1] = static_cast<Byte>((Uint)value >> 8);
|
||||
_stream->Write(_buffer, 0, 2);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Int value, xna_error_ptr_arg)
|
||||
{
|
||||
_buffer[0] = static_cast<Byte>(value);
|
||||
_buffer[1] = static_cast<Byte>(value >> 8);
|
||||
_buffer[2] = static_cast<Byte>(value >> 16);
|
||||
_buffer[3] = static_cast<Byte>(value >> 24);
|
||||
_stream->Write(_buffer, 0, 4);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Uint value, xna_error_ptr_arg)
|
||||
{
|
||||
_buffer[0] = static_cast<Byte>(value);
|
||||
_buffer[1] = static_cast<Byte>(value >> 8);
|
||||
_buffer[2] = static_cast<Byte>(value >> 16);
|
||||
_buffer[3] = static_cast<Byte>(value >> 24);
|
||||
_stream->Write(_buffer, 0, 4);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Ulong value, xna_error_ptr_arg)
|
||||
{
|
||||
_buffer[0] = static_cast<Byte>(value);
|
||||
_buffer[1] = static_cast<Byte>(value >> 8);
|
||||
_buffer[2] = static_cast<Byte>(value >> 16);
|
||||
_buffer[3] = static_cast<Byte>(value >> 24);
|
||||
_buffer[4] = static_cast<Byte>(value >> 32);
|
||||
_buffer[5] = static_cast<Byte>(value >> 40);
|
||||
_buffer[6] = static_cast<Byte>(value >> 48);
|
||||
_buffer[7] = static_cast<Byte>(value >> 56);
|
||||
_stream->Write(_buffer, 0, 8);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(float value, xna_error_ptr_arg)
|
||||
{
|
||||
Uint num = *(Uint*)&value;
|
||||
_buffer[0] = static_cast<Byte>(num);
|
||||
_buffer[1] = static_cast<Byte>(num >> 8);
|
||||
_buffer[2] = static_cast<Byte>(num >> 16);
|
||||
_buffer[3] = static_cast<Byte>(num >> 24);
|
||||
_stream->Write(_buffer, 0, 4);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(std::string const& value, xna_error_ptr_arg)
|
||||
{
|
||||
Write(value.c_str(), value.size());
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(const char* _string, size_t stringLength, xna_error_ptr_arg)
|
||||
{
|
||||
Write7BitEncodedInt(static_cast<Int>(stringLength));
|
||||
const auto b = reinterpret_cast<const Byte*>(_string);
|
||||
_stream->Write(b, static_cast<Int>(stringLength), 0, static_cast<Int>(stringLength));
|
||||
}
|
||||
|
||||
void BinaryWriter::Write7BitEncodedInt(Int value)
|
||||
{
|
||||
Uint num;
|
||||
for (num = (Uint)value; num >= (Uint)128U; num >>= 7)
|
||||
Write((Byte)(num | (Uint)128U));
|
||||
|
||||
Write((Byte)num);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Long value, xna_error_ptr_arg)
|
||||
{
|
||||
_buffer[0] = static_cast<Byte>(value);
|
||||
_buffer[1] = static_cast<Byte>(value >> 8);
|
||||
_buffer[2] = static_cast<Byte>(value >> 16);
|
||||
_buffer[3] = static_cast<Byte>(value >> 24);
|
||||
_buffer[4] = static_cast<Byte>(value >> 32);
|
||||
_buffer[5] = static_cast<Byte>(value >> 40);
|
||||
_buffer[6] = static_cast<Byte>(value >> 48);
|
||||
_buffer[7] = static_cast<Byte>(value >> 56);
|
||||
_stream->Write(_buffer, 0, 8);
|
||||
}
|
||||
}
|
@ -5,42 +5,70 @@
|
||||
#include "../default.hpp"
|
||||
|
||||
namespace xna {
|
||||
//A simplified port of the BinaryReader class.
|
||||
class BinaryReader {
|
||||
public:
|
||||
BinaryReader(sptr<Stream> const& input) {
|
||||
if (!input)
|
||||
throw std::invalid_argument("input is null.");
|
||||
|
||||
stream = input;
|
||||
buffer = std::vector<Byte>(bufferLength);
|
||||
}
|
||||
|
||||
Int PeekChar(xna_error_nullarg);
|
||||
Int Read(xna_error_nullarg);
|
||||
bool ReadBoolean(xna_error_nullarg);
|
||||
Byte ReadByte(xna_error_nullarg);
|
||||
Sbyte ReadSByte(xna_error_nullarg);
|
||||
Char ReadChar(xna_error_nullarg);
|
||||
Short ReadInt16(xna_error_nullarg);
|
||||
Ushort ReadUInt16(xna_error_nullarg);
|
||||
Int ReadInt32(xna_error_nullarg);
|
||||
Uint ReadUInt32(xna_error_nullarg);
|
||||
Long ReadInt64(xna_error_nullarg);
|
||||
Ulong ReadUInt64(xna_error_nullarg);
|
||||
float ReadSingle(xna_error_nullarg);
|
||||
double ReadDouble(xna_error_nullarg);
|
||||
std::string ReadString(xna_error_nullarg);
|
||||
//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.
|
||||
Int Read();
|
||||
//Reads a Boolean value from the current stream and advances the current position of the stream by one byte.
|
||||
bool ReadBoolean();
|
||||
//Reads the next byte from the current stream and advances the current position of the stream by one byte.
|
||||
Byte ReadByte();
|
||||
//Reads a signed byte from this stream and advances the current position of the stream by one byte.
|
||||
Sbyte ReadSByte();
|
||||
//Reads the next character from the current stream and advances the current position of the stream.
|
||||
Char ReadChar();
|
||||
//Reads a 2-byte signed integer from the current stream and advances the current position of the stream by two bytes.
|
||||
Short ReadInt16();
|
||||
//Reads a 2-byte unsigned integer from the current stream and advances the position of the stream by two bytes.
|
||||
Ushort ReadUInt16();
|
||||
//Reads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes.
|
||||
Int ReadInt32();
|
||||
//Reads a 4-byte unsigned integer from the current stream and advances the position of the stream by four bytes.
|
||||
Uint ReadUInt32();
|
||||
//Reads a 8-byte signed integer from the current stream and advances the current position of the stream by eight bytes.
|
||||
Long ReadInt64();
|
||||
//Reads a 8-byte unsigned integer from the current stream and advances the position of the stream by eight bytes.
|
||||
Ulong ReadUInt64();
|
||||
//Reads a 4-byte floating point value from the current stream and advances the current position of the stream by four bytes.
|
||||
float ReadSingle();
|
||||
//Reads an 8-byte floating point value from the current stream and advances the current position of the stream by eight bytes.
|
||||
double ReadDouble();
|
||||
//Reads a string from the current stream.
|
||||
std::string ReadString();
|
||||
|
||||
Int Read(std::vector<Char>& buffer, size_t index, size_t count, xna_error_nullarg);
|
||||
Int Read(std::vector<Byte>& buffer, size_t index, size_t count, xna_error_nullarg);
|
||||
//Reads chars from the underlying stream and advances the current position of the stream.
|
||||
Int Read(std::vector<Char>& buffer, size_t index, size_t count);
|
||||
//Reads bytes from the underlying stream and advances the current position of the stream.
|
||||
Int Read(std::vector<Byte>& buffer, size_t index, size_t count);
|
||||
|
||||
std::vector<Byte> ReadBytes(size_t count, xna_error_nullarg);
|
||||
// Reads the specified number of bytes from the current stream into a byte array
|
||||
// and advances the current position by that number of bytes.
|
||||
std::vector<Byte> ReadBytes(size_t count);
|
||||
|
||||
// Reads a 32-bit integer in compressed format.
|
||||
// This function may throw a std::format_error exception or returns -1 if stream is null.
|
||||
Int Read7BitEncodedInt() noexcept(false);
|
||||
|
||||
// This function may throw a std::format_error exception.
|
||||
Int Read7BitEncodedInt();
|
||||
|
||||
// Reads a 64-bit integer in compressed format.
|
||||
// This function may throw a std::format_error exception or returns -1 if stream is null.
|
||||
Long Read7BitEncodedInt64() noexcept(false);
|
||||
// This function may throw a std::format_error exception.
|
||||
Long Read7BitEncodedInt64();
|
||||
|
||||
protected:
|
||||
Int InternalReadOneChar();
|
||||
void FillBuffer(Int numBytes);
|
||||
Int InternalReadChars(Char* buffer, size_t bufferSize, size_t index, size_t count);
|
||||
|
||||
private:
|
||||
static constexpr int maxCharBytesSize = 128;
|
||||
static constexpr int bufferLength = 16;
|
||||
@ -50,12 +78,7 @@ namespace xna {
|
||||
std::vector<Byte> buffer;
|
||||
std::vector<Char> charBuffer;
|
||||
|
||||
bool m2BytesPerChar{ false };
|
||||
|
||||
protected:
|
||||
Int InternalReadOneChar();
|
||||
void FillBuffer(Int numBytes) noexcept(false);
|
||||
Int InternalReadChars(Char* buffer, size_t bufferSize, size_t index, size_t count, xna_error_nullarg);
|
||||
//bool m2BytesPerChar{ false };
|
||||
};
|
||||
|
||||
class BinaryWriter {
|
||||
|
Loading…
x
Reference in New Issue
Block a user