Beispiel #1
0
        public static void CompareFiles()
        {
            CLIComparer.Init();

            //If I am here I have valid files, start comparing
            CompareEngine.CompareEngine compareEngine = new CompareEngine.CompareEngine();

            //Load the file paths into objects
            CompareText sourceDiffList      = new CompareText(LeftSide);
            CompareText destinationDiffList = new CompareText(RightSide);

            //Perform the comparison
            compareEngine.StartDiff(sourceDiffList, destinationDiffList);
            //Get Results
            ArrayList resultLines = compareEngine.DiffResult();
            string    numberMask  = GetNumberMask(sourceDiffList.Count(), destinationDiffList.Count());

            int lineCounter = 1;

            foreach (CompareResultSpan compareResultSpan in resultLines)
            {
                switch (compareResultSpan.Status)
                {
                case CompareResultSpanStatus.DeleteSource:
                    for (int i = 0; i < compareResultSpan.Length; i++)
                    {
                        string initial = "";
                        string rewrite = "";

                        initial += lineCounter.ToString(numberMask);
                        rewrite += lineCounter.ToString(numberMask);

                        initial += " < " + sourceDiffList.GetByIndex(compareResultSpan.SourceIndex + i).Line + "";

                        CLIComparer.DiffLines.Add(initial);

                        lineCounter++;
                        CLIComparer.MissingSourceCounter++;
                    }

                    break;

                case CompareResultSpanStatus.NoChange:
                    for (int i = 0; i < compareResultSpan.Length; i++)
                    {
                        lineCounter++;
                    }

                    break;

                case CompareResultSpanStatus.AddDestination:
                    for (int i = 0; i < compareResultSpan.Length; i++)
                    {
                        string rewrite = "";

                        rewrite += lineCounter.ToString(numberMask);


                        rewrite += " > " + destinationDiffList.GetByIndex(compareResultSpan.DestinationIndex + i).Line + "";

                        CLIComparer.DiffLines.Add(rewrite);

                        lineCounter++;
                        CLIComparer.MissingDestCounter++;
                    }

                    break;

                case CompareResultSpanStatus.Replace:
                    for (int i = 0; i < compareResultSpan.Length; i++)
                    {
                        string initial = "";
                        string rewrite = "";

                        initial += lineCounter.ToString(numberMask);
                        rewrite += lineCounter.ToString(numberMask);

                        initial += " initial:";
                        rewrite += " rewrite:";

                        initial += sourceDiffList.GetByIndex(compareResultSpan.SourceIndex + i).Line;
                        rewrite += destinationDiffList.GetByIndex(compareResultSpan.DestinationIndex + i).Line;

                        CLIComparer.DiffLines.Add(initial);
                        CLIComparer.DiffLines.Add(rewrite);

                        lineCounter++;
                        CLIComparer.DiffsCounter++;
                    }

                    break;
                }
            }

            CLIComparer.ShowSummary();
        }
Beispiel #2
0
        public void DisplayDirectoryResults(CompareText SourceDiffList, CompareText DestinationDiffList,
                                            ArrayList ResultLines)
        {
            CompareItem lviS;
            CompareItem lviD;

            ListFiles1 = new List <CompareItem>();
            ListFiles2 = new List <CompareItem>();

            sourceDiffList      = SourceDiffList;
            destinationDiffList = DestinationDiffList;
            resultLines         = ResultLines;

            int cnt = 1;

            DifferenceLines = new ArrayList();

            string numberMask = "000";

            if (sourceDiffList.Count() > 999 && sourceDiffList.Count() > sourceDiffList.Count())
            {
                for (int x = 0; x < sourceDiffList.Count().ToString().Length; x++)
                {
                    numberMask = numberMask + "0";
                }
            }
            else if (destinationDiffList.Count() > 999)
            {
                for (int x = 0; x < destinationDiffList.Count().ToString().Length; x++)
                {
                    numberMask = numberMask + "0";
                }
            }


            foreach (CompareResultSpan drs in resultLines)
            {
                switch (drs.Status)
                {
                case CompareResultSpanStatus.DeleteSource:
                    for (int i = 0; i < drs.Length; i++)
                    {
                        lviS = new CompareItem(cnt.ToString(numberMask));
                        lviD = new CompareItem(cnt.ToString(numberMask));

                        DifferenceLines.Add(cnt);

                        lviS.CompareResultTag = CompareItem.CompareResult.Add;
                        lviD.CompareResultTag = CompareItem.CompareResult.Add;
                        lviS.ItemSourceTag    = CompareItem.ItemSource.Original;
                        lviD.ItemSourceTag    = CompareItem.ItemSource.NotInOriginal;

                        summary.LinesLeftOnly++;

                        //Green
                        lviS.BackColor = Color.FromArgb(208, 236, 204);
                        lviD.BackColor = Color.FromArgb(208, 236, 204);

                        lviS.SubItems.Add((sourceDiffList.GetByIndex(drs.SourceIndex + i)).Line);
                        lviS.SubItems.Add((sourceDiffList.GetByIndex(drs.SourceIndex + i)).Size);
                        lviS.SubItems.Add(
                            (sourceDiffList.GetByIndex(drs.SourceIndex + i)).Type.ToString());
                        lviS.SubItems.Add((sourceDiffList.GetByIndex(drs.SourceIndex + i)).Path);

                        if ((sourceDiffList.GetByIndex(drs.SourceIndex + i)).Type == PathType.Directory)
                        {
                            lviS.ImageIndex = 0;
                        }
                        else
                        {
                            lviS.ImageIndex = 1;
                        }

                        lviD.SubItems.Add("");

                        ListFiles1.Add(lviS);
                        ListFiles2.Add(lviD);
                        cnt++;
                    }

                    break;

                case CompareResultSpanStatus.NoChange:
                    for (int i = 0; i < drs.Length; i++)
                    {
                        lviS = new CompareItem(cnt.ToString(numberMask));
                        lviD = new CompareItem(cnt.ToString(numberMask));

                        lviS.CompareResultTag = CompareItem.CompareResult.Equal;
                        lviD.CompareResultTag = CompareItem.CompareResult.Equal;
                        lviS.ItemSourceTag    = CompareItem.ItemSource.Original;
                        lviD.ItemSourceTag    = CompareItem.ItemSource.Original;

                        summary.LinesMatch++;

                        lviS.BackColor = Color.White;
                        lviD.BackColor = Color.White;

                        lviS.SubItems.Add((sourceDiffList.GetByIndex(drs.SourceIndex + i)).Line);
                        lviS.SubItems.Add((sourceDiffList.GetByIndex(drs.SourceIndex + i)).Size);
                        lviS.SubItems.Add(
                            (sourceDiffList.GetByIndex(drs.SourceIndex + i)).Type.ToString());
                        lviS.SubItems.Add((sourceDiffList.GetByIndex(drs.SourceIndex + i)).Path);

                        if ((sourceDiffList.GetByIndex(drs.SourceIndex + i)).Type ==
                            PathType.Directory)
                        {
                            lviS.ImageIndex = 0;
                        }
                        else
                        {
                            lviS.ImageIndex = 1;
                        }

                        lviD.SubItems.Add(
                            (destinationDiffList.GetByIndex(drs.DestinationIndex + i)).Line);
                        lviD.SubItems.Add(
                            (destinationDiffList.GetByIndex(drs.DestinationIndex + i)).Size);
                        lviD.SubItems.Add(
                            (destinationDiffList.GetByIndex(drs.DestinationIndex + i)).Type.
                            ToString());
                        lviD.SubItems.Add(
                            (destinationDiffList.GetByIndex(drs.DestinationIndex + i)).Path);


                        if ((destinationDiffList.GetByIndex(drs.DestinationIndex + i)).Type ==
                            PathType.Directory)
                        {
                            lviD.ImageIndex = 0;
                        }
                        else
                        {
                            lviD.ImageIndex = 1;
                        }



                        ListFiles1.Add(lviS);
                        ListFiles2.Add(lviD);
                        cnt++;
                    }

                    break;

                case CompareResultSpanStatus.AddDestination:
                    for (int i = 0; i < drs.Length; i++)
                    {
                        lviS = new CompareItem(cnt.ToString(numberMask));
                        lviD = new CompareItem(cnt.ToString(numberMask));

                        DifferenceLines.Add(cnt);

                        lviS.CompareResultTag = CompareItem.CompareResult.Delete;
                        lviD.CompareResultTag = CompareItem.CompareResult.Delete;
                        lviS.ItemSourceTag    = CompareItem.ItemSource.NotInOriginal;
                        lviD.ItemSourceTag    = CompareItem.ItemSource.Original;

                        summary.LinesRightOnly++;

                        //Gray
                        lviS.BackColor = Color.FromArgb(224, 224, 224);
                        //Light Coral
                        lviD.BackColor = Color.FromArgb(241, 213, 214);

                        lviS.SubItems.Add("");


                        lviD.SubItems.Add(
                            (destinationDiffList.GetByIndex(drs.DestinationIndex + i)).Line);
                        lviD.SubItems.Add(
                            (destinationDiffList.GetByIndex(drs.DestinationIndex + i)).Size);
                        lviD.SubItems.Add(
                            (destinationDiffList.GetByIndex(drs.DestinationIndex + i)).Type.
                            ToString());
                        lviD.SubItems.Add(
                            (destinationDiffList.GetByIndex(drs.DestinationIndex + i)).Path);

                        if ((destinationDiffList.GetByIndex(drs.DestinationIndex + i)).Type ==
                            PathType.Directory)
                        {
                            lviD.ImageIndex = 0;
                        }
                        else
                        {
                            lviD.ImageIndex = 1;
                        }



                        ListFiles1.Add(lviS);
                        ListFiles2.Add(lviD);

                        cnt++;
                    }

                    break;

                case CompareResultSpanStatus.Replace:
                    for (int i = 0; i < drs.Length; i++)
                    {
                        lviS = new CompareItem(cnt.ToString(numberMask));
                        lviD = new CompareItem(cnt.ToString(numberMask));

                        lviS.CompareResultTag = CompareItem.CompareResult.Replace;
                        lviD.CompareResultTag = CompareItem.CompareResult.Replace;
                        lviS.ItemSourceTag    = CompareItem.ItemSource.Original;
                        lviD.ItemSourceTag    = CompareItem.ItemSource.Original;

                        summary.LinesDifferent++;

                        DifferenceLines.Add(cnt);

                        //Green
                        lviS.BackColor = Color.FromArgb(208, 236, 204);
                        lviD.BackColor = Color.FromArgb(208, 236, 204);

                        lviS.SubItems.Add((sourceDiffList.GetByIndex(drs.SourceIndex + i)).Line);
                        lviS.SubItems.Add((sourceDiffList.GetByIndex(drs.SourceIndex + i)).Size);
                        lviS.SubItems.Add(
                            (sourceDiffList.GetByIndex(drs.SourceIndex + i)).Type.ToString());
                        lviS.SubItems.Add((sourceDiffList.GetByIndex(drs.SourceIndex + i)).Path);


                        if ((sourceDiffList.GetByIndex(drs.SourceIndex + i)).Type ==
                            PathType.Directory)
                        {
                            lviS.ImageIndex = 0;
                        }
                        else
                        {
                            lviS.ImageIndex = 1;
                        }



                        lviD.SubItems.Add(
                            (destinationDiffList.GetByIndex(drs.DestinationIndex + i)).Line);
                        lviD.SubItems.Add(
                            (destinationDiffList.GetByIndex(drs.DestinationIndex + i)).Size);
                        lviD.SubItems.Add(
                            (destinationDiffList.GetByIndex(drs.DestinationIndex + i)).Type.
                            ToString());
                        lviD.SubItems.Add(
                            (destinationDiffList.GetByIndex(drs.DestinationIndex + i)).Path);

                        if ((destinationDiffList.GetByIndex(drs.DestinationIndex + i)).Type ==
                            PathType.Directory)
                        {
                            lviD.ImageIndex = 0;
                        }
                        else
                        {
                            lviD.ImageIndex = 1;
                        }



                        ListFiles1.Add(lviS);
                        ListFiles2.Add(lviD);
                        cnt++;
                    }

                    break;
                }
            }
        }