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

📄 streamoncstring.h

📁 Visual C++.net编成指南一书的源代码
💻 H
字号:
// File: StreamOnCString.h
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Classes Reference and related electronic
// documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft C++ Libraries products.

#pragma once
#ifndef STREAMONCSTRING_H_INCLUDED
#define STREAMONCSTRING_H_INCLUDED

class CReadWriteStreamOnCString : public IWriteStream,
			public IStreamImpl
{
public:
	HRESULT __stdcall QueryInterface(REFIID riid, void **ppv) throw()
	{
		if (ppv == NULL)
		{
			return E_POINTER;
		}

		*ppv = NULL;

		if (InlineIsEqualGUID(riid, IID_IUnknown) ||
			InlineIsEqualGUID(riid, IID_IStream) ||
			InlineIsEqualGUID(riid, IID_ISequentialStream))
		{
			*ppv = static_cast<IStream *>(this);
			return S_OK;
		}

		return E_NOINTERFACE;
	}

	ULONG __stdcall AddRef() throw()
	{
		::InterlockedIncrement( &l_refCount );
		return 1;
	}

	ULONG __stdcall Release() throw()
	{
		if( l_refCount > 0 )
		{
			::InterlockedDecrement( &l_refCount );
			if( l_refCount == 0 )
				Cleanup();
		}
		return 1;
	}

public:
	CStringA		m_str;
	long			m_nCurr;
	long			m_nBodyLen;

	long			l_refCount;


public:

	CReadWriteStreamOnCString() throw()
		: m_nCurr(0), m_nBodyLen(0), l_refCount(0)
	{
	}

	BOOL Cleanup() throw()
	{
		m_nCurr		=	0;
		m_nBodyLen	=	0;

		m_str.Empty();

		return TRUE;
	}

	HRESULT __stdcall Read(void *pDest, ULONG nMaxLen, ULONG *pnRead) throw()
	{
		ATLASSERT( pDest != NULL );

		if (pnRead != NULL)
		{
			*pnRead = 0;
		}

		long nRead = m_nCurr;
		if (nRead < m_nBodyLen)
		{
			long nLength = min((int)(m_nBodyLen - nRead), (LONG) nMaxLen);
			memcpy(pDest, (LPCSTR)m_str + nRead, nLength);
			m_nCurr+= nLength;

			if (pnRead != NULL)
			{
				*pnRead = (ULONG) nLength;
			}
		}

		return S_OK;
	}

	HRESULT WriteStream(LPCSTR szOut, int nLen, LPDWORD pdwWritten) throw()
	{
		ATLASSERT( szOut != NULL );

		if (nLen < 0)
		{
			nLen = (int) strlen(szOut);
		}

		_ATLTRY
		{
			m_str.Append(szOut, nLen);
			m_nBodyLen	=	m_str.GetLength();
		}
		_ATLCATCHALL()
		{
			return E_OUTOFMEMORY;
		}

		if (pdwWritten != NULL)
		{
			*pdwWritten = (DWORD) nLen;
		}

		return S_OK;
	}

	HRESULT FlushStream() throw()
	{
		return S_OK;
	}

}; // class CReadWriteStreamOnCString


#endif STREAMONCSTRING_H_INCLUDED

⌨️ 快捷键说明

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