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

📄 streambuf.cpp

📁 用到内存的序列化和反序列化
💻 CPP
字号:
#include "stdafx.h"
#include "StreamBuf.h"

//////////////////////////////////////
//									//
//     ||   (\../) (\../)  ||		//
//   __||____(oo)___(oo)___||___	//
//   --||---"----"-"----"--||---	//
//   __||__@( __ )_( __ )__||___	//
//   --||----"--"---"--"---||---	//
//     ||//              \\||//		//
//   ^^^^^^^^^^^^^^^^^^^^^^^^^^^	//
//////////////////////////////////////

IMPLEMENT_DYNAMIC(CStreamBufException, CException)

UINT CStreamBuf::Read(void* lpBuf, UINT nMax)
{
	ASSERT(IsLoading());
	if(nMax==0)return 0;

	if(m_pBufCur)
	{
		ASSERT(AfxIsValidAddress(lpBuf, nMax));  // read-only access needed
		DWORD dwLeft = GetLeftBufSize();
		nMax = min(nMax, dwLeft);
		memcpy(lpBuf, m_pBufCur, nMax);
		m_pBufCur += nMax;
	}
	m_dwDataSize += nMax;

	return nMax;
}

UINT CStreamBuf::Write(const void* lpBuf, UINT nMax)
{
	ASSERT(IsStoring());
	if(nMax==0)return 0;

	if(m_pBufCur)
	{
		ASSERT(AfxIsValidAddress(lpBuf, nMax, FALSE));  // read-only access needed
		DWORD dwLeft = GetLeftBufSize();
		nMax = min(nMax, dwLeft);
		memcpy(m_pBufCur, lpBuf, nMax);
		m_pBufCur += nMax;
	}
	m_dwDataSize += nMax;

	return nMax;
}

CStreamBuf& CStreamBuf::operator<<(const CString& string)
{
	ASSERT(IsStoring());

	// special signature to recognize unicode strings
#ifdef _UNICODE
	CStreamBuf::operator<<((BYTE)0xff);
	CStreamBuf::operator<<((WORD)0xfffe);
#endif

	UINT nLength = string.GetLength();
	
	if (nLength < 255)
	{
		CStreamBuf::operator<<((BYTE)nLength);
	}
	else if (nLength < 0xfffe)
	{
		CStreamBuf::operator<<((BYTE)0xff);
		CStreamBuf::operator<<((WORD)nLength);
	}
	else
	{
		CStreamBuf::operator<<((BYTE)0xff);
		CStreamBuf::operator<<((WORD)0xffff);
		CStreamBuf::operator<<((DWORD)nLength);
	}

	UINT nSize = nLength*sizeof(TCHAR);
	if(Write((LPCTSTR)string, nSize) != nSize)
		ThrowStreamBufException(CStreamBufException::endOfBuf);
	
	return *this;
}

// return string length or -1 if UNICODE string is found in the archive
UINT _SBReadStringLength(CStreamBuf& sb)
{
	DWORD nNewLen;

	// attempt BYTE length first
	BYTE bLen;
	sb >> bLen;

	if (bLen < 0xff)
		return bLen;

	// attempt WORD length
	WORD wLen;
	sb >> wLen;
	if (wLen == 0xfffe)
	{
		// UNICODE string prefix (length will follow)
		return (UINT)-1;
	}
	else if (wLen == 0xffff)
	{
		// read DWORD of length
		sb >> nNewLen;
		return (UINT)nNewLen;
	}
	else
		return wLen;
}

CStreamBuf& CStreamBuf::operator>>(CString& string)
{
	ASSERT(IsLoading());

#ifdef _UNICODE
	int nConvert = 1;   // if we get ANSI, convert
	typedef CHAR	ITCHAR;	//与TCHAR相反
#else
	int nConvert = 0;   // if we get UNICODE, convert
	typedef WCHAR	ITCHAR; //与TCHAR相反
#endif

	UINT nNewLen = _SBReadStringLength(*this);
	if (nNewLen == (UINT)-1)//如果要读取的是UNICODE
	{
		nConvert = 1 - nConvert;
		nNewLen = _SBReadStringLength(*this);
		ASSERT(nNewLen != -1);
	}

	if(nNewLen==0)
	{
		string.Empty();
	}
	else
	{
		if(nConvert)
		{
			ITCHAR *pBuf = new ITCHAR[nNewLen+1];
			UINT nSize = nNewLen*sizeof(ITCHAR);
			if(Read(pBuf, nSize) != nSize)
				ThrowStreamBufException(CStreamBufException::endOfBuf);
			pBuf[nNewLen] = 0;
			
			string = pBuf;
			delete []pBuf;
		}
		else
		{
			TCHAR *pBuf = new TCHAR[nNewLen + 1];
			UINT nSize = nNewLen*sizeof(TCHAR);
			if(Read(pBuf, nSize) != nSize)
				ThrowStreamBufException(CStreamBufException::endOfBuf);
			pBuf[nNewLen] = 0;

			string = pBuf;
			delete []pBuf;
		}
	}
	return *this;
}

//转换成为ANSI字符集写入
UINT CStreamBuf::WriteLineANSI(LPCTSTR lpszString)
{
#ifdef _UNICODE
	int n = WideCharToMultiByte(CP_ACP, 0, lpszString, -1, NULL, 0, NULL, NULL);
	if(n <= 0 )return 0;
	
	UINT iWrite = 0;
	if(m_pBufCur==NULL)
		iWrite = Write(NULL, n-1);
	else
	{
		PCHAR cBuf = new CHAR[n];
		WideCharToMultiByte(CP_ACP, 0, lpszString, -1, cBuf, n, NULL, NULL);
		iWrite = Write(cBuf, n-1);
		delete []cBuf;
	}

#else
	UINT iWrite = Write(lpszString, strlen(lpszString));
#endif

	iWrite += Write("\r\n", 2);
	return iWrite;
}

//以ANSI字符集方式读入,并转换成为相应字符集
UINT CStreamBuf::ReadLineANSI(LPTSTR lpszString, UINT nMax)
{
	char ch;
	int nStop = nMax;
	int nRead = 0;
	char* pText = (char*)lpszString;
	while (nRead < nStop)
	{
		*this >> ch;

		// stop and end-of-line (trailing '\n' is ignored)
		if (ch == '\n' || ch == '\r')
		{
			if (ch == '\r')
				*this >> ch;
			break;
		}
		pText[nRead++] = ch;
	}

#ifdef _UNICODE
	int n = MultiByteToWideChar(CP_ACP, 0, pText, nRead, NULL, 0);
	if(n <= 0 )return 0;
	PCHAR Buf = new char[n];
	memcpy(Buf, pText, n);
	nRead = MultiByteToWideChar(CP_ACP, 0, Buf, nRead, lpszString, n);
	delete []Buf;
#endif

	return nRead;
}

void CStreamBuf::SkipReadTo(const void* lpFind, UINT nSize)
{
	char ch;
	*this>>ch;

	for(;;)
	{
		const char *pCh = (char *)lpFind;
		UINT nCount = nSize;

		while(nCount)
		{
			if(ch != *pCh)
			{
				if(nCount == nSize)
					*this>>ch;
				break;
			}
			if(--nCount==0)return;

			*this>>ch;
			pCh++;
		}
	}
}

⌨️ 快捷键说明

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