Click here to Skip to main content
15,895,839 members
Articles / Desktop Programming / WTL

Form Designer

26 Jul 2021CPOL24 min read 352.1K   82.5K   230  
Component for adding scriptable forms capabilities to an application.
// Stream.cpp
//
// Author : David Shepherd
//			Copyright (c) 2002, DaeDoe-Software
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Stream.h"

// daedoe signature as hex
#define DAEDOE_SIGNATURE	0x000EEDDA	// 0xDAED0E00
// stream version
// todo : increment this whenever the stream format changes
#define STREAM_VERSION		3
// stream header guid
// {5570A1E1-279A-11d6-B6A6-F2A9931D1E45}
DEFINE_GUID(GUID_STREAM_HEADER,
0x5570a1e1, 0x279a, 0x11d6, 0xb6, 0xa6, 0xf2, 0xa9, 0x93, 0x1d, 0x1e, 0x45);
// stream footer guid
// {5CDF6741-279A-11d6-B6A6-F2A9931D1E45}
DEFINE_GUID(GUID_STREAM_FOOTER,
0x5cdf6741, 0x279a, 0x11d6, 0xb6, 0xa6, 0xf2, 0xa9, 0x93, 0x1d, 0x1e, 0x45);

/////////////////////////////////////////////////////////////////////////////
// CStreamHeader

CStreamHeader::CStreamHeader()
{
	// initialise everything
	m_DaeDoe=DAEDOE_SIGNATURE;
	m_Signature=GUID_STREAM_HEADER;
	m_Version=STREAM_VERSION;
	m_Reserved0=0;
	m_Reserved1=0;
}

void CStreamHeader::ReadFromStream(const CComPtr<IStream> &spStream)
{
	// clear the header
	// this is required to correctly check for validity
	ZeroMemory(this,sizeof(*this));
	// read the header from the passed stream
	::ReadFromStream(spStream,*this);
	if(IsValid()==FALSE)
	{
		throw std::exception();
	}
}

void CStreamHeader::WriteToStream(const CComPtr<IStream> &spStream)
{
	// write the header to the passed stream
	::WriteToStream(spStream,*this);
}

BOOL CStreamHeader::IsValid()
{
	// determine if the header is valid
	if(m_DaeDoe!=DAEDOE_SIGNATURE)
	{
		return FALSE;	// invalid
	}
	if(m_Signature!=GUID_STREAM_HEADER)
	{
		return FALSE;	// invalid
	}
	return TRUE;		// valid
}

/////////////////////////////////////////////////////////////////////////////
// CStreamFooter

CStreamFooter::CStreamFooter()
{
	// initialise everything
	m_Signature=GUID_STREAM_FOOTER;
	m_Reserved0=0;
	m_Reserved1=0;
}

void CStreamFooter::ReadFromStream(const CComPtr<IStream> &spStream)
{
	// clear the footer
	// this is required to correctly check for validity
	ZeroMemory(this,sizeof(*this));
	// read the footer from the passed stream
	::ReadFromStream(spStream,*this);
	if(IsValid()==FALSE)
	{
		throw std::exception();
	}
}

void CStreamFooter::WriteToStream(const CComPtr<IStream> &spStream)
{
	// write the footer to the passed stream
	::WriteToStream(spStream,*this);
}

BOOL CStreamFooter::IsValid()
{
	// determine if the footer is valid
	return (m_Signature==GUID_STREAM_FOOTER) ? TRUE : FALSE;
}

/////////////////////////////////////////////////////////////////////////////
// CStreamFormBlock

CStreamFormBlock::CStreamFormBlock()
{
	// initialise everything
	m_Size=CSize(0,0);
}

void CStreamFormBlock::ReadFromStream(
	const CComPtr<IStream> &spStream,DWORD StreamVersion)
{
	// read the form block from the passed stream
	::ReadFromStream(spStream,m_Name);
	::ReadFromStream(spStream,m_Size);
	::ReadFromStream(spStream,m_BackColor);
	::ReadFromStream(spStream,m_ForeColor);
}

void CStreamFormBlock::WriteToStream(const CComPtr<IStream> &spStream)
{
	// write the form block to the passed stream
	::WriteToStream(spStream,m_Name);
	::WriteToStream(spStream,m_Size);
	::WriteToStream(spStream,m_BackColor);
	::WriteToStream(spStream,m_ForeColor);
}

/////////////////////////////////////////////////////////////////////////////
// CStreamItemListHeader

CStreamItemListHeader::CStreamItemListHeader()
{
	// initialise everything
	m_Count=0;
	m_BoundingRect=CRect(0,0,0,0);
}

void CStreamItemListHeader::ReadFromStream(
	const CComPtr<IStream> &spStream,DWORD StreamVersion)
{
	// read the item list header from the passed stream
	::ReadFromStream(spStream,*this);
}

void CStreamItemListHeader::WriteToStream(const CComPtr<IStream> &spStream)
{
	// write the item list header to the passed stream
	::WriteToStream(spStream,*this);
}

/////////////////////////////////////////////////////////////////////////////
// CStreamItemBlock

CStreamItemBlock::CStreamItemBlock()
{
	// initialise everything
	m_Rect=CRect(0,0,0,0);
	m_ClassId=CLSID_NULL;
	m_HasPersistantData=FALSE;
}

void CStreamItemBlock::ReadFromStream(
	const CComPtr<IStream> &spStream,DWORD StreamVersion)
{
	// read the item block from the passed stream
	::ReadFromStream(spStream,m_Name);
	if(StreamVersion > 1)
	{
		::ReadFromStream(spStream,m_Tag);
	}
	::ReadFromStream(spStream,m_Rect);
	::ReadFromStream(spStream,m_ClassId);
	::ReadFromStream(spStream,m_HasPersistantData);
}

void CStreamItemBlock::WriteToStream(const CComPtr<IStream> &spStream)
{
	// write the item block to the passed stream
	::WriteToStream(spStream,m_Name);
	::WriteToStream(spStream,m_Tag);
	::WriteToStream(spStream,m_Rect);
	::WriteToStream(spStream,m_ClassId);
	::WriteToStream(spStream,m_HasPersistantData);
}

/////////////////////////////////////////////////////////////////////////////
// CStreamItemPersistantDataHeader

CStreamItemPersistantDataHeader::CStreamItemPersistantDataHeader()
{
	// initialise everything
	m_Length.QuadPart=0;
}

void CStreamItemPersistantDataHeader::ReadFromStream(
	const CComPtr<IStream> &spStream,DWORD StreamVersion)
{
	// read the item persistant data header from the passed stream
	::ReadFromStream(spStream,*this);
}

void CStreamItemPersistantDataHeader::WriteToStream(const CComPtr<IStream> &spStream)
{
	// write the item persistant data header to the passed stream
	::WriteToStream(spStream,*this);
}

/////////////////////////////////////////////////////////////////////////////
// CStreamScriptHeader

CStreamScriptHeader::CStreamScriptHeader()
{
	// initialise everything
	m_LineCount=0;
}

void CStreamScriptHeader::ReadFromStream(
	const CComPtr<IStream> &spStream,DWORD StreamVersion)
{
	// read the script header from the passed stream
	::ReadFromStream(spStream,*this);
}

void CStreamScriptHeader::WriteToStream(const CComPtr<IStream> &spStream)
{
	// write the script header to the passed stream
	::WriteToStream(spStream,*this);
}

/////////////////////////////////////////////////////////////////////////////
// Stream helpers

// reads a std::wstring from the passed stream
template <>
void ReadFromStream(
	const CComPtr<IStream> &spStream,std::wstring &Data)
{
	// length
	DWORD Length=0;
	ReadFromStream(spStream,Length);
	// short data
	const DWORD MAX_LENGTH=1024;
	if(Length < MAX_LENGTH)
	{
		WCHAR Buf[MAX_LENGTH]=L"";
		ReadArrayFromStream(spStream,Buf,Length);
		// null terminate and assign
		Buf[Length]=L'\0';
		Data=Buf;
	}
	// long data
	else
	{
		std::auto_ptr<WCHAR> Buf(new WCHAR [Length+1]);
		ReadArrayFromStream(spStream,Buf.get(),Length);
		// null terminate and assign
		Buf.get()[Length]=L'\0';
		Data=Buf.get();
	}
}

// writes a std::wstring to the passed stream
template <>
void WriteToStream(
	const CComPtr<IStream> &spStream,const std::wstring &Data)
{
	// length
	DWORD Length=Data.length();
	WriteToStream(spStream,Length);
	// data
	WriteArrayToStream(spStream,Data.data(),Data.length());
}

By viewing downloads associated with this article you agree to the Terms of Service 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)


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions