📄 vmstring.cpp
字号:
/*****************************************************************************/
/* SOURCE FILE */
/*****************************************************************************/
/*
$Archive: $
$Revision: $
$Date: $
$Author: $
Description: Implementation of a string class that does not require MFC
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: $ : $Date: $";
/*****************************************************************************/
#include "../../../stdafx.h"
#include <stdio.h>
#include <new> // for std::bad_alloc
#include <crtdbg.h> // for _ASSERT
#include "VMString.h"
std::bad_alloc VMStringBadAlloc;
// VMString allocation extra size; if no extra allocation is wanted
// then set VMStringA_EXTRA_SIZE to 0
//
int VMString::BUFFER_EXTRA_SIZE = 16;
// do not modify the following constants
//
int VMString::FORMAT_EXTRA_SIZE = 32;
int VMString::INT_SIZE = 16;
int VMString::LONG_SIZE = 16;
int VMString::ULONG_SIZE = 20;
/*****************************************************************************/
/*
FUNCTION NAME: VMString::AllocBuffer
DESCRIPTION: allocates a buffer of the given size
INPUT: dwSize - size to reserve
OUTPUT: none
RETURNS: void
*/
void VMString::AllocBuffer( int dwSize )
{
_ASSERT( dwSize > 0 );
m_iSize = dwSize;
m_pchBuffer = (TCHAR*) malloc( VMString::TLEN( m_iSize ) );
memset( m_pchBuffer, 0, m_iSize );
_ASSERT( m_pchBuffer );
if ( !m_pchBuffer )
{
throw VMStringBadAlloc;
}
}
/* End of function "VMString::AllocBuffer"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMString::FreeBuffer
DESCRIPTION: free the internal buffer
INPUT: void
OUTPUT: none
RETURNS: void
*/
inline void VMString::FreeBuffer( void )
{
_ASSERT( m_pchBuffer );
if ( m_pchBuffer )
{
free( m_pchBuffer );
m_pchBuffer = NULL;
m_iSize = 0;
m_iLength = 0;
}
}
/* End of function "VMString::FreeBuffer"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMString::Initialize
DESCRIPTION: initialize this to the defined size
INPUT: dwAllocSize - size to pre-allocate
OUTPUT: none
RETURNS: void
*/
void VMString::Initialize( int dwAllocSize )
{
_ASSERT( dwAllocSize > 0 );
m_pchBuffer = NULL;
m_iSize = 0;
m_iLength = 0;
AllocBuffer( GetPaddedSize( dwAllocSize ) );
m_pchBuffer[0] = '\0';
}
/* End of function "VMString::Initialize"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMString::GetPaddedSize
DESCRIPTION: Adds the internal buffer pad value to the requested size
INPUT: dwSize - requested size
OUTPUT: none
RETURNS: padded size
*/
inline int VMString::GetPaddedSize( int dwSize )
{
_ASSERT( BUFFER_EXTRA_SIZE >= 0 );
return( dwSize + BUFFER_EXTRA_SIZE );
}
/* End of function "VMString::GetPaddedSize"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMString::UpdateBufferSize
DESCRIPTION: re-allocates the internal buffer to the requested size
INPUT: dwSize - new size to pre-reserve
OUTPUT: none
RETURNS: void
*/
void VMString::UpdateBufferSize( const int dwSize )
{
_ASSERT( dwSize >= 0 );
if ( m_iSize < dwSize )
{
FreeBuffer();
AllocBuffer( GetPaddedSize( dwSize ) );
}
}
/* End of function "VMString::UpdateBufferSize"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMString::CopyToBuffer
DESCRIPTION: copies the string passed to this to the internal buffer
INPUT: chSource - pointer to the string to copy
dwSize - size of the string to copy
OUTPUT:
RETURNS: void
*/
void VMString::CopyToBuffer( const char* pchSource, int dwSize )
{
_ASSERT( pchSource && dwSize >= 0 );
memcpy( m_pchBuffer, pchSource, VMString::TLEN( dwSize ) );
m_pchBuffer[ dwSize ] = '\0';
m_iLength = dwSize;
}
/* End of function "VMString::CopyToBuffer"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMString::TLEN
DESCRIPTION: returns the number of bytes scaled up the size of each
buffer cell size
INPUT: iLength - length to multiply
OUTPUT: none
RETURNS: byte count of length x size of each cell
*/
inline int VMString::TLEN( int iLength )
{
return( iLength * sizeof( TCHAR ) );
}
/* End of function "VMString::TLEN"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMString::VMString
DESCRIPTION: ctor. inits this
INPUT: void
OUTPUT: none
RETURNS: none
*/
VMString::VMString( void )
{
Initialize();
}
/* End of function "VMString::VMString"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMString::~VMString
DESCRIPTION: dtor. clean up allocations
INPUT: void
OUTPUT: none
RETURNS: none
*/
VMString::~VMString( void )
{
FreeBuffer();
}
/* End of function "VMString::~VMString"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMString::VMString
DESCRIPTION: copy constructor
INPUT: roOther - other instance
OUTPUT: none
RETURNS: none
*/
VMString::VMString( const VMString& roOther )
{
Initialize( roOther.Length() + 1 );
*this = roOther;
}
/* End of function "VMString::VMString"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMString::VMString
DESCRIPTION: construct with string
INPUT: pchValue - pointer to string to assign to this
OUTPUT: none
RETURNS: none
*/
VMString::VMString( const char* pchValue )
{
_ASSERT( pchValue );
Initialize( strlen( pchValue ) + 1 );
*this = pchValue;
}
/* End of function "VMString::VMString"
/*****************************************************************************/
VMString::VMString( const char* pchValue, int iLength )
{
_ASSERT( pchValue );
Initialize( max( static_cast<int>(strlen( pchValue ) + 1), iLength + 1 ) ); // 2b|!2b==?
*this = pchValue;
}
/*****************************************************************************/
/*
FUNCTION NAME: VMString::VMString
DESCRIPTION: construct and pre-allocate a number of bytes in the buffer
INPUT: dwAllocSize - initial size to reserve for this
OUTPUT: none
RETURNS: none
*/
VMString::VMString( int dwAllocSize )
{
Initialize( dwAllocSize > 0 ? dwAllocSize : 1 );
}
/* End of function "VMString::VMString"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMString::operator =
DESCRIPTION: assignment construction
INPUT: roOther - the other instance of this class to construct
from
OUTPUT: none
RETURNS: self
*/
VMString& VMString::operator = ( const VMString& roOther )
{
if ( this == &roOther )
{
return( *this );
}
UpdateBufferSize( roOther.Length() + 1 );
CopyToBuffer( roOther.Buffer(), roOther.Length() );
return( *this );
}
/* End of function "VMString::operator = "
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMString::operator =
DESCRIPTION: assignment construction
INPUT: pchValue - pointer to initial value
OUTPUT: none
RETURNS: self
*/
VMString& VMString::operator = ( const char* pchValue )
{
// _ASSERT( pchValue );
if ( NULL != pchValue )
{
int dwLen = strlen( pchValue );
UpdateBufferSize( dwLen + 1 );
CopyToBuffer( pchValue, dwLen );
}
else
{
char chNull[ 2 ];
chNull[ 0 ] = 0;
chNull[ 1 ] = 0;
UpdateBufferSize( 1 );
CopyToBuffer( chNull, 1 );
}
return( *this );
}
/* End of function "VMString::operator = "
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMString::operator =
DESCRIPTION: assignment construction
INPUT: iValue - initial value
OUTPUT: none
RETURNS: self
*/
VMString& VMString::operator = ( int iValue )
{
return( SetInt( iValue ) );
}
/* End of function "VMString::operator = "
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMString::operator =
DESCRIPTION: assignment construction
INPUT: lValue - initial value
OUTPUT: none
RETURNS: self
*/
VMString& VMString::operator = ( long lValue )
{
return( SetLong( lValue ) );
}
/* End of function "VMString::operator = "
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMString::operator =
DESCRIPTION: assignment construction
INPUT: ulValue - initial value
OUTPUT: none
RETURNS: self
*/
VMString& VMString::operator = ( unsigned long ulValue )
{
return( SetULong( ulValue ) );
}
/* End of function "VMString::operator = "
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMString::operator =
DESCRIPTION: assignment construction
INPUT: dblValue - initial value
OUTPUT: none
RETURNS: self
*/
VMString& VMString::operator = ( double dblValue )
{
return( SetDouble( dblValue ) );
}
/* End of function "VMString::operator = "
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMString::operator const char*
DESCRIPTION: internal buffer access
INPUT: void
OUTPUT: none
RETURNS: const pointer to internal buffer
*/
VMString::operator const char* ( void ) const
{
return( m_pchBuffer );
}
/* End of function "VMString::operator const char*"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMString::Buffer
DESCRIPTION: buffer access
INPUT: void -
OUTPUT: none
RETURNS: const pointer to internal buffer
*/
TCHAR* VMString::Buffer( void ) const
{
return( m_pchBuffer );
}
/* End of function "VMString::Buffer"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMString::Buffer
DESCRIPTION: buffer access
INPUT: void -
OUTPUT: none
RETURNS: pointer to internal buffer
*/
TCHAR* VMString::Buffer( void )
{
return( m_pchBuffer );
}
/* End of function "VMString::Buffer"
/*****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -