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

📄 datastack2.cpp

📁 自己改写的在WINCE上开发用的EVC++的FTP操作示例工程,希望能给相关人士提供帮助.
💻 CPP
字号:

#include "StdAfx.h"
#include "DataStack2.h"

CDataStack2::CDataStack2 ()
{
	m_buffer = NULL;
	m_length = 0;
}

CDataStack2::~CDataStack2 ()
{
	delete[] m_buffer;
}

CDataStack2::operator char* ()
{
	return m_buffer;
}

char* CDataStack2::GetData()
{
	return m_buffer;
}

void CDataStack2::FlushData()
{
	if(!m_buffer || m_length <= 0)
		return;
	
	delete m_buffer;
	m_buffer = NULL;
	m_length = 0;
}

void CDataStack2::Append (const char* data, int len)
{
	if(data == NULL || len <= 0) 
		return;
	
	if(m_length == 0)
	{
		m_buffer = new char[len];
		memcpy(m_buffer, data, len);
		m_length = len;
	}
	else
	{
		//	backup old buffer (temporarily)
		char* oldbuff = new char[m_length];
		memcpy(oldbuff, m_buffer, m_length);
		
		//	create a new buffer that holds the old one PLUS the new stuff
		delete[] m_buffer;
		m_buffer = new char[m_length + len];
		
		//	copy old buffer and new data into the new (bigger) buffer
		memcpy(m_buffer, oldbuff, m_length);
		memcpy(&m_buffer[m_length], data, len);
		
		//	store the new length of the data
		m_length += len;
		
		//	do clean-up
		delete[] oldbuff;
	}
}

int CDataStack2::Remove(char* data, int len)
{
	return GetData(data,len);
}

int CDataStack2::GetData(char* data, int len)
{
	memset(data, 0, len);

	//	if the buffer is empty, don't remove anything
	if(!m_buffer || m_length <= 0)
		return 0;
	
	//	determine how much we will actually copy
	int remlen = len;
	if(remlen > m_length)
		remlen = m_length;
	
	//	backup old buffer (temporarily)
	char* oldbuff = new char[m_length];
	memcpy(oldbuff, m_buffer, m_length);
	
	//	copy part of buffer (or all) to 'data'
	if(data)
		memcpy(data, oldbuff, remlen);
	
	//	create a new buffer that holds the old one MINUS the removed stuff
	delete[] m_buffer;
	m_buffer = NULL;

	if(m_length - remlen <= 0)
	{
		m_buffer = NULL;
		m_length = 0;
	}
	else
	{
		m_buffer = new char[m_length - remlen];
		
		//	remove the part that has been copied from the buffer
		memcpy(m_buffer, &oldbuff[remlen], m_length - remlen);
		
		//	store the new length of the data
		m_length -= remlen;
	}
	
	//	do clean-up
	delete[] oldbuff;
	
	return remlen;
}

int CDataStack2::Length ()
{
	long length;
	length = m_length;
	
	return length;
}

char* CDataStack2::ReadLine(int &nLen,int &nFlag,LPSTR strEol,int nRemoveFlag)
{
	if(!m_buffer || m_length <= 0)
	{
		nFlag = 30;//no data
		nLen = 0;
		return NULL;
	}

	char *pData=NULL;
	char *pDest=NULL;
	int  nResult=0;

	nLen = 0;
	pDest = strstr(m_buffer, strEol);
	nResult = pDest - m_buffer;

	if(!pDest && m_length >= 81920)
	{
		nFlag = 36;//无效协议包
		delete m_buffer;
		m_buffer = NULL;
		m_length = 0;
		
		return NULL;
	}

	if((nResult > 0) && (nResult + 2 > m_length))
	{
		nFlag = 35;//数据没接收完
		return NULL;
	}

	if(pDest != NULL)
	{
		nFlag = 1; //接收到完整数量包
		nLen = nResult + 2 + 1;

		if(nRemoveFlag)
		{
			pData = new char [nLen];
			GetData(pData,nLen - 1);
		}
	}
	else
	{
		nFlag = 32;//有数据但还没检查到"\r\n"
	}

	return pData;
}

CStringA CDataStack2::ReadLine(int &nFlag,LPSTR strEol)
{
	int nDataLen = 0;
	CStringA strResult = "";

	char *pResult = ReadLine(nDataLen,nFlag,strEol);
	if(pResult)
	{
		if(nDataLen > 1)
		{
			pResult[nDataLen - 1] = '\0';
			strResult.Format("%s",pResult);
		}
		delete pResult;
	}

	return strResult;
}

CStringA CDataStack2::FlushLine()
{
	if(!m_buffer || m_length <= 0)
		return "";

	int nDataLen = m_length + 1;
	char *pData=NULL;
	pData = new char [nDataLen - 1];
	GetData(pData,nDataLen);

	CStringA strResult = "";
	pData[nDataLen - 1] = '\0';
	strResult.Format("%s",pData);
	delete pData;

	return strResult;
}

void CDataStack2::NewStack(int nLen,int nFlag)
{
	if(nFlag == 1)
	{
		if(m_length == 0)
		{
			m_buffer = new char[nLen];
			memset(m_buffer, 0, nLen);
			m_length = nLen;
		}
		else
		{
			//	backup old buffer (temporarily)
			char* oldbuff = new char[m_length];
			memcpy(oldbuff, m_buffer, m_length);
			
			//	create a new buffer that holds the old one PLUS the new stuff
			delete[] m_buffer;
			m_buffer = new char[m_length + nLen];
			memset(m_buffer, 0, m_length + nLen);
			
			//	copy old buffer and new data into the new (bigger) buffer
			memcpy(m_buffer, oldbuff, m_length);
			
			//	store the new length of the data
			m_length += nLen;
			
			//	do clean-up
			delete[] oldbuff;
		}
	}
	else
	{
		if(!m_buffer)
		{
			m_buffer = new char[nLen];
			memset(m_buffer, 0,  nLen);
			m_length = nLen;
		}
		else
		{
			delete m_buffer;
			m_buffer = NULL;
			m_length = 0;

			m_buffer = new char[nLen];
			memset(m_buffer, 0,  nLen);
			m_length = nLen;
		}
	}
}

BOOL CDataStack2::SetAt(int nIndex,char* pValue,int nLen)
{
	if(nIndex < 0 || nIndex + nLen >= m_length)
		return FALSE;

	memcpy(m_buffer + nIndex,pValue,nLen);
	return TRUE;
}

BOOL CDataStack2::SetAt(int nIndex,char cValue)
{
	if(nIndex < 0 || nIndex >= m_length)
		return FALSE;

	m_buffer[nIndex] = cValue;
	return TRUE;
}

BOOL CDataStack2::GetAt(int nIndex,char& cValue)
{
	if(nIndex < 0 || nIndex >= m_length)
		return FALSE;

	cValue = m_buffer[nIndex];
	return TRUE;
}

⌨️ 快捷键说明

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