Home   Cover Cover Cover Cover
  Diff.cs
// Diff  -- A file compare utility
// H. Moessenboeck, University of Linz
//--------------------------------------------------------------------------
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

class Diff {
  static Form f;                    // the main window
  static TextBox tb1, tb2;          // the two text boxes
  static Label fileLab1, fileLab2;  // the file names above the text boxes
  static CheckBox skipWhiteSpace;   // the check box to skip white space
  
  static int selPos1, selPos2;      // position of selections in text boxes 1 and 2
  
  // Open a window and show a short help text
  static void ShowHelp(object sender, EventArgs e) {
    Form help = new Form();
    help.Location = new Point(10, 10);
    help.ClientSize = new Size(280, 150);
    
    Label lab = new Label();
    lab.Location = new Point(5, 5);
    lab.Size = new Size(help.Width - 10, help.Height - 10);
    lab.Text = "Call this program as:\r\n"
             + "    Diff [ filename1 [ filename2 ]]\r\n"
             + "or specify the file names using the Open buttons.\r\n"
             + "Select the character from where you want to start\r\n"
             + "the comparison both in the upper and lower text box\r\n"
             + "and click Compare, which will set the selection to the\r\n"
             + "first difference between the two texts.\r\n"
             + "Check 'Skip white space' if you want to ignore\r\n"
             + "differences in white space (blank, tab, eol).\r\n\r\n"
             + "Author: H. Moessenboeck, University of Linz";
    
    help.Controls.Add(lab);
    help.Show();
  }

  // Activated when the user clicks the Open button for text 1
  static void Open1(object sender, EventArgs e) {
    OpenFile(tb1, fileLab1);
  }
  
  // Activated when the user clicks the Open button for text 2
  static void Open2(object sender, EventArgs e) {
    OpenFile(tb2, fileLab2);
  }
  
  // This is the main comparison routine
  static void Compare(string t1, string t2, ref int pos1, ref int pos2) {
    while (pos1 < t1.Length && pos2 < t2.Length) {
      if (t1[pos1] == t2[pos2]) {pos1++; pos2++;}
      else if (skipWhiteSpace.Checked) {
        if (t1[pos1] <= ' ') pos1++;
        else if (t2[pos2] <= ' ') pos2++;
        else break;
      } else break;
    }
  }
  
  // Set the selection in tb to pos and scroll to this position
  static void Select(TextBox tb, int pos) {
    tb.Focus();
    tb.SelectionStart = pos;
    tb.SelectionLength = 1;
    tb.ScrollToCaret();
  }
  
  // Activated when the user clicks the Compare button
  static void Exec(object sender, EventArgs e) {
    Compare(tb1.Text, tb2.Text, ref selPos1, ref selPos2);
    Select(tb2, selPos2);
    Select(tb1, selPos1);
  }
  
  // Activated when a text box loses its focus. 
  // The current selection is remembered in selPos.
  static void Unfocus(object sender, EventArgs e) {
    TextBox tb = (TextBox)sender;
    if (tb.Name == "text1") selPos1 = tb.SelectionStart; else selPos2 = tb.SelectionStart;
  }
  
  // Uses a file dialog to open a file and show its contents in tb
  static void OpenFile(TextBox tb, Label lab) {
    OpenFileDialog dlg = new OpenFileDialog();
    if (dlg.ShowDialog() == DialogResult.OK) {
      FileStream s = (FileStream)dlg.OpenFile();
      StreamReader r = new StreamReader(s);
      tb.Text = r.ReadToEnd();
      s.Close();
      lab.Text = dlg.FileName;
    }
  }
  
  public static void Main(string[] arg) {
    string file1, file2;
    if (arg.Length > 0) file1 = arg[0]; else file1 = "";
    if (arg.Length > 1) file2 = arg[1]; else file2 = "";
    
    //----- set up controls for text 1
    
    fileLab1 = new Label();
    fileLab1.Location = new Point(10, 10);
    fileLab1.Size = new Size(300, 20);
    fileLab1.Name = "name1";
    fileLab1.Text = file1;
    
    tb1 = new TextBox();
    tb1.Name = "text1";
    tb1.Location = new Point(10, 30);
    tb1.Size = new Size(830, 300);
    tb1.Multiline = true;
    tb1.AcceptsReturn = true;
    tb1.AcceptsTab = true;
    tb1.HideSelection = false;
    tb1.ScrollBars = ScrollBars.Vertical;
    tb1.Leave += new EventHandler(Unfocus);
    if (file1 != "") {
      try {
        FileStream s = new FileStream(file1, FileMode.Open);
        StreamReader r = new StreamReader(s);
        tb1.Text = r.ReadToEnd();
        tb1.SelectionLength = 0;
        s.Close();
      } catch (FileNotFoundException) {
        Console.WriteLine("-- " + file1 + " not found"); return;
      }
    }
    
    Button helpBut = new Button();
    helpBut.Location = new Point(745, 7);
    helpBut.Size = new Size(20, 20);
    helpBut.Text = "?";
    helpBut.Font = new Font("Arial", 10, FontStyle.Bold);
    helpBut.Click += new EventHandler(ShowHelp);
    
    Button openBut1 = new Button();
    openBut1.Location = new Point(770, 7);
    openBut1.Size = new Size(70, 20);
    openBut1.Text = "Open";
    openBut1.Font = new Font("Arial", 10, FontStyle.Bold);
    openBut1.Click += new EventHandler(Open1);
    
    //----- set up controls for text 2
    
    fileLab2 = new Label();
    fileLab2.Location = new Point(10, 340);
    fileLab2.Size = new Size(300, 20);
    fileLab2.Name = "name2";
    fileLab2.Text = file2;
    
    tb2 = new TextBox();
    tb2.Name = "text2";
    tb2.Location = new Point(10, 360);
    tb2.Size = new Size(830, 300);
    tb2.Multiline = true;
    tb2.AcceptsReturn = true;
    tb2.AcceptsTab = true;
    tb2.HideSelection = false;
    tb2.ScrollBars = ScrollBars.Vertical;
    tb2.Leave += new EventHandler(Unfocus);
    if (file2 != "") {
      try {
        FileStream s = new FileStream(file2, FileMode.Open);
        StreamReader r = new StreamReader(s);
        tb2.Text = r.ReadToEnd();
        tb2.SelectionLength = 0;
        s.Close();
      } catch (FileNotFoundException) {
        Console.WriteLine("-- " + file2 + " not found"); return;
      }
    }
    
    Button openBut2 = new Button();
    openBut2.Location = new Point(770, 337);
    openBut2.Size = new Size(70, 20);
    openBut2.Text = "Open";
    openBut2.Font = new Font("Arial", 10, FontStyle.Bold);
    openBut2.Click += new EventHandler(Open2);
    
    //----- set up Compare button and check box to skip white space
    
    Button compBut = new Button();
    compBut.Location = new Point(10, 670);
    compBut.Size = new Size(100, 25);
    compBut.Text = "Compare";
    compBut.Font = new Font("Arial", 10, FontStyle.Bold);
    compBut.Click += new EventHandler(Exec);
    
    skipWhiteSpace = new CheckBox();
    skipWhiteSpace.Location = new Point(120, 670);
    skipWhiteSpace.Size = new Size(200, 25);
    skipWhiteSpace.Text = "Skip white space";
    skipWhiteSpace.Font = new Font("Arial", 10, FontStyle.Bold);

    //----- open main window and run the application
    
    f = new Form();
    f.DesktopLocation = new Point(0, 0);
    f.ClientSize = new Size(850, 700);
    f.Text = "Diff -- File Compare Utility";
    f.Controls.Add(fileLab1);
    f.Controls.Add(tb1);
    f.Controls.Add(helpBut);
    f.Controls.Add(openBut1);
    f.Controls.Add(fileLab2);
    f.Controls.Add(tb2);
    f.Controls.Add(openBut2);
    f.Controls.Add(compBut);
    f.Controls.Add(skipWhiteSpace);
    Application.Run(f);
  }
  
}