From 3f583916719eeef598d10a5d4e14ef14f008243b Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Tue, 11 May 2021 07:36:37 -0700 Subject: Merge Dtf --- src/samples/Dtf/DDiff/TextFileDiffEngine.cs | 83 +++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/samples/Dtf/DDiff/TextFileDiffEngine.cs (limited to 'src/samples/Dtf/DDiff/TextFileDiffEngine.cs') diff --git a/src/samples/Dtf/DDiff/TextFileDiffEngine.cs b/src/samples/Dtf/DDiff/TextFileDiffEngine.cs new file mode 100644 index 00000000..22567023 --- /dev/null +++ b/src/samples/Dtf/DDiff/TextFileDiffEngine.cs @@ -0,0 +1,83 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. + +using System; +using System.IO; +using System.Diagnostics; + +namespace WixToolset.Dtf.Samples.DDiff +{ + public class TextFileDiffEngine : IDiffEngine + { + public TextFileDiffEngine() + { + } + + private bool IsTextFile(string file) + { + // Guess whether this is a text file by reading the first few bytes and checking for non-ascii chars. + + bool isText = true; + FileStream stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read); + byte[] buf = new byte[256]; + int count = stream.Read(buf, 0, buf.Length); + for(int i = 0; i < count; i++) + { + if((buf[i] & 0x80) != 0) + { + isText = false; + break; + } + } + stream.Close(); + return isText; + } + + public float GetDiffQuality(string diffInput1, string diffInput2, string[] options, IDiffEngineFactory diffFactory) + { + if(diffInput1 != null && File.Exists(diffInput1) && + diffInput2 != null && File.Exists(diffInput2) && + (IsTextFile(diffInput1) && IsTextFile(diffInput2))) + { + return .70f; + } + else + { + return 0; + } + } + + public bool GetDiff(string diffInput1, string diffInput2, string[] options, TextWriter diffOutput, string linePrefix, IDiffEngineFactory diffFactory) + { + try + { + bool difference = false; + ProcessStartInfo psi = new ProcessStartInfo("diff.exe"); + psi.Arguments = String.Format("\"{0}\" \"{1}\"", diffInput1, diffInput2); + psi.WorkingDirectory = null; + psi.UseShellExecute = false; + psi.WindowStyle = ProcessWindowStyle.Hidden; + psi.RedirectStandardOutput = true; + Process proc = Process.Start(psi); + + string line; + while((line = proc.StandardOutput.ReadLine()) != null) + { + diffOutput.WriteLine("{0}{1}", linePrefix, line); + difference = true; + } + + proc.WaitForExit(); + return difference; + } + catch(System.ComponentModel.Win32Exception) // If diff.exe is not found, just compare the bytes + { + return new FileDiffEngine().GetDiff(diffInput1, diffInput2, options, diffOutput, linePrefix, diffFactory); + } + } + + public IDiffEngine Clone() + { + return new TextFileDiffEngine(); + } + } +} -- cgit v1.2.3-55-g6feb