mirror of
https://github.com/borgesdan/xn65
synced 2024-12-29 21:54:47 +01:00
Implementa BinaryWriter
This commit is contained in:
parent
93a94d0aae
commit
35ff33b0b5
@ -190,7 +190,8 @@ namespace csharp {
|
||||
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");
|
||||
@ -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;
|
||||
|
||||
|
@ -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 {
|
||||
@ -49,18 +56,13 @@ namespace csharp {
|
||||
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) {
|
||||
return IsInRangeInclusive(value, 0xD800U, 0xDBFFU);
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
|
@ -12,25 +12,6 @@ namespace xna {
|
||||
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.
|
||||
}
|
||||
|
||||
void Initialize() override {
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user