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

REG file parser using the Boost Spirit Parser Framework

, , 29 Jun 2009 CPOL
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_H_INCLUDED__
#define __TEST_RUNNER_H_INCLUDED__

#include <string>
#include <vector>
#include <iostream>
#include <iosfwd>

#include "RegFileParser.hpp"
#include "PrintObserver.hpp"

#include <boost/scope_exit.hpp>
#include <boost/algorithm/string.hpp>

inline void AddRegHeader(std::string* string) 
{ 
	*string += "REGEDIT4\n"; 
}
inline void AddRegHeader(std::wstring* string) 
{
	*string += (wchar_t)0xFEFF; // signifies that the bytes are big endian
	*string += L"Windows Registry Editor Version 5.00\n"; 
}

template<class charT>
class TestRunner
{
protected:
    typedef std::basic_string<charT> stringT;
	typedef std::basic_stringstream<charT> stringstreamT;

    typedef std::basic_fstream<charT> fstreamT;
public:
    static void 
	ReadFile(const std::wstring& fileName,
		     std::vector<char>* pBuffer)
    {
        FILE * fin = 0;
        _wfopen_s(&fin,fileName.c_str(),L"r,ccs=UNICODE");
        if (!fin)
			throw std::exception("Can't open file.\n");

		BOOST_SCOPE_EXIT( (&fin) )
		{
			if (!fin)
                fclose(fin);
		} 
		BOOST_SCOPE_EXIT_END
		
        fseek(fin,0, SEEK_END);
        int filesize = ftell(fin);
		fseek(fin,0, SEEK_SET);

		if(filesize == 0)
			throw std::exception("File is empty");

		// Reserve one character for trailing null
        pBuffer->resize(filesize+sizeof(charT)); 

        size_t readed = fread(&pBuffer->at(0), sizeof(char),filesize, fin);       
        if(readed == 0)
            throw std::exception("Can't read file or file is empty");

		// Add trailing null
        charT* pString = (charT*)&pBuffer->at(0);       
        size_t neededIndex = readed / sizeof(charT);            
        pString[neededIndex] = '\0'; 
    }
    static void 
	RunTest(const std::wstring& fileName)
    {
        std::vector<char> buffer;
        ReadFile(fileName,&buffer);

		stringstreamT stream; 
		reg_parser::CRegPrintObserver<charT,stringstreamT> coutObserver(stream);
		reg_parser::CRegFileParser<charT> regParser(&coutObserver);

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

		// Prepare data for comparison
		stringT parsedStr;
		AddRegHeader(&parsedStr);
		parsedStr += stream.str();

		stringT originalStr = stringT((charT*)&buffer[0]);

		// Trim unneeded symbols
		using namespace boost;
		trim_if(originalStr,is_any_of("\n\t\0 "));
		trim_if(parsedStr,is_any_of("\n\t\0 "));

		if( parsedStr != originalStr)
		{
			// Save to file result of parsing
			std::wstring parsedFileName = fileName + L"_parsed";
			fstreamT file(parsedFileName.c_str(),fstreamT::out | fstreamT::trunc);
			if( file.is_open() == false )
				throw std::exception("Can't create file for result of parsing.");

			BOOST_SCOPE_EXIT( (&file) )
			{
				file.close();
			} 
			BOOST_SCOPE_EXIT_END

			file << parsedStr;

			throw std::exception("Parsed data not equal to original data.");
		}
	}
};

#endif // __TEST_RUNNER_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.141022.2 | 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