diff --git a/includes/csharp/io/binary.hpp b/includes/csharp/io/binary.hpp index b211f76..0e23609 100644 --- a/includes/csharp/io/binary.hpp +++ b/includes/csharp/io/binary.hpp @@ -43,7 +43,7 @@ namespace csharp { } virtual int32_t PeekChar(); - virtual int32_t Read(); + virtual int32_t Read(bool twoBytesPerChar = false); virtual uint8_t ReadByte() { return InternalReadByte(); @@ -58,6 +58,7 @@ namespace csharp { } virtual char ReadChar(); + virtual char ReadChar8(); virtual int16_t ReadInt16() { return ReadNumeric(); @@ -183,7 +184,6 @@ namespace csharp { std::shared_ptr _stream; bool _leaveOpen; bool _disposed{false}; - bool _2BytesPerChar{ false }; std::vector _auxBuffer; }; diff --git a/samples/01_blank/xna.cpp b/samples/01_blank/xna.cpp index fc4889c..5f36356 100644 --- a/samples/01_blank/xna.cpp +++ b/samples/01_blank/xna.cpp @@ -20,6 +20,7 @@ namespace xna { auto reader = csharp::BinaryReader(stream); auto bo = reader.ReadBoolean(); //reader.ReadChar() auto x = reader.ReadChar(); //x + auto cedilha = reader.ReadChar8(); //รง auto sb = reader.ReadSByte(); //127 auto by = reader.ReadByte(); //255 auto i16 = reader.ReadInt16(); //32767 diff --git a/sources/csharp/io/binary.cpp b/sources/csharp/io/binary.cpp index 87a4422..81fc225 100644 --- a/sources/csharp/io/binary.cpp +++ b/sources/csharp/io/binary.cpp @@ -20,7 +20,7 @@ namespace csharp { return ch; } - int32_t BinaryReader::Read() { + int32_t BinaryReader::Read(bool twoBytesPerChar) { if (_disposed) throw InvalidOperationException(); @@ -36,7 +36,7 @@ namespace csharp { char singleChar = '\0'; while (charsRead == 0) { - numBytes = _2BytesPerChar ? 2 : 1; + numBytes = twoBytesPerChar ? 2 : 1; auto r = _stream->ReadByte(); @@ -46,7 +46,8 @@ namespace csharp { } if (numBytes == 2) { - r |= _stream->ReadByte(); + auto r2 = _stream->ReadByte(); + r |= r2; if (r == -1) { @@ -90,7 +91,18 @@ namespace csharp { } return static_cast(value); - } + } + + char BinaryReader::ReadChar8() { + const auto value = Read(true); + + if (value == -1) + { + throw EndOfStreamException(SR::IO_EOF_ReadBeyondEOF); + } + + return static_cast(value); + } void BinaryReader::InternalRead(std::vector& buffer) { if (_disposed) @@ -142,11 +154,6 @@ namespace csharp { while (bufferLength > 0) { auto numBytes = bufferLength; - - if (_2BytesPerChar) - { - numBytes <<= 1; - } std::vector byteBuffer; @@ -169,9 +176,7 @@ namespace csharp { bufferLength = charsRead.length(); totalCharsRead += charsRead.length(); } - - // we may have read fewer than the number of characters requested if end of stream reached - // or if the encoding makes the char count too big for the buffer (e.g. fallback sequence) + return totalCharsRead; }