Click here to Skip to main content
Click here to Skip to main content

An Asynchronous Pluggable Protocol Handler for data: URLs

By , 28 Jan 2006
 
dataprotocol_src.zip
DataProtocol
DataPluggableProtocol.rgs
DataProtocol.def
DataProtocol.rgs
DataProtocol.vcproj.CCC.ramakrishna.user
DataProtocolps.def
DataProtocolPS.vcproj.CCC.ramakrishna.user
#pragma once

class CDataURL
{
public:
	CDataURL()
		: m_strAttribute(L"charset")
		, m_strValue(L"US-ASCII")
		, m_strMimeType(L"text/plain")
		, m_pvData(NULL)
		, m_dwDataLength(0)
	{
	}
	
	void Reset()
	{
		*this = CDataURL();
	}

	bool Parse(LPCWSTR szURL);
	
	CAtlStringA GetMimeType()
	{
		return m_strMimeType;
	}

	CAtlStringA GetCharset()
	{
		if (m_strAttribute.CompareNoCase("charset") == 0)
		{
			return m_strValue;
		}
		
		return CAtlStringA();
	}

	CAtlStringA GetAttribute()
	{
		return m_strAttribute;
	}

	CAtlStringA GetValue()
	{
		return m_strValue;
	}

	CAtlStringA GetDataString()
	{
		return m_strData;
	}

	BYTE* GetData()
	{
		return m_pvData;
	}

	DWORD GetDataLength()
	{
		return m_dwDataLength;
	}

public:
	CDataURL(const CDataURL& u)
		: m_strMimeType(u.m_strMimeType)
		, m_strAttribute(u.m_strAttribute)
		, m_strValue(u.m_strValue)
		, m_strData(u.m_strData)
		, m_dwDataLength(u.m_dwDataLength)
		, m_pvData(NULL)
	{
		if (m_dwDataLength)
		{
			m_pvData = new BYTE[u.m_dwDataLength];
			memcpy_s(m_pvData, m_dwDataLength, u.m_pvData, u.m_dwDataLength);
		}
	}
	
	CDataURL& operator = (const CDataURL& u)
	{	
		m_strMimeType = u.m_strMimeType;
		m_strAttribute = u.m_strAttribute;
		m_strValue = u.m_strValue;
		m_dwDataLength = u.m_dwDataLength;
		delete [] m_pvData;
		m_pvData = new BYTE[m_dwDataLength];
		memcpy_s(m_pvData, m_dwDataLength, u.m_pvData, u.m_dwDataLength);
		
		return *this;
	}

	~CDataURL()
	{
		delete [] m_pvData;
	}

private:
	void AssignMatchGroup(CAtlStringA& str, CAtlREMatchContext<CAtlRECharTraitsA>& ctxt, int nGroup)
	{
		CAtlREMatchContext<CAtlRECharTraitsA>::MatchGroup g;
		ctxt.GetMatch(nGroup, &g);

		intptr_t nLen = g.szEnd - g.szStart;

		if (nLen != 0)
			str = CAtlString(g.szStart, int(nLen));
	}

private:
	CAtlStringA m_strMimeType;
	CAtlStringA m_strAttribute;
	CAtlStringA m_strValue;
	CAtlStringA m_strData;
	BYTE* m_pvData;
	DWORD m_dwDataLength;

	friend bool operator == (CDataURL& u1, CDataURL& u2);
};

bool operator == (CDataURL& u1, CDataURL& u2)
{
	return (u1.m_strMimeType.CompareNoCase(u2.m_strMimeType) == 0  && 
		u1.m_dwDataLength == u2.m_dwDataLength &&
		(u1.m_pvData == u2.m_pvData ||
			memcmp(u1.m_pvData, u2.m_pvData, u1.m_dwDataLength)
		)
	);
}

By viewing downloads associated with this article you agree to the Terms of use and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Rama Krishna Vavilala
Architect
United States United States
Member
No Biography provided

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 28 Jan 2006
Article Copyright 2006 by Rama Krishna Vavilala
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid