Click here to Skip to main content
15,891,902 members
Articles / Desktop Programming / MFC

Diff Tool

Rate me:
Please Sign up or sign in to vote.
4.97/5 (64 votes)
17 May 20037 min read 400.3K   10K   153  
A simple diff tool, usable on arbitrary file formats, with a nice HTML rendering
This article provides a simple C++ WIN32 tool to perform diffs on arbitrary files. It also features a nice and workable HTML output.


#pragma once

typedef enum {Normal=0, Changed, Added, Deleted} LineStatus;


// CFileLine ///////////////////////////////////////////////
//
////////////////////////////////////////////////////////////
// S.Rodriguez - Feb 2003
////////////////////////////////////////////////////////////
//
class CFileLine
{
	// Members
protected:
	CString m_s;
	LineStatus m_status;

	// Constructor
public:
	CFileLine();

	// Accessors
public:
	long SetLine(/*in*/CString &s); // store string and build token
	void SetLine(/*in*/CString &s, /*in*/LineStatus ls); // store string and status (does not eval token)
	CString GetLine();
	void SetStatus(/*in*/LineStatus ls);
	LineStatus GetStatus();
	
};


typedef CTemplateArray<CFileLine*>		ArrayLines;
typedef CTemplateArray<long>			ArrayLong;


// CFilePartition ///////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////
// S.Rodriguez - Feb 2003
/////////////////////////////////////////////////////////////////////
//
//
class CFilePartition
{

	// Members
protected:
	CString		m_szFilename;
	ArrayLines	m_arrLines;
	ArrayLong	m_arrTokens;

	// Constructor
public:
	CFilePartition();
	virtual ~CFilePartition();

	// Accessors
public:
	void SetName(CString &);
	CString GetName();
	long GetNBLines(); // amount of lines of this file
	CString GetLine(/*in*/long i); // returns an arbitrary line
	LineStatus GetStatusLine(/*in*/long i); // returns the status of an arbitrary lin
	long *GetTokens(); // token table

	// Methods
	BOOL PreProcess(/*in*/CString &szFilename);
	void AddString(/*in*/CString &s, /*in*/LineStatus ls);
	void AddBlankLine();

	BOOL MatchLine(/*in*/long i1, /*in*/CFilePartition &f2,  /*out*/long &i2);

	void Dump(/*in*/CString &szTitle);


	// Helpers
protected:
	void AddString(/*in*/CString &s, /*in*/long i);
};

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