| 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 | |
| 6 | namespace WixToolset.Dtf.Tools.DDiff |
| 7 | { |
| 8 | public class FileDiffEngine : IDiffEngine |
| 9 | { |
| 10 | public FileDiffEngine() |
| 11 | { |
| 12 | } |
| 13 | |
| 14 | public virtual float GetDiffQuality(string diffInput1, string diffInput2, string[] options, IDiffEngineFactory diffFactory) |
| 15 | { |
| 16 | if(diffInput1 != null && File.Exists(diffInput1) && |
| 17 | diffInput2 != null && File.Exists(diffInput2)) |
| 18 | { |
| 19 | return .10f; |
| 20 | } |
| 21 | else |
| 22 | { |
| 23 | return 0; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | public bool GetDiff(string diffInput1, string diffInput2, string[] options, TextWriter diffOutput, string linePrefix, IDiffEngineFactory diffFactory) |
| 28 | { |
| 29 | bool difference = false; |
| 30 | |
| 31 | FileInfo file1 = new FileInfo(diffInput1); |
| 32 | FileInfo file2 = new FileInfo(diffInput2); |
| 33 | |
| 34 | if(file1.Length != file2.Length) |
| 35 | { |
| 36 | diffOutput.WriteLine("{0}File size: {1} -> {2}", linePrefix, file1.Length, file2.Length); |
| 37 | difference = true; |
| 38 | } |
| 39 | else |
| 40 | { |
| 41 | FileStream stream1 = file1.Open(FileMode.Open, FileAccess.Read, FileShare.Read); |
| 42 | FileStream stream2 = file2.Open(FileMode.Open, FileAccess.Read, FileShare.Read); |
| 43 | |
| 44 | byte[] buf1 = new byte[512]; |
| 45 | byte[] buf2 = new byte[512]; |
| 46 | |
| 47 | while(!difference) |
| 48 | { |
| 49 | int count1 = stream1.Read(buf1, 0, buf1.Length); |
| 50 | int count2 = stream2.Read(buf2, 0, buf2.Length); |
| 51 | |
| 52 | for(int i = 0; i < count1; i++) |
| 53 | { |
| 54 | if(i == count2 || buf1[i] != buf2[i]) |
| 55 | { |
| 56 | difference = true; |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | if(count1 < buf1.Length) // EOF |
| 61 | { |
| 62 | break; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | stream1.Close(); |
| 67 | stream2.Close(); |
| 68 | |
| 69 | if(difference) |
| 70 | { |
| 71 | diffOutput.WriteLine("{0}Files differ.", linePrefix); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return difference; |
| 76 | } |
| 77 | |
| 78 | public virtual IDiffEngine Clone() |
| 79 | { |
| 80 | return new FileDiffEngine(); |
| 81 | } |
| 82 | } |
| 83 | } |