aboutsummaryrefslogtreecommitdiff
path: root/src/tools/Dtf/DDiff/IDiffEngine.cs
blob: 918e42e6ef13840ddf9d5a45456f2fce0e0a2181 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// 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.Collections;

namespace WixToolset.Dtf.Tools.DDiff
{
	public interface IDiffEngine
	{
		float GetDiffQuality(string diffInput1, string diffInput2, string[] options, IDiffEngineFactory diffFactory);

		bool GetDiff(string diffInput1, string diffInput2, string[] options, TextWriter diffOutput, string linePrefix, IDiffEngineFactory diffFactory);

		IDiffEngine Clone();
	}

	public interface IDiffEngineFactory
	{
		IDiffEngine GetDiffEngine(string diffInput1, string diffInput2, string[] options);
	}

	public class BestQualityDiffEngineFactory : IDiffEngineFactory
	{
		public virtual IDiffEngine GetDiffEngine(string diffInput1, string diffInput2, string[] options)
		{
			float bestDiffQuality = 0;
			IDiffEngine bestDiffEngine = null;

			foreach(IDiffEngine diffEngine in diffEngines)
			{
				float diffQuality = diffEngine.GetDiffQuality(diffInput1, diffInput2, options, this);
				if(diffQuality > bestDiffQuality)
				{
					bestDiffQuality = diffQuality;
					bestDiffEngine = diffEngine;
				}
			}
			return (bestDiffEngine != null ? bestDiffEngine.Clone() : null);
		}

		public BestQualityDiffEngineFactory() : this(null) { }
		public BestQualityDiffEngineFactory(IDiffEngine[] diffEngines)
		{
			this.diffEngines = (diffEngines != null ? new ArrayList(diffEngines) : new ArrayList());
		}

		protected IList diffEngines;

		public virtual void Add(IDiffEngine diffEngine)
		{
			diffEngines.Add(diffEngine);
		}

		public virtual void Remove(IDiffEngine diffEngine)
		{
			diffEngines.Remove(diffEngine);
		}

		public IList DiffEngines
		{
			get
			{
				return ArrayList.ReadOnly(diffEngines);
			}
		}
	}
}