diff options
Diffstat (limited to 'src/samples/Dtf/DDiff/TextFileDiffEngine.cs')
-rw-r--r-- | src/samples/Dtf/DDiff/TextFileDiffEngine.cs | 83 |
1 files changed, 83 insertions, 0 deletions
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 @@ | |||
1 | // 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. | ||
2 | |||
3 | using System; | ||
4 | using System.IO; | ||
5 | using System.Diagnostics; | ||
6 | |||
7 | namespace WixToolset.Dtf.Samples.DDiff | ||
8 | { | ||
9 | public class TextFileDiffEngine : IDiffEngine | ||
10 | { | ||
11 | public TextFileDiffEngine() | ||
12 | { | ||
13 | } | ||
14 | |||
15 | private bool IsTextFile(string file) | ||
16 | { | ||
17 | // Guess whether this is a text file by reading the first few bytes and checking for non-ascii chars. | ||
18 | |||
19 | bool isText = true; | ||
20 | FileStream stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read); | ||
21 | byte[] buf = new byte[256]; | ||
22 | int count = stream.Read(buf, 0, buf.Length); | ||
23 | for(int i = 0; i < count; i++) | ||
24 | { | ||
25 | if((buf[i] & 0x80) != 0) | ||
26 | { | ||
27 | isText = false; | ||
28 | break; | ||
29 | } | ||
30 | } | ||
31 | stream.Close(); | ||
32 | return isText; | ||
33 | } | ||
34 | |||
35 | public float GetDiffQuality(string diffInput1, string diffInput2, string[] options, IDiffEngineFactory diffFactory) | ||
36 | { | ||
37 | if(diffInput1 != null && File.Exists(diffInput1) && | ||
38 | diffInput2 != null && File.Exists(diffInput2) && | ||
39 | (IsTextFile(diffInput1) && IsTextFile(diffInput2))) | ||
40 | { | ||
41 | return .70f; | ||
42 | } | ||
43 | else | ||
44 | { | ||
45 | return 0; | ||
46 | } | ||
47 | } | ||
48 | |||
49 | public bool GetDiff(string diffInput1, string diffInput2, string[] options, TextWriter diffOutput, string linePrefix, IDiffEngineFactory diffFactory) | ||
50 | { | ||
51 | try | ||
52 | { | ||
53 | bool difference = false; | ||
54 | ProcessStartInfo psi = new ProcessStartInfo("diff.exe"); | ||
55 | psi.Arguments = String.Format("\"{0}\" \"{1}\"", diffInput1, diffInput2); | ||
56 | psi.WorkingDirectory = null; | ||
57 | psi.UseShellExecute = false; | ||
58 | psi.WindowStyle = ProcessWindowStyle.Hidden; | ||
59 | psi.RedirectStandardOutput = true; | ||
60 | Process proc = Process.Start(psi); | ||
61 | |||
62 | string line; | ||
63 | while((line = proc.StandardOutput.ReadLine()) != null) | ||
64 | { | ||
65 | diffOutput.WriteLine("{0}{1}", linePrefix, line); | ||
66 | difference = true; | ||
67 | } | ||
68 | |||
69 | proc.WaitForExit(); | ||
70 | return difference; | ||
71 | } | ||
72 | catch(System.ComponentModel.Win32Exception) // If diff.exe is not found, just compare the bytes | ||
73 | { | ||
74 | return new FileDiffEngine().GetDiff(diffInput1, diffInput2, options, diffOutput, linePrefix, diffFactory); | ||
75 | } | ||
76 | } | ||
77 | |||
78 | public IDiffEngine Clone() | ||
79 | { | ||
80 | return new TextFileDiffEngine(); | ||
81 | } | ||
82 | } | ||
83 | } | ||