Yate
Public Member Functions | Static Public Member Functions | List of all members
DataBlock Class Reference

A class that holds just a block of raw data. More...

#include <yateclass.h>

Inheritance diagram for DataBlock:
GenObject Base64

Public Member Functions

 DataBlock (unsigned int overAlloc=0)
 
 DataBlock (const DataBlock &value)
 
 DataBlock (const DataBlock &value, unsigned int overAlloc)
 
 DataBlock (void *value, unsigned int len, bool copyData=true, unsigned int overAlloc=0)
 
virtual ~DataBlock ()
 
virtual void * getObject (const String &name) const
 
void * data () const
 
unsigned char * data (unsigned int offs, unsigned int len=1) const
 
int at (unsigned int offs, int defvalue=-1) const
 
bool null () const
 
unsigned int length () const
 
unsigned int overAlloc () const
 
void overAlloc (unsigned int bytes)
 
void clear (bool deleteData=true)
 
DataBlockassign (void *value, unsigned int len, bool copyData=true, unsigned int allocated=0)
 
void append (void *value, unsigned int len)
 
void append (const DataBlock &value)
 
void append (const String &value)
 
void insert (const DataBlock &value)
 
void resize (unsigned int len)
 
void truncate (unsigned int len)
 
void cut (int len)
 
int operator[] (signed int index) const
 
int operator[] (unsigned int index) const
 
DataBlockoperator= (const DataBlock &value)
 
DataBlockoperator+= (const DataBlock &value)
 
DataBlockoperator+= (const String &value)
 
bool convert (const DataBlock &src, const String &sFormat, const String &dFormat, unsigned maxlen=0)
 
bool unHexify (const char *data, unsigned int len, char sep)
 
bool unHexify (const char *data, unsigned int len)
 
bool unHexify (const String &data)
 
String sqlEscape (char extraEsc) const
 
- Public Member Functions inherited from GenObject
 GenObject ()
 
virtual ~GenObject ()
 
virtual bool alive () const
 
virtual void destruct ()
 
virtual const StringtoString () const
 
NamedCountergetObjCounter () const
 
NamedCountersetObjCounter (NamedCounter *counter)
 

Static Public Member Functions

static const DataBlockempty ()
 
- Static Public Member Functions inherited from GenObject
static void * getObject (const String &name, const GenObject *obj)
 
static bool getObjCounting ()
 
static void setObjCounting (bool enable)
 
static NamedCountergetObjCounter (const String &name, bool create=true)
 
static ObjListgetObjCounters ()
 

Detailed Description

A class that holds just a block of raw data.

The DataBlock holds a data buffer with no specific formatting.

Constructor & Destructor Documentation

DataBlock ( unsigned int  overAlloc = 0)

Constructs an empty data block

Parameters
overAllocHow many bytes of memory to overallocate
DataBlock ( const DataBlock value)

Copy constructor

Parameters
valueData block to copy from
DataBlock ( const DataBlock value,
unsigned int  overAlloc 
)

Copy constructor with overallocation

Parameters
valueData block to copy from
overAllocHow many bytes of memory to overallocate
DataBlock ( void *  value,
unsigned int  len,
bool  copyData = true,
unsigned int  overAlloc = 0 
)

Constructs an initialized data block

Parameters
valueData to assign, may be NULL to fill with zeros
lenLength of data, may be zero (then value is ignored)
copyDataTrue to make a copy of the data, false to just insert the pointer
overAllocHow many bytes of memory to overallocate
virtual ~DataBlock ( )
virtual

Destroys the data, disposes the memory.

Member Function Documentation

void append ( void *  value,
unsigned int  len 
)
inline

Append data to the current block

Parameters
valueData to append
lenLength of data

References DataBlock::clear().

Referenced by DataBlock::operator+=(), and Base64::operator<<().

void append ( const DataBlock value)

Append data to the current block

Parameters
valueData to append
void append ( const String value)

Append a String to the current block

Parameters
valueString to append
DataBlock& assign ( void *  value,
unsigned int  len,
bool  copyData = true,
unsigned int  allocated = 0 
)

Assign data to the object

Parameters
valueData to assign, may be NULL to fill with zeros
lenLength of data, may be zero (then value is ignored)
copyDataTrue to make a copy of the data, false to just insert the pointer
allocatedReal allocated data length in case it should not be copied

Referenced by DataBlock::resize().

int at ( unsigned int  offs,
int  defvalue = -1 
) const
inline

Get the value of a single byte inside the stored data

Parameters
offsByte offset inside the stored data
defvalueDefault value to return if offset is outside data
Returns
Byte value at offset (0-255) or defvalue if offset outside data

Referenced by DataBlock::operator[]().

void clear ( bool  deleteData = true)

Clear the data and optionally free the memory

Parameters
deleteDataTrue to free the deta block, false to just forget it

Referenced by DataBlock::append().

bool convert ( const DataBlock src,
const String sFormat,
const String dFormat,
unsigned  maxlen = 0 
)

Convert data from a different format

Parameters
srcSource data block
sFormatName of the source format
dFormatName of the destination format
maxlenMaximum amount to convert, 0 to use source
Returns
True if converted successfully, false on failure
void cut ( int  len)

Cut off a number of bytes from the data block

Parameters
lenAmount to cut, positive to cut from end, negative to cut from start of block
void* data ( ) const
inline
unsigned char* data ( unsigned int  offs,
unsigned int  len = 1 
) const
inline

Get a pointer to a byte range inside the stored data.

Parameters
offsByte offset inside the stored data
lenNumber of bytes that must be valid starting at offset
Returns
A pointer to the data or NULL if the range is not available.
static const DataBlock& empty ( )
static

A static empty data block

virtual void* getObject ( const String name) const
virtual

Get a pointer to a derived class given that class name

Parameters
nameName of the class we are asking for
Returns
Pointer to the requested class or NULL if this object doesn't implement it

Reimplemented from GenObject.

void insert ( const DataBlock value)

Insert data before the current block

Parameters
valueData to insert
unsigned int length ( ) const
inline
bool null ( ) const
inline

Checks if the block holds a NULL pointer.

Returns
True if the block holds NULL, false otherwise.
DataBlock& operator+= ( const DataBlock value)
inline

Appending operator.

References DataBlock::append().

DataBlock& operator+= ( const String value)
inline

Appending operator for Strings.

References DataBlock::append().

DataBlock& operator= ( const DataBlock value)

Assignment operator.

int operator[] ( signed int  index) const
inline

Byte indexing operator with signed parameter

Parameters
indexIndex of the byte to retrieve
Returns
Byte value at offset (0-255) or -1 if index outside data

References DataBlock::at().

int operator[] ( unsigned int  index) const
inline

Byte indexing operator with unsigned parameter

Parameters
indexIndex of the byte to retrieve
Returns
Byte value at offset (0-255) or -1 if index outside data

References DataBlock::at().

unsigned int overAlloc ( ) const
inline

Get the memory overallocation setting.

Returns
Amount of memory that will be overallocated.
void overAlloc ( unsigned int  bytes)
inline

Set the memory overallocation.

Parameters
bytesHow many bytes of memory to overallocate
void resize ( unsigned int  len)
inline

Resize (re-alloc or free) this block if required size is not the same as the current one

Parameters
lenRequired block size

References DataBlock::assign(), and DataBlock::length().

String sqlEscape ( char  extraEsc) const

Create an escaped string suitable for use in SQL queries

Parameters
extraEscCharacter to escape other than the default ones
Returns
A string with binary zeros and other special characters escaped
void truncate ( unsigned int  len)

Truncate the data block

Parameters
lenThe maximum length to keep
bool unHexify ( const char *  data,
unsigned int  len,
char  sep 
)

Build this data block from a hexadecimal string representation. Each octet must be represented in the input string with 2 hexadecimal characters. If a separator is specified, the octets in input string must be separated using exactly 1 separator. Only 1 leading or 1 trailing separators are allowed.

Parameters
dataInput character string
lenLength of the input string
sepSeparator character used between octets. 0 if no separator is expected
Returns
True if the input string was succesfully parsed, false otherwise

Referenced by SliceVector< Obj >::unHexify(), and DataBlock::unHexify().

bool unHexify ( const char *  data,
unsigned int  len 
)

Build this data block from a hexadecimal string representation. Each octet must be represented in the input string with 2 hexadecimal characters. This method guesses if separators are used. If so the octets in input string must be separated using exactly 1 separator. Only 1 leading or 1 trailing separators are allowed.

Parameters
dataInput character string
lenLength of the input string
Returns
True if the input string was succesfully parsed, false otherwise
bool unHexify ( const String data)
inline

Build this data block from a hexadecimal string representation. This version parses a String and guesses separators presence.

Parameters
dataInput character string
Returns
True if the input string was succesfully parsed, false otherwise

References String::c_str(), String::length(), and DataBlock::unHexify().


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