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

Implementações em ReadString e ReadString8

This commit is contained in:
Danilo Borges Santos 2024-12-09 16:37:55 -03:00
parent 5957633790
commit 98ee98d67a
3 changed files with 56 additions and 46 deletions

View File

@ -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 <class TSTRING>
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<uint8_t>(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<TSTRING::value_type*>(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<uint8_t>& buffer);
@ -135,7 +183,7 @@ namespace csharp {
std::shared_ptr<Stream> _stream;
bool _leaveOpen;
bool _disposed{false};
bool _2BytesPerChar{ true };
bool _2BytesPerChar{ false };
std::vector<uint8_t> _auxBuffer;
};

View File

@ -19,9 +19,9 @@ namespace xna {
auto stream = std::make_shared<csharp::FileStream>("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 {

View File

@ -100,50 +100,12 @@ namespace csharp {
}
std::string BinaryReader::ReadString() {
if (_disposed)
throw InvalidOperationException();
return GenericReadString<std::string>();
}
const auto stringLength = Read7BitEncodedInt();
if (stringLength < 0)
{
throw IOException(SR::IO_InvalidStringLen_Len);
}
if (stringLength == 0)
{
return {};
}
auto charBytes = std::vector<uint8_t>(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<char*>(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<std::u8string>();
}
int32_t BinaryReader::Read(char* buffer, int32_t bufferLength, int32_t index, int32_t count) {
ArgumentNullException::ThrowIfNull(buffer, "buffer");