Main Page · Modules · All Classes · Class Hierarchy
Public Member Functions | Protected Attributes | List of all members
MCBinaryData Class Reference

Binary data class. More...

#include <MCBinaryData.hpp>

Public Member Functions

 MCBinaryData ()
 Class constructor.
 
 MCBinaryData (int size)
 Class constructor. More...
 
 MCBinaryData (int size, unsigned char value)
 Class constructor. More...
 
 MCBinaryData (const MCBinaryData &other)
 Copy constructor. More...
 
 MCBinaryData (MCBinaryData &&other)
 Move constructor. More...
 
void Allocate (int size)
 Allocate a certain data size. More...
 
void Shrink (int size)
 Shrink to a certain data size. More...
 
void Free ()
 Free the allocated data.
 
void Clear ()
 Clear the allocated data. More...
 
bool IsEmpty () const
 Check if the binary data is not filled with invalid data. More...
 
void Set (unsigned char *data, int size)
 Set binary data content. More...
 
void Clone (const MCBinaryData &other)
 Clone an other binary data instance. More...
 
MCBinaryDataClone ()
 Clone the binary data.
 
bool WriteData (MCBinaryData &target, int capacity=-1)
 Write data into a target instance. More...
 
unsigned int CopyData (MCBinaryData &target, int capacity=-1) const
 Copy data into a target instance. More...
 
bool LoadFromFile (const std::string &file_name)
 Load data from a file. More...
 
bool LoadFromQtResource (const QString &resource_str)
 Load data from a Qt resource. More...
 
bool SaveToFile (const std::string &file_name) const
 Save data to a file. More...
 
MCBinaryData GenerateMd5Hash () const
 Generate md5 hash. More...
 
void AddUChar (unsigned char new_char)
 Add an unsigned char at the current position. More...
 
void AddInt16 (int16_t new_int, bool reverse_order=false)
 Add a 16 bit integer at the current position. More...
 
void AddInt32 (int32_t new_int, bool reverse_order=false)
 Add a 32 bit integer at the current position. More...
 
void AddString (const std::string &str)
 Add a string at the current position. More...
 
unsigned char GetUChar ()
 Get an unsigned char from the current position. More...
 
int16_t GetInt16 (bool reverse_order=false)
 Get a 16 bit integer from the current position. More...
 
int32_t GetInt32 (bool reverse_order=false)
 Get a 32 bit integer from the current position. More...
 
bool ValidateString (unsigned int length, const std::string &additional_chars="")
 Check if a string can be read from the current position. More...
 
std::string GetString (unsigned int length, const std::string &additional_chars="")
 Get a string from the current position. More...
 
unsigned char * GetData () const
 Get direct access to the binary data. More...
 
int GetSize () const
 Get binary data size. More...
 
const std::vector< unsigned char > & GetVectorData () const
 Get binary data in vector form. More...
 
int GetPosition () const
 Get the current position in the binary data. More...
 
bool IsPositionAtEnd () const
 Check if the current position is at the end of the binary data. More...
 
unsigned int GetRemainingCapacity (unsigned int capacity) const
 Check if the binary data has enough remaining capacity. More...
 
void SetPosition (unsigned int position)
 Set the cursor position. More...
 
void IncrementPosition (unsigned int position=1)
 Increment the cursor position. More...
 
void ResetPosition ()
 Reset the cursor position back to the start position.
 
std::string ToBase64 () const
 Convert the content to base64. More...
 
void FromBase64 (const std::string &base64_str)
 Convert back from base64. More...
 
MCBinaryDataoperator= (MCBinaryData &&other)
 Move assignment operator. More...
 
MCBinaryDataoperator= (const MCBinaryData &other)
 Assignment operator. More...
 
bool operator== (const MCBinaryData &other) const
 Equality (==) operator. More...
 
bool operator!= (const MCBinaryData &other) const
 Inequality (!=) operator. More...
 

Protected Attributes

std::vector< unsigned char > Data
 Pointer to the encapsulated data.
 
int Size
 Size of the data.
 
int Position
 Internal position index.
 

Detailed Description

Binary data class.

Definition at line 50 of file MCBinaryData.hpp.

Constructor & Destructor Documentation

MCBinaryData::MCBinaryData ( int  size)
explicit

Class constructor.

Parameters
sizeInitial size

Definition at line 149 of file MCBinaryData.cpp.

MCBinaryData::MCBinaryData ( int  size,
unsigned char  value 
)

Class constructor.

Parameters
sizeInitial size
valueInitialize the bufer with this value

Definition at line 155 of file MCBinaryData.cpp.

MCBinaryData::MCBinaryData ( const MCBinaryData other)

Copy constructor.

Parameters
otherOther instance

Definition at line 162 of file MCBinaryData.cpp.

MCBinaryData::MCBinaryData ( MCBinaryData &&  other)

Move constructor.

Parameters
otherOther instance

Definition at line 168 of file MCBinaryData.cpp.

Member Function Documentation

void MCBinaryData::AddInt16 ( int16_t  new_int,
bool  reverse_order = false 
)

Add a 16 bit integer at the current position.

Parameters
new_intNew integer
reverse_orderReverse order mode

Definition at line 391 of file MCBinaryData.cpp.

void MCBinaryData::AddInt32 ( int32_t  new_int,
bool  reverse_order = false 
)

Add a 32 bit integer at the current position.

Parameters
new_intNew integer
reverse_orderReverse order mode

Definition at line 412 of file MCBinaryData.cpp.

void MCBinaryData::AddString ( const std::string &  str)

Add a string at the current position.

Parameters
strString

Definition at line 437 of file MCBinaryData.cpp.

void MCBinaryData::AddUChar ( unsigned char  new_char)

Add an unsigned char at the current position.

Parameters
new_charNew character

Definition at line 379 of file MCBinaryData.cpp.

void MCBinaryData::Allocate ( int  size)

Allocate a certain data size.

Parameters
sizeData size

Definition at line 181 of file MCBinaryData.cpp.

void MCBinaryData::Clear ( )

Clear the allocated data.

The data is filled with 0xff as "invalid data".

Definition at line 230 of file MCBinaryData.cpp.

void MCBinaryData::Clone ( const MCBinaryData other)

Clone an other binary data instance.

Parameters
otherOther instance

Definition at line 263 of file MCBinaryData.cpp.

unsigned int MCBinaryData::CopyData ( MCBinaryData target,
int  capacity = -1 
) const

Copy data into a target instance.

Parameters
targetTarget instance
capacityCapacity
Returns
Copied byte count if the write operation is successful otherwise false.

If capacity is -1, the target size is used for writing. The Position variable is not modified by this function and therefore it is const.

Definition at line 295 of file MCBinaryData.cpp.

void MCBinaryData::FromBase64 ( const std::string &  base64_str)

Convert back from base64.

Parameters
base64_strBase64 string

Definition at line 614 of file MCBinaryData.cpp.

MCBinaryData MCBinaryData::GenerateMd5Hash ( ) const

Generate md5 hash.

Returns
Md5 hash (16 bytes)

Definition at line 370 of file MCBinaryData.cpp.

unsigned char * MCBinaryData::GetData ( ) const

Get direct access to the binary data.

Returns
Pointer to the internal data.

Definition at line 548 of file MCBinaryData.cpp.

int16_t MCBinaryData::GetInt16 ( bool  reverse_order = false)

Get a 16 bit integer from the current position.

Parameters
reverse_orderReverse order mode
Returns
The read integer if successful otherwise zero.

Definition at line 463 of file MCBinaryData.cpp.

int32_t MCBinaryData::GetInt32 ( bool  reverse_order = false)

Get a 32 bit integer from the current position.

Parameters
reverse_orderReverse order mode
Returns
The read integer if successful otherwise zero.

Definition at line 483 of file MCBinaryData.cpp.

int MCBinaryData::GetPosition ( ) const

Get the current position in the binary data.

Returns
Current position in the binary data

The cursor position is zero in the beginning and reading/writing increases the current position towards the end of the binary data.

Definition at line 566 of file MCBinaryData.cpp.

unsigned int MCBinaryData::GetRemainingCapacity ( unsigned int  capacity) const

Check if the binary data has enough remaining capacity.

Parameters
capacityDesired remaining capacity
Returns
The true remaining capacity. Equal or less than desired.

The remaining capacity is the size of the remaining bytes after the current cursor position.

Definition at line 572 of file MCBinaryData.cpp.

int MCBinaryData::GetSize ( ) const

Get binary data size.

Returns
Binary data size

Definition at line 554 of file MCBinaryData.cpp.

std::string MCBinaryData::GetString ( unsigned int  length,
const std::string &  additional_chars = "" 
)

Get a string from the current position.

Parameters
lengthString length
additional_charsAdditional valid characters
Returns
The string if it can be read otherwise empty string.

The latin characters, numbers and space are valid, but additional characters can be specified by additional_chars.

Definition at line 534 of file MCBinaryData.cpp.

unsigned char MCBinaryData::GetUChar ( )

Get an unsigned char from the current position.

Returns
The read character if successful otherwise zero.

Definition at line 449 of file MCBinaryData.cpp.

const std::vector< unsigned char > & MCBinaryData::GetVectorData ( ) const

Get binary data in vector form.

Returns
Binary data in vector form

Definition at line 560 of file MCBinaryData.cpp.

void MCBinaryData::IncrementPosition ( unsigned int  position = 1)

Increment the cursor position.

Parameters
positionPosition increment

Definition at line 590 of file MCBinaryData.cpp.

bool MCBinaryData::IsEmpty ( ) const

Check if the binary data is not filled with invalid data.

Returns
True if binary data contains valid data otherwise false.

Definition at line 239 of file MCBinaryData.cpp.

bool MCBinaryData::IsPositionAtEnd ( ) const

Check if the current position is at the end of the binary data.

Returns
True if the end of the binary data reached otherwise false.

Definition at line 578 of file MCBinaryData.cpp.

bool MCBinaryData::LoadFromFile ( const std::string &  file_name)

Load data from a file.

Parameters
file_nameFile name
Returns
True if the data was loaded successfully otherwise false.

Definition at line 317 of file MCBinaryData.cpp.

bool MCBinaryData::LoadFromQtResource ( const QString &  resource_str)

Load data from a Qt resource.

Parameters
resource_strResource string
Returns
True if the data was loaded successfully otherwise false.

Definition at line 335 of file MCBinaryData.cpp.

bool MCBinaryData::operator!= ( const MCBinaryData other) const

Inequality (!=) operator.

Parameters
otherOther instance
Returns
True if the instances are not equal.

Definition at line 670 of file MCBinaryData.cpp.

MCBinaryData & MCBinaryData::operator= ( MCBinaryData &&  other)

Move assignment operator.

Parameters
otherOther instance

Definition at line 627 of file MCBinaryData.cpp.

MCBinaryData & MCBinaryData::operator= ( const MCBinaryData other)

Assignment operator.

Parameters
otherOther instance

Definition at line 646 of file MCBinaryData.cpp.

bool MCBinaryData::operator== ( const MCBinaryData other) const

Equality (==) operator.

Parameters
otherOther instance
Returns
True if the instances are equal.

Definition at line 656 of file MCBinaryData.cpp.

bool MCBinaryData::SaveToFile ( const std::string &  file_name) const

Save data to a file.

Parameters
file_nameFile name
Returns
True if the data was saved successfully otherwise false.

Definition at line 355 of file MCBinaryData.cpp.

void MCBinaryData::Set ( unsigned char *  data,
int  size 
)

Set binary data content.

Parameters
dataNew data
sizeBinary data size

If the new data has different size then the binary data is reallocated.

Definition at line 250 of file MCBinaryData.cpp.

void MCBinaryData::SetPosition ( unsigned int  position)

Set the cursor position.

Parameters
positionNew position

Definition at line 584 of file MCBinaryData.cpp.

void MCBinaryData::Shrink ( int  size)

Shrink to a certain data size.

Parameters
sizeNew data size

The data is freed if size is zero.

Definition at line 204 of file MCBinaryData.cpp.

std::string MCBinaryData::ToBase64 ( ) const

Convert the content to base64.

Returns
Encoded string in base64

Definition at line 602 of file MCBinaryData.cpp.

bool MCBinaryData::ValidateString ( unsigned int  length,
const std::string &  additional_chars = "" 
)

Check if a string can be read from the current position.

Parameters
lengthString length
additional_charsAdditional valid characters
Returns
True if the string can be read otherwise false.

The latin characters, numbers and space are valid, but additional characters can be specified by additional_chars.

Definition at line 503 of file MCBinaryData.cpp.

bool MCBinaryData::WriteData ( MCBinaryData target,
int  capacity = -1 
)

Write data into a target instance.

Parameters
targetTarget instance
capacityCapacity
Returns
True if the write operation is successful otherwise false.

If capacity is -1, the target size is used for writing.

Definition at line 282 of file MCBinaryData.cpp.


The documentation for this class was generated from the following files: