From 229242cf7c328b89b5aa65ed7a04e33c8b93b393 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Thu, 14 Jul 2022 15:19:53 -0700 Subject: Rename "samples" segment to "tools" This segment is a bit of a "miscellaneous section" in the WiX repo. As such it has been difficult to name. I originally eschewed the name "tools" because what is in the "wix" segment was once called "tools". However, now that wix.exe is firmly established as the entry point for WiX operations, I've become comfortable with its segment being named "wix". That meant "tools" was again available and "tools" better describes the content of this section. --- src/tools/Dtf/DDiff/TextFileDiffEngine.cs | 83 +++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/tools/Dtf/DDiff/TextFileDiffEngine.cs (limited to 'src/tools/Dtf/DDiff/TextFileDiffEngine.cs') diff --git a/src/tools/Dtf/DDiff/TextFileDiffEngine.cs b/src/tools/Dtf/DDiff/TextFileDiffEngine.cs new file mode 100644 index 00000000..beb5ea84 --- /dev/null +++ b/src/tools/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.Tools.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