private static void GetTextLines(string textA, string textB, TextBinaryDiffArgs args, out IList <string> a, out IList <string> b, IDiffProgress progress) { a = null; b = null; CompareType compareType = args.CompareType; bool isAuto = compareType == CompareType.Auto; if (compareType == CompareType.Xml || isAuto) { a = TryGetXmlLines(DiffUtility.GetXmlTextLinesFromXml, "the left side text", textA, !isAuto, args, progress); // If A failed to parse with Auto, then there's no reason to try B. if (a != null) { b = TryGetXmlLines(DiffUtility.GetXmlTextLinesFromXml, "the right side text", textB, !isAuto, args, progress); } // If we get here and the compare type was XML, then both // inputs parsed correctly, and both lists should be non-null. // If we get here and the compare type was Auto, then one // or both lists may be null, so we'll fallthrough to the text // handling logic. } if (a == null || b == null) { a = DiffUtility.GetStringTextLines(textA, progress); b = DiffUtility.GetStringTextLines(textB, progress); } }
private static IList <string> TryGetXmlLines( Func <string, bool, IDiffProgress, IList <string> > converter, string name, string input, bool throwOnError, TextBinaryDiffArgs args, IDiffProgress progress) { IList <string> result = null; try { result = converter(input, args.IgnoreXmlWhitespace, progress); } catch (XmlException ex) { if (throwOnError) { StringBuilder sb = new StringBuilder("An XML comparison was attempted, but an XML exception occurred while parsing "); sb.Append(name).AppendLine(".").AppendLine(); sb.AppendLine("Exception Message:").Append(ex.Message); throw new XmlException(sb.ToString(), ex); } } return(result); }
public IList <string> TryGetXmlLines( Func <string, bool, IDiffProgress, IList <string> > converter, bool throwOnError, TextBinaryDiffArgs args, IDiffProgress progress) { IList <string> result = null; try { if (FileExists) { result = converter(FileNamePath, args.IgnoreXmlWhitespace, progress); _typeOfFile = FileType.Xml; } } catch (XmlException ex) { if (throwOnError) { StringBuilder sb = new StringBuilder("An XML comparison was attempted, but an XML exception occurred while parsing "); sb.Append("'" + FileNamePath + "'").AppendLine(".").AppendLine(); sb.AppendLine("Exception Message:").Append(ex.Message); throw new XmlException(sb.ToString(), ex); } } return(result); }
/// <summary> /// Sets up the left and right diff viewmodels which contain line by line information /// with reference to textual contents and whether it should be handled as insertion, /// deletion, change, or no change when comparing left side (ViewA) with right side (ViewB). /// </summary> /// <param name="listA"></param> /// <param name="listB"></param> /// <param name="script"></param> /// <param name="args"></param> /// <param name="changeDiffIgnoreCase"></param> /// <param name="changeDiffIgnoreWhiteSpace"></param> /// <param name="changeDiffTreatAsBinaryLines"></param> private void SetData(IList <string> listA, IList <string> listB, EditScript script, TextBinaryDiffArgs args, bool changeDiffIgnoreCase, bool changeDiffIgnoreWhiteSpace, bool changeDiffTreatAsBinaryLines) { _Args = args; ChangeDiffOptions changeDiffOptions = ChangeDiffOptions.None; if (changeDiffTreatAsBinaryLines) { changeDiffOptions |= ChangeDiffOptions.IgnoreBinaryPrefix; } else { if (changeDiffIgnoreCase) { changeDiffOptions |= ChangeDiffOptions.IgnoreCase; } if (changeDiffIgnoreWhiteSpace) { changeDiffOptions |= ChangeDiffOptions.IgnoreWhitespace; } } _ViewA.ChangeDiffOptions = changeDiffOptions; _ViewB.ChangeDiffOptions = changeDiffOptions; _ViewLineDiff.ChangeDiffOptions = changeDiffOptions; var factory = new LinesFactory(); factory.SetData(listA, listB, script); _ViewA.SetData(args.A, factory.LinesA, factory.TextA, args.SpacesPerTab); _ViewB.SetData(args.B, factory.LinesB, factory.TextB, args.SpacesPerTab); NotifyPropertyChanged(() => this.IsDiffDataAvailable); Debug.Assert(this._ViewA.LineCount == this._ViewB.LineCount, "Both DiffView's LineCounts must be the same"); // Sets the similarity value (0% - 100%) between 2 things shown in toolbar this.Similarity_Text = string.Format("{0:P}", script.Similarity); // Show left and right file name labels over each ViewA and ViewB bool showNames = !string.IsNullOrEmpty(args.A) || !string.IsNullOrEmpty(args.B); this.edtLeft_Right_Visible = showNames; if (showNames) { this.edtLeft_Text = args.A; this.edtRight_Text = args.B; } this.currentDiffLine = -1; this.UpdateViewLineDiff(args.SpacesPerTab); // Update 2 line diff ViewLineDiff }
/// <summary> /// Updates the diff view with the supplied information. /// </summary> /// <param name="args"></param> /// <param name="r"></param> public void ShowDifferences(TextBinaryDiffArgs args, ProcessTextDiff r) { string captionA = string.Empty; string captionB = string.Empty; if (args.DiffType == DiffType.File) { try { this.StatusText = string.Format("{0} : {1}", Path.GetFileName(args.A), Path.GetFileName(args.B)); } catch { // System.IO throws exception on invalid file name } } else { this.StatusText = "Text Comparison"; } SetData(r.ListA, r.ListB, r.Script, args, r.IgnoreCase, r.IgnoreTextWhitespace, r.IsBinaryCompare); // Update the stats this.NumberOfLines = (uint)r.ListA.Count; this.MaxNumberOfLines = (uint)_ViewA.LineCount; int iDeletes = 0, iChanges = 0, iInserts = 0; foreach (var item in r.Script) { switch (item.EditType) { case AehnlichLib.Enums.EditType.Delete: iDeletes++; break; case AehnlichLib.Enums.EditType.Insert: iInserts++; break; case AehnlichLib.Enums.EditType.Change: iChanges++; break; case AehnlichLib.Enums.EditType.None: default: break; } } this.CountInserts = iInserts; this.CountDeletes = iDeletes; this.CountChanges = iChanges; _Args = args; }
private static void GetFileLines(string fileNameA, string fileNameB, out IList <string> a, out IList <string> b, out int leadingCharactersToIgnore, TextBinaryDiffArgs args, IDiffProgress progress) { a = null; b = null; leadingCharactersToIgnore = 0; CompareType compareType = args.CompareType; if (compareType == CompareType.Binary || (args.IsAuto && (DiffUtility.IsBinaryFile(fileNameA) || DiffUtility.IsBinaryFile(fileNameB)))) { using (FileStream fileA = File.OpenRead(fileNameA)) using (FileStream fileB = File.OpenRead(fileNameB)) { BinaryDiff diff = new BinaryDiff { FootprintLength = args.BinaryFootprintLength }; AddCopyCollection addCopy = diff.Execute(fileA, fileB); BinaryDiffLines lines = new BinaryDiffLines(fileA, addCopy, args.BinaryFootprintLength); a = lines.BaseLines; b = lines.VersionLines; leadingCharactersToIgnore = BinaryDiffLines.PrefixLength; } } if (compareType == CompareType.Xml || (args.IsAuto && (a == null || b == null))) { a = TryGetXmlLines(DiffUtility.GetXmlTextLines, fileNameA, fileNameA, !args.IsAuto, args, progress); // If A failed to parse with Auto, then there's no reason to try B. if (a != null) { b = TryGetXmlLines(DiffUtility.GetXmlTextLines, fileNameB, fileNameB, !args.IsAuto, args, progress); } // If we get here and the compare type was XML, then both // inputs parsed correctly, and both lists should be non-null. // If we get here and the compare type was Auto, then one // or both lists may be null, so we'll fallthrough to the text // handling logic. } if (a == null || b == null) { a = DiffUtility.GetFileTextLines(fileNameA, progress); b = DiffUtility.GetFileTextLines(fileNameB, progress); } }
internal TextBinaryDiffArgs GetTextBinaryDiffSetup(string A, string B) { var setup = new TextBinaryDiffArgs(A, B, DiffType.File); setup.CompareType = this.OptionCompareType; setup.IgnoreCase = this.IgnoreCase; setup.IgnoreTextWhitespace = this.IgnoreTextWhitespace; setup.IgnoreXmlWhitespace = this.IgnoreXmlWhitespace; setup.ShowChangeAsDeleteInsert = this.ShowChangeAsDeleteInsert; setup.SpacesPerTab = (int)this.SpacesPerTabValue; return(setup); }
private DiffBinaryTextResults GetTextLines(DiffBinaryTextResults src , TextBinaryDiffArgs args , IDiffProgress progress) { IList <string> a = null, b = null; bool isAuto = args.CompareType == CompareType.Auto; if (args.CompareType == CompareType.Xml || isAuto) { a = TryGetXmlText(DiffUtility.GetXmlTextLinesFromXml, "the left side text", src.A.TextContent, !isAuto, args, progress); // If A failed to parse with Auto, then there's no reason to try B. if (a != null) { b = TryGetXmlText(DiffUtility.GetXmlTextLinesFromXml, "the right side text", src.B.TextContent, !isAuto, args, progress); } // If we get here and the compare type was XML, then both // inputs parsed correctly, and both lists should be non-null. // If we get here and the compare type was Auto, then one // or both lists may be null, so we'll fallthrough to the text // handling logic. } if (a == null || b == null) { a = DiffUtility.GetStringTextLines(src.A.TextContent, progress); b = DiffUtility.GetStringTextLines(src.B.TextContent, progress); } else { src.IsComparedAs = CompareType.Xml; } src.A.Lines = a; src.B.Lines = b; return(src); }
private DiffBinaryTextResults GetTextLines(string textA, string textB , TextBinaryDiffArgs args , IDiffProgress progress) { FileContentInfo af = null, bf = null; bool isAuto = args.CompareType == CompareType.Auto; if (args.CompareType == CompareType.Xml || isAuto) { af = TryGetXmlText(DiffUtility.GetXmlTextFromXml, "the left side text", textA, !isAuto, args, progress); // If A failed to parse with Auto, then there's no reason to try B. if (af != null) { bf = TryGetXmlText(DiffUtility.GetXmlTextFromXml, "the right side text", textB, !isAuto, args, progress); } // If we get here and the compare type was XML, then both // inputs parsed correctly, and both lists should be non-null. // If we get here and the compare type was Auto, then one // or both lists may be null, so we'll fallthrough to the text // handling logic. } if (af == null || bf == null) { af = new FileContentInfo(); bf = new FileContentInfo(); af.Lines = DiffUtility.GetStringTextLines(textA, progress); bf.Lines = DiffUtility.GetStringTextLines(textB, progress); } af.TextContent = textA; bf.TextContent = textB; DiffBinaryTextResults result = new DiffBinaryTextResults(CompareType.Text, af, bf); return(result); }
/// <summary> /// Compares two files using binary or text comparison methods. /// </summary> /// <param name="fileA"></param> /// <param name="fileB"></param> /// <param name="a"></param> /// <param name="b"></param> /// <param name="args"></param> /// <param name="progress"></param> private DiffBinaryTextResults GetFileLines(IFileInfo fileA , IFileInfo fileB , TextBinaryDiffArgs args , IDiffProgress progress) { // Nothing to compare if both files do not exist if (fileA.FileExists == false && fileB.FileExists == false) { return(new DiffBinaryTextResults(CompareType.Text, new FileContentInfo(), new FileContentInfo())); } if (args.CompareType == CompareType.Binary || (args.IsAuto && fileA.Is == FileType.Binary || fileB.Is == FileType.Binary)) { return(GetBinaryFileLines(fileA, fileB, args, progress)); } FileContentInfo af = null, bf = null; if (fileA.FileExists) { af = AsyncPump.Run(() => FileEx.GetFileTextAsync(fileA.FullName)); } else { af = new FileContentInfo(); } if (fileB.FileExists) { bf = AsyncPump.Run(() => FileEx.GetFileTextAsync(fileB.FullName)); } else { bf = new FileContentInfo(); } return(new DiffBinaryTextResults(CompareType.Text, af, bf)); }
/// <summary> /// Class constructor /// </summary> public ProcessTextDiff(TextBinaryDiffArgs args) : this() { _Args = args; }
private static void GetBinaryFileLines(FileCompInfo fileA, FileCompInfo fileB, TextBinaryDiffArgs args, IDiffProgress progress, out IList <string> a, out IList <string> b, out int leadingCharactersToIgnore) { a = new List <string>(); b = new List <string>(); leadingCharactersToIgnore = BinaryDiffLines.PrefixLength; // Neither left nor right file exist or cannot be accessed if (fileA.FileExists == false && fileB.FileExists == false) { return; } Stream fileStreamA = null, fileStreamB = null; try { // Open the file or an internal empty stream to compare against if (fileA.FileExists) { fileStreamA = File.OpenRead(fileA.FileNamePath); } else { fileStreamA = Assembly.GetExecutingAssembly().GetManifestResourceStream("AehnlichLib.Binaries.Resources.NonExistingFile.bin"); } // Open the file or an internal empty stream to compare against if (fileB.FileExists) { fileStreamB = File.OpenRead(fileB.FileNamePath); } else { fileStreamB = Assembly.GetExecutingAssembly().GetManifestResourceStream("AehnlichLib.Binaries.Resources.NonExistingFile.bin"); } BinaryDiff diff = new BinaryDiff { FootprintLength = args.BinaryFootprintLength }; AddCopyCollection addCopy = diff.Execute(fileStreamA, fileStreamB, progress); BinaryDiffLines lines = new BinaryDiffLines(fileStreamA, addCopy, args.BinaryFootprintLength); a = lines.BaseLines; b = lines.VersionLines; leadingCharactersToIgnore = BinaryDiffLines.PrefixLength; } finally { if (fileStreamA != null) { fileStreamA.Dispose(); } if (fileStreamB != null) { fileStreamB.Dispose(); } } return; }
private static void GetFileLines(FileCompInfo fileA, FileCompInfo fileB, out IList <string> a, out IList <string> b, out int leadingCharactersToIgnore, TextBinaryDiffArgs args, IDiffProgress progress) { a = null; b = null; leadingCharactersToIgnore = 0; CompareType compareType = args.CompareType; // Nothing to compare if both files do not exist if (fileA.FileExists == false && fileB.FileExists == false) { a = new List <string>(); b = new List <string>(); return; } if (compareType == CompareType.Binary || (args.IsAuto && fileA.Is == FileType.Binary || fileB.Is == FileType.Binary)) { GetBinaryFileLines(fileA, fileB, args, progress, out a, out b, out leadingCharactersToIgnore); return; } if (compareType == CompareType.Xml || (args.IsAuto && (a == null || b == null))) { a = fileA.TryGetXmlLines(DiffUtility.GetXmlTextLines, !args.IsAuto, args, progress); // If A failed to parse with Auto, then there's no reason to try B. if (fileA.Is == FileType.Xml) { b = fileB.TryGetXmlLines(DiffUtility.GetXmlTextLines, !args.IsAuto, args, progress); } // If we get here and the compare type was XML, then both // inputs parsed correctly, and both lists should be non-null. // If we get here and the compare type was Auto, then one // or both lists may be null, so we'll fallthrough to the text // handling logic. } if (fileA.Is != FileType.Xml || fileB.Is != FileType.Xml) { if (fileA.FileExists) { a = DiffUtility.GetFileTextLines(fileA.FileNamePath, progress); } else { a = new List <string>(); } if (fileB.FileExists) { b = DiffUtility.GetFileTextLines(fileB.FileNamePath, progress); } else { b = new List <string>(); } } }
/// <summary> /// Get Binary file contents rendered as text lines with line number marker at beginning of each line. /// </summary> /// <param name="fileA"></param> /// <param name="fileB"></param> /// <param name="args"></param> /// <param name="progress"></param> /// <param name="a"></param> /// <param name="b"></param> /// <param name="leadingCharactersToIgnore">Leading number of characters to ignore for diff in each line. /// This space is used in binary diff to display 8 digit line number and 4 digit space.</param> private DiffBinaryTextResults GetBinaryFileLines(IFileInfo fileA, IFileInfo fileB , TextBinaryDiffArgs args , IDiffProgress progress) { // Neither left nor right file exist or cannot be accessed if (fileA.FileExists == false && fileB.FileExists == false) { return(new DiffBinaryTextResults(CompareType.Binary, new FileContentInfo(), new FileContentInfo())); } Stream fileStreamA = null, fileStreamB = null; IList <string> a = null, b = null; try { // Open the file or an internal empty stream to compare against if (fileA.FileExists) { fileStreamA = File.OpenRead(fileA.FullName); } else { fileStreamA = Assembly.GetExecutingAssembly().GetManifestResourceStream("AehnlichLib.Binaries.Resources.NonExistingFile.bin"); } // Open the file or an internal empty stream to compare against if (fileB.FileExists) { fileStreamB = File.OpenRead(fileB.FullName); } else { fileStreamB = Assembly.GetExecutingAssembly().GetManifestResourceStream("AehnlichLib.Binaries.Resources.NonExistingFile.bin"); } BinaryDiff diff = new BinaryDiff { FootprintLength = args.BinaryFootprintLength }; AddCopyCollection addCopy = diff.Execute(fileStreamA, fileStreamB, progress); BinaryDiffLines lines = new BinaryDiffLines(fileStreamA, addCopy, args.BinaryFootprintLength); a = lines.BaseLines; b = lines.VersionLines; } finally { if (fileStreamA != null) { fileStreamA.Dispose(); } if (fileStreamB != null) { fileStreamB.Dispose(); } } FileContentInfo af = new FileContentInfo(true, a); FileContentInfo bf = new FileContentInfo(true, b); return(new DiffBinaryTextResults(CompareType.Binary, af, bf, BinaryDiffLines.PrefixLength)); }
/// <summary> /// Sets up the left and right diff viewmodels which contain line by line information /// with reference to textual contents and whether it should be handled as insertion, /// deletion, change, or no change when comparing left side (ViewA) with right side (ViewB). /// </summary> /// <param name="args"></param> /// <param name="r"></param> private void SetData(TextBinaryDiffArgs args, ProcessTextDiff r) { _Args = args; ChangeDiffOptions changeDiffOptions = ChangeDiffOptions.None; if (r.IsComparedAs == CompareType.Binary) { changeDiffOptions |= ChangeDiffOptions.IgnoreBinaryPrefix; } else { if (r.IgnoreCase) { changeDiffOptions |= ChangeDiffOptions.IgnoreCase; } if (r.IgnoreTextWhitespace) { changeDiffOptions |= ChangeDiffOptions.IgnoreWhitespace; } } _ViewA.ChangeDiffOptions = changeDiffOptions; _ViewB.ChangeDiffOptions = changeDiffOptions; _ViewLineDiff.ChangeDiffOptions = changeDiffOptions; var factory = new LinesFactory(); factory.SetData(r.ListA, r.ListB, r.Script); _ViewA.SetData(args.A, factory.LinesA, factory.TextA , r.TextEncodingA, r.TextOriginalA, r.TextIsDirtyA, args.SpacesPerTab, r.IsComparedAs); _ViewB.SetData(args.B, factory.LinesB, factory.TextB , r.TextEncodingB, r.TextOriginalB, r.TextIsDirtyB, args.SpacesPerTab, r.IsComparedAs); NotifyPropertyChanged(() => this.IsDiffDataAvailable); this.IsComparedAs = r.IsComparedAs; Debug.Assert(IsComparedAs != CompareType.Auto, "This should always be specific (eg: Xml, Bunary, or Text)"); Debug.Assert(this._ViewA.LineCount == this._ViewB.LineCount, "Both DiffView's LineCounts must be the same"); // Sets the similarity value (0% - 100%) between 2 things shown in toolbar this.Similarity_Text = string.Format("{0:P}", r.Script.Similarity); // Show left and right file name labels over each ViewA and ViewB bool showNames = !string.IsNullOrEmpty(args.A) || !string.IsNullOrEmpty(args.B); this.edtLeft_Right_Visible = showNames; if (showNames) { this.edtLeft_Text = args.A; this.edtRight_Text = args.B; } this.currentDiffLine = -1; this.UpdateViewLineDiff(args.SpacesPerTab); // Update 2 line diff ViewLineDiff }