⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vmbytearray.cpp

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*****************************************************************************/
/*                              SOURCE FILE                                  */
/*****************************************************************************/
/*
       $Archive:   $

      $Revision:   $
          $Date:   $
        $Author:   $

    Description:   Implementation of an expandable byte array and a derivation 
                   that converts back and forth between byte arrays and char
                   arrays.

                      TOOL And XML FORMS License
                      ==========================

                      Except where otherwise noted, all of the documentation 
                      and software included in the TOOL package is 
                      copyrighted by Michael Swartzendruber.

                      Copyright (C) 2005 Michael John Swartzendruber. 
                      All rights reserved.

                      Access to this code, whether intentional or accidental,
                      does NOT IMPLY any transfer of rights.

                      This software is provided "as-is," without any express 
                      or implied warranty. In no event shall the author be held
                      liable for any damages arising from the use of this software.

                      Permission is granted to anyone to use this software for 
                      any purpose, including commercial applications, and to 
                      alter and redistribute it, provided that the following 
                      conditions are met:

                      1. All redistributions of source code files must retain 
                         all copyright notices that are currently in place, 
                         and this list of conditions without modification.

                      2. The origin of this software must not be misrepresented;
                         you must not claim that you wrote the original software.

                      3. If you use this software in another product, an acknowledgment
                         in the product documentation would be appreciated but is
                         not required.

                      4. Modified versions in source or binary form must be plainly 
                         marked as such, and must not be misrepresented as being 
                         the original software.
*/
static char OBJECT_ID[] = "$Revision: 3 $ : $Date: 3/08/00 12:11p $";
/*****************************************************************************/

#include "../../../stdafx.h"
#include "assert.h"
#include "VMByteArray.h"


/*****************************************************************************/
/*
     FUNCTION NAME:  VMByteArray::VMByteArray

       DESCRIPTION:  default ctor. inits this to be an empty array

             INPUT:  void 
            OUTPUT:  none

           RETURNS:  none
*/
VMByteArray::VMByteArray( void )
{
  m_pData = NULL;
  m_lBufferSize = m_lAllocated = m_lGrowBy = 0;
}
/* End of function "VMByteArray::VMByteArray"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMByteArray::VMByteArray

       DESCRIPTION:  ctor. inits this from a pointer to a string

             INPUT:  pchString - the char array to init this from
            OUTPUT:  none

           RETURNS:  none
*/
VMByteArray::VMByteArray( const char* pchString )
{
  m_pData = NULL;
 
  assert( NULL != pchString );
  assert( strlen( pchString ) );

  m_lBufferSize = m_lAllocated = m_lGrowBy = strlen( pchString );

  m_pData = new BYTE[ m_lBufferSize ];
  memcpy( m_pData, pchString, m_lBufferSize );  
}
/* End of function "VMByteArray::VMByteArray"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMByteArray::VMByteArray

       DESCRIPTION:  ctor. inits this from a pointer to a string

             INPUT:  pchString - the char array to init this from
            OUTPUT:  none

           RETURNS:  none
*/
VMByteArray::VMByteArray( const char* pchString, int iSize )
{
  m_pData = NULL;
 
  assert( NULL != pchString );

  m_lBufferSize = m_lAllocated = m_lGrowBy = iSize;

  m_pData = new BYTE[ m_lBufferSize ];
  memcpy( m_pData, pchString, m_lBufferSize );  
}
/* End of function "VMByteArray::VMByteArray"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMByteArray::VMByteArray

       DESCRIPTION:  copy ctor. inits this from another instance of the same 
                     object type

             INPUT:  roOther - the other object to do a deep copy of
            OUTPUT:  none

           RETURNS:  none
*/
VMByteArray::VMByteArray( const VMByteArray& roOther )
{
  m_pData = NULL;
  Copy( roOther );
}
/* End of function "VMByteArray::VMByteArray"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  operator = 

       DESCRIPTION:  assignment operator. sets this by performing a deep copy
                     of the other instance

             INPUT:  roOther - the other instance of this to copy from
            OUTPUT:  none

           RETURNS:  contents of this
*/
VMByteArray& VMByteArray::operator= ( const VMByteArray& roOther )
{
  Copy( roOther );
  return( *this );
}
/* End of function ""
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMByteArray::~VMByteArray

       DESCRIPTION:  dtor. clean up allocations

             INPUT:  void
            OUTPUT:  none

           RETURNS:  none
*/
VMByteArray::~VMByteArray( void )
{
  if ( NULL != m_pData )
    delete [] m_pData;
}
/* End of function "VMByteArray::~VMByteArray"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMByteArray::Copy

       DESCRIPTION:  method that performs a deep copy from another instance

             INPUT:  roOther - the other instance to copy from
            OUTPUT:  none

           RETURNS:  void
*/
void VMByteArray::Copy( const VMByteArray& roOther )
{
  delete [] m_pData;

  m_lBufferSize = roOther.GetSize();
  m_lAllocated  = roOther.GetUpperBound();
  m_lGrowBy     = roOther.GetGrowBySize();

  m_pData = new BYTE[ m_lBufferSize ];
  memcpy( m_pData, roOther.GetData(), m_lAllocated );
}
/* End of function "VMByteArray::Copy"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMByteArray::SetSize

       DESCRIPTION:  used to set the size of the internal array

             INPUT:  lNewSize - the new size of the array 
                     lGrowBy = sets the growth increment 
                               default is -1 
            OUTPUT:  

           RETURNS:  void  - 
*/
void VMByteArray::SetSize( long lNewSize, long lGrowBy )
{
  BYTE* pOld           = m_pData;
  long  lOldBufferSize = m_lBufferSize;

  m_lBufferSize = lNewSize;
  m_lGrowBy     = lGrowBy;

  if ( m_lAllocated > m_lBufferSize )
    m_lAllocated = m_lBufferSize;

  m_pData = new BYTE[ m_lBufferSize ];
  memcpy( m_pData, pOld, min( m_lBufferSize, lOldBufferSize ) );

  delete [] pOld;
}
/* End of function "VMByteArray::SetSize"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMByteArray::GetSize

       DESCRIPTION:  returns the size of the array to the caller

             INPUT:  void  
            OUTPUT:  none

           RETURNS:  the current size of the array
*/
long VMByteArray::GetSize( void ) const
{
  return( m_lBufferSize );
}
/* End of function "VMByteArray::GetSize"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMByteArray::GetUpperBound

       DESCRIPTION:  returns the number of cells in the array that have actually
                     been assigned

             INPUT:  void
            OUTPUT:  none

           RETURNS:  the number of cells in the array that have been assigned
*/
long VMByteArray::GetUpperBound( void ) const
{
  return( m_lAllocated );
}
/* End of function "VMByteArray::GetUpperBound"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMByteArray::GetGrowBySize

       DESCRIPTION:  allows the caller to discover the grow by size on this

             INPUT:  void
            OUTPUT:  none

           RETURNS:  the value of the grow by variable
*/
long VMByteArray::GetGrowBySize( void ) const
{
  return( m_lGrowBy );
}
/* End of function "VMByteArray::GetGrowBySize"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMByteArray::FreeExtra

       DESCRIPTION:  eliminate buffer space that is not used

             INPUT:  void  
            OUTPUT:  none

           RETURNS:  void 
*/
void VMByteArray::FreeExtra( void )
{
  if ( m_lAllocated < m_lBufferSize )
  {
    BYTE* pOld  = m_pData;

    m_pData = new BYTE[ m_lAllocated ];
    memcpy( m_pData, pOld, m_lAllocated );

    m_lBufferSize = m_lAllocated;

    delete [] pOld;
  }
}
/* End of function "VMByteArray::FreeExtra"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMByteArray::FreeAll

       DESCRIPTION:  reset the entire class buffer and counters

             INPUT:  void 
            OUTPUT:  none

           RETURNS:  void  
*/
void VMByteArray::FreeAll( void )
{
  delete [] m_pData;
  m_pData = NULL;

  m_lBufferSize = 0;
  m_lAllocated  = 0;
  m_lGrowBy     = 0;
}
/* End of function "VMByteArray::FreeAll"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMByteArray::GetAt

       DESCRIPTION:  fetch a cell by index

             INPUT:  lIndex - the index to fetch
            OUTPUT:  none

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -