Click here to Skip to main content
15,880,967 members
Articles / Desktop Programming / MFC

Import/Export Registry Sections as XML

Rate me:
Please Sign up or sign in to vote.
4.33/5 (16 votes)
21 Jan 20033 min read 165.7K   6.1K   73  
Export registry sections as XML to simplify registry diffs
This article details a tool aimed to import/export registry sections in XML format, to make registry diff easier in practice.
#include "stdafx.h"
#include "xmlwriter.h"


XmlWriter::XmlWriter()
{
	m_pFile = new CFile();
	Init();
}
XmlWriter::~XmlWriter()
{
	delete m_pFile;
	m_pFile = NULL;
}
void XmlWriter::Init()
{
	SetIndentLevel(0);
	m_bFileOpen = FALSE;
	m_bPrototypeWritten = FALSE;
}
BOOL XmlWriter::Open(CString &szFilename)
{
	if (m_bFileOpen)
		return TRUE;

	m_szFilename = szFilename;

	return TRUE;
}
BOOL XmlWriter::WriteString(CString &szData)
{
	if (!m_bFileOpen) // open file
	{
		m_bFileOpen = m_pFile->Open( (LPCTSTR)m_szFilename, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyNone );
	}
	if (!m_bFileOpen)
		return FALSE;

	if (!m_bPrototypeWritten)
	{
		m_bPrototypeWritten = TRUE; // make sure to set this flag to true before we call a reentrant method such like .WritePInstruction

		// write XML prototype, once and only once
		//
		int nOldIndent = GetIndentLevel();
		SetIndentLevel(0);

		CString s;
		XmlElement xml("xml");
		xml.AddAttrib( CString("version"), CString("1.0") );
		xml.AddAttrib( CString("encoding"), CString("UTF-8") );
		xml.WritePInstruction(*this);
		
		SetIndentLevel(nOldIndent);

	}

	// actual write
	m_pFile->SeekToEnd();
	m_pFile->Write( szData.GetBuffer(0), szData.GetLength() );
	m_pFile->Flush();

	return TRUE;
}
BOOL XmlWriter::Close()
{
	if (m_pFile && m_bFileOpen)
		m_pFile->Close();
	Init();
	return TRUE;
}



XmlStringWriter::XmlStringWriter()
{
	Init();
}
XmlStringWriter::~XmlStringWriter()
{
}
void XmlStringWriter::Init()
{
	m_szContent.Empty();
}
CString &XmlStringWriter::GetContent()
{
	return m_szContent;
}
BOOL XmlStringWriter::WriteString(CString &szData)
{
	if (!m_bPrototypeWritten)
	{
		m_bPrototypeWritten = TRUE; // make sure to set this flag to true before we call a reentrant method such like .WritePInstruction

		// write XML prototype, once and only once
		//
		int nOldIndent = GetIndentLevel();
		SetIndentLevel(0);

		CString s;
		XmlElement xml("xml");
		xml.AddAttrib( CString("version"), CString("1.0") );
		xml.AddAttrib( CString("encoding"), CString("UTF-8") );
		xml.WritePInstruction(*this);
		
		SetIndentLevel(nOldIndent);

	}

	// actual write
	m_szContent += szData;

	return TRUE;
}
BOOL XmlStringWriter::Close()
{
	return TRUE;
}




void HelperBuildIndent(CString &szSpaces, int nLevel)
{
	szSpaces.Empty();
	for (int i=0;i<nLevel;i++)
		szSpaces += " ";
}

XmlElement::XmlElement()
{
}
XmlElement::XmlElement(const CString &szName)
{
	SetName(szName);
}
void XmlElement::SetName(/*in*/const CString &szName)
{
	m_szName = szName;
}
void XmlElement::GetName(/*out*/CString &szName)
{
	szName = m_szName;
}
void XmlElement::GetAttribName(int nIndex, /*out*/ CString &szAttribName)
{
	szAttribName.Empty();
	if (nIndex>-1 && nIndex<GetAttribCount())
		szAttribName = m_szAttribNames.GetAt( nIndex );
}
void XmlElement::GetAttribValue(int nIndex, /*out*/ CString &szAttribValue)
{
	szAttribValue.Empty();
	if (nIndex>-1 && nIndex<m_szAttribValues.GetSize())
		szAttribValue = m_szAttribValues.GetAt( nIndex );
}
int XmlElement::GetAttribCount()
{
	return (int) m_szAttribNames.GetSize();
}
BOOL XmlElement::FindAttrib(/*in*/CString &szAttribName)
{
	BOOL bFound = FALSE;
	int i=0;
	int nSize = (int) m_szAttribNames.GetSize();
	while (i<nSize && !bFound)
	{
		bFound = (m_szAttribNames.GetAt(i)==szAttribName);
		i++;
	}
	return bFound;
}
void XmlElement::AddAttrib(/*in*/CString &szAttribName, /*in*/CString &szAttribValue)
{
	BOOL bFound = FALSE;
	int i=0;
	int nSize = (int) m_szAttribNames.GetSize();
	while (i<nSize && !bFound)
	{
		bFound = (m_szAttribNames.GetAt(i)==szAttribName);
		i++;
	}
	if (bFound) // already known
	{
		i--;
		m_szAttribValues.SetAt( i, szAttribValue );
	}
	else
	{
		m_szAttribNames.Add( szAttribName );
		m_szAttribValues.Add( szAttribValue );
	}
}
void XmlElement::Write(XmlWriter &writer, int nDeltaLevel, BOOL bIndent, BOOL bEOL) // for any kind of open tag
{
	writer.SetIndentLevel( writer.GetIndentLevel()+nDeltaLevel );

	CString s;
	if (bIndent)
		HelperBuildIndent(s,writer.GetIndentLevel());
	s += "<";
	s += m_szName;
	int i;
	int nCount = GetAttribCount();
	for (i=0;i<nCount;i++)
	{
		s += " "; // separator between attribute pairs
		s += m_szAttribNames.GetAt( i );
		s += "=\"";
		s += m_szAttribValues.GetAt( i );
		s += "\"";
	}
	s += ">";
	if (bEOL)
		s += "\r\n"; // ENDL
	writer.WriteString( s );
}
void XmlElement::WriteEmpty(XmlWriter &writer, int nDeltaLevel, BOOL bIndent, BOOL bEOL)
{
	writer.SetIndentLevel( writer.GetIndentLevel()+nDeltaLevel );

	CString s;
	if (bIndent)
		HelperBuildIndent(s,writer.GetIndentLevel());
	s += "<";
	s += m_szName;
	int i;
	int nCount = GetAttribCount();
	for (i=0;i<nCount;i++)
	{
		s += " "; // separator between attribute pairs
		s += m_szAttribNames.GetAt( i );
		s += "=\"";
		s += m_szAttribValues.GetAt( i );
		s += "\"";
	}
	s += "/>";
	if (bEOL)
		s += "\r\n"; // ENDL
	writer.WriteString( s );

	writer.SetIndentLevel( writer.GetIndentLevel()-nDeltaLevel );
}
void XmlElement::WritePInstruction(XmlWriter &writer, int nDeltaLevel)
{
	writer.SetIndentLevel( writer.GetIndentLevel()+nDeltaLevel );

	CString s;
	HelperBuildIndent(s,writer.GetIndentLevel());
	s += "<?";
	s += m_szName;
	int i;
	int nCount = GetAttribCount();
	for (i=0;i<nCount;i++)
	{
		s += " "; // separator between attribute pairs
		s += m_szAttribNames.GetAt( i );
		s += "=\"";
		s += m_szAttribValues.GetAt( i );
		s += "\"";
	}
	s += "?>";
	s += "\r\n"; // ENDL
	writer.WriteString( s );
}
void XmlElement::WriteClosingTag(XmlWriter &writer, int nDeltaLevel, BOOL bIndent, BOOL bEOL)
{
	CString s;
	if (bIndent)
		HelperBuildIndent(s,writer.GetIndentLevel());
	s += "</";
	s += m_szName;
	s += ">";
	if (bEOL)
		s += "\r\n"; // ENDL
	writer.WriteString( s );

	writer.SetIndentLevel( writer.GetIndentLevel()+nDeltaLevel );
}

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.


Written By
France France
Addicted to reverse engineering. At work, I am developing business intelligence software in a team of smart people (independent software vendor).

Need a fast Excel generation component? Try xlsgen.

Comments and Discussions