1

My current project is to implement a kind of WinMerge clone to compare two files.

I am using C++ with the Win32-API (WTL, ATL, that stuff), and I already designed an UI for this. However, I am now sitting here, thinking about a design on how to manage the whole file comparison and the data flow.

Following problem:

I have a custom control for each of the 2 edit controls, which are showing the respective file content. Of course, I could just add a method to make them load their file, and display it, but that won't help, as I'm not writing an editor, but rather a comparison tool.

I could kind of add a reference to the file which I want to compare the original file to in the class of the original file, but I would rather have a dedicated class responsible for all managing and comparing.

The current implementation I have in mind looks like this:

Each edit control is an object of "FileEdit". This class is the GUI class at the same time, and receives events like button press, clicks etc. This gives me a base to build on if I want to add certain keybind features later on.

Both FileEdits are being managed by a "FileManager". The FileManager receives both FileEdits from the main class which is the GUI class for the whole dialog.

As soon as the files are entered in controls dedicated to that, the file manager receives the paths and starts to load the files for both FileEdits. Considering the appending process for the edit controls is relatively slow, I decided to load 500 lines, and then append the whole packet at once.

Therefore, I would read 500 lines for both files inside the FileManager. For every line read, the manager compares the lines, checks for differences, and creates a struct, containing the line content and the state (equal, different, error... etc.).

After 500 lines are read, the manager sends each file edit a vector containing the 500 structs, and then the FileEdit would actually append the lines in the packet to the edit control.

This is some kind of idea which is swirling in my head right now. The problem would however be the handling of actual user input. Considering the FileEdit objects are managed by the FileManager, but they actually receive input events themselves - how could the FileManager be notified of this input?

EDIT: Maybe it makes sense to actually let both Edits load the files themselves and manage a vector of structs for each line, containing all the information, and then letting the manager fetch information for comparison from the Edit objects.

Or, even better, make the comparison manager a member of both Edits. This way, the events can trigger a comparison on their own.

Did anyone implement something like this, and could give me an idea of how to do this?

  • Win32 API? You must like to hurt yourself. Why not use a wheel someone else already invented? – user22815 Feb 11 '17 at 19:31
  • @Snowman I know it might not be the most comfortable way to do GUIs, but it's actually not too bad with all the wrappers, if you are not using WIN32 directly. I'd just like to learn it, nevertheless. – Sossenbinder Feb 11 '17 at 19:34
  • 1
    Look at google-diff-match-patch which is based on Myer's diff algorithm. Also see the optimizations made by the author of the library (Neil Fraser). Note: the code works on strings, not on files. – Theraot Feb 11 '17 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.