Appends the chain with the first nBytes nB (void*) buffer of given size. This operation makes no assumptions about buffer content and so the operation is not null terminated.
| Return Type | Function name | Arguments |
|---|---|---|
| uint32_t | hzXbuf::Append | (const void*,uint32_t,) |
Declared in file: hzXbuf.h
Defined in file : hzXbuf.cpp
Function Logic:
Function body:
uint32_t hzXbuf::Append (const void* vpStr)uint32_t nBytes,
{
// Appends the chain with the first nBytes nB (void*) buffer of given size. This operation makes no assumptions about buffer content and so the operation is
// not null terminated.
//
// Arguments: 1) vpStr The buffer address as void*
// 2) nBytes The number of bytes from the buffer to append to the chain
//
// Returns: Number of bytes added
_hzfunc("hzXbuf::Append(1)") ;
_xblk* curBlk ; // Working block pointer
_xblk* newBlk ; // Working block pointer
const char* i ; // Input iterator
uint32_t nCan ; // Max number of bytes that can be written to current block
uint32_t nWritten = 0; // Limiter
if (!nBytes)
return nBytes ;
// If nothing in chain, create the first block
if (!mx)
mx = new _xbuf() ;
if (!mx->m_Begin)
mx->m_Begin = mx->m_End = _xblk_alloc() ;
curBlk = (_xblk*) mx->m_End ;
if (!curBlk)
Fatal("Chain %p has no end block\n", this) ;
i = (char*) vpStr ;
for (; nWritten < nBytes ;)
{
if (curBlk->xize == XBLKSIZE)
{
// Out of space - make new block
newBlk = _xblk_alloc() ;
if (!newBlk)
Fatal("No allocation (case 2)\n") ;
// newBlk->Prev((_xblk*) mx->m_End) ;
curBlk->Next(newBlk) ;
mx->m_End = newBlk ;
curBlk = newBlk ;
}
nCan = XBLKSIZE - curBlk->xize ;
if ((nWritten + nCan) > nBytes)
nCan = nBytes - nWritten ;
// Add bytes to current block
memcpy(curBlk->m_Data + curBlk->xize, i, nCan) ;
curBlk->xize += nCan ;
i += nCan ;
mx->m_nSize += nCan ;
nWritten += nCan ;
}
return nWritten ;
}