diff --git a/includes/csharp/io/binary.hpp b/includes/csharp/io/binary.hpp index 9c2f9d9..b211f76 100644 --- a/includes/csharp/io/binary.hpp +++ b/includes/csharp/io/binary.hpp @@ -92,6 +92,7 @@ namespace csharp { } virtual std::string ReadString(); + virtual std::u8string ReadString8(); virtual int32_t Read(char* buffer, int32_t bufferLength, int32_t index, int32_t count); @@ -110,6 +111,53 @@ namespace csharp { int32_t Read7BitEncodedInt(); int64_t Read7BitEncodedInt64(); + template + TSTRING GenericReadString() { + if (_disposed) + throw InvalidOperationException(); + + const auto stringLength = Read7BitEncodedInt(); + if (stringLength < 0) + { + throw IOException(SR::IO_InvalidStringLen_Len); + } + + if (stringLength == 0) + { + return {}; + } + + auto charBytes = std::vector(MaxCharBytesSize); + int32_t currPos = 0; + + TSTRING sb{}; + + do + { + const auto readLength = std::min(MaxCharBytesSize, stringLength - currPos); + const auto n = _stream->Read(charBytes.data(), readLength); + + if (n == 0) + { + throw EndOfStreamException(SR::IO_EOF_ReadBeyondEOF); + } + + const auto chars = reinterpret_cast(charBytes.data()); + + if (currPos == 0 && n == stringLength) + { + return TSTRING(chars); + } + + sb.append(chars); + + currPos += n; + + } while (currPos < stringLength); + + return sb; + } + private: uint8_t InternalReadByte(); void InternalRead(std::vector& buffer); @@ -135,7 +183,7 @@ namespace csharp { std::shared_ptr _stream; bool _leaveOpen; bool _disposed{false}; - bool _2BytesPerChar{ true }; + bool _2BytesPerChar{ false }; std::vector _auxBuffer; }; diff --git a/samples/01_blank/xna.cpp b/samples/01_blank/xna.cpp index 5ffee7f..fc4889c 100644 --- a/samples/01_blank/xna.cpp +++ b/samples/01_blank/xna.cpp @@ -19,9 +19,9 @@ namespace xna { auto stream = std::make_shared("D:/file.bin", csharp::FileMode::Open); auto reader = csharp::BinaryReader(stream); auto bo = reader.ReadBoolean(); //reader.ReadChar() + auto x = reader.ReadChar(); //x auto sb = reader.ReadSByte(); //127 auto by = reader.ReadByte(); //255 - auto ch = reader.ReadChar(); //x auto i16 = reader.ReadInt16(); //32767 auto ui16 = reader.ReadUInt16();//65535 auto i32 = reader.ReadInt32(); //2147483647 @@ -29,7 +29,7 @@ namespace xna { auto i64 = reader.ReadInt64(); //9223372036854775807 auto ui64 = reader.ReadUInt64(); //18446744073709551615 auto str = reader.ReadString(); //The string in stream. - auto str2 = reader.ReadString(); //Ç de cedilha e ñ com til mas sem ¨ trema. + auto str2 = reader.ReadString8(); //Ç de cedilha e ñ com til mas sem ¨ trema. } void Initialize() override { diff --git a/sources/csharp/io/binary.cpp b/sources/csharp/io/binary.cpp index c769ace..87a4422 100644 --- a/sources/csharp/io/binary.cpp +++ b/sources/csharp/io/binary.cpp @@ -100,50 +100,12 @@ namespace csharp { } std::string BinaryReader::ReadString() { - if (_disposed) - throw InvalidOperationException(); + return GenericReadString(); + } - const auto stringLength = Read7BitEncodedInt(); - if (stringLength < 0) - { - throw IOException(SR::IO_InvalidStringLen_Len); - } - - if (stringLength == 0) - { - return {}; - } - - auto charBytes = std::vector(MaxCharBytesSize); - int32_t currPos = 0; - - std::string sb; - - do - { - const auto readLength = std::min(MaxCharBytesSize, stringLength - currPos); - const auto n = _stream->Read(charBytes.data(), readLength); - - if (n == 0) - { - throw EndOfStreamException(SR::IO_EOF_ReadBeyondEOF); - } - - const auto chars = reinterpret_cast(charBytes.data()); - - if (currPos == 0 && n == stringLength) - { - return std::string(chars); - } - - sb.append(chars); - - currPos += n; - - } while (currPos < stringLength); - - return sb; - } + std::u8string BinaryReader::ReadString8() { + return GenericReadString(); + } int32_t BinaryReader::Read(char* buffer, int32_t bufferLength, int32_t index, int32_t count) { ArgumentNullException::ThrowIfNull(buffer, "buffer");