Esempio n. 1
0
        public IDiffProgress ProcessDiff(IDiffProgress progress)
        {
            try
            {
                DiffBinaryTextResults result = null;

                if (_Args.DiffType == DiffType.File)
                {
                    var fileA = new FileCompInfo(_Args.A);
                    var fileB = new FileCompInfo(_Args.B);

                    result = GetFileLines(fileA, fileB, _Args, progress);
                }
                else
                {
                    // DiffType in-memory text content
                    result = GetTextLines(TextContentA, TextContentB, _Args, progress);
                }

                if (result.IsComparedAs == CompareType.Text || result.IsComparedAs == CompareType.Xml)
                {
                    // Render Text or XML lines from in-memory text content
                    if (_Args.DiffType == DiffType.File)
                    {
                        result = GetTextLines(result, _Args, progress);
                    }

                    // Assumption: Binary cannot be edit and RAW data cannot be stored in string
                    //             Therefore, binary lines are rendered only once directly from file
                    TextOriginalA = result.A.TextContent;
                    TextContentA  = result.A.TextContent;
                    TextEncodingA = result.A.TextEncoding;

                    TextOriginalB = result.B.TextContent;
                    TextContentB  = result.B.TextContent;
                    TextEncodingB = result.B.TextEncoding;
                }

                ListA = result.A.Lines;
                ListB = result.B.Lines;

                IsComparedAs         = result.IsComparedAs;
                IgnoreCase           = result.IgnoreCase;
                IgnoreTextWhitespace = result.IgnoreTextWhitespace;

                TextDiff diff = new TextDiff(_Args.HashType, IgnoreCase, IgnoreTextWhitespace,
                                             result.LeadingCharactersToIgnore, !_Args.ShowChangeAsDeleteInsert);

                Script = diff.Execute(ListA, ListB, progress);

                progress.ResultData = this;

                return(progress);
            }
            finally
            {
            }
        }
Esempio n. 2
0
        public IDiffProgress ProcessDiff(IDiffProgress progress)
        {
            progress.ShowIndeterminatedProgress();

            try
            {
                IList <string> a, b;
                int            leadingCharactersToIgnore = 0;

                if (_Args.DiffType == DiffType.File)
                {
                    var fileA = new FileCompInfo(_Args.A);
                    var fileB = new FileCompInfo(_Args.B);

                    GetFileLines(fileA, fileB, out a, out b, out leadingCharactersToIgnore, _Args, progress);
                }
                else
                {
                    GetTextLines(_Args.A, _Args.B, _Args, out a, out b, progress);
                }

                IsBinaryCompare      = leadingCharactersToIgnore > 0;
                IgnoreCase           = IsBinaryCompare ? false : _Args.IgnoreCase;
                IgnoreTextWhitespace = IsBinaryCompare ? false : _Args.IgnoreTextWhitespace;
                TextDiff diff = new TextDiff(_Args.HashType, IgnoreCase, IgnoreTextWhitespace, leadingCharactersToIgnore, !_Args.ShowChangeAsDeleteInsert);

                ListA  = a;
                ListB  = b;
                Script = diff.Execute(a, b, progress);

                progress.ResultData = this;

                return(progress);
            }
            finally
            {
                progress.ProgressDisplayOff();
            }
        }
Esempio n. 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fileA"></param>
        /// <param name="fileB"></param>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="args"></param>
        /// <param name="progress"></param>
        private DiffBinaryTextResults GetFileLines(FileCompInfo fileA
                                                   , FileCompInfo fileB
                                                   , TextBinaryDiffArgs args
                                                   , IDiffProgress progress)
        {
            // Nothing to compare if both files do not exist
            if (fileA.FileExists == false && fileB.FileExists == false)
            {
                return(new DiffBinaryTextResults(CompareType.Text, new FileContentInfo(), new FileContentInfo()));
            }

            if (args.CompareType == CompareType.Binary ||
                (args.IsAuto && fileA.Is == FileType.Binary || fileB.Is == FileType.Binary))
            {
                return(GetBinaryFileLines(fileA, fileB, args, progress));
            }

            FileContentInfo af = null, bf = null;

            if (fileA.FileExists)
            {
                af = AsyncPump.Run(() => FileEx.GetFileTextAsync(fileA.FileNamePath));
            }
            else
            {
                af = new FileContentInfo();
            }

            if (fileB.FileExists)
            {
                bf = AsyncPump.Run(() => FileEx.GetFileTextAsync(fileB.FileNamePath));
            }
            else
            {
                bf = new FileContentInfo();
            }

            return(new DiffBinaryTextResults(CompareType.Text, af, bf));
        }
Esempio n. 4
0
        private static void GetBinaryFileLines(FileCompInfo fileA, FileCompInfo fileB,
                                               TextBinaryDiffArgs args,
                                               IDiffProgress progress,
                                               out IList <string> a, out IList <string> b,
                                               out int leadingCharactersToIgnore)
        {
            a = new List <string>();
            b = new List <string>();
            leadingCharactersToIgnore = BinaryDiffLines.PrefixLength;

            // Neither left nor right file exist or cannot be accessed
            if (fileA.FileExists == false && fileB.FileExists == false)
            {
                return;
            }

            Stream fileStreamA = null, fileStreamB = null;

            try
            {
                // Open the file or an internal empty stream to compare against
                if (fileA.FileExists)
                {
                    fileStreamA = File.OpenRead(fileA.FileNamePath);
                }
                else
                {
                    fileStreamA = Assembly.GetExecutingAssembly().GetManifestResourceStream("AehnlichLib.Binaries.Resources.NonExistingFile.bin");
                }

                // Open the file or an internal empty stream to compare against
                if (fileB.FileExists)
                {
                    fileStreamB = File.OpenRead(fileB.FileNamePath);
                }
                else
                {
                    fileStreamB = Assembly.GetExecutingAssembly().GetManifestResourceStream("AehnlichLib.Binaries.Resources.NonExistingFile.bin");
                }

                BinaryDiff diff = new BinaryDiff
                {
                    FootprintLength = args.BinaryFootprintLength
                };

                AddCopyCollection addCopy = diff.Execute(fileStreamA, fileStreamB, progress);

                BinaryDiffLines lines = new BinaryDiffLines(fileStreamA, addCopy, args.BinaryFootprintLength);
                a = lines.BaseLines;
                b = lines.VersionLines;
                leadingCharactersToIgnore = BinaryDiffLines.PrefixLength;
            }
            finally
            {
                if (fileStreamA != null)
                {
                    fileStreamA.Dispose();
                }

                if (fileStreamB != null)
                {
                    fileStreamB.Dispose();
                }
            }

            return;
        }
Esempio n. 5
0
        private static void GetFileLines(FileCompInfo fileA,
                                         FileCompInfo fileB,
                                         out IList <string> a, out IList <string> b,
                                         out int leadingCharactersToIgnore,
                                         TextBinaryDiffArgs args,
                                         IDiffProgress progress)
        {
            a = null;
            b = null;
            leadingCharactersToIgnore = 0;
            CompareType compareType = args.CompareType;

            // Nothing to compare if both files do not exist
            if (fileA.FileExists == false && fileB.FileExists == false)
            {
                a = new List <string>();
                b = new List <string>();
                return;
            }

            if (compareType == CompareType.Binary ||
                (args.IsAuto && fileA.Is == FileType.Binary || fileB.Is == FileType.Binary))
            {
                GetBinaryFileLines(fileA, fileB, args, progress, out a, out b, out leadingCharactersToIgnore);
                return;
            }

            if (compareType == CompareType.Xml || (args.IsAuto && (a == null || b == null)))
            {
                a = fileA.TryGetXmlLines(DiffUtility.GetXmlTextLines, !args.IsAuto, args, progress);

                // If A failed to parse with Auto, then there's no reason to try B.
                if (fileA.Is == FileType.Xml)
                {
                    b = fileB.TryGetXmlLines(DiffUtility.GetXmlTextLines, !args.IsAuto, args, progress);
                }

                // If we get here and the compare type was XML, then both
                // inputs parsed correctly, and both lists should be non-null.
                // If we get here and the compare type was Auto, then one
                // or both lists may be null, so we'll fallthrough to the text
                // handling logic.
            }

            if (fileA.Is != FileType.Xml || fileB.Is != FileType.Xml)
            {
                if (fileA.FileExists)
                {
                    a = DiffUtility.GetFileTextLines(fileA.FileNamePath, progress);
                }
                else
                {
                    a = new List <string>();
                }

                if (fileB.FileExists)
                {
                    b = DiffUtility.GetFileTextLines(fileB.FileNamePath, progress);
                }
                else
                {
                    b = new List <string>();
                }
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Get Binary file contents rendered as text lines with line number marker at beginning of each line.
        /// </summary>
        /// <param name="fileA"></param>
        /// <param name="fileB"></param>
        /// <param name="args"></param>
        /// <param name="progress"></param>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="leadingCharactersToIgnore">Leading number of characters to ignore for diff in each line.
        /// This space is used in binary diff to display 8 digit line number and 4 digit space.</param>
        private DiffBinaryTextResults GetBinaryFileLines(FileCompInfo fileA, FileCompInfo fileB
                                                         , TextBinaryDiffArgs args
                                                         , IDiffProgress progress)
        {
            // Neither left nor right file exist or cannot be accessed
            if (fileA.FileExists == false && fileB.FileExists == false)
            {
                return(new DiffBinaryTextResults(CompareType.Binary, new FileContentInfo(), new FileContentInfo()));
            }

            Stream         fileStreamA = null, fileStreamB = null;
            IList <string> a = null, b = null;

            try
            {
                // Open the file or an internal empty stream to compare against
                if (fileA.FileExists)
                {
                    fileStreamA = File.OpenRead(fileA.FileNamePath);
                }
                else
                {
                    fileStreamA = Assembly.GetExecutingAssembly().GetManifestResourceStream("AehnlichLib.Binaries.Resources.NonExistingFile.bin");
                }

                // Open the file or an internal empty stream to compare against
                if (fileB.FileExists)
                {
                    fileStreamB = File.OpenRead(fileB.FileNamePath);
                }
                else
                {
                    fileStreamB = Assembly.GetExecutingAssembly().GetManifestResourceStream("AehnlichLib.Binaries.Resources.NonExistingFile.bin");
                }

                BinaryDiff diff = new BinaryDiff
                {
                    FootprintLength = args.BinaryFootprintLength
                };

                AddCopyCollection addCopy = diff.Execute(fileStreamA, fileStreamB, progress);

                BinaryDiffLines lines = new BinaryDiffLines(fileStreamA, addCopy, args.BinaryFootprintLength);
                a = lines.BaseLines;
                b = lines.VersionLines;
            }
            finally
            {
                if (fileStreamA != null)
                {
                    fileStreamA.Dispose();
                }

                if (fileStreamB != null)
                {
                    fileStreamB.Dispose();
                }
            }

            FileContentInfo af = new FileContentInfo(true, a);
            FileContentInfo bf = new FileContentInfo(true, b);

            return(new DiffBinaryTextResults(CompareType.Binary, af, bf, BinaryDiffLines.PrefixLength));
        }