📄 vmbytearray.cpp
字号:
RETURNS: contents of the cell location specified by lIndex
*/
const BYTE VMByteArray::GetAt( long lIndex ) const
{
return( ElementAt( lIndex ) );
}
/* End of function "VMByteArray::GetAt"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMByteArray::ElementAt
DESCRIPTION: fetch a cell by index
INPUT: lIndex - the index to fetch
OUTPUT: none
RETURNS: contents of the cell location specified by lIndex
*/
const BYTE VMByteArray::ElementAt( long lIndex ) const
{
if ( lIndex <= m_lBufferSize )
return( *( m_pData + lIndex ) );
else
return( 0 );
}
/* End of function "VMByteArray::ElementAt"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMByteArray::operator[]
DESCRIPTION: indexing operator. returns the element contents at the
given location
INPUT: lIndex - the index to fetch
OUTPUT: none
RETURNS: contents of the cell location specified by lIndex
*/
const BYTE VMByteArray::operator[]( long lIndex )
{
return( ElementAt( lIndex ) );
}
/* End of function "VMByteArray::operator[]"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMByteArray::SetAt
DESCRIPTION: set the value of a specific cell in the array
INPUT: lIndex - the offset to assign a value to
rNewByte - the value to assign at the given offset
OUTPUT:
RETURNS: void
*/
void VMByteArray::SetAt( long lIndex, BYTE& rNewByte )
{
SetAtGrow( lIndex, rNewByte );
}
/* End of function "VMByteArray::SetAt"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMByteArray::SetAtGrow
DESCRIPTION: sets the value of a specific offset in the array. grows
the array if necessary to accomodate the given index
INPUT: lIndex - the index to assign a value to
roNewByte - the value to assign at the given index
OUTPUT:
RETURNS: void
*/
void VMByteArray::SetAtGrow( long lIndex, BYTE& roNewByte )
{
if ( lIndex < m_lBufferSize )
{
if ( lIndex > m_lAllocated )
{
m_lAllocated = lIndex;
}
*( m_pData + lIndex ) = roNewByte;
}
else
if ( m_lGrowBy != - 1 )
{
BYTE* pOld = m_pData;
if ( lIndex <= m_lBufferSize + m_lGrowBy )
{
m_pData = new BYTE[ m_lBufferSize + m_lGrowBy ];
memcpy( m_pData, pOld, m_lAllocated );
m_lBufferSize = m_lBufferSize + m_lGrowBy;
delete [] pOld;
m_lAllocated = lIndex;
*( m_pData + lIndex ) = roNewByte;
}
else
{
m_pData = new BYTE[ lIndex ];
memcpy( m_pData, pOld, m_lAllocated );
m_lBufferSize = lIndex;
delete [] pOld;
m_lAllocated = lIndex;
*( m_pData + lIndex ) = roNewByte;
}
}
}
/* End of function "VMByteArray::SetAtGrow"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMByteArray::Append
DESCRIPTION: Add a new value to the "end" of the array
INPUT: roNewByte - the value to assign
OUTPUT: none
RETURNS: the number of cells allocated in the array
*/
long VMByteArray::Append( BYTE& roNewByte )
{
SetAtGrow( m_lAllocated, roNewByte );
return( m_lAllocated );
}
/* End of function "VMByteArray::Append"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMByteArray::Append
DESCRIPTION: appends an array of bytes on to the end of this array
INPUT: roToAppend - the array to append on to this array
OUTPUT: none
RETURNS: the number of cells allocated in this array
*/
long VMByteArray::Append( const VMByteArray& roToAppend )
{
long lOtherBufferSize = roToAppend.GetSize();
long lOtherAllocated = roToAppend.GetUpperBound();
if ( lOtherAllocated <= m_lBufferSize - m_lAllocated )
{
memcpy( m_pData + m_lAllocated, roToAppend.GetData(), lOtherAllocated );
m_lAllocated += lOtherAllocated;
}
else
{
BYTE* pOld = m_pData;
m_pData = new BYTE[ m_lBufferSize + lOtherBufferSize ];
m_lBufferSize += lOtherBufferSize;
memcpy( m_pData, pOld, m_lAllocated );
memcpy( m_pData + m_lAllocated, roToAppend.GetData(), lOtherAllocated );
m_lAllocated += lOtherAllocated;
delete [] pOld;
}
return( m_lAllocated );
}
/* End of function "VMByteArray::Append"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMByteArray::Append
DESCRIPTION: appends an array of bytes on to the end of this array
INPUT: pchToAppend - the characters to append on to this array
iCount - the number of characters to append
OUTPUT: none
RETURNS: the number of cells allocated in this array
*/
long VMByteArray::Append( const char* pchToAppend, int iCount )
{
if ( iCount <= m_lBufferSize - m_lAllocated )
{
memcpy( m_pData + m_lAllocated, pchToAppend, iCount );
m_lAllocated += iCount;
}
else
{
BYTE* pOld = m_pData;
m_pData = new BYTE[ m_lBufferSize + iCount ];
m_lBufferSize += iCount;
memcpy( m_pData, pOld, m_lAllocated );
memcpy( m_pData + m_lAllocated, pchToAppend, iCount );
m_lAllocated += iCount;
delete [] pOld;
}
return( m_lAllocated );
}
/* End of function "VMByteArray::Append"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMByteArray::Add
DESCRIPTION: appends a single character to the end of the byte array
INPUT: chNewElement : the character to add
OUTPUT:
RETURNS: the index where the character was placed
*/
int VMByteArray::Add( BYTE chNewElement )
{
int iIndex = m_lAllocated;
SetAtGrow( iIndex, chNewElement );
return( iIndex );
}
/* End of function "VMByteArray::Add"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMByteArray::InsertAt
DESCRIPTION: allows a block of cells in this array to be stamped
with a given value starting at the specified index
INPUT: lIndex - the index to start at
roToInsert - the value to stamp in the affected cells
lCount - the number of cells to stamp with the given
value
OUTPUT:
RETURNS: void
*/
void VMByteArray::InsertAt( long lIndex, BYTE& roToInsert, long lCount )
{
for( long iLoop = 0; iLoop < lCount; iLoop++ )
SetAtGrow( lIndex, roToInsert );
}
/* End of function "VMByteArray::InsertAt"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMByteArray::InsertAt
DESCRIPTION: allows another array to be inserted in to this array
at a specified index
INPUT: lIndex - the index to start the insertion at
roToInsert - the array to insert
OUTPUT:
RETURNS: void
*/
void VMByteArray::InsertAt( long lIndex, VMByteArray& roToInsert )
{
long lOtherBufferSize = roToInsert.GetSize();
long lOtherAllocated = roToInsert.GetUpperBound();
if ( lOtherAllocated + lIndex <= m_lBufferSize )
{
memcpy( m_pData + lIndex, roToInsert.GetData(), lOtherAllocated );
if ( lIndex + lOtherAllocated > m_lAllocated )
m_lAllocated += ( lIndex + lOtherAllocated ) - m_lAllocated;
}
else
{
BYTE* pOld = m_pData;
m_pData = new BYTE[ m_lBufferSize + lOtherBufferSize ];
m_lBufferSize += lOtherBufferSize;
memcpy( m_pData, pOld, m_lAllocated );
memcpy( m_pData + lIndex, roToInsert.GetData(), lOtherAllocated );
if ( lIndex + lOtherAllocated > m_lAllocated )
m_lAllocated += ( lIndex + lOtherAllocated ) - m_lAllocated;
delete [] pOld;
}
}
/* End of function "VMByteArray::InsertAt"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMByteArray::InsertAt
DESCRIPTION: allows another array to be inserted in to this array
at a specified index
INPUT: lIndex - the index to start the insertion at
pchToInsert - the characters to insert
lCount - the number of characters to append
OUTPUT:
RETURNS: void
*/
void VMByteArray::InsertAt( long lIndex, const char* pchToInsert, long lCount )
{
if ( lCount + lIndex <= m_lBufferSize )
{
memcpy( m_pData + lIndex, pchToInsert, lCount );
if ( lIndex + lCount > m_lAllocated )
m_lAllocated += ( lIndex + lCount ) - m_lAllocated;
}
else
{
BYTE* pOld = m_pData;
m_pData = new BYTE[ m_lBufferSize + lCount ];
m_lBufferSize += lCount;
memcpy( m_pData, pOld, m_lAllocated );
memcpy( m_pData + lIndex, pchToInsert, lCount );
if ( lIndex + lCount > m_lAllocated )
m_lAllocated += ( lIndex + lCount ) - m_lAllocated;
delete [] pOld;
}
}
/* End of function "VMByteArray::InsertAt"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMByteArray::RemoveAt
DESCRIPTION: allows a number of cells to be removed from the array
starting at a given index
INPUT: lIndex - the index to start removing at
lCount - the number of cells to remove
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -