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

📄 resultset.cpp

📁 基于dialogic语音卡的IVR系统源代码
💻 CPP
字号:
// ResultSet.cpp: implementation of the CResultSet class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ResultSet.h"

#include "setup.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CResultSet::CResultSet()
{

}

CResultSet::~CResultSet()
{

}

int CResultSet::Create(int iRowCount, int iColCount)
{
	//add by lj
	if(iRowCount==0 || iColCount==0)
		return -1;
	//end add
	m_presultSet = (struct ResultSet *)malloc(sizeof(struct ResultSet));

	m_presultSet->iRowCount = iRowCount;
	m_presultSet->iColCount = iColCount;

	m_presultSet->ppfield = (struct Field **)malloc(iRowCount * sizeof(struct Field *));
	for(int i = 0; i < iRowCount; i ++)
	{
		struct Field *pfield = (struct Field *)malloc(iColCount * sizeof(struct Field));
		for(int j = 0; j < iColCount; j++)
		{
			//设置每个字段的初始状态为0
			(pfield + j)->iStatus = 0;
			//字段的名为空
			((pfield + j)->aszFieldName)[0] = 0;
		}

		*(m_presultSet->ppfield + i) = pfield;
	}

	m_iRowCount = iRowCount;
	m_iColCount = iColCount;

	return(0);
}

int CResultSet::AddField(int row, int col, void *p, int iSize, int iType)
{
	(*(m_presultSet->ppfield + row) + col)->iSize = iSize;
	(*(m_presultSet->ppfield + row) + col)->iType = iType;
	(*(m_presultSet->ppfield + row) + col)->pdata = p;

	//设置这个字段的状态为1
	(*(m_presultSet->ppfield + row) + col)->iStatus = 1;

	return(0);
}

int CResultSet::AddField2(int row, int col, void *p, int iSize, int iType)
{
/*******************************************
将传入的指针复制一份,加到FIELD中
*******************************************/
	(*(m_presultSet->ppfield + row) + col)->iSize = iSize;
	(*(m_presultSet->ppfield + row) + col)->iType = iType;
	if(iType == DATATYPE_STRING)
	{
		(*(m_presultSet->ppfield + row) + col)->pdata = malloc(strlen((char *)p) + 1);
		strcpy((char *)(*(m_presultSet->ppfield + row) + col)->pdata, (char *)p);
	}
	else
	{
		(*(m_presultSet->ppfield + row) + col)->pdata = malloc(iSize);
		memcpy((*(m_presultSet->ppfield + row) + col)->pdata, p, iSize);
	}

	//设置这个字段的状态为1
	(*(m_presultSet->ppfield + row) + col)->iStatus = 1;

	return(0);
}

int CResultSet::AddField_Integer(int row, int col, int iField)
{
	int *p = (int *)malloc(sizeof(int));
	*p = iField;
	AddField(row, col, p, sizeof(int), DATATYPE_INTEGER);

	return(0);
}

int CResultSet::AddField_String(int row, int col, char *szField)
{
	char *p = (char *)malloc(strlen(szField) + 1);
	strcpy(p, szField);
	AddField(row, col, p, strlen(p), DATATYPE_STRING);

	return(0);
}

int CResultSet::AddField_Float(int row, int col, float fField)
{
	float *p = (float *)malloc(sizeof(float));
	*p = fField;
	AddField(row, col, p, sizeof(float), DATATYPE_FLOAT);

	return(0);
}

int CResultSet::Free(struct ResultSet *presultSet)
{
	if(presultSet == NULL)
		return 0;

	for(int i = 0; i < presultSet->iRowCount; i ++)
	{
		struct Field *pfield = *(presultSet->ppfield + i);
		for(int j = 0; j < presultSet->iColCount; j ++)
		{
			if((pfield + j)->iStatus != 1)
			{
				free((pfield + j)->pdata);	//释放FIELD
			}
		}
		free(pfield);	//释放行
	}

	free(presultSet->ppfield);	//释放ppField

	free(presultSet);	//释放结构指针

	return(0);
}

int CResultSet::Free()
{
/*	for(int i = 0; i < m_presultSet->iRowCount; i ++)
	{
		struct Field *pfield = *(m_presultSet->ppfield + i);
		for(int j = 0; j < m_presultSet->iColCount; j ++)
		{
			free((pfield + j)->pdata);	//释放FIELD
		}
		free(pfield);	//释放行
	}

	free(m_presultSet->ppfield);	//释放ppField

	free(m_presultSet);	//释放结构指针*/

	return Free(m_presultSet);
}

struct ResultSet * CResultSet::Get()
{
int i, j;

	//检查她的所有成员都已被分配
	for(i = 0; i < m_presultSet->iRowCount; i ++)
	{
		struct Field *pfield = *(m_presultSet->ppfield + i);
		for(j = 0; j < m_presultSet->iColCount; j ++)
		{
			if((pfield + j)->iStatus != 1)
				break;
		}
	}

	//特殊处理:行数为0,表示结果集为空(比如:在成交查询时可表示"无成交")
	if(m_presultSet->iRowCount == 0)
	{
		return(m_presultSet);
	}
	if(i == m_presultSet->iRowCount && j == m_presultSet->iColCount)
	{
		return(m_presultSet);
	}
	else
	{
		return(NULL);
	}
}

int CResultSet::Create(ResultSet *presultSet)
{
/*******************************************************
根据传入的指针复制一个RESULTSET
*******************************************************/
	if(Create(presultSet->iRowCount, presultSet->iColCount) != 0)
		return -1;

	for(int i = 0; i < presultSet->iRowCount; i ++)
	{
		for(int j = 0; j < presultSet->iColCount; j ++)
		{
			if(AddField2(i, j, (presultSet->ppfield)[i][j].pdata, (presultSet->ppfield)[i][j].iSize, (presultSet->ppfield)[i][j].iType)
				!= 0)
			{
				Free();
				return -1;
			}
		}
	}

	return 0;
}

int CResultSet::BirthFromBuffer(char *pBuffer, int iLength, ResultSet **ppresultSet)
{
//暂不支持BINARY
int iOffset = 0;

	CResultSet resultSet;
	struct Header_Table *pheader_Table = (struct Header_Table *)pBuffer;
	resultSet.Create(pheader_Table->uiRow, pheader_Table->uiCol);
	iOffset += sizeof(struct Header_Table);
	char *pData;

	for(int i = 0; i < pheader_Table->uiRow; i ++)
	{
		for(int j = 0; j < pheader_Table->uiCol; j ++)
		{
			struct Header_TableUnit *pheader_TableUnit = (struct Header_TableUnit *)(pBuffer + iOffset);
			iOffset += sizeof(struct Header_TableUnit);
			pData = pBuffer + iOffset + pheader_TableUnit->uiLength_Name;
			switch(pheader_TableUnit->byteType)
			{
				case DATATYPE_INTEGER:
					resultSet.AddField_Integer(i, j, *(int *)pData);
					break;

				case DATATYPE_FLOAT:
					resultSet.AddField_Float(i, j, *(float *)pData);
					break;

				case DATATYPE_STRING:
					char *szTemp;
					szTemp = (char *)malloc(pheader_TableUnit->uiLength_Data + 1);
					memcpy(szTemp, pData, pheader_TableUnit->uiLength_Data);
					szTemp[pheader_TableUnit->uiLength_Data] = 0;
					resultSet.AddField_String(i, j, (char *)szTemp);
					free(szTemp);
					break;

				default:
//					AfxMessageBox("BirthFromBuffer_ResultSet() not support the data type");
					break;
			}//end of switch()

			iOffset += pheader_TableUnit->uiLength_Name + pheader_TableUnit->uiLength_Data;
		}
	}

	//填写返回参数
	*ppresultSet = resultSet.Get();

	return 0;
}

int CResultSet::DumpToBuffer(ResultSet *presultSet, char **ppBuffer, int *piLength)
{
//暂不支持BINARY
int iOffset = 0;

	int iTotalLen = CalculateSize(presultSet);
	if(iTotalLen <= 0)
		return -1;
	char *pBuffer = (char *)malloc(iTotalLen);
	((struct Header_Table *)(pBuffer + iOffset))->uiCol = presultSet->iColCount;
	((struct Header_Table *)(pBuffer + iOffset))->uiRow = presultSet->iRowCount;
	((struct Header_Table *)(pBuffer + iOffset))->uiLength = iTotalLen - sizeof(struct Header_Table);
	iOffset += sizeof(struct Header_Table);

	for(int i = 0; i < presultSet->iRowCount; i ++)
	{
		for(int j = 0; j < presultSet->iColCount; j ++)
		{
			//数据类型
			((struct Header_TableUnit *)(pBuffer + iOffset))->byteType = (BYTE)((presultSet->ppfield)[i][j].iType);
			//变量名长度
			((struct Header_TableUnit *)(pBuffer + iOffset))->uiLength_Name = strlen((presultSet->ppfield)[i][j].aszFieldName);
			//变量数据长度
			((struct Header_TableUnit *)(pBuffer + iOffset))->uiLength_Data = (presultSet->ppfield)[i][j].iSize;
			iOffset += sizeof(struct Header_TableUnit);

			//变量名内容
			memcpy(pBuffer + iOffset, (presultSet->ppfield)[i][j].aszFieldName, strlen((presultSet->ppfield)[i][j].aszFieldName));
			iOffset += strlen((presultSet->ppfield)[i][j].aszFieldName);

			//变量数据内容
			memcpy(pBuffer + iOffset, (presultSet->ppfield)[i][j].pdata, (presultSet->ppfield)[i][j].iSize);
			iOffset += (presultSet->ppfield)[i][j].iSize;
		}
	}

	//填写返回值
	*ppBuffer = pBuffer;
	*piLength = iTotalLen;

	return 0;
}

int CResultSet::CalculateSize(ResultSet *presultSet)
{
int iTotalLen = 0;

	iTotalLen += sizeof(struct Header_Table);

	for(int i = 0; i < presultSet->iRowCount; i ++)
	{
		for(int j = 0; j < presultSet->iColCount; j ++)
		{
			//参见数据小包结构(struct Header_TableUnit)说明
			iTotalLen += sizeof(Header_TableUnit);
			iTotalLen += strlen((presultSet->ppfield)[i][j].aszFieldName);
			iTotalLen += (presultSet->ppfield)[i][j].iSize;
		}
	}

	return iTotalLen;
}

⌨️ 快捷键说明

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