对不同数据类型拼接成一个内存块的封装

原创 2015年04月06日 14:23:05
/*================================================================*
// NSDataParser.h: interface for the CNSDataParser class.
【头文件】:	NSDataParser.h
【功  能】:	该模块属于独立模块,主要完成管理各种类型数据,
			转换成一个连续的内存数据块。返回的连续数据块为LPVOID类型,
			通过GetData()函数获取到该数据块的地址,通过GetDataSize()
			返回数据块的大小
【注  意】:	
*================================================================*/
#if !defined(AFX_DATAPARSER_H__C18BF820_ED69_42B0_A8F9_9570387F753C__INCLUDED_)
#define AFX_DATAPARSER_H__C18BF820_ED69_42B0_A8F9_9570387F753C__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <afxtempl.h>
//=========================================================================
//=========================================================================
class  CDataParser
{
public:
	CDataParser();
	virtual ~CDataParser();
public:
	// 将数据写入内存
	/*================================================================*
	【函數】:	Add(...)
	【功能】:	往内存中添加各种数据,数据内存所占的长度会根据所加入的
				数据类型动态增加
	【参数】:	
	【返回】:	
	【例子】:	
	【注意】:	当参数为CNSVarObj类型时,往内存中加的是变量的工程值,
				即pVar->GetVarData(),并不是把变量对象中所有数据全加入内存
	*================================================================*/
	virtual BOOL Add(COleVariant* pvData);
	virtual BOOL Add(WORD wData);
	virtual BOOL Add(DWORD wData);
	virtual BOOL Add(LPVOID pData,int iLen);
	virtual BOOL Add(BOOL wData);
	virtual BOOL Add(BYTE wData);
	virtual BOOL Add(char wData);
	virtual BOOL Add(short wData);
	virtual BOOL Add(long wData);
	virtual BOOL Add(float wData);
	virtual BOOL Add(double wData);
	virtual BOOL Add(const COleDateTime& wData);
	virtual BOOL Add(const CString& strData);
public:
	// 从内存中读取值,注:先调用InitData初始化内存数据然后再调用GetData
	virtual void InitData( void *pData,unsigned long nDataSize);
	virtual BOOL GetData(COleVariant& pvData);
	virtual BOOL GetData(WORD &wData);
	virtual BOOL GetData(DWORD &wData);
	virtual BOOL GetData(LPVOID pData);
	virtual BOOL GetData(BOOL &wData);
	virtual BOOL GetData(BYTE &wData);
	virtual BOOL GetData(char &wData);
	virtual BOOL GetData(short &wData);
	virtual BOOL GetData(long &wData);
	virtual BOOL GetData(float &wData);
	virtual BOOL GetData(double wData);
	virtual BOOL GetData(COleDateTime& wData);
	virtual BOOL GetData(CString& strData);
	// 获取当前内存位置
	unsigned long GetCurrentPos();
public:
	/*================================================================*
	【函數】:	RemoveAllDataItem()
	【功能】:	删除所有数据将所占的内存清空
	【参数】:	
	【返回】:	返回BOOL类型,成功为TRUE,否则为FALSE
	【例子】:	
	【注意】:	
	*================================================================*/
	virtual BOOL	RemoveAllDataItem();

	/*================================================================*
	【函數】:	GetData()
	【功能】:	获取连续内存地址
	【参数】:	
	【返回】:	返回指向内存的首地址
	【例子】:	
	【注意】:	
	*================================================================*/
	virtual LPVOID  GetData();

	/*================================================================*
	【函數】:	GetDataSize()
	【功能】:	获取连续内存的大小
	【参数】:	
	【返回】:	返回指向内存的大小
	【例子】:	
	【注意】:	
	*================================================================*/
	virtual DWORD	GetDataSize();
protected:
	double VarToDouble(COleVariant* pvData);
protected:
	// 数据大小
	UINT m_uDataSize;
	// 数据
	LPVOID m_pData;
	// 内存需要删除标志
	bool m_bMustClean;
	unsigned long m_pCurDataPos;
private:
	CRITICAL_SECTION CRITICAL_LOCK;
};
#endif // !defined(AFX_DATAPARSER_H__C18BF820_ED69_42B0_A8F9_9570387F753C__INCLUDED_)
#include "stdafx.h"
#include "WSDataParser.h"

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

CDataParser::CDataParser()
{
	m_bMustClean = true;
	m_pData = NULL;
	m_uDataSize = 0;
	m_pCurDataPos = 0;
	InitializeCriticalSection( &CRITICAL_LOCK);
}

CDataParser::~CDataParser()
{
	RemoveAllDataItem();
	DeleteCriticalSection(&CRITICAL_LOCK);
}
double CDataParser::VarToDouble(COleVariant* pvData)
{
	double dfData;
	COleVariant vData;
	
	vData = *pvData;
	
	switch(pvData->vt)
	{
	case VT_I1:
		dfData = (double)vData.cVal ;
		break;
	case VT_UI1:
		dfData = (double)vData.bVal;
		break;
	case VT_I2:
		dfData = (double)vData.iVal;
		break;
	case VT_UI2:
		dfData = (double)vData.uiVal;
		break;
	case VT_INT:
		dfData = (double)vData.intVal;
		break;
	case VT_UINT:
		dfData = (double)vData.uintVal;
		break;
	case VT_I4:
		dfData = (double)vData.lVal;
		break;
	case VT_UI4:
		dfData = (double)vData.ulVal;
		break;
	case VT_R4:
		dfData = (double)vData.fltVal;
		break;
	case VT_R8:
		dfData = (double)vData.dblVal;
		break;
	case VT_BOOL:
		if(vData.boolVal == VARIANT_FALSE)
			dfData = 0;
		else
			dfData = 1;
		break;
	case VT_CY:
	case VT_DATE:
	case VT_BSTR:
	case VT_DISPATCH:
	case VT_ERROR:
	case VT_VARIANT:
	case VT_UNKNOWN:
	case VT_DECIMAL:
	case VT_EMPTY:
	case VT_NULL:
	default:
		dfData = 0.0;
		break;
	}
	
	return dfData;
}
BOOL CDataParser::Add(LPVOID pData,int iLen)
{
	BOOL bSuccess = FALSE;
	EnterCriticalSection(&CRITICAL_LOCK);
	if(pData != NULL && iLen > 0)
	{
		if(!m_pData) 
		m_pData = GlobalAlloc(GMEM_FIXED,1);

		// 先构造类型
		DWORD dwDataVT = VT_VOID;
		m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
		CopyMemory((char*)m_pData + m_uDataSize,&dwDataVT,4);
		m_uDataSize += 4;

		// 构造长度
		m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
		CopyMemory((char*)m_pData + m_uDataSize,&iLen,4);
		m_uDataSize += 4;

		// 构造值
		m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + iLen, GMEM_MOVEABLE);
		CopyMemory((char*)m_pData + m_uDataSize,(char*)pData,iLen);
		
		m_uDataSize += iLen;
		bSuccess = TRUE;
	}
	LeaveCriticalSection(&CRITICAL_LOCK);
	return bSuccess;
}
BOOL CDataParser::Add(const CString& strData)
{
	BOOL bSuccess = FALSE;
	
	EnterCriticalSection(&CRITICAL_LOCK);

	char *chAnsiCode = CUtility::ConvertStringToChar(strData);
	if(chAnsiCode)
	{
		//=================================================================================
		
		if(!m_pData) 
			m_pData = GlobalAlloc(GMEM_FIXED,1);
		
		// 先构造变量类型
		DWORD dwDataVT = VT_BSTR;
		m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
		CopyMemory((char*)m_pData + m_uDataSize,&dwDataVT,4);
		m_uDataSize += 4;
		
		int istrVarNameLen = strlen(chAnsiCode);
		
		// 构造长度
		m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
		CopyMemory((char*)m_pData + m_uDataSize,&istrVarNameLen,4);
		m_uDataSize += 4;
		
		
		//构造值
		m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + istrVarNameLen, GMEM_MOVEABLE);
		CopyMemory((char*)m_pData + m_uDataSize,chAnsiCode,istrVarNameLen);
		
		m_uDataSize += istrVarNameLen;
		
		//=================================================================================
		
		delete chAnsiCode;
		chAnsiCode = NULL;

		bSuccess = TRUE;
	}
	LeaveCriticalSection(&CRITICAL_LOCK);
	return bSuccess;
}
BOOL CDataParser::Add(WORD wData)
{
	BOOL bSuccess = FALSE;

	EnterCriticalSection(&CRITICAL_LOCK);
	if(!m_pData) 
		m_pData = GlobalAlloc(GMEM_FIXED,1);
	//=================================================================================
	// 先构造类型
	DWORD dwDataVT = VT_UI2;
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&dwDataVT,4);
	m_uDataSize += 4;
	
	int iDataLen = sizeof(wData);
	
	//构造值
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + iDataLen, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&wData,iDataLen);
	m_uDataSize += iDataLen;
	
	bSuccess = TRUE;
	//=================================================================================
	LeaveCriticalSection(&CRITICAL_LOCK);
	return bSuccess;
}
BOOL CDataParser::Add(DWORD wData)
{
	BOOL bSuccess = FALSE;

	EnterCriticalSection(&CRITICAL_LOCK);

	if(!m_pData) 
		m_pData = GlobalAlloc(GMEM_FIXED,1);
	//=================================================================================
	// 先构造类型
	DWORD dwDataVT = VT_UI4;
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&dwDataVT,4);
	m_uDataSize += 4;
	
	int iDataLen = sizeof(wData);
	
	//构造值
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + iDataLen, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&wData,iDataLen);
	m_uDataSize += iDataLen;
	
	//=================================================================================
	bSuccess = TRUE;
	LeaveCriticalSection(&CRITICAL_LOCK);
	
	return bSuccess;
}
BOOL CDataParser::Add(BOOL wData)
{
	BOOL bSuccess = FALSE;
	
	EnterCriticalSection(&CRITICAL_LOCK);
	
	if(!m_pData) 
		m_pData = GlobalAlloc(GMEM_FIXED,1);
	//=================================================================================
	// 先构造类型
	DWORD dwDataVT = VT_BOOL;
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&dwDataVT,4);
	m_uDataSize += 4;
	
	int iDataLen = sizeof(wData);
	
	//构造值
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + iDataLen, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&wData,iDataLen);
	m_uDataSize += iDataLen;
	
	//=================================================================================
	bSuccess = TRUE;
	LeaveCriticalSection(&CRITICAL_LOCK);
	
	return bSuccess;
}
BOOL CDataParser::Add(BYTE wData)
{
	BOOL bSuccess = FALSE;
	
	EnterCriticalSection(&CRITICAL_LOCK);
	
	if(!m_pData) 
		m_pData = GlobalAlloc(GMEM_FIXED,1);
	//=================================================================================
	// 先构造类型
	DWORD dwDataVT = VT_UI1;
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&dwDataVT,4);
	m_uDataSize += 4;
	
	int iDataLen = sizeof(wData);
	
	//构造值
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + iDataLen, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&wData,iDataLen);
	m_uDataSize += iDataLen;
	
	//=================================================================================
	bSuccess = TRUE;
	LeaveCriticalSection(&CRITICAL_LOCK);

	return bSuccess;
}
BOOL CDataParser::Add(char wData)
{
	BOOL bSuccess = FALSE;
	
	EnterCriticalSection(&CRITICAL_LOCK);
	
	if(!m_pData) 
		m_pData = GlobalAlloc(GMEM_FIXED,1);
	//=================================================================================
	// 先构造类型
	DWORD dwDataVT = VT_I1;
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&dwDataVT,4);
	m_uDataSize += 4;
	
	int iDataLen = sizeof(wData);
	
	//构造值
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + iDataLen, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&wData,iDataLen);
	m_uDataSize += iDataLen;
	
	//=================================================================================
	bSuccess = TRUE;
	LeaveCriticalSection(&CRITICAL_LOCK);

	return bSuccess;
}
BOOL CDataParser::Add(short wData)
{
	BOOL bSuccess = FALSE;
	
	EnterCriticalSection(&CRITICAL_LOCK);
	
	if(!m_pData) 
		m_pData = GlobalAlloc(GMEM_FIXED,1);
	//=================================================================================
	// 先构造类型
	DWORD dwDataVT = VT_I2;
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&dwDataVT,4);
	m_uDataSize += 4;
	
	int iDataLen = sizeof(wData);
	
	//构造值
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + iDataLen, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&wData,iDataLen);
	m_uDataSize += iDataLen;
	
	//=================================================================================
	bSuccess = TRUE;
	LeaveCriticalSection(&CRITICAL_LOCK);

	return bSuccess;
}
BOOL CDataParser::Add(long wData)
{
	BOOL bSuccess = FALSE;
	
	EnterCriticalSection(&CRITICAL_LOCK);
	
	if(!m_pData) 
		m_pData = GlobalAlloc(GMEM_FIXED,1);
	//=================================================================================
	// 先构造类型
	DWORD dwDataVT = VT_I4;
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&dwDataVT,4);
	m_uDataSize += 4;
	
	int iDataLen = sizeof(wData);
	
	//构造值
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + iDataLen, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&wData,iDataLen);
	m_uDataSize += iDataLen;
	
	//=================================================================================
	bSuccess = TRUE;
	LeaveCriticalSection(&CRITICAL_LOCK);

	return bSuccess;
}
BOOL CDataParser::Add(float wData)
{
	BOOL bSuccess = FALSE;
	
	EnterCriticalSection(&CRITICAL_LOCK);
	
	if(!m_pData) 
		m_pData = GlobalAlloc(GMEM_FIXED,1);
	//=================================================================================
	// 先构造类型
	DWORD dwDataVT = VT_R4;
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&dwDataVT,4);
	m_uDataSize += 4;
	
	int iDataLen = sizeof(wData);
	
	//构造值
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + iDataLen, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&wData,iDataLen);
	m_uDataSize += iDataLen;
	
	//=================================================================================
	bSuccess = TRUE;
	LeaveCriticalSection(&CRITICAL_LOCK);

	return bSuccess;
}
BOOL CDataParser::Add(double wData)
{
	BOOL bSuccess = FALSE;
	
	EnterCriticalSection(&CRITICAL_LOCK);
	
	if(!m_pData) 
		m_pData = GlobalAlloc(GMEM_FIXED,1);
	//=================================================================================
	// 先构造类型
	DWORD dwDataVT = VT_R8;
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&dwDataVT,4);
	m_uDataSize += 4;
	
	int iDataLen = sizeof(wData);
	
	//构造值
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + iDataLen, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&wData,iDataLen);
	m_uDataSize += iDataLen;
	
	//=================================================================================
	bSuccess = TRUE;
	LeaveCriticalSection(&CRITICAL_LOCK);

	return bSuccess;
}
BOOL CDataParser::Add(const COleDateTime& wData)
{
	BOOL bSuccess = FALSE;
	
	EnterCriticalSection(&CRITICAL_LOCK);
	
	if(!m_pData) 
		m_pData = GlobalAlloc(GMEM_FIXED,1);
	//=================================================================================
	// 先构造类型
	DWORD dwDataVT = VT_DATE;
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&dwDataVT,4);
	m_uDataSize += 4;
	
	int iDataLen = sizeof(double);
	
	//构造值
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + iDataLen, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&wData.m_dt,iDataLen);
	m_uDataSize += iDataLen;
	
	//=================================================================================
	bSuccess = TRUE;
	LeaveCriticalSection(&CRITICAL_LOCK);

	return bSuccess;
}
BOOL CDataParser::Add(COleVariant* pvData)
{
	BOOL bSuccess = FALSE;
	
	EnterCriticalSection(&CRITICAL_LOCK);

	if(pvData)
	{
		if(!m_pData) 
			m_pData = GlobalAlloc(GMEM_FIXED,1);

		COleVariant nVarData = *pvData;
		// 构造数据类型
		DWORD dwvDataVT = pvData->vt;
		m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
		CopyMemory((char*)m_pData + m_uDataSize,&dwvDataVT,4);
		m_uDataSize += 4;

		double dbData = VarToDouble(pvData);
		switch(dwvDataVT)
		{
		case VT_UI1:	//Byte                    bVal;
			{
				BYTE VarDataTmp = (BYTE)dbData;
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 1, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&VarDataTmp,1);
				m_uDataSize += 1;
			}break;
		case VT_I1:		//char                    cVal;
			{
				char VarDataTmp = (char)dbData;
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 1, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&VarDataTmp,1);
				m_uDataSize += 1;
			}break;
		case VT_I2:		//Short                   iVal;
			{
				short VarDataTmp = (short)dbData;
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 2, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&VarDataTmp,2);
				m_uDataSize += 2;
			}break;
		case VT_UI2:	//unsigned short          uiVal;
			{
				WORD VarDataTmp = (WORD)dbData;
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 2, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&VarDataTmp,2);
				m_uDataSize += 2;
			}break;
		case VT_INT:	//int                     intVal;
			{
				int VarDataTmp = (int)dbData;
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&VarDataTmp,4);
				m_uDataSize += 4;
			}break;
		case VT_UINT:	//unsigned int            uintVal;
			{

				DWORD VarDataTmp = (DWORD)dbData;
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&VarDataTmp,4);
				m_uDataSize += 4;
			}break;
		case VT_I4:		//long                    lVal;
			{
				long VarDataTmp = (long)dbData;
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&VarDataTmp,4);
				m_uDataSize += 4;
			}break;
		case VT_UI4:	//unsigned long           ulVal;
			{
				unsigned long VarDataTmp = (unsigned long)dbData;
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&VarDataTmp,4);
				m_uDataSize += 4;
			}break;
		case VT_R4:		//float                   fltVal;
			{
				float VarDataTmp = (float)dbData;
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&VarDataTmp,4);
				m_uDataSize += 4;
			}break;
		case VT_R8:		//double                  dblVal;
			{
				double VarDataTmp = (double)dbData;
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 8, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&VarDataTmp,8);
				m_uDataSize += 8;
			}break;
		case VT_BOOL:	//VARIANT_BOOL            boolVal;
			{
				bool VarDataTmp = (dbData == 0.0) ? false : true;
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 1, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&VarDataTmp,1);
				m_uDataSize += 1;
			}break;
		case VT_DATE:	//DATE                    date;
			{
				DATE VarDataTmp = nVarData.date;
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 8, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&VarDataTmp,8);
				m_uDataSize += 8;
			}break;
		case VT_BSTR:	//BSTR                    bstrVal;
			{
				CString strData = nVarData.bstrVal;
				int iLen = strData.GetLength();
				
				// 先写长度
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&iLen,4);
				m_uDataSize += 4;
				
				// 写字符串
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + iLen, GMEM_MOVEABLE);

				char *chAnsiCode = CUtility::ConvertStringToChar(strData);
				if(chAnsiCode)
				{
					CopyMemory((char*)m_pData + m_uDataSize,chAnsiCode,iLen);
				
					delete chAnsiCode;
					chAnsiCode = NULL;
				}
				m_uDataSize += iLen;
			}break;
		case VT_EMPTY:
			{

			}break;
		case VT_NULL:
			{

			}break;
		default:
			break;
		}
		bSuccess = TRUE;
	}
	
	LeaveCriticalSection(&CRITICAL_LOCK);
	
	return bSuccess;
}

BOOL CDataParser::RemoveAllDataItem()
{
	if(m_bMustClean)
	{
		if(m_pData)
		{
			GlobalFree(m_pData);
			m_pData = NULL;
		}
	}
	m_uDataSize = 0;
	return TRUE;
}

LPVOID CDataParser::GetData()
{
	return m_pData;
}

DWORD CDataParser::GetDataSize()
{
	return m_uDataSize;
}
//============================================================================================
void CDataParser::InitData( void *pData,unsigned long nDataSize)
{
	m_pCurDataPos = 0;
	m_pData = pData;
	m_uDataSize = nDataSize;
	// 数据由应用层传进来,在析构中不能释放
	m_bMustClean = false;
}
unsigned long CDataParser::GetCurrentPos()
{
	return m_pCurDataPos;
}
BOOL CDataParser::GetData(COleVariant& pvData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			pvData.vt = dwDataVT;
			switch(dwDataVT)
			{
			case VT_UI1:	//Byte                    bVal;
				{
					// 构造值
					memcpy(&pvData.bVal,(char*)m_pData + m_pCurDataPos, 1);
					m_pCurDataPos += 1;
					bSuccess = TRUE;
				}break;
			case VT_I1:		//char                    cVal;
				{
					memcpy(&pvData.cVal,(char*)m_pData + m_pCurDataPos, 1);
					m_pCurDataPos += 1;
					bSuccess = TRUE;
				}break;
			case VT_I2:		//Short                   iVal;
				{
					memcpy(&pvData.iVal,(char*)m_pData + m_pCurDataPos, 2);
					m_pCurDataPos += 2;
					bSuccess = TRUE;
				}break;
			case VT_UI2:	//unsigned short          uiVal;
				{
					memcpy(&pvData.uiVal,(char*)m_pData + m_pCurDataPos, 2);
					m_pCurDataPos += 2;
					bSuccess = TRUE;
				}break;
			case VT_INT:	//int                     intVal;
				{
					memcpy(&pvData.intVal,(char*)m_pData + m_pCurDataPos, 4);
					m_pCurDataPos += 4;
					bSuccess = TRUE;
				}break;
			case VT_UINT:	//unsigned int            uintVal;
				{
					memcpy(&pvData.uintVal,(char*)m_pData + m_pCurDataPos, 4);
					m_pCurDataPos += 4;
					bSuccess = TRUE;
				
				}break;
			case VT_I4:		//long                    lVal;
				{
					memcpy(&pvData.lVal,(char*)m_pData + m_pCurDataPos, 4);
					m_pCurDataPos += 4;
					bSuccess = TRUE;
				}break;
			case VT_UI4:	//unsigned long           ulVal;
				{
					memcpy(&pvData.ulVal,(char*)m_pData + m_pCurDataPos, 4);
					m_pCurDataPos += 4;
					bSuccess = TRUE;
				}break;
			case VT_R4:		//float                   fltVal;
				{
					memcpy(&pvData.fltVal,(char*)m_pData + m_pCurDataPos, 4);
					m_pCurDataPos += 4;
					bSuccess = TRUE;
				}break;
			case VT_R8:		//double                  dblVal;
				{
					memcpy(&pvData.dblVal,(char*)m_pData + m_pCurDataPos, 8);
					m_pCurDataPos += 8;
					bSuccess = TRUE;
				}break;
			case VT_BOOL:	//VARIANT_BOOL            boolVal;
				{
					BOOL bData = FALSE;
					memcpy(&bData,(char*)m_pData + m_pCurDataPos, 1);
					pvData.boolVal = (bData == TRUE ) ? VARIANT_TRUE : VARIANT_FALSE;
					m_pCurDataPos += 1;
					bSuccess = TRUE;
				}break;
			case VT_DATE:	//DATE                    date;
				{
					memcpy(&pvData.date,(char*)m_pData + m_pCurDataPos, 8);
					m_pCurDataPos += 8;
					bSuccess = TRUE;
				}break;
			case VT_BSTR:	//BSTR                    bstrVal;
				{
					// 先读长度
					DWORD dwDataLen = 0;
					memcpy(&dwDataLen,(char*)m_pData + m_pCurDataPos, 4);
					m_pCurDataPos += 4;

					char *chName = new char[dwDataLen + 1];
					memcpy((char*)chName,(char*)m_pData + m_pCurDataPos,dwDataLen);
					chName[dwDataLen] = '\0';
					m_pCurDataPos += dwDataLen;

					CString strData = CString(chName);
					pvData = strData;
					delete []chName;
					bSuccess = TRUE;
				}break;
			case VT_EMPTY:
				{
					
				}break;
			case VT_NULL:
				{
					
				}break;
			default:
				{
					// 当前索引退回上一次位置
					m_pCurDataPos -= 4;
					bSuccess = FALSE;
				}break;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}
BOOL CDataParser::GetData(WORD &wData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			if(dwDataVT == VT_UI2)
			{
				// 构造值
				memcpy(&wData,(char*)m_pData + m_pCurDataPos, 2);
				m_pCurDataPos += 2;
				bSuccess = TRUE;
			}
			else
			{
				// 当前索引退回上一次位置
				m_pCurDataPos -= 4;
				bSuccess = FALSE;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}
BOOL CDataParser::GetData(DWORD &wData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			if(dwDataVT == VT_UI4)
			{
				// 构造值
				memcpy(&wData,(char*)m_pData + m_pCurDataPos, 4);
				m_pCurDataPos += 4;
				bSuccess = TRUE;
			}
			else
			{
				// 当前索引退回上一次位置
				m_pCurDataPos -= 4;
				bSuccess = FALSE;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}
BOOL CDataParser::GetData(LPVOID pData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			if(dwDataVT == VT_VOID)
			{
				// 先构造长度
				DWORD dwDataLen = 0;
				memcpy(&dwDataLen,(char*)m_pData + m_pCurDataPos,  4);
				m_pCurDataPos += 4;

				// 构造值
				memcpy(&pData,(char*)m_pData + m_pCurDataPos, dwDataLen);
				m_pCurDataPos += dwDataLen;
				bSuccess = TRUE;
			}
			else
			{
				// 当前索引退回上一次位置
				m_pCurDataPos -= 4;
				bSuccess = FALSE;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}
BOOL CDataParser::GetData(BOOL &wData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			if(dwDataVT == VT_BOOL)
			{
				// 构造值
				memcpy(&wData,(char*)m_pData + m_pCurDataPos, 1);
				m_pCurDataPos += 1;
				bSuccess = TRUE;
			}
			else
			{
				// 当前索引退回上一次位置
				m_pCurDataPos -= 4;
				bSuccess = FALSE;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}
BOOL CDataParser::GetData(BYTE &wData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			if(dwDataVT == VT_UI1)
			{
				// 构造值
				memcpy(&wData,(char*)m_pData + m_pCurDataPos, 1);
				m_pCurDataPos += 1;
				bSuccess = TRUE;
			}
			else
			{
				// 当前索引退回上一次位置
				m_pCurDataPos -= 4;
				bSuccess = FALSE;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}
BOOL CDataParser::GetData(char &wData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			if(dwDataVT == VT_I1)
			{
				// 构造值
				memcpy(&wData,(char*)m_pData + m_pCurDataPos, 1);
				m_pCurDataPos += 1;
				bSuccess = TRUE;
			}
			else
			{
				// 当前索引退回上一次位置
				m_pCurDataPos -= 4;
				bSuccess = FALSE;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}
BOOL CDataParser::GetData(short &wData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			if(dwDataVT == VT_I2)
			{
				// 构造值
				memcpy(&wData,(char*)m_pData + m_pCurDataPos, 2);
				m_pCurDataPos += 2;
				bSuccess = TRUE;
			}
			else
			{
				// 当前索引退回上一次位置
				m_pCurDataPos -= 4;
				bSuccess = FALSE;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}
BOOL CDataParser::GetData(long &wData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			if(dwDataVT == VT_I4)
			{
				// 构造值
				memcpy(&wData,(char*)m_pData + m_pCurDataPos,4);
				m_pCurDataPos += 4;
				bSuccess = TRUE;
			}
			else
			{
				// 当前索引退回上一次位置
				m_pCurDataPos -= 4;
				bSuccess = FALSE;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}
BOOL CDataParser::GetData(float &wData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			if(dwDataVT == VT_R4)
			{
				// 构造值
				memcpy(&wData,(char*)m_pData + m_pCurDataPos,4);
				m_pCurDataPos += 4;
				bSuccess = TRUE;
			}
			else
			{
				// 当前索引退回上一次位置
				m_pCurDataPos -= 4;
				bSuccess = FALSE;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}
BOOL CDataParser::GetData(double wData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			if(dwDataVT == VT_R8)
			{
				// 构造值
				memcpy(&wData,(char*)m_pData + m_pCurDataPos,8);
				m_pCurDataPos += 8;
				bSuccess = TRUE;
			}
			else
			{
				// 当前索引退回上一次位置
				m_pCurDataPos -= 4;
				bSuccess = FALSE;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}
BOOL CDataParser::GetData(COleDateTime& wData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			if(dwDataVT == VT_DATE)
			{
				// 构造值
				memcpy(&wData.m_dt,(char*)m_pData + m_pCurDataPos,8);
				m_pCurDataPos += 8;
				bSuccess = TRUE;
			}
			else
			{
				// 当前索引退回上一次位置
				m_pCurDataPos -= 4;
				bSuccess = FALSE;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}
BOOL CDataParser::GetData(CString& strData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			if(dwDataVT == VT_BSTR)
			{
				// 先构造长度
				DWORD dwDataLen = 0;
				memcpy(&dwDataLen,(char*)m_pData + m_pCurDataPos,  4);
				m_pCurDataPos += 4;
				
				char *chName = new char[dwDataLen + 1];

				// 构造值
				memcpy((char*)chName,(char*)m_pData + m_pCurDataPos, dwDataLen);
				chName[dwDataLen] = '\0';
				strData = CString(chName);
				delete []chName;

				m_pCurDataPos += dwDataLen;
				bSuccess = TRUE;
			}
			else
			{
				// 当前索引退回上一次位置
				m_pCurDataPos -= 4;
				bSuccess = FALSE;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

C++ 不同数据类型占据的内存空间大小

首先这个没有标准答案, 除非我们加上xiand

java不同数据类型在内存中的存储方式

转载自http://www.open-open.com/lib/view/open1415453057980.html 1、java的内存管理就是对象的分配和释放 分配:内存的分配是由程序完成的,程...

【C语言】使用回调函数实现一个通用的冒泡排序,可以排序不同的数据类型。

使用回调函数实现一个通用的冒泡排序,可以排序不同的数据类型

使用回调函数实现一个通用的冒泡排序,可以排序不同的数据类型。

使用回调函数实现一个通用的冒泡排序,可以排序不同的数据类型。

一个关于c/c++语言内存数据类型的实验

今天在群里有朋友问到编译器是怎么识别不同数据类型和处理他们的。如果学过汇编或者编译原理应该很好理解。 没学过的话也没关系,这里大概讲解一下。 在计算机内部其实是没有变量类型的,只有由 段地址+偏移地址...

VC++不同数据类型的转换

  • 2012-04-13 15:49
  • 110KB
  • 下载

day14总结(1. 不同修饰符及自定义数据类型的使用 2. 导出javadoc(API)文档 3. jar包的导出、导入)

不同修饰符的使用   类,最常使用public修饰   成员变量,最常使用private修饰   成员方法,最常使用public修饰   abstract与private不能同时使用;   ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)