Change tracking

S.Rodriguez - Oct 14, 2004


Download the source code (8 kb). C++ and PHP



This article is about the development of a feature that I call change tracking which you can find in products like MS Word. The screen capture above shows change tracking in practice.

The source code provided along with the article is in C++, and in PHP. C++ developers are able to integrate it with regular WIN32/MFC applications. PHP developers however can directly integrate this in their websites.


What are the effects

Change tracking helps users keep track of additions, deletions and mere changes in text between two revisions. In fact, additions and deletions are the same concept, it's all a matter of changing the point of view. Changes are also a combination of additions and deletions, this is why modelling the differences between two revisions of a piece of text is quite easy.

In its current form, additions are visually represented by a html-based red color, and deletions are represented by a html-based black strikethrough.


How does it work

The algorithm separates the change tracking with the visual representation of change tracking, that's why if you need a different styling, like not using html at all, it's easy and you just need to change the visual code without changing the algorithm itself.

  • buildChangeList : this is the core algorithm, the input is two revisions of an arbitrary text, the output is a collection of changes. Each change is a simple 4-uple structure describing which text of the two is being processed, what type of change has been discovered, and the boundaries of it.
  • buildTrackChanges : this is where visual styles are added to the original text in order to reflect the changes.


How to use it

In PHP, there is one single function call :

  buildTrackChanges("Two typo here. And another technical paper.<br>", 
"Two typosss he. And another marketing paper.<br>",1);

The 1 value passed as last argument is to tell whether we want the CSS headers, pass 0 otherwise. If you pass 0, you are responsible for declaring the CSS styles elsewhere.

In C++, there are two method calls :

  node* n = new node(NULL);
  CTrackChanges c;

  char* s1 = "Two typo here. technical paper"; 
  char* s2 = "Two typosss here. marketing paper";

  c.buildChangeList(n, s1, s2);


  OutputDebugString("\r\n\r\nstring tracking the changes : \r\n");

  char* s = c.buildTrackChanges(n,s1,s2);

  OutputDebugString(s ? s : "some error occured!");

  delete [] s;

  delete n;