Exemple #1
1
        static void Main(string[] args)
        {
            string pathfileA = @"C:\xcmpdata\trunk\lineTypeCodeListing.txt";
            string pathfileB = @"C:\xcmpdata\branch\lineTypeCodeListing.txt";

            FileInfo a = new FileInfo(pathfileA);
            FileInfo b = new FileInfo(pathfileB);

            //string s1 = "hello, world - parag,";
            //string s2 = "hello, WXld - parag.";
            StreamReader sra = null;
            StreamReader srb = null;

            try
            {
                //Open Text files for Reading
                sra = a.OpenText();
                string sa = sra.ReadToEnd();
                srb = b.OpenText();
                string sb = srb.ReadToEnd();

                //Pass the strings to be matched for Diff
                DiffMatchPatch.diff_match_patch xdiff = new diff_match_patch();
                List<DiffMatchPatch.Diff> diffs = xdiff.diff_main(sa, sb, true);

                //Print the Diff with content
                string html = xdiff.diff_prettyHtml(diffs);

                string diffFileURI = @"C:\xcmpdata\results\diff.html";
                StreamWriter diffFile = null;
                try
                {
                    diffFile = new StreamWriter(diffFileURI);
                    diffFile.Write(html);
                }
                catch (Exception e)
                {
                    Console.Write("Exception occurred: " + e.InnerException);
                }
                finally
                {
                    diffFile.Close();
                }
            }
            catch (FileNotFoundException fnf)
            {
                Console.WriteLine("File Not found at given location:" + fnf.Message);
            }
            catch (DirectoryNotFoundException dnf)
            {
                Console.WriteLine("Directory Not found at given location:" + dnf.Message);
            }

            Console.WriteLine("xcmp completed finding differences in the given files, Results are available at designated location.");
            Console.ReadKey();
        }
Exemple #2
0
        public void Diff(string Text1, string Text2)
        {
            this.Text1 = Text1;
            this.Text2 = Text2;

            var dmp = new diff_match_patch();
            var diffs = dmp.diff_main(Text1, Text2);
            Html = dmp.diff_prettyHtml(diffs);

            InsertCount = 0;
            InsertLength = 0;
            DeleteCount = 0;
            DeleteLength = 0;

            foreach (var d in diffs)
            {
                if (d.operation == Operation.INSERT)
                {
                    InsertCount++;
                    InsertLength += d.text.Length;
                }
                if (d.operation == Operation.DELETE)
                {
                    DeleteCount++;
                    DeleteLength += d.text.Length;
                }
            }
        }
 private string DiffToHtml(string s1, string s2)
 {
     var dmp = new diff_match_patch();
     var diffs = dmp.diff_main(s1 ?? string.Empty, s2 ?? string.Empty);
     dmp.diff_cleanupSemantic(diffs);
     return dmp.diff_prettyHtml(diffs);
 }
Exemple #4
0
        protected void Page_Load(object sender, EventArgs e)
        {
            var text1 = "In the demo they cross out the words that are different and that is what I am trying to achieve.";
            var text2 = "In the they demo cross out the words that are different and that is what I am trying to achieve.";

            var dmp = new diff_match_patch();
            var diffs = dmp.diff_main(text1, text2);
            var html = dmp.diff_prettyHtml(diffs);

            foreach (var d in diffs)
            {
                Result.Text += d.operation.ToString() + ";" + d.text.ToString() + "</br>";
            }

            //Result.Text = html;
        }
        public static string GetDiffHtml(string text1, string text2)
        {
            diff_match_patch dmp = new diff_match_patch();
            dmp.Diff_Timeout = 0;

            var diffs = dmp.diff_main(text1, text2);
            var html = dmp.diff_prettyHtml(diffs);

            return html;
        }
Exemple #6
0
        //File Comparison
        static void FileCompare(FileInfo a, FileInfo b, string diffFileURI)
        {
            StreamReader sra = null;
            StreamReader srb = null;
            try
            {
                //Open Text files for Reading
                sra = a.OpenText();
                string sa = sra.ReadToEnd();
                srb = b.OpenText();
                string sb = srb.ReadToEnd();

                //Pass the strings to be matched for Diff
                DiffMatchPatch.diff_match_patch xdiff = new diff_match_patch();
                List<DiffMatchPatch.Diff> diffs = xdiff.diff_main(sa, sb, true);

                //Print the Diff with content
                StringBuilder html = new StringBuilder();
                html.Append("<strong>" + a.DirectoryName + "\\" + a.Name + " compared to " + b.DirectoryName + "\\" + b.Name + "</strong>\n");
                html.Append(xdiff.diff_prettyHtml(diffs));
                StreamWriter diffFile = null;
                try
                {
                    diffFile = File.AppendText(diffFileURI);
                    diffFile.Write(html.ToString());
                }
                catch (Exception e)
                {
                    Console.Write("Exception occurred: " + e.InnerException);
                }
                finally
                {
                    diffFile.Close();
                }
            }
            catch (FileNotFoundException fnf)
            {
                Console.WriteLine("File Not found at given location:" + fnf.Message);
            }
            catch (DirectoryNotFoundException dnf)
            {
                Console.WriteLine("Directory Not found at given location:" + dnf.Message);
            }
        }