Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

REG file parser using the Boost Spirit Parser Framework

, , 29 Jun 2009
This article describes a sample parser of reg files using the Boost Spirit Parser Framework. We'll discuss why we use the curtain libraries and make one or another solution, algorithm, and also tests.
RegFileParser_bin.zip
RegFileParser_bin
bin
RegFileParserAutoTest.exe
RegFileParserTestCmd.exe
RegFileParser_source.zip
RegFileParser_source
src
boost_libs
libboost_program_options-vc90-mt-s-1_38.lib
libboost_program_options-vc90-mt-sgd-1_38.lib
RegFileParserAutoTest
TestFiles
RegFileParserLib
RegFileParserTestCmd
RegFileParserTestCmd.idc
RegFileParser_source_no_boost.zip
RegFileParser_source_no_boost
src
RegFileParserAutoTest
TestFiles
RegFileParserLib
RegFileParserTestCmd
RegFileParserTestCmd.idc
#ifndef __TEST_RUNNER_CMD_H_INCLUDED__
#define __TEST_RUNNER_CMD_H_INCLUDED__

#include "TestRunner.hpp"

#include "CountObserver.hpp"
#include "PrintObserver.hpp"
#include "StatusObserver.hpp"
#include "ObserversPool.hpp"

#include <iosfwd>
#include <fstream>

class TestRunnerCmd
{
public:
	enum RunMode
	{
		tst_Print = 0x1,
		tst_Count = 0x2,
		tst_Status = 0x4,
		tst_ToFile = 0x10,
		tst_UsingApi = 0x20
	};

	static void 
	RunTest(const std::wstring& fileName, int mode)
	{
		std::vector<char> buffer;
		TestRunner<wchar_t>::ReadFile(fileName,&buffer);

		switch(GetRegVersion(buffer))
		{
		case reg_NT4:RunTest<char>(buffer,mode);break;
		case reg_NT5:RunTest<wchar_t>(buffer,mode);break;
		default:
			throw std::exception("Wrong file format");
		}
	}

private:
	enum FileType
	{
		reg_unknown,
		reg_NT4,
		reg_NT5
	};
	static int GetRegVersion(const std::vector<char>& buffer)
	{
		std::string regNt4("REGEDIT4\n");

		std::wstring regNt5;
		regNt5 += (wchar_t)0xFEFF; // signifies that the bytes are big endian
		regNt5 += L"Windows Registry Editor Version 5.00\n";
		
		if(StrFind(buffer,regNt4))
			return reg_NT4;
		if(StrFind(buffer,regNt5))
			return reg_NT5;
		else
			return reg_unknown;
	}
	template<class stringT>
	static bool StrFind(const std::vector<char>& buffer,
						const stringT& str)
	{
		size_t strMemSize = str.size()*sizeof(stringT::value_type); 
		if(buffer.size() < strMemSize)
			return false;

		return ( memcmp(&buffer[0],str.c_str(),strMemSize) ==0 );
	}

	template<class> static void 
	RunTest(const std::vector<char>& buffer, int mode){throw std::exception("Wrong type");}
	
	template<> static void 
	RunTest<char>(const std::vector<char>& buffer, int mode){RunTestImpl(buffer,mode,std::cout);}
	
	template<> static void 
	RunTest<wchar_t>(const std::vector<char>& buffer, int mode){RunTestImpl(buffer,mode,std::wcout);}

	template<class charT> static void 
	RunTestImpl(const std::vector<char>& buffer, 
				int mode,
				std::basic_ostream<charT>& coutRef)
	{
		using namespace reg_parser;
		typedef std::basic_string<charT> stringT;
		typedef std::basic_ostream<charT> streamT;
		typedef std::basic_stringstream<charT> stringstreamT;
		typedef std::basic_fstream<charT> fstreamT;
		typedef std::auto_ptr< IRegFileObserver<charT> > ObserverPtr;
		typedef CRegObserversPool<charT> ObserversPool;
		typedef CRegPrintObserver<charT, streamT> PrintObserver;
		typedef CRegStatusObserver<charT, streamT> StatusObserver;
		typedef CRegCountObserver<charT,streamT> CountObserver;
		
		ObserversPool observersPool;

		stringstreamT stream; 
		ObserverPtr printObsrv;
		if( mode & tst_Print )
		{
			printObsrv.reset( new PrintObserver(coutRef) );
			observersPool.PushBack(printObsrv.get());
		}

		ObserverPtr statusObsrv;
		if( mode & tst_Status )
		{
			statusObsrv.reset( new StatusObserver((size_t)&buffer[0],buffer.size(),coutRef) );
			observersPool.PushBack(statusObsrv.get());
		}

		ObserverPtr countObsrv;
		if( mode & tst_Count )
		{
			countObsrv.reset( new CountObserver(coutRef) );
			observersPool.PushBack(countObsrv.get());
		}

		CRegFileParser<charT> regParser(&observersPool);

		// Run parsing
		if( !regParser.Parse( (charT*)&buffer[0] ) )
			throw std::exception("Parsing fail.");
	}
};

#endif // __TEST_RUNNER_CMD_H_INCLUDED__

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)

Share

About the Authors

Apriorit Inc
Apriorit Inc.
Ukraine Ukraine
ApriorIT is a Software Research and Development company that works in advanced knowledge-intensive scopes.
 
Company offers integrated research&development services for the software projects in such directions as Corporate Security, Remote Control, Mobile Development, Embedded Systems, Virtualization, Drivers and others.
 
Official site http://www.apriorit.com
Group type: Organisation

31 members

Follow on   LinkedIn

Ivan Romanenko
Software Developer Codedgers Inc
Ukraine Ukraine
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 30 Jun 2009
Article Copyright 2009 by Apriorit Inc, Ivan Romanenko
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid