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

📄 tdrecord.cpp

📁 This is the main project file for VC++ projects generated using an Application Wizard. It cont
💻 CPP
字号:
#include "TDRecord.h"

CTDRecord::~CTDRecord(void)
{
	if( _lpBuffer != 0 )
		delete[] _lpBuffer;
}

int CTDRecord::GetField( const char* szFieldName, void* vpBuffer, unsigned int iBufferSize )
{
	if( !IsValid() )
		return TDRERR_INVALID_RECORD;

	unsigned int iIndex;
	if( GetIndexFromName( szFieldName, &iIndex ) )
		return GetField( iIndex, vpBuffer, iBufferSize );
	else
		return TDRERR_COLUMN_NOT_FOUND;
}

int CTDRecord::GetField( unsigned int iIndex, void* vpBuffer, unsigned int iBufferSize )
{
	if( !IsValid() )
		return TDRERR_INVALID_RECORD;

	//if the index is invalid, return
	if( iIndex >= _lptableDef->iColumnCount )
		return TDRERR_COLUMN_NOT_FOUND;

	//get the offset of the field
	unsigned int iByteOffset = GetFieldByteOffset( iIndex );

	//store each byte untill buffer is full or number of bytes in the field is reached
	char* lpBuffer = (char*) vpBuffer;
	for( unsigned int i = 0; i < iBufferSize && i < _lptableDef->Columns[iIndex].iBytes; i++ ){
		lpBuffer[i] = _lpBuffer[iByteOffset + i];
	}

	return TDR_OK;
}

int CTDRecord::SetField( const char* szFieldName, void* vpBuffer, unsigned int iBufferSize )
{
	if( !IsValid() )
		return TDRERR_INVALID_RECORD;

	unsigned int iIndex;
	if( GetIndexFromName( szFieldName, &iIndex ) )
		return SetField( iIndex, vpBuffer, iBufferSize );
	else
		return TDRERR_COLUMN_NOT_FOUND;
}

int CTDRecord::SetField( unsigned int iIndex, void* vpBuffer, unsigned int iBufferSize )
{
	if( !IsValid() )
		return TDRERR_INVALID_RECORD;

	//if the index is invalid, return
	if( iIndex >= _lptableDef->iColumnCount )
		return TDRERR_COLUMN_NOT_FOUND;

	//get the offset of the field
	unsigned int iByteOffset = GetFieldByteOffset( iIndex );

	//store each byte untill buffer is emptied or number of bytes in the field is reached
	char* lpBuffer = (char*) vpBuffer;
	for( unsigned int i = 0; i < iBufferSize && i < _lptableDef->Columns[iIndex].iBytes; i++ ){
		_lpBuffer[iByteOffset + i] = lpBuffer[i];
	}
	
	return TDR_OK;
}

unsigned int CTDRecord::GetFieldByteOffset( unsigned int iIndex )
{
	if( !IsValid() )
		return TDRERR_INVALID_RECORD;

	unsigned int iByteOffset = sizeof( TDR_RECORD_HEADER );
	for( unsigned int i = 0; i < iIndex; i++ ){
		iByteOffset += _lptableDef->Columns[i].iBytes;
	}
	return iByteOffset;
}

int CTDRecord::GetIndexFromName( const char* szFieldName, unsigned int* lpiIndex )
{
	if( !IsValid() )
		return TDRERR_INVALID_RECORD;

	//for every column
	for( unsigned int i = 0; i < _lptableDef->iColumnCount; i++ ){
		//check if it is the right column
		if( strcmp( szFieldName, _lptableDef->Columns[i].szName ) == 0 ){
			//set the index argument and return true
			*lpiIndex = i;
			return 1;
		}
	}

	//no column was found to match, return false
	return 0;
}

int CTDRecord::GetFieldSize( const char* szFieldName, unsigned int* lpiSizeInBytes )
{
	if( !IsValid() )
		return TDRERR_INVALID_RECORD;

	unsigned int iIndex;
	if( GetIndexFromName( szFieldName, &iIndex ) )
		return GetFieldSize( iIndex, lpiSizeInBytes );
	else
		return TDRERR_COLUMN_NOT_FOUND;

}

int CTDRecord::GetFieldSize( unsigned int iIndex, unsigned int* lpiSizeInBytes )
{
	if( !IsValid() )
		return TDRERR_INVALID_RECORD;

	if( iIndex >= _lptableDef->iColumnCount )
		return TDRERR_COLUMN_NOT_FOUND;

	*lpiSizeInBytes = _lptableDef->Columns[iIndex].iBytes;

	return TDR_OK;
}

CTDRecord::CTDRecord( TDDBTable* lptableDef, unsigned int iBytesNeeded )
{
	if( iBytesNeeded == 0 ){
		_lptableDef = 0;
		_iBytes = 0;
		_lpBuffer = 0;
		return;
	}

	_lptableDef = lptableDef;
	_iBytes = iBytesNeeded;
	_lpBuffer = new char[_iBytes];
	//zero the buffer
	for( unsigned int i = 0; i < _iBytes; i++ ){
		_lpBuffer[i] = 0;
	}
}

int CTDRecord::IsValid( void )
{
	if( _lptableDef == 0 || _lpBuffer == 0 )
		return 0;

	return 1;
}

int CTDRecord::GetHeader( TDR_RECORD_HEADER* rhOut )
{
	if( !IsValid() )
		return TDRERR_INVALID_RECORD;

	//char* -> void* -> record header* -> record header
	*rhOut = *((TDR_RECORD_HEADER*)((void*)_lpBuffer));

	return TDR_OK;
}

int CTDRecord::SetHeader( TDR_RECORD_HEADER* rhIn )
{
	if( !IsValid() )
		return TDRERR_INVALID_RECORD;

	//char* -> void* -> record header* -> record header
	*((TDR_RECORD_HEADER*)((void*)_lpBuffer)) = *rhIn;

	return TDR_OK;
}

void CTDRecord::operator = ( CTDRecord recOther )
{
	if( !recOther.IsValid() )
		return;

	if( _lpBuffer )
		delete[] _lpBuffer;

	_iBytes = recOther._iBytes;
	_lptableDef = recOther._lptableDef;
	_lpBuffer = new char[_iBytes];
	for(unsigned int i = 0; i < _iBytes; i++ ){
		_lpBuffer[i] = recOther._lpBuffer[i];
	}
}

⌨️ 快捷键说明

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