eolib-cpp 0.1.0-beta.7
Core C++ library for writing Endless Online applications
Loading...
Searching...
No Matches
eolib::data::EoWriter Class Reference

A class for writing EO data to a sequence of bytes. More...

#include <eolib/data/eo_writer.hpp>

Public Member Functions

 EoWriter ()=default
 Creates an empty writer.
 EoWriter (std::size_t capacity)
 Creates an empty writer with an initial buffer capacity.
void AddByte (int value)
 Adds a raw byte to the writer data.
void AddBytes (const std::vector< std::uint8_t > &bytes)
 Adds an array of raw bytes to the writer data.
void AddBytes (const std::uint8_t *bytes, std::size_t length)
 Adds an array of raw bytes to the writer data.
void AddChar (int number)
 Adds an encoded 1-byte integer to the writer data.
void AddShort (int number)
 Adds an encoded 2-byte integer to the writer data.
void AddThree (int number)
 Adds an encoded 3-byte integer to the writer data.
void AddInt (int number)
 Adds an encoded 4-byte integer to the writer data.
void AddString (std::string_view str)
 Adds a string to the writer data.
void AddFixedString (std::string_view str, int length, bool padded=false)
 Adds a fixed-length string to the writer data.
void AddEncodedString (std::string_view str)
 Adds an encoded string to the writer data.
void AddFixedEncodedString (std::string_view str, int length, bool padded=false)
 Adds a fixed-length encoded string to the writer data.
bool GetStringSanitization () const noexcept
 Gets the string sanitization mode for the writer.
void SetStringSanitization (bool string_sanitization) noexcept
 Sets the string sanitization mode for the writer.
int Length () const noexcept
 Gets the length of the writer data.
std::vector< std::uint8_t > ToByteArray () const &
 Gets a copy of the writer data as a byte array.
std::vector< std::uint8_t > ToByteArray () &&
 Moves the writer data out as a byte array, avoiding a copy.
std::string ToByteString () const
 Gets a copy of the writer data as a byte string.
const std::vector< std::uint8_t > & Data () const noexcept
 Gets a read-only view of the writer data.

Detailed Description

A class for writing EO data to a sequence of bytes.

Strings are treated as raw byte sequences (Windows-1252 in the official client). No transcoding is performed.

Constructor & Destructor Documentation

◆ EoWriter() [1/2]

eolib::data::EoWriter::EoWriter ( )
default

Creates an empty writer.

◆ EoWriter() [2/2]

eolib::data::EoWriter::EoWriter ( std::size_t capacity)
explicit

Creates an empty writer with an initial buffer capacity.

Parameters
capacitythe number of bytes to reserve.

Member Function Documentation

◆ AddByte()

void eolib::data::EoWriter::AddByte ( int value)

Adds a raw byte to the writer data.

Parameters
valuethe byte value to add.
Exceptions
std::invalid_argumentif the value is negative or above 0xFF.

◆ AddBytes() [1/2]

void eolib::data::EoWriter::AddBytes ( const std::vector< std::uint8_t > & bytes)

Adds an array of raw bytes to the writer data.

Parameters
bytesthe bytes to add.

◆ AddBytes() [2/2]

void eolib::data::EoWriter::AddBytes ( const std::uint8_t * bytes,
std::size_t length )

Adds an array of raw bytes to the writer data.

Parameters
bytesa pointer to the bytes to add.
lengththe number of bytes to add from bytes.

◆ AddChar()

void eolib::data::EoWriter::AddChar ( int number)

Adds an encoded 1-byte integer to the writer data.

Parameters
numberthe number to encode and add.
Exceptions
std::invalid_argumentif the value is negative or not below EoNumericLimits::CharMax.

◆ AddShort()

void eolib::data::EoWriter::AddShort ( int number)

Adds an encoded 2-byte integer to the writer data.

Parameters
numberthe number to encode and add.
Exceptions
std::invalid_argumentif the value is negative or not below EoNumericLimits::ShortMax.

◆ AddThree()

void eolib::data::EoWriter::AddThree ( int number)

Adds an encoded 3-byte integer to the writer data.

Parameters
numberthe number to encode and add.
Exceptions
std::invalid_argumentif the value is negative or not below EoNumericLimits::ThreeMax.

◆ AddInt()

void eolib::data::EoWriter::AddInt ( int number)

Adds an encoded 4-byte integer to the writer data.

Parameters
numberthe number to encode and add. It is treated as an unsigned 32-bit integer, so values above INT_MAX may be passed as (wrapped) negative numbers.
Exceptions
std::invalid_argumentif the unsigned value is not below EoNumericLimits::IntMax.

◆ AddString()

void eolib::data::EoWriter::AddString ( std::string_view str)

Adds a string to the writer data.

Parameters
strthe string to add.

◆ AddFixedString()

void eolib::data::EoWriter::AddFixedString ( std::string_view str,
int length,
bool padded = false )

Adds a fixed-length string to the writer data.

Parameters
strthe string to add.
lengththe expected length of the string.
paddedtrue if the string should be padded to the length with trailing 0xFF bytes.
Exceptions
std::invalid_argumentif the length is negative, or the string does not have the expected length (or is longer than it, if padded).

◆ AddEncodedString()

void eolib::data::EoWriter::AddEncodedString ( std::string_view str)

Adds an encoded string to the writer data.

Parameters
strthe string to encode and add.

◆ AddFixedEncodedString()

void eolib::data::EoWriter::AddFixedEncodedString ( std::string_view str,
int length,
bool padded = false )

Adds a fixed-length encoded string to the writer data.

Parameters
strthe string to encode and add.
lengththe expected length of the string.
paddedtrue if the string should be padded to the length with trailing 0xFF bytes.
Exceptions
std::invalid_argumentif the length is negative, or the string does not have the expected length (or is longer than it, if padded).

◆ GetStringSanitization()

bool eolib::data::EoWriter::GetStringSanitization ( ) const
noexcept

Gets the string sanitization mode for the writer.

With string sanitization enabled, the writer will switch 0xFF bytes in strings (ΓΏ) to 0x79 (y).

See: https://github.com/Cirras/eo-protocol/blob/master/docs/chunks.md#sanitization

Returns
true if string sanitization is enabled.

◆ SetStringSanitization()

void eolib::data::EoWriter::SetStringSanitization ( bool string_sanitization)
noexcept

Sets the string sanitization mode for the writer.

Parameters
string_sanitizationtrue to enable string sanitization.

◆ Length()

int eolib::data::EoWriter::Length ( ) const
noexcept

Gets the length of the writer data.

Returns
the number of bytes written.

◆ ToByteArray() [1/2]

std::vector< std::uint8_t > eolib::data::EoWriter::ToByteArray ( ) const &

Gets a copy of the writer data as a byte array.

Returns
a copy of the bytes written.

◆ ToByteArray() [2/2]

std::vector< std::uint8_t > eolib::data::EoWriter::ToByteArray ( ) &&

Moves the writer data out as a byte array, avoiding a copy.

The writer is left empty.

Example: auto bytes = std::move(writer).ToByteArray();

Returns
the bytes written.

◆ ToByteString()

std::string eolib::data::EoWriter::ToByteString ( ) const

Gets a copy of the writer data as a byte string.

Returns
a copy of the bytes written, as a string.

◆ Data()

const std::vector< std::uint8_t > & eolib::data::EoWriter::Data ( ) const
noexcept

Gets a read-only view of the writer data.

Returns
a reference to the bytes written, valid until the writer is modified or destroyed.