CompareFiles.cs

// Copyright � 2007 Jeffrey Bazinet, http://www.vwd-cms.com/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

namespace VwdCms.Admin
{
    public class CompareFiles : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Label lblInstructions;
        protected System.Web.UI.WebControls.Label lblFileInfo1;
        protected System.Web.UI.WebControls.Label lblFileInfo2;
        protected System.Web.UI.WebControls.TextBox txtFile1;
        protected System.Web.UI.WebControls.TextBox txtText1;
        protected System.Web.UI.WebControls.TextBox txtFile2;
        protected System.Web.UI.WebControls.TextBox txtText2;
        protected System.Web.UI.WebControls.Button btnCompare;
        protected System.Web.UI.WebControls.TextBox txtError;
        protected System.Web.UI.WebControls.Table tblChars1;
        protected System.Web.UI.WebControls.Table tblChars2;

        protected void Page_Load(object sender, EventArgs e)
        {
            VwdCms.Configuration.Utilities.ProtectAdminPages();

            this.lblInstructions.Style.Add("display", "none");
            this.lblInstructions.Text = string.Empty;
            
            if (!this.Page.IsPostBack)
            {
                string lpApp = this.Server.MapPath("~/");
                string file1 = this.Page.Request.QueryString["file1"];
                string file2 = this.Page.Request.QueryString["file2"];
                string text1 = null;
                string text2 = null;

                if (!string.IsNullOrEmpty(file1))
                {
                    this.txtFile1.Text = file1;
                    text1 = ReadFile(this.txtFile1.Text);
                    this.txtText1.Text = text1;
                }
                else
                {
                    this.txtFile1.Text = lpApp;
                    this.txtText1.Text = string.Empty;
                }

                if (!string.IsNullOrEmpty(file2))
                {
                    this.txtFile2.Text = file2;
                    text2 = ReadFile(this.txtFile2.Text);
                    this.txtText2.Text = text2;
                }
                else
                {
                    this.txtFile2.Text = lpApp;
                    this.txtText2.Text = string.Empty;
                }
            }

            this.txtError.Text = string.Empty;
            this.txtError.Style.Add("display", "none");

            this.btnCompare.Command += new CommandEventHandler(btnCompare_Command);
        }

        void btnCompare_Command(object sender, CommandEventArgs e)
        {
            string text1 = ReadFile(this.txtFile1.Text);
            string text2 = ReadFile(this.txtFile2.Text);
            
            this.txtText1.Text = text1;
            this.txtText2.Text = text2;

            StringBuilder sb = null;
            string newline = "<br/>";

            sb = new StringBuilder();
            sb.Append("File 1: ");
            sb.Append(this.txtFile1.Text);
            sb.Append(newline);
            sb.Append("Length: ");
            sb.Append(text1.Length.ToString());
            sb.Append(newline);
            this.lblFileInfo1.Text = sb.ToString();

            sb = new StringBuilder();
            sb.Append("File 2: ");
            sb.Append(this.txtFile2.Text);
            sb.Append(newline);
            sb.Append("Length: ");
            sb.Append(text2.Length.ToString());
            sb.Append(newline);
            this.lblFileInfo2.Text = sb.ToString();


            char[] chars1 = text1.ToCharArray();
            char[] chars2 = text2.ToCharArray();
            int i = 0;
            TableRow tr = null;
            TableCell tc = null;
            Unit colWidth = new Unit("50px");
            System.Drawing.Color bg;

            this.tblChars1.Rows.Clear();
            i = 0;
            foreach (char c in chars1)
            {
                if (i  chars2.Length  chars1[i] == chars2[i])
                {
                    bg = System.Drawing.Color.LightGreen;
                }
                else
                {
                    bg = System.Drawing.Color.LightYellow;
                }
                tr = new TableRow();
                tr.BackColor = bg;

                tc = new TableCell();
                tc.Width = colWidth;
                tc.HorizontalAlign = HorizontalAlign.Center;
                tc.Text = i.ToString();
                tr.Cells.Add(tc);

                tc = new TableCell();
                tc.Width = colWidth;
                tc.HorizontalAlign = HorizontalAlign.Center;
                tc.Text = ((int)c).ToString();
                tr.Cells.Add(tc);

                tc = new TableCell();
                tc.Width = colWidth;
                tc.HorizontalAlign = HorizontalAlign.Center;

                if (c == '\r')
                {
                    tc.Text = "\\r";
                }
                else if (c == '\n')
                {
                    tc.Text = "\\n";
                }
                else if (c == '\t')
                {
                    tc.Text = "\\t";
                }
                else
                {
                    tc.Text = c.ToString();
                }
                tr.Cells.Add(tc);

                this.tblChars1.Rows.Add(tr);
                i++;
            }

            this.tblChars2.Rows.Clear();
            i = 0;
             
            foreach (char c in chars2)
            {
                if (i  chars1.Length  chars1[i] == chars2[i])
                {
                    bg = System.Drawing.Color.LightGreen;
                }
                else
                {
                    bg = System.Drawing.Color.MediumVioletRed;
                }
                tr = new TableRow();
                tr.BackColor = bg;

                tc = new TableCell();
                tc.Width = colWidth;
                tc.HorizontalAlign = HorizontalAlign.Center;
                tc.Text = i.ToString();
                tr.Cells.Add(tc);

                tc = new TableCell();
                tc.Width = colWidth;
                tc.HorizontalAlign = HorizontalAlign.Center;
                tc.Text = ((int)c).ToString();
                tr.Cells.Add(tc);

                tc = new TableCell();
                tc.Width = colWidth;
                tc.HorizontalAlign = HorizontalAlign.Center;
                if (c == '\r')
                {
                    tc.Text = "\\r";
                }
                else if (c == '\n')
                {
                    tc.Text = "\\n";
                }
                else if (c == '\t')
                {
                    tc.Text = "\\t";
                }
                else
                {
                    tc.Text = c.ToString();
                } 
                tr.Cells.Add(tc);

                this.tblChars2.Rows.Add(tr);
                i++;
            }
        }

        private string ReadFile(string localpathfile)
        {
            string text = null;

            if (File.Exists(localpathfile))
            {
                StreamReader sr = null;

                try
                {
                    sr = new StreamReader(localpathfile);
                    text = sr.ReadToEnd();
                }
                catch (Exception ex)
                {
                    string msg = "Failed while trying to read the file '{0}'.\r\n\r\n";
                    msg = string.Format(msg, localpathfile) + ex.ToString();
                    this.txtError.Text += msg;
                    this.txtError.Style.Add("display", "block");
                }
                finally
                {
                    if (sr != null)
                    {
                        sr.Close();
                        sr = null;
                    }
                }
            }
            else
            {
                string msg = "ERROR: The file '{0}' does not exist.";
                this.txtError.Text += string.Format(msg, localpathfile);
                this.txtError.Style.Add("display", "block");
            }
            return text;
        }
    }
}