Click here to Skip to main content
15,860,943 members
Articles / Programming Languages / C++

REG file parser using the Boost Spirit Parser Framework

,
Rate me:
Please Sign up or sign in to vote.
4.88/5 (18 votes)
29 Jun 2009CPOL12 min read 33K   1.1K   37  
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.
#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)


Written By
Chief Technology Officer Apriorit Inc.
United States United States
ApriorIT is a software research and development company specializing in cybersecurity and data management technology engineering. We work for a broad range of clients from Fortune 500 technology leaders to small innovative startups building unique solutions.

As Apriorit offers integrated research&development services for the software projects in such areas as endpoint security, network security, data security, embedded Systems, and virtualization, we have strong kernel and driver development skills, huge system programming expertise, and are reals fans of research projects.

Our specialty is reverse engineering, we apply it for security testing and security-related projects.

A separate department of Apriorit works on large-scale business SaaS solutions, handling tasks from business analysis, data architecture design, and web development to performance optimization and DevOps.

Official site: https://www.apriorit.com
Clutch profile: https://clutch.co/profile/apriorit
This is a Organisation

33 members

Written By
Software Developer Codedgers Inc
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions