private void DiffChars(string line1, string line2) { if (line1 == line2) return; var del = new DiffEngine(line1, line2, true, false); ArrayList res = del.ProcessDiff(DiffEngineLevel.SlowPerfect); if (SemanticMerge) { del.CleanupSemantic(res); } else { del.MergeShortSpan(res); } foreach (DiffResultSpan drs in res) { switch (drs.Status) { case DiffResultSpanStatus.DeleteSource: SetRangeStyle(tb1, drs.SourceIndex, drs.SourceIndex + drs.Length, true); break; case DiffResultSpanStatus.AddDestination: SetRangeStyle(tb2, drs.DestIndex, drs.DestIndex + drs.Length, false); break; case DiffResultSpanStatus.Replace: SetRangeStyle(tb1, drs.SourceIndex, drs.SourceIndex + drs.Length, true ); SetRangeStyle(tb2, drs.DestIndex , drs.DestIndex + drs.Length, false); break; } } }
/// <summary> /// Runs the diff process for the specified files. /// </summary> public void CompareDifference(Object sender, DoWorkEventArgs e) { this.sourceListView.Items.Clear(); this.destinationListView.Items.Clear(); this.sourceListView.BeginUpdate(); this.destinationListView.BeginUpdate(); try { String source = ((String[])e.Argument)[0]; String destination = ((String[])e.Argument)[1]; Int32 count = 1; ListViewItem lviS, lviD; TextFile file1 = new TextFile(source); TextFile file2 = new TextFile(destination); DiffEngine de = new DiffEngine(); de.ProcessDiff(file1, file2, DiffEngineLevel.Medium); ArrayList lines = de.DiffReport(); Int32 counter = 0, total = lines.Count; foreach (DiffResultSpan drs in lines) { switch (drs.Status) { case DiffResultSpanStatus.DeleteSource: for (Int32 i = 0; i < drs.Length; i++) { lviS = new ListViewItem(count.ToString("00000")); lviD = new ListViewItem(count.ToString("00000")); lviS.Font = lviD.Font = new Font("Courier New", 8.25F); lviS.BackColor = Color.Red; lviS.SubItems.Add(((TextLine)file1.GetByIndex(drs.SourceIndex + i)).Line); lviD.BackColor = Color.LightGray; lviD.SubItems.Add(""); this.sourceListView.Items.Add(lviS); this.destinationListView.Items.Add(lviD); count++; } break; case DiffResultSpanStatus.NoChange: for (Int32 i = 0; i < drs.Length; i++) { lviS = new ListViewItem(count.ToString("00000")); lviD = new ListViewItem(count.ToString("00000")); lviS.Font = lviD.Font = new Font("Courier New", 8.25F); lviS.BackColor = Color.White; lviS.SubItems.Add(((TextLine)file1.GetByIndex(drs.SourceIndex + i)).Line); lviD.BackColor = Color.White; lviD.SubItems.Add(((TextLine)file2.GetByIndex(drs.DestIndex + i)).Line); this.sourceListView.Items.Add(lviS); this.destinationListView.Items.Add(lviD); count++; } break; case DiffResultSpanStatus.AddDestination: for (Int32 i = 0; i < drs.Length; i++) { lviS = new ListViewItem(count.ToString("00000")); lviD = new ListViewItem(count.ToString("00000")); lviS.Font = lviD.Font = new Font("Courier New", 8.25F); lviS.BackColor = Color.LightGray; lviS.SubItems.Add(""); lviD.BackColor = Color.LightGreen; lviD.SubItems.Add(((TextLine)file2.GetByIndex(drs.DestIndex + i)).Line); this.sourceListView.Items.Add(lviS); this.destinationListView.Items.Add(lviD); count++; } break; case DiffResultSpanStatus.Replace: for (Int32 i = 0; i < drs.Length; i++) { lviS = new ListViewItem(count.ToString("00000")); lviD = new ListViewItem(count.ToString("00000")); lviS.Font = lviD.Font = new Font("Courier New", 8.25F); lviS.BackColor = Color.Red; lviS.SubItems.Add(((TextLine)file1.GetByIndex(drs.SourceIndex + i)).Line); lviD.BackColor = Color.LightGreen; lviD.SubItems.Add(((TextLine)file2.GetByIndex(drs.DestIndex + i)).Line); this.sourceListView.Items.Add(lviS); this.destinationListView.Items.Add(lviD); count++; } break; } counter++; Int32 percent = (100 * counter) / total; this.backgroundWorker.ReportProgress(percent); } e.Result = true; } catch (Exception ex) { MessageBox.Show(ex.ToString()); e.Result = false; } this.destinationListView.EndUpdate(); this.sourceListView.EndUpdate(); }
public void Setup() { var source = Path.Combine(this.sampleDataPath, "SonarSource.txt"); var destination = Path.Combine(this.sampleDataPath, "LocalSource.txt"); this.sLf = new DiffListTextFile(source); this.dLf = new DiffListTextFile(destination); this.de = new DiffEngine(); this.de.ProcessDiff(this.sLf, this.dLf, DiffEngineLevel.SlowPerfect); this.rep = this.de.DiffReport(); }
public void Compare(bool findNextDiff = true) { RangesGreen.Clear(); RangesRed .Clear(); int l_changed = 0; int l_added = 0; int l_deleted = 0; int l_nochang = 0; double change = 0; try { DiffEngine de = new DiffEngine(Text1, Text2, false, TrimEndWhenDiff); ArrayList rep = de.ProcessDiff(DiffEngineLevel.Medium); Color red = Color.FromArgb(100, Color.Red ); Color green = Color.FromArgb( 80, Color.Green); RedLines1 .Clear(); GreenLines2.Clear(); lock (tb1.Lines) { lock (tb2.Lines) { tb1.Clear(); tb2.Clear(); int i; LineCount1 = 0; LineCount2 = 0; foreach (DiffResultSpan drs in rep) { switch (drs.Status) { case DiffResultSpanStatus.DeleteSource: for (i = 0; i < drs.Length; i++) { string line1 = de.GetSrcLineByIndex(drs.SourceIndex + i); tb1.AddLine(line1, red, ref LineCount1); tb2.AddUnavaliableLine(); RedLines1.Add(drs.SourceIndex + i); DiffChars(line1, ""); if (line1.Length == 0) SetRangeStyle(tb1, 0, 0, true); l_deleted++; } break; case DiffResultSpanStatus.NoChange: for (i = 0; i < drs.Length; i++) { tb1.AddLine(de.GetSrcLineByIndex(drs.SourceIndex + i), Color.Transparent, ref LineCount1); tb2.AddLine(de.GetDstLineByIndex(drs.DestIndex + i), Color.Transparent, ref LineCount2); l_nochang++; } break; case DiffResultSpanStatus.AddDestination: for (i = 0; i < drs.Length; i++) { string line2 = de.GetDstLineByIndex(drs.DestIndex + i); tb1.AddUnavaliableLine(); tb2.AddLine(line2, green, ref LineCount2); GreenLines2.Add(drs.DestIndex + i); DiffChars("", line2); if (line2.Length == 0) SetRangeStyle(tb2, 0, 0, false); l_added++; } break; case DiffResultSpanStatus.Replace: for (i = 0; i < drs.Length; i++) { string line1 = de.GetSrcLineByIndex(drs.SourceIndex + i); string line2 = de.GetDstLineByIndex(drs.DestIndex + i); tb1.AddLine(line1, red , ref LineCount1); tb2.AddLine(line2, green, ref LineCount2); RedLines1 .Add(drs.SourceIndex + i); GreenLines2.Add(drs.DestIndex + i); DiffChars(line1, line2); l_changed++; } break; } } } } tb1.NeedRecalc(true); tb2.NeedRecalc(true); tb1.OnSyntaxHighlight(new TextChangedEventArgs(tb1.Range)); tb2.OnSyntaxHighlight(new TextChangedEventArgs(tb2.Range)); } catch (Exception ex) { HMS.Err(ex.Message); //HMS.LogError(ex); } // Statinstics string s_change = "0%"; if (l_nochang != 0) { change = (double)(LineCount2 - l_nochang) / l_nochang * 100; if (change > 100) change = 100; s_change = change < 1 && change > 0 ? " < 1%" : (Math.Round(change).ToString() + "%"); } else if (l_added!=0 || l_deleted!=0) { s_change = "100%"; } toolStripStatusLabelStat.Text = string.Format(" Изменённых строк: {0} Удалённых строк: {1} Добавлено строк: {2} Изменений: {3}", l_changed, l_deleted, l_added, s_change); if (NoSelectEmptyAreas) { for (int i = RangesRed.Count - 1; i >= 0; i--) { if (RangesRed[i].Text.Trim().Length == 0) RangesRed.RemoveAt(i); } for (int i = RangesGreen.Count - 1; i >= 0; i--) { if (RangesGreen[i].Text.Trim().Length == 0) RangesGreen.RemoveAt(i); } } Invalidate(); if (findNextDiff) FindNextDiff(); }
private void treeView_AfterSelect(object sender, TreeViewEventArgs e) { this.lvSource.Items.Clear(); this.lvDestination.Items.Clear(); ChangeInfo info = (ChangeInfo)this.treeView.SelectedNode.Tag; DiffList_TextString source = new DiffList_TextString(info.Original != null ? info.Original.Documentation : String.Empty); DiffList_TextString destination = new DiffList_TextString(info.Change != null ? info.Change.Documentation : String.Empty); DiffEngine de = new DiffEngine(); de.ProcessDiff(source, destination, DiffEngineLevel.SlowPerfect); ArrayList DiffLines = de.DiffReport(); 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; } } this.radioButtonOriginal.Checked = !this.treeView.SelectedNode.Checked; this.radioButtonChange.Checked = this.treeView.SelectedNode.Checked; }
/// <summary> /// Use DifferenceEngine to calculate Differences /// </summary> /// <param name="sFile"></param> /// <param name="dFile"></param> private ArrayList TextDiff(DiffList_TextFile sLF, DiffList_TextFile dLF) { double time = 0; DiffEngine de = new DiffEngine(); time = de.ProcessDiff(sLF, dLF, DifferenceEngine.DiffEngineLevel.SlowPerfect); ArrayList rep = de.DiffReport(); return rep; }
/// <summary> /// The get source diff from strings. /// </summary> /// <param name="source"> /// The source. /// </param> /// <param name="destination"> /// The destination. /// </param> /// <param name="level"> /// The level. /// </param> /// <returns> /// The <see cref="ArrayList"/>. /// </returns> public static ArrayList GetSourceDiffFromStrings(string source, string destination, DiffEngineLevel level = DiffEngineLevel.SlowPerfect) { var sourceseparator = "\n"; if (source.Contains("\r\n")) { sourceseparator = "\r\n"; } var destinationsepartor = "\n"; if (destination.Contains("\r\n")) { destinationsepartor = "\r\n"; } var sLf = new DiffListTextFile(source, sourceseparator); var dLf = new DiffListTextFile(destination, destinationsepartor); var de = new DiffEngine(); de.ProcessDiff(sLf, dLf, level); return de.DiffReport(); }
/// <summary> /// The get difference report. /// </summary> /// <param name="filePath"> /// The file Path. /// </param> /// <param name="sourceInServer"> /// The in server source. /// </param> /// <param name="displayWindow"> /// The display Window. /// </param> /// <returns> /// The <see cref="ArrayList"/>. /// </returns> public static ArrayList GetDifferenceReport(string filePath, string sourceInServer, bool displayWindow) { var sLf = new DiffListTextFile(filePath); var dLf = new DiffListTextFile(sourceInServer, "\r\n"); var de = new DiffEngine(); de.ProcessDiff(dLf, sLf, DiffEngineLevel.SlowPerfect); if (!displayWindow) { return de.DiffReport(); } using (var win = new Results(sLf, dLf, de.DiffReport(), 0)) { win.ShowDialog(); } return de.DiffReport(); }
private void BinaryDiff(string sFile, string dFile) { this.Cursor = Cursors.WaitCursor; DiffList_BinaryFile sLF = null; DiffList_BinaryFile dLF = null; try { sLF = new DiffList_BinaryFile(sFile); dLF = new DiffList_BinaryFile(dFile); } catch (Exception ex) { this.Cursor = Cursors.Default; MessageBox.Show(ex.Message,"File Error"); return; } try { double time = 0; DiffEngine de = new DiffEngine(); time = de.ProcessDiff(sLF,dLF,_level); ArrayList rep = de.DiffReport(); BinaryResults dlg = new BinaryResults(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); MessageBox.Show(tmp,"Compare Error"); return; } this.Cursor = Cursors.Default; }
private void TextDiff(string sFile, string dFile) { _level = DiffEngineLevel.SlowPerfect; this.Cursor = Cursors.WaitCursor; 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; MessageBox.Show(ex.Message, "File Error"); return; } try { double time = 0; DiffEngine de = new DiffEngine(); time = de.ProcessDiff(sLF, dLF, _level); ArrayList rep = de.DiffReport(); Results dlg = new Results(sLF, dLF, rep, time); // dlg.MdiParent = this.MdiParent; dlg.Text = Path.GetFileName(sFile); 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); MessageBox.Show(tmp, "Compare Error"); return; } this.Cursor = Cursors.Default; }
private void CompareFrom_Click(object sender, EventArgs e) { int typeIndex = this.FileTypeComBox.SelectedIndex; if (typeIndex != 3 && typeIndex != 4) { MessageBox.Show("请选择要对比文件类型", "警告!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } string parentFileName = PathManager.GetParentFolderName(typeIndex); string compareFileName = PathManager.GetCompareFileName(typeIndex); string sFile = this.OldServiceWsdCmb.Text.Trim(); string dFile = this.NewServiceWsdCmb.Text.Trim(); if (sFile.Contains("{0}")) { sFile = string.Format(this.OldServiceWsdCmb.Text, this.ChangeSetTextBox.Text, this.CompanyComBox.Text, parentFileName, compareFileName); } if (dFile.Contains("{0}")) { dFile = string.Format(this.NewServiceWsdCmb.Text, parentFileName, parentFileName, compareFileName); } if (!PathManager.ValidFileExist(sFile) || !PathManager.ValidFileExist(dFile)) return; this.Cursor = Cursors.WaitCursor; 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; MessageBox.Show(ex.Message, "File Error"); return; } try { double time = 0; DiffEngine de = new DiffEngine(); time = de.ProcessDiff(sLF, dLF, DiffEngineLevel.FastImperfect); ArrayList rep = de.DiffReport(); Results dlg = new Results(sLF, dLF, rep); dlg.ShowDialog(); dlg.Dispose(); //this.BindData(sLF, dLF, rep); } catch (Exception ex) { this.Cursor = Cursors.Default; string tmp = string.Format("{0}{1}{1}***STACK***{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace); MessageBox.Show(tmp, "Compare Error"); return; } this.Cursor = Cursors.Default; }