/// <summary> /// Read the specified text file and return its contents as a block of element characters. /// </summary> /// <param name="psFile">Text file specification.</param> /// <returns>Entire file contents as a block of element characters.</returns> public override Block Read(string psFile) { StreamReader oSr; string sContents = string.Empty; ElementChar[] aElementChars = null; try { if (File.Exists(psFile)) { oSr = new StreamReader(psFile); sContents = oSr.ReadToEnd(); oSr.Close(); } else { sContents = string.Empty; } } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.ToString()); } aElementChars = new ElementChar[sContents.Length]; char[] aCharacters = sContents.ToCharArray(); for (int nCount = 0; nCount < aCharacters.Length; nCount++) { aElementChars[nCount] = new ElementChar(aCharacters[nCount]); } BlockOfCharacters oBlock = new BlockOfCharacters(aElementChars); return(oBlock); }
/// <summary> /// Return the contents of this block as a block of characters. /// </summary> /// <returns>Block of characters.</returns> public virtual BlockOfCharacters GetCharacters() { long nLength = 0; for (long nLine = 0; nLine < Elements.Length; nLine++) { ElementLine oLine = new ElementLine(Elements[nLine].Contents.ToString()); nLength += oLine.Contents.ToString().Length + 1; } long nIndex = 0; ElementChar[] aElements = new ElementChar[nLength]; for (long nLine = 0; nLine < Elements.Length; nLine++) { ElementLine oLine = new ElementLine(Elements[nLine].Contents.ToString()); ElementChar oElement = null; char[] aContents = oLine.Contents.ToString().ToCharArray(); for (int nPos = 0; nPos < aContents.Length; nPos++) { oElement = new ElementChar(aContents[nPos]); aElements[nIndex] = oElement; nIndex++; } oElement = new ElementChar('\n'); aElements[nIndex] = oElement; } BlockOfCharacters oBlock = new BlockOfCharacters(aElements); return(oBlock); }
/// <summary> /// Check if both blocks of elements are equal. /// </summary> /// <param name="poBlock1">Left hand side operand block to compare.</param> /// <param name="poBlock2">Right hand side operand block to compare.</param> /// <returns>True if equal, false otherwise.</returns> private static bool AreEqual(BlockOfCharacters poBlock1, BlockOfCharacters poBlock2) { bool bEqual = false; if (poBlock1.IsEmpty && !poBlock2.IsEmpty) { bEqual = false; } else if (!poBlock1.IsEmpty && poBlock2.IsEmpty) { bEqual = false; } else if (poBlock1.IsEmpty && poBlock2.IsEmpty) { bEqual = true; } else if (poBlock1.Length == 0 && poBlock2.Length == 0) { bEqual = true; } else if (poBlock1.Length != poBlock2.Length) { bEqual = false; } else if (poBlock1.Length == poBlock2.Length) { ElementChar[] aElements1 = (ElementChar[])poBlock1.Elements; ElementChar[] aElements2 = (ElementChar[])poBlock2.Elements; bEqual = true; for (long nCount = 0; nCount < aElements1.Length; nCount++) { if (aElements1[nCount] != aElements2[nCount]) { bEqual = false; break; } } } return(bEqual); }
/// <summary> /// Compare two blocks of characters against each other. /// </summary> /// <param name="poAlpha">First block of characters.</param> /// <param name="poBeta">Second block of characters.</param> public override void Compare(Block poAlpha1, Block poBeta1) { Sections.OnAddSection += new Sections.AddSectionHandler(Sections_OnAddSection); BlockOfCharacters poAlpha = (BlockOfCharacters)poAlpha1; BlockOfCharacters poBeta = (BlockOfCharacters)poBeta1; this.MinElements = this.MinChars; Analyse(1, poAlpha, 1, poBeta); //Add missing alpha segments. SegmentComparer oSegAComparer = new SegmentComparer(); oSegAComparer.ComparisonMethod = SegmentComparer.ComparisonType.AlphaPosition; AddMissingSegments("A1", oSegAComparer, mcSegA, poAlpha); //Add missing beta segments. SegmentComparer oSegBComparer = new SegmentComparer(); oSegBComparer.ComparisonMethod = SegmentComparer.ComparisonType.BetaPosition; AddMissingSegments("B1", oSegBComparer, mcSegB, poBeta); //Check if user has chosen to cancel run. if (Interrupt.Reason == "Cancel") { return; } //Merge into report. long nCountA = 0; long nCountB = 0; string sMatchIndent = string.Empty; string sInsertIndent = string.Empty; SegmentOfCharacters oSegA = new SegmentOfCharacters(); SegmentOfCharacters oSegB = new SegmentOfCharacters(); oSegA.Type = "X1"; oSegA.Data = new BlockOfCharacters(); oSegB.Type = "Z1"; oSegB.Data = new BlockOfCharacters(); SectionOfCharacters oSection = new SectionOfCharacters(); while (nCountA < mcSegA.Count || nCountB < mcSegB.Count) { if (nCountA < mcSegA.Count) { oSegA = (SegmentOfCharacters)mcSegA[(int)nCountA]; } if (nCountB < mcSegB.Count) { oSegB = (SegmentOfCharacters)mcSegB[(int)nCountB]; } if (oSegA.Type.Substring(0, 1) == "M" && oSegB.Type.Substring(0, 1) == "M") { if (!oSegA.Data.IsEmpty) { oSection = new SectionOfCharacters(); oSection.Type = oSegA.Type; oSection.Position = oSegA.PositionA; oSection.PositionA = oSegA.PositionA; oSection.PositionB = oSegB.PositionB; oSection.Size = oSegA.Size; oSection.Data = oSegA.Data; oSection.Indentation = sInsertIndent; Sections.Add(oSection); oSegA.Display(sInsertIndent); sMatchIndent = oSegA.Indent; sInsertIndent = oSegA.Indent; } nCountA++; nCountB++; } else if (oSegA.Type.Substring(0, 1) == "M" && oSegB.Type.Substring(0, 1) != "M") { if (!oSegB.Data.IsEmpty) { oSection = new SectionOfCharacters(); oSection.Type = oSegB.Type; oSection.Position = oSegB.PositionB; oSection.PositionA = oSegA.PositionA; oSection.PositionB = oSegB.PositionB; oSection.Size = oSegB.Size; oSection.Data = oSegB.Data; oSection.Indentation = sMatchIndent; Sections.Add(oSection); oSegB.Display(sMatchIndent); } nCountB++; } else if (oSegA.Type.Substring(0, 1) != "M" && oSegB.Type.Substring(0, 1) == "M") { if (!oSegA.Data.IsEmpty) { oSection = new SectionOfCharacters(); oSection.Type = oSegA.Type; oSection.Position = oSegA.PositionA; oSection.PositionA = oSegA.PositionA; oSection.PositionB = oSegB.PositionB; oSection.Size = oSegA.Size; oSection.Data = oSegA.Data; oSection.Indentation = sMatchIndent; Sections.Add(oSection); oSegA.Display(sMatchIndent); sInsertIndent = oSegA.Indent; } nCountA++; } else if (oSegA.Type.Substring(0, 1) != "M" && oSegB.Type.Substring(0, 1) != "M") { if (oSegA.PositionA < oSegB.PositionB) { if (!oSegA.Data.IsEmpty) { oSection = new SectionOfCharacters(); oSection.Type = oSegA.Type; oSection.Position = oSegA.PositionA; oSection.PositionA = oSegA.PositionA; oSection.PositionB = oSegB.PositionB; oSection.Size = oSegA.Size; oSection.Data = oSegA.Data; oSection.Indentation = sMatchIndent; Sections.Add(oSection); oSegA.Display(sMatchIndent); sInsertIndent = oSegA.Indent; } if (!oSegB.Data.IsEmpty) { oSection = new SectionOfCharacters(); oSection.Type = oSegB.Type; oSection.Position = oSegB.PositionB; oSection.PositionA = oSegA.PositionA; oSection.PositionB = oSegB.PositionB; oSection.Size = oSegB.Size; oSection.Data = oSegB.Data; oSection.Indentation = sMatchIndent; Sections.Add(oSection); oSegB.Display(sMatchIndent); } } else { if (!oSegB.Data.IsEmpty) { oSection = new SectionOfCharacters(); oSection.Type = oSegB.Type; oSection.Position = oSegB.PositionB; oSection.PositionA = oSegA.PositionA; oSection.PositionB = oSegB.PositionB; oSection.Size = oSegB.Size; oSection.Data = oSegB.Data; oSection.Indentation = sMatchIndent; Sections.Add(oSection); oSegB.Display(sMatchIndent); } if (!oSegA.Data.IsEmpty) { oSection = new SectionOfCharacters(); oSection.Type = oSegA.Type; oSection.Position = oSegA.PositionA; oSection.PositionA = oSegA.PositionA; oSection.PositionB = oSegB.PositionB; oSection.Size = oSegA.Size; oSection.Data = oSegA.Data; oSection.Indentation = sMatchIndent; Sections.Add(oSection); oSegA.Display(sMatchIndent); sInsertIndent = oSegA.Indent; } } nCountA++; nCountB++; } } }
/// <summary> /// Build comparisons list. /// </summary> private void BuildComparisons() { mcComparisons = new List <Comparison>(); for (int nCount = 0; nCount < Sections.Count; nCount++) { Section oSection = (Section)Sections[nCount]; if (oSection is SectionOfLines) { SectionOfLines oSectionOfLines = (SectionOfLines)oSection; BlockOfLines moBlock = (BlockOfLines)oSectionOfLines.Data; if (oSectionOfLines.Type.Substring(0, 1) == "M") { for (long nIndex = 0; nIndex < moBlock.Elements.Length; nIndex++) { long position = oSectionOfLines.PositionA + nIndex; string sData = moBlock.Elements[nIndex].ToDisplay(); Comparison oComparison = new Comparison(Comparison.GranularityEnum.Line, Comparison.MatchEnum.Match, position, sData, position, sData); mcComparisons.Add(oComparison); } } else if (oSectionOfLines.Type.Substring(0, 1) == "A") { for (long nIndex = 0; nIndex < moBlock.Elements.Length; nIndex++) { long position = oSectionOfLines.PositionA + nIndex; string sData = moBlock.Elements[nIndex].ToDisplay(); Comparison oComparison = new Comparison(Comparison.GranularityEnum.Line, Comparison.MatchEnum.InsertNew, position, sData, -1, string.Empty); mcComparisons.Add(oComparison); } } else if (oSectionOfLines.Type.Substring(0, 1) == "B") { for (long nIndex = 0; nIndex < moBlock.Elements.Length; nIndex++) { long position = oSectionOfLines.PositionB + nIndex; string sData = moBlock.Elements[nIndex].ToDisplay(); Comparison oComparison = new Comparison(Comparison.GranularityEnum.Line, Comparison.MatchEnum.DeleteOld, -1, string.Empty, position, sData); mcComparisons.Add(oComparison); } } } if (oSection is SectionOfCharacters) { SectionOfCharacters oSectionOfCharacters = (SectionOfCharacters)oSection; BlockOfCharacters moBlock = (BlockOfCharacters)oSectionOfCharacters.Data; if (oSectionOfCharacters.Type.Substring(0, 1) == "M") { for (long nIndex = 0; nIndex < moBlock.Elements.Length; nIndex++) { long position = oSectionOfCharacters.PositionA + nIndex; string sData = moBlock.Elements[nIndex].ToDisplay(); Comparison oComparison = new Comparison(Comparison.GranularityEnum.Character, Comparison.MatchEnum.Match, position, sData, position, sData); mcComparisons.Add(oComparison); } } else if (oSectionOfCharacters.Type.Substring(0, 1) == "A") { for (long nIndex = 0; nIndex < moBlock.Elements.Length; nIndex++) { long position = oSectionOfCharacters.PositionA + nIndex; string sData = moBlock.Elements[nIndex].ToDisplay(); Comparison oComparison = new Comparison(Comparison.GranularityEnum.Character, Comparison.MatchEnum.InsertNew, position, sData, -1, string.Empty); mcComparisons.Add(oComparison); } } else if (oSectionOfCharacters.Type.Substring(0, 1) == "B") { for (long nIndex = 0; nIndex < moBlock.Elements.Length; nIndex++) { long position = oSectionOfCharacters.PositionB + nIndex; string sData = moBlock.Elements[nIndex].ToDisplay(); Comparison oComparison = new Comparison(Comparison.GranularityEnum.Character, Comparison.MatchEnum.DeleteOld, -1, string.Empty, position, sData); mcComparisons.Add(oComparison); } } } } }
/// <summary> /// Compare two blocks of lines against each other. /// </summary> /// <param name="poAlpha">First block of lines.</param> /// <param name="poBeta">Second block of lines.</param> public override void Compare(Block poAlpha1, Block poBeta1) { Sections.OnAddSection += new Sections.AddSectionHandler(Sections_OnAddSection); BlockOfLines poAlpha = (BlockOfLines)poAlpha1; BlockOfLines poBeta = (BlockOfLines)poBeta1; BlockOfCharacters oAlphaChars = null; BlockOfCharacters oBetaChars = null; if (poAlpha.ContentsLength >= poBeta.ContentsLength) { mcSegA.ProgressOn = Segments.ProgressOnEnum.Alpha; mcSegB.ProgressOn = Segments.ProgressOnEnum.Alpha; SignalBeginCompare(poAlpha.ContentsLength); } else { mcSegA.ProgressOn = Segments.ProgressOnEnum.Beta; mcSegB.ProgressOn = Segments.ProgressOnEnum.Beta; SignalBeginCompare(poBeta.ContentsLength); } this.MinElements = this.MinLines; Analyse(1, poAlpha, 1, poBeta); //Add missing alpha segments. SegmentComparer oSegAComparer = new SegmentComparer(); oSegAComparer.ComparisonMethod = SegmentComparer.ComparisonType.AlphaPosition; AddMissingSegments("A1", oSegAComparer, mcSegA, poAlpha); //Add missing beta segments. SegmentComparer oSegBComparer = new SegmentComparer(); oSegBComparer.ComparisonMethod = SegmentComparer.ComparisonType.BetaPosition; AddMissingSegments("B1", oSegBComparer, mcSegB, poBeta); SignalEndOfCompare(); //Check if user has chosen to cancel run. if (Interrupt.Reason == "Cancel") { return; } //Report. if (poAlpha.ContentsLength >= poBeta.ContentsLength) { Sections.ProgressOn = Sections.ProgressOnEnum.Alpha; SignalBeginReport(poAlpha.ContentsLength); } else { Sections.ProgressOn = Sections.ProgressOnEnum.Beta; SignalBeginReport(poBeta.ContentsLength); } //Merge into report. long nCountA = 0; long nCountB = 0; SegmentOfLines oSegA = new SegmentOfLines(); SegmentOfLines oSegB = new SegmentOfLines(); oSegA.Type = "X1"; oSegA.Data = new BlockOfLines(); oSegB.Type = "Z1"; oSegB.Data = new BlockOfLines(); SectionOfLines oSection = new SectionOfLines(); while (nCountA < mcSegA.Count || nCountB < mcSegB.Count) { if (nCountA < mcSegA.Count) { oSegA = (SegmentOfLines)mcSegA[(int)nCountA]; } if (nCountB < mcSegB.Count) { oSegB = (SegmentOfLines)mcSegB[(int)nCountB]; } if (oSegA.Type.Substring(0, 1) == "M" && oSegB.Type.Substring(0, 1) == "M") { if (!oSegA.Data.IsEmpty) { oSegA.Display(); oSection = new SectionOfLines(); oSection.Type = oSegA.Type; oSection.Position = oSegA.PositionA; oSection.PositionA = oSegA.PositionA; oSection.PositionB = oSegB.PositionB; oSection.Size = oSegA.Size; oSection.Data = oSegA.Data; Sections.Add(oSection); } nCountA++; nCountB++; } else if (oSegA.Type.Substring(0, 1) == "M" && oSegB.Type.Substring(0, 1) != "M") { if (!oSegB.Data.IsEmpty) { oSegB.Display(); oSection = new SectionOfLines(); oSection.Type = oSegB.Type; oSection.Position = oSegB.PositionB; oSection.PositionA = oSegA.PositionA; oSection.PositionB = oSegB.PositionB; oSection.Size = oSegB.Size; oSection.Data = oSegB.Data; Sections.Add(oSection); } nCountB++; } else if (oSegA.Type.Substring(0, 1) != "M" && oSegB.Type.Substring(0, 1) == "M") { if (!oSegA.Data.IsEmpty) { oSegA.Display(); oSection = new SectionOfLines(); oSection.Type = oSegA.Type; oSection.Position = oSegA.PositionA; oSection.PositionA = oSegA.PositionA; oSection.PositionB = oSegB.PositionB; oSection.Size = oSegA.Size; oSection.Data = oSegA.Data; Sections.Add(oSection); } nCountA++; } else if (oSegA.Type.Substring(0, 1) != "M" && oSegB.Type.Substring(0, 1) != "M") { if (this.CompleteLines) { if (oSegA.PositionA < oSegB.PositionB) { if (!oSegA.Data.IsEmpty) { oSegA.Display(); oSection = new SectionOfLines(); oSection.Type = oSegA.Type; oSection.Position = oSegA.PositionA; oSection.PositionA = oSegA.PositionA; oSection.PositionB = oSegB.PositionB; oSection.Size = oSegA.Size; oSection.Data = oSegA.Data; Sections.Add(oSection); } if (!oSegB.Data.IsEmpty) { oSegB.Display(); oSection = new SectionOfLines(); oSection.Type = oSegB.Type; oSection.Position = oSegB.PositionB; oSection.PositionA = oSegA.PositionA; oSection.PositionB = oSegB.PositionB; oSection.Size = oSegB.Size; oSection.Data = oSegB.Data; Sections.Add(oSection); } } else { if (!oSegB.Data.IsEmpty) { oSegB.Display(); oSection = new SectionOfLines(); oSection.Type = oSegB.Type; oSection.Position = oSegB.PositionB; oSection.PositionA = oSegA.PositionA; oSection.PositionB = oSegB.PositionB; oSection.Size = oSegB.Size; oSection.Data = oSegB.Data; Sections.Add(oSection); } if (!oSegA.Data.IsEmpty) { oSegA.Display(); oSection = new SectionOfLines(); oSection.Type = oSegA.Type; oSection.Position = oSegA.PositionA; oSection.PositionA = oSegA.PositionA; oSection.PositionB = oSegB.PositionB; oSection.Size = oSegA.Size; oSection.Data = oSegA.Data; Sections.Add(oSection); } } nCountA++; nCountB++; } else { if (!oSegA.Data.IsEmpty && !oSegB.Data.IsEmpty) { if (oSegA.Data.Length < this.SubLineMatchLimit && oSegB.Data.Length < this.SubLineMatchLimit) { //Look for sub-line matches. oAlphaChars = oSegA.Data.GetCharacters(); oBetaChars = oSegB.Data.GetCharacters(); CompareCharacters oCompareCharacters = new CompareCharacters(); oCompareCharacters.MinElements = this.MinChars; oCompareCharacters.MinChars = this.MinChars; oCompareCharacters.MinLines = this.MinLines; oCompareCharacters.LimitElements = this.LimitCharacters; oCompareCharacters.LimitCharacters = this.LimitCharacters; oCompareCharacters.LimitLines = this.LimitLines; oCompareCharacters.SubLineMatchLimit = this.SubLineMatchLimit; oCompareCharacters.CompleteLines = this.CompleteLines; oCompareCharacters.Sections = this.Sections; oCompareCharacters.Interrupt = this.Interrupt; oCompareCharacters.Compare(oAlphaChars, oBetaChars); } else { //Don't look for sub-line matches if the mismatched blocks are too large, //since this has a large performance impact for little likely benefit. if (oSegA.PositionA < oSegB.PositionB) { if (!oSegA.Data.IsEmpty) { oSegA.Display(); oSection = new SectionOfLines(); oSection.Type = oSegA.Type; oSection.Position = oSegA.PositionA; oSection.PositionA = oSegA.PositionA; oSection.PositionB = oSegB.PositionB; oSection.Size = oSegA.Size; oSection.Data = oSegA.Data; Sections.Add(oSection); } if (!oSegB.Data.IsEmpty) { oSegB.Display(); oSection = new SectionOfLines(); oSection.Type = oSegB.Type; oSection.Position = oSegB.PositionB; oSection.PositionA = oSegA.PositionA; oSection.PositionB = oSegB.PositionB; oSection.Size = oSegB.Size; oSection.Data = oSegB.Data; Sections.Add(oSection); } } else { if (!oSegB.Data.IsEmpty) { oSegB.Display(); oSection = new SectionOfLines(); oSection.Type = oSegB.Type; oSection.Position = oSegB.PositionB; oSection.PositionA = oSegA.PositionA; oSection.PositionB = oSegB.PositionB; oSection.Size = oSegB.Size; oSection.Data = oSegB.Data; Sections.Add(oSection); } if (!oSegA.Data.IsEmpty) { oSegA.Display(); oSection = new SectionOfLines(); oSection.Type = oSegA.Type; oSection.Position = oSegA.PositionA; oSection.PositionA = oSegA.PositionA; oSection.PositionB = oSegB.PositionB; oSection.Size = oSegA.Size; oSection.Data = oSegA.Data; Sections.Add(oSection); } } nCountA++; nCountB++; } } nCountA++; nCountB++; } } } SignalEndOfReport(); }
/// <summary> /// Construct a new block to contain the character element array contained in the specified block. /// </summary> /// <param name="poBlock">Block of character elements.</param> public BlockOfCharacters(BlockOfCharacters poBlock) { maElements = (ElementChar[])poBlock.Elements; }