/// <summary> /// returns the number of differences /// </summary> /// <param name="sFile"></param> /// <param name="dFile"></param> /// <returns></returns> private int TextDiff(string sFile, string dFile) { this.Cursor = Cursors.WaitCursor; int nChanges = 0; DiffList_TextFile sLF = null; DiffList_TextFile dLF = null; try { sLF = new DiffList_TextFile(sFile); dLF = new DiffList_TextFile(dFile); } catch (Exception ex) { this.Cursor = Cursors.Default; CoreUtilities.NewMessage.Show(ex.Message, "File Error"); return(-1); } try { double time = 0; DiffEngine de = new DiffEngine(); time = de.ProcessDiff(sLF, dLF, _level); ArrayList rep = de.DiffReport(); nChanges = rep.Count; //Results dlg = new Results(sLF, dLF, rep, time); this.DoCompare(sLF, dLF, rep, time); //dlg.ShowDialog(); //dlg.Dispose(); } catch (Exception ex) { this.Cursor = Cursors.Default; string tmp = string.Format("{0}{1}{1}***STACK***{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace); CoreUtilities.NewMessage.Show(tmp, "Compare Error"); return(-1); } this.Cursor = Cursors.Default; return(nChanges); }
private void DoCompare(DiffList_TextFile source, DiffList_TextFile destination, ArrayList DiffLines, double seconds) { // label1.Text = string.Format("Results: {0} secs.", seconds.ToString("#0.00")); lvSource.Items.Clear(); lvDestination.Items.Clear(); int nChanges = 0; ListViewItem lviS; ListViewItem lviD; int cnt = 1; int i; foreach (DiffResultSpan drs in DiffLines) { switch (drs.Status) { case DiffResultSpanStatus.DeleteSource: for (i = 0; i < drs.Length; i++) { lviS = new ListViewItem(cnt.ToString("00000")); lviD = new ListViewItem(cnt.ToString("00000")); lviS.BackColor = Color.Red; lviS.SubItems.Add(((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line); lviD.BackColor = Color.LightGray; lviD.SubItems.Add(""); lvSource.Items.Add(lviS); lvDestination.Items.Add(lviD); cnt++; nChanges++; } break; case DiffResultSpanStatus.NoChange: for (i = 0; i < drs.Length; i++) { lviS = new ListViewItem(cnt.ToString("00000")); lviD = new ListViewItem(cnt.ToString("00000")); lviS.BackColor = Color.White; lviS.SubItems.Add(((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line); lviD.BackColor = Color.White; lviD.SubItems.Add(((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line); lvSource.Items.Add(lviS); lvDestination.Items.Add(lviD); cnt++; } break; case DiffResultSpanStatus.AddDestination: for (i = 0; i < drs.Length; i++) { lviS = new ListViewItem(cnt.ToString("00000")); lviD = new ListViewItem(cnt.ToString("00000")); lviS.BackColor = Color.LightGray; lviS.SubItems.Add(""); lviD.BackColor = Color.LightGreen; lviD.SubItems.Add(((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line); lvSource.Items.Add(lviS); lvDestination.Items.Add(lviD); cnt++; nChanges++; } break; case DiffResultSpanStatus.Replace: for (i = 0; i < drs.Length; i++) { lviS = new ListViewItem(cnt.ToString("00000")); lviD = new ListViewItem(cnt.ToString("00000")); lviS.BackColor = Color.Red; lviS.SubItems.Add(((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line); lviD.BackColor = Color.LightGreen; lviD.SubItems.Add(((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line); lvSource.Items.Add(lviS); lvDestination.Items.Add(lviD); cnt++; nChanges++; } break; } } statusLabel.Text = String.Format("Changes {0}", nChanges.ToString()); labelWordsNew.Text = String.Format("Words: {0}", destination.words.ToString()); labelWordsOld.Text = String.Format("Words: {0}", source.words.ToString()); }
/// <summary> /// now we highlight any changes between the actual textboxes /// </summary> private void UpdateComparisonBox() { // now we highlight any changes between the actual textboxes DiffList_TextFile sLF = new DiffList_TextFile(textBoxSource.Text, true); DiffList_TextFile dLF = new DiffList_TextFile(textBoxDest.Text, true); double time = 0; DiffEngine de = new DiffEngine(); time = de.ProcessDiff(sLF, dLF, _level); ArrayList rep = de.DiffReport(); int nChanges = rep.Count; //Results dlg = new Results(sLF, dLF, rep, time); // this.DoCompare(sLF, dLF, rep, time); // textBoxSource.Text = textBoxSource.Text + " >>>> " + nChanges.ToString(); // textBoxSource.Text = ""; // textBoxDest.Text = ""; int i = 0; foreach (DiffResultSpan drs in rep) { switch (drs.Status) { case DiffResultSpanStatus.DeleteSource: for (i = 0; i < drs.Length; i++) { string sSource = ((TextLine)sLF.GetByIndex(drs.SourceIndex + i)).Line; textBoxSource.Find(sSource); textBoxSource.SelectionBackColor = Color.Red; // lviD.BackColor = Color.LightGray; // cnt++; nChanges++; } break; case DiffResultSpanStatus.NoChange: for (i = 0; i < drs.Length; i++) { /* lviS = new ListViewItem(cnt.ToString("00000")); * lviD = new ListViewItem(cnt.ToString("00000")); * lviS.BackColor = Color.White; * lviS.SubItems.Add(((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line); * lviD.BackColor = Color.White; * lviD.SubItems.Add(((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line); * * lvSource.Items.Add(lviS); * lvDestination.Items.Add(lviD); */// cnt++; } break; case DiffResultSpanStatus.AddDestination: for (i = 0; i < drs.Length; i++) { string sDest = ((TextLine)dLF.GetByIndex(drs.DestIndex + i)).Line; textBoxDest.Find(sDest); textBoxDest.SelectionBackColor = Color.LightGreen; // cnt++; nChanges++; } break; case DiffResultSpanStatus.Replace: for (i = 0; i < drs.Length; i++) { string sSource = (((TextLine)sLF.GetByIndex(drs.SourceIndex + i)).Line); textBoxSource.Find(sSource); textBoxSource.SelectionBackColor = Color.LightGreen; string sDest = (((TextLine)dLF.GetByIndex(drs.DestIndex + i)).Line); textBoxDest.Find(sDest); textBoxDest.SelectionBackColor = Color.Red; // cnt++; nChanges++; } break; } } }
public Results(DiffList_TextFile source, DiffList_TextFile destination, ArrayList DiffLines, double seconds) { InitializeComponent(); this.Text = string.Format("Results: {0} secs.", seconds.ToString("#0.00")); ListViewItem lviS; ListViewItem lviD; int cnt = 1; int i; foreach (DiffResultSpan drs in DiffLines) { switch (drs.Status) { case DiffResultSpanStatus.DeleteSource: for (i = 0; i < drs.Length; i++) { lviS = new ListViewItem(cnt.ToString("00000")); lviD = new ListViewItem(cnt.ToString("00000")); lviS.BackColor = Color.Red; lviS.SubItems.Add(((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line); lviD.BackColor = Color.LightGray; lviD.SubItems.Add(""); lvSource.Items.Add(lviS); lvDestination.Items.Add(lviD); cnt++; } break; case DiffResultSpanStatus.NoChange: for (i = 0; i < drs.Length; i++) { lviS = new ListViewItem(cnt.ToString("00000")); lviD = new ListViewItem(cnt.ToString("00000")); lviS.BackColor = Color.White; lviS.SubItems.Add(((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line); lviD.BackColor = Color.White; lviD.SubItems.Add(((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line); lvSource.Items.Add(lviS); lvDestination.Items.Add(lviD); cnt++; } break; case DiffResultSpanStatus.AddDestination: for (i = 0; i < drs.Length; i++) { lviS = new ListViewItem(cnt.ToString("00000")); lviD = new ListViewItem(cnt.ToString("00000")); lviS.BackColor = Color.LightGray; lviS.SubItems.Add(""); lviD.BackColor = Color.LightGreen; lviD.SubItems.Add(((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line); lvSource.Items.Add(lviS); lvDestination.Items.Add(lviD); cnt++; } break; case DiffResultSpanStatus.Replace: for (i = 0; i < drs.Length; i++) { lviS = new ListViewItem(cnt.ToString("00000")); lviD = new ListViewItem(cnt.ToString("00000")); lviS.BackColor = Color.Red; lviS.SubItems.Add(((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line); lviD.BackColor = Color.LightGreen; lviD.SubItems.Add(((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line); lvSource.Items.Add(lviS); lvDestination.Items.Add(lviD); cnt++; } break; } } }