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

Implementa BinaryWriter

This commit is contained in:
Danilo Borges Santos 2024-12-13 13:40:51 -03:00
parent 93a94d0aae
commit 35ff33b0b5
4 changed files with 121 additions and 43 deletions

View File

@ -8,10 +8,10 @@
namespace csharp {
/*
* The BinaryReader class uses byte encodings, by default UTF8.
* The BinaryReader class uses byte encodings, by default UTF8.
* This was not implemented, but we tried to follow the same standard.
* Also the reading of primitives was modified.
*
*
*/
//TODO: ReadString and ReadChar as it only reads ASCII characters
@ -22,7 +22,7 @@ namespace csharp {
class BinaryReader {
public:
BinaryReader(std::shared_ptr<Stream> const& input, bool leaveOpen = false)
: _stream(input), _leaveOpen(leaveOpen)
: _stream(input), _leaveOpen(leaveOpen)
{
ArgumentNullException::ThrowIfNull(input.get(), "input");
@ -48,7 +48,7 @@ namespace csharp {
virtual uint8_t ReadByte() {
return InternalReadByte();
}
virtual inline int8_t ReadSByte() {
return static_cast<int8_t>(InternalReadByte());
}
@ -58,8 +58,8 @@ namespace csharp {
}
virtual char ReadChar(bool twoBytesPerChar = false);
virtual int16_t ReadInt16() {
virtual int16_t ReadInt16() {
return ReadNumeric<int16_t>();
}
@ -141,7 +141,7 @@ namespace csharp {
{
throw EndOfStreamException(SR::IO_EOF_ReadBeyondEOF);
}
const auto chars = reinterpret_cast<TSTRING::value_type*>(charBytes.data());
if (currPos == 0 && n == stringLength)
@ -165,11 +165,11 @@ namespace csharp {
template<class TNUMERIC>
TNUMERIC ReadNumeric() {
const auto numericSize = sizeof(TNUMERIC);
const auto numericSize = sizeof(TNUMERIC);
if (_auxBuffer.size() != numericSize)
_auxBuffer.resize(numericSize);
InternalRead(_auxBuffer);
const auto ptr = reinterpret_cast<TNUMERIC*>(_auxBuffer.data());
@ -182,19 +182,20 @@ namespace csharp {
std::shared_ptr<Stream> _stream;
bool _leaveOpen;
bool _disposed{false};
bool _disposed{ false };
std::vector<uint8_t> _auxBuffer;
std::vector<uint8_t> _auxBuffer;
};
class BinaryWriter {
public:
BinaryWriter(std::shared_ptr<Stream> const& output)
: BinaryWriter(output, false) { }
: BinaryWriter(output, false) {
}
BinaryWriter(std::shared_ptr<Stream> const& output, bool leaveOpen) {
ArgumentNullException::ThrowIfNull(output.get(), "output");
if (!output->CanWrite())
throw ArgumentException(SR::Argument_StreamNotWritable);
@ -242,13 +243,82 @@ namespace csharp {
OutStream->Write(buffer, bufferLength, index, count);
}
virtual void Write(char ch);
inline virtual void Write(char ch) {
const auto byte = static_cast<uint8_t>(ch);
OutStream->Write(&byte, 1);
}
inline virtual void Write(char* chars, int32_t charsLength) {
ArgumentNullException::ThrowIfNull(chars, "chars");
auto bytes = reinterpret_cast<const uint8_t*>(chars);
OutStream->Write(bytes, charsLength);
}
inline virtual void Write(char* chars, int32_t charsLength, int32_t index, int32_t count) {
ArgumentNullException::ThrowIfNull(chars, "chars");
ArgumentOutOfRangeException::ThrowIfNegative(index, "index");
ArgumentOutOfRangeException::ThrowIfNegative(count, "count");
if (index > charsLength - count)
throw ArgumentOutOfRangeException("index");
auto bytes = reinterpret_cast<const uint8_t*>(chars);
OutStream->Write(bytes, charsLength, index, count);
}
inline virtual void Write(double value) {
WriteNumeric(value);
}
inline virtual void Write(int16_t value) {
WriteNumeric(value);
}
inline virtual void Write(uint16_t value) {
WriteNumeric(value);
}
inline virtual void Write(int32_t value) {
WriteNumeric(value);
}
inline virtual void Write(uint32_t value) {
WriteNumeric(value);
}
inline virtual void Write(int64_t value) {
WriteNumeric(value);
}
inline virtual void Write(uint64_t value) {
WriteNumeric(value);
}
inline virtual void Write(float value) {
WriteNumeric(value);
}
virtual void Write(std::string const& value) {
Write7BitEncodedInt(static_cast<int64_t>(value.size()));
auto bytes = reinterpret_cast<const uint8_t*>(value.data());
OutStream->Write(bytes, value.size());
}
void Write7BitEncodedInt(int32_t value);
void Write7BitEncodedInt(int64_t value);
protected:
BinaryWriter() {
OutStream = Stream::Null;
}
template <typename TNUMERIC>
void WriteNumeric(TNUMERIC const& value) {
auto bytes = reinterpret_cast<const uint8_t*>(&value);
const auto size = sizeof(value);
OutStream->Write(bytes, static_cast<int32_t>(size));
}
protected:
std::shared_ptr<Stream> OutStream;

View File

@ -2,6 +2,13 @@
#define CSHARP_TEXT_UNICODE_HPP
#include <cstdint>
#include <vector>
#include "csharp/exception.hpp"
//
// The char type keyword is an alias for the .NET System.Char structure type that represents a Unicode UTF-16 character.
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/char
//
namespace csharp {
struct UnicodeUtility {
@ -48,11 +55,6 @@ namespace csharp {
static constexpr bool IsAsciiCodePoint(uint32_t value) {
return value <= 0x7Fu;
}
// Returns true if value is in the Basic Multilingual Plane (BMP
static constexpr bool IsAsciiCodePoint(uint32_t value) {
return value <= 0xFFFFu;
}
//Returns true if is a UTF-16 high surrogate code point, i.e., is in [ U+D800..U+DBFF ], inclusive.
static constexpr bool IsHighSurrogateCodePoint(uint32_t value) {
@ -60,7 +62,7 @@ namespace csharp {
}
//Returns true if is between
static consexpr bool IsInRangeInclusive(uint32_t value, uint32_t lowerBound, uint32_t upperBound) {
static constexpr bool IsInRangeInclusive(uint32_t value, uint32_t lowerBound, uint32_t upperBound) {
return (value - lowerBound) <= (upperBound - lowerBound);
}
@ -75,7 +77,7 @@ namespace csharp {
}
//Returns true if value is a valid Unicode code point, i.e., is in [ U+0000..U+10FFFF ], inclusive.
static constexpr bool IsValidCodePoint(uint32_t value) {
static constexpr bool IsValidCodePoint(uint32_t codePoint) {
return codePoint <= 0x10FFFFU;
}

View File

@ -11,26 +11,7 @@ namespace xna {
class Game1 : public Game {
public:
Game1() : Game() {
Content()->RootDirectory("Content");
short inteiro = 0xC3 | 0xA7;
char charpa = inteiro;
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 cedilha = reader.ReadChar8(); //ç
auto sb = reader.ReadSByte(); //127
auto by = reader.ReadByte(); //255
auto i16 = reader.ReadInt16(); //32767
auto ui16 = reader.ReadUInt16();//65535
auto i32 = reader.ReadInt32(); //2147483647
auto ui32 = reader.ReadUInt32(); //4294967295
auto i64 = reader.ReadInt64(); //9223372036854775807
auto ui64 = reader.ReadUInt64(); //18446744073709551615
auto str = reader.ReadString(); //The string in stream.
auto str2 = reader.ReadString8(); //Ç de cedilha e ñ com til mas sem ¨ trema.
Content()->RootDirectory("Content");
}
void Initialize() override {

View File

@ -1,6 +1,7 @@
#include "csharp/io/binary.hpp"
#include <vector>
#include <cstdint>
#include "csharp/text/unicode.hpp"
namespace csharp {
int32_t BinaryReader::PeekChar() {
@ -82,7 +83,7 @@ namespace csharp {
return static_cast<uint8_t>(b);
}
char BinaryReader::ReadChar(bool twoBytes = false) {
char BinaryReader::ReadChar(bool twoBytes) {
const auto value = Read(twoBytes);
if (value == -1)
@ -295,4 +296,28 @@ namespace csharp {
result |= static_cast<int64_t>(byteReadJustNow) << (MaxBytesWithoutOverflow * 7);
return static_cast<int64_t>(result);
}
void BinaryWriter::Write7BitEncodedInt(int32_t value) {
auto uValue = static_cast<uint32_t>(value);
while (uValue > 0x7Fu)
{
Write(static_cast<uint8_t>(uValue | ~0x7Fu));
uValue >>= 7;
}
Write(static_cast<uint8_t>(uValue));
}
void BinaryWriter::Write7BitEncodedInt(int64_t value) {
auto uValue = static_cast<uint32_t>(value);
while (uValue > 0x7Fu)
{
Write(static_cast<uint8_t>(uValue | ~0x7Fu));
uValue >>= 7;
}
Write(static_cast<uint8_t>(uValue));
}
}