"Fossies" - the Fresh Open Source Software Archive

Member "cppcheck-1.89/tools/compare.cs" (1 Sep 2019, 14619 Bytes) of package /windows/misc/cppcheck-1.89.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C# source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "compare.cs" see the Fossies "Dox" file reference documentation.

    1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5 using System.Threading.Tasks;
    6 using System.IO;
    7 using System.Text.RegularExpressions;
    8 
    9 namespace difftool
   10 {
   11     class Program
   12     {
   13         static private string FirstLine(string s)
   14         {
   15             int i = s.IndexOf('\n');
   16             return i > 0 ? s.Substring(0, i) : s;
   17         }
   18 
   19         static private List<string> GetDiff(string[] s1, string[] s2)
   20         {
   21             List<string> ret = new List<string>();
   22 
   23             int i2 = 0;
   24             while (i2 < s2.Length && (s2[i2] == "" || !s2[i2].StartsWith("mozilla/")))
   25                 i2++;
   26             for (int i1 = 0; i1 < s1.Length; ++i1)
   27             {
   28                 if (s1[i1] == "" || !s1[i1].StartsWith("mozilla/"))
   29                     continue;
   30 
   31                 if (i2 >= s2.Length)
   32                 {
   33                     ret.Add(s1[i1]);
   34                     continue;
   35                 }
   36                 if (FirstLine(s1[i1]) == FirstLine(s2[i2]))
   37                 {
   38                     ++i2;
   39                     continue;
   40                 }
   41                 for (int i3 = i2 + 1; i3 < i2 + 300 && i3 < s2.Length; ++i3)
   42                 {
   43                     if (FirstLine(s1[i1]) == FirstLine(s2[i3]))
   44                     {
   45                         i2 = i3;
   46                         break;
   47                     }
   48                 }
   49                 if (FirstLine(s1[i1]) == FirstLine(s2[i2]))
   50                 {
   51                     ++i2;
   52                     continue;
   53                 }
   54                 ret.Add(s1[i1]);
   55             }
   56             return ret;
   57         }
   58 
   59         static private string TextToHtml(string s)
   60         {
   61             return s.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;");
   62         }
   63 
   64         static Boolean IsNameChar(char c)
   65         {
   66             return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
   67         }
   68 
   69         // color theme:
   70 
   71         // http://www.eclipsecolorthemes.org/?view=theme&id=47
   72         /*
   73         private const string backgroundColor = "1E1E1E";
   74         private const string foregroundFont = "<font color=\"#E2E2E2\">";
   75         private const string keywordFont = "<font color=\"#8DCBE2\">";
   76         private const string numberFont = "<font color=\"#EAB882\">";
   77         private const string stringFont = "<font color=\"#CC9393\">";
   78         private const string singleLineCommentFont = "<font color=\"#7F9F7F\">";
   79         private const string multiLineCommentFont = "<font color=\"#73879B\">";
   80         private const string lineNumberFont = "<font color=\"#C0C0C0\">";
   81         private const string graphBackgroundColor = "#303030";
   82         private const string graphBorderColor = "#8DCBE2";
   83         private const string graphTextColor = "#8DCBE2";
   84         */
   85         private const string backgroundColor = "white";
   86         private const string foregroundFont = "<font color=\"black\">";
   87         private const string symbolFont = "<font style=\"background-color:#c0c0c0\">";
   88         private const string keywordFont = "<font color=\"blue\">";
   89         private const string numberFont = "<font color=\"green\">";
   90         private const string stringFont = "<font color=\"#800000\">";
   91         private const string singleLineCommentFont = "<font color=\"gray\">";
   92         private const string multiLineCommentFont = "<font color=\"gray\">";
   93         private const string lineNumberFont = "<font color=\"black\">";
   94         private const string graphBackgroundColor = "#e0e0e0";
   95         private const string graphBorderColor = "black";
   96         private const string graphTextColor = "black";
   97 
   98         static private void writeSourceFile(string path, string w, string filename)
   99         {
  100             string errorLoc;
  101             string errorMessage;
  102             if (w[0] == '[')
  103             {
  104                 errorLoc = w.Substring(1, w.IndexOf(']') - 1);
  105                 int i1 = w.IndexOf(')') + 1;
  106                 errorMessage = w.Substring(i1);
  107             }
  108             else
  109             {
  110                 errorLoc = w.Substring(0, w.IndexOf(':', w.IndexOf(':') + 1));
  111                 int i1 = w.IndexOf(": ", w.IndexOf(": ") + 1) + 1;
  112                 int i2 = w.LastIndexOf('[') - 1;
  113                 errorMessage = w.Substring(i1, i2 - i1).Trim();
  114             }
  115             string f = errorLoc.Substring(0, errorLoc.LastIndexOf(':'));
  116             int errorLine = Int32.Parse(errorLoc.Substring(1 + errorLoc.LastIndexOf(':')));
  117             string symbolName = "";
  118             if (errorMessage.IndexOf(": ") > 5)
  119             {
  120                 string end = errorMessage.Substring(errorMessage.LastIndexOf(": "));
  121                 // get symbol name
  122                 Regex regex = new Regex(": '?[a-zA-Z_][a-zA-Z0-9_]*'?\\.?");
  123                 if (regex.IsMatch(end))
  124                     symbolName = end.Trim(':', ' ', '\'', '.');
  125             }
  126             string[] lines = System.IO.File.ReadAllLines(path + f);
  127             string[] keywords = { "void", "bool", "char", "short", "int", "long", "double", "float",
  128                                   "class", "struct", "union", "enum", "namespace", "friend", "using",
  129                                   "for", "if", "while", "switch", "case", "default",
  130                                   "return", "goto", "break", "continue", "throw",
  131                                   "unsigned", "signed",
  132                                   "private", "protected", "public",
  133                                   "static", "const", "template", "typename", "auto", "virtual",
  134                                   "true", "false", "this" };
  135             System.IO.StreamWriter file = new System.IO.StreamWriter(path + filename);
  136             file.WriteLine("<html>");
  137             file.WriteLine("<head>");
  138             file.WriteLine("<title>" + TextToHtml(w) + "</title>");
  139             file.WriteLine("<style>");
  140             file.WriteLine("  table {");
  141             file.WriteLine("    border: 0px;");
  142             file.WriteLine("    border-spacing: 0;");
  143             file.WriteLine("  }");
  144             file.WriteLine("  td {");
  145             file.WriteLine("    border: 1px solid red;");
  146             file.WriteLine("    padding: 5px;");
  147             file.WriteLine("    background-color: #ffe0e0;");
  148             file.WriteLine("}");
  149             file.WriteLine("</style>");
  150             file.WriteLine("</head>");
  151             file.WriteLine("<body bgcolor=\"" + backgroundColor + "\"><pre>" + foregroundFont + TextToHtml(w) + "\n");
  152             int linenr = 0;
  153             bool multiline = false;
  154             foreach (string line in lines)
  155             {
  156                 ++linenr;
  157                 file.Write(lineNumberFont + (linenr.ToString() + "        ").Substring(0, 8) + "</font>");
  158 
  159                 for (int i = 0; i < line.Length;)
  160                 {
  161                     if (line[i] == ' ')
  162                     {
  163                         file.Write(line[i]);
  164                         i++;
  165                     }
  166                     else if (multiline)
  167                     {
  168                         string currentToken = "";
  169                         while (i < line.Length && multiline)
  170                         {
  171                             currentToken += line[i];
  172                             i++;
  173                             if (currentToken.EndsWith("*/"))
  174                                 multiline = false;
  175                         }
  176                         file.Write(multiLineCommentFont + TextToHtml(currentToken) + "</font>");
  177                     }
  178                     else if (IsNameChar(line[i]))
  179                     {
  180                         string currentToken = "";
  181                         while (i < line.Length && IsNameChar(line[i]))
  182                         {
  183                             currentToken += line[i];
  184                             i++;
  185                         }
  186                         if (currentToken == symbolName)
  187                             file.Write(symbolFont + currentToken + "</font>");
  188                         else if (keywords.Contains(currentToken))
  189                             file.Write(keywordFont + currentToken + "</font>");
  190                         else if (currentToken[0] >= '0' && currentToken[0] <= '9')
  191                             file.Write(numberFont + currentToken + "</font>");
  192                         else
  193                             file.Write(currentToken);
  194                     }
  195                     else if (line[i] == '\"' || line[i] == '\'')
  196                     {
  197                         string currentToken = "";
  198                         while (i < line.Length)
  199                         {
  200                             currentToken += line[i];
  201                             i++;
  202                             if (currentToken.Length > 1 && currentToken[0] == line[i - 1])
  203                                 break;
  204                         }
  205                         file.Write(stringFont + TextToHtml(currentToken) + "</font>");
  206                     }
  207                     else if (line[i] == '/' && i + 1 < line.Length && line[i + 1] == '/')
  208                     {
  209                         file.Write(singleLineCommentFont + TextToHtml(line.Substring(i)) + "</font>");
  210                         i = line.Length;
  211                     }
  212                     else if (line[i] == '/' && i + 1 < line.Length && line[i + 1] == '*')
  213                     {
  214                         multiline = true;
  215                         string currentToken = "/*";
  216                         i += 2;
  217                         while (i < line.Length && multiline)
  218                         {
  219                             currentToken += line[i];
  220                             i++;
  221                             if (currentToken.Length > 3 && currentToken.EndsWith("*/"))
  222                                 multiline = false;
  223                         }
  224                         file.Write(multiLineCommentFont + TextToHtml(currentToken) + "</font>");
  225                     }
  226                     else
  227                     {
  228                         file.Write(TextToHtml(line.Substring(i, 1)));
  229                         i++;
  230                     }
  231                 }
  232                 file.Write('\n');
  233 
  234                 if (errorLine == linenr)
  235                     file.WriteLine("<table width=\"100%\"><tr><td><b>" + TextToHtml(errorMessage) + "</td></tr></table>");
  236             }
  237             file.Write("</font></pre></body></html>");
  238             file.Close();
  239         }
  240 
  241         static private void combineWarnings(string[] lines)
  242         {
  243             for (int i = 0; i < lines.Length; ++i)
  244             {
  245                 if (lines[i].IndexOf(": note:") > 0 || !lines[i].StartsWith("mozilla/"))
  246                     lines[i] = "";
  247                 continue;
  248 
  249                 int i2 = i + 1;
  250                 while (i2 < lines.Length && lines[i2].IndexOf(": note:") > 0)
  251                 {
  252                     lines[i] = lines[i] + '\n' + lines[i2];
  253                     lines[i2] = "";
  254                     i2++;
  255                 }
  256             }
  257         }
  258 
  259         static void Main(string[] args)
  260         {
  261             string path;
  262             if (Directory.Exists("/home/danielm/shared/testing"))
  263                 path = "/home/danielm/shared/testing/";
  264             else if (Directory.Exists("/home/danielm/testing"))
  265                 path = "/home/danielm/testing/";
  266             else if (Directory.Exists(@"c:\Users\danielm.evidente\testing"))
  267                 path = @"c:\Users\danielm.evidente\testing\";
  268             else
  269             {
  270                 Console.WriteLine("Directory testing not found");
  271                 return;
  272             }
  273 
  274             string[] old = System.IO.File.ReadAllLines(path + "1.82.txt");
  275             string[] head = System.IO.File.ReadAllLines(path + "head.txt");
  276 
  277             combineWarnings(old);
  278             combineWarnings(head);
  279 
  280             Array.Sort(old, StringComparer.InvariantCulture);
  281             Array.Sort(head, StringComparer.InvariantCulture);
  282 
  283             List<string> removed = GetDiff(old, head);
  284             List<string> added = GetDiff(head, old);
  285 
  286             int fileIndex = 1;
  287 
  288             using (System.IO.StreamWriter file = new System.IO.StreamWriter(path + "html/main.html"))
  289             {
  290                 file.WriteLine("<html>");
  291                 file.WriteLine("<head>");
  292                 file.WriteLine("<title>Comparison 1.82 / Head</title>");
  293                 file.WriteLine("</head>");
  294                 file.WriteLine("<body bgcolor=\"" + backgroundColor + "\">");
  295                 file.WriteLine(foregroundFont);
  296 
  297                 file.WriteLine("<h1>Comparison 1.82 / Head</h1>");
  298                 file.WriteLine("<p>Package: {0}</p>", @"ftp://ftp.se.debian.org/debian/pool/main/f/firefox/firefox_59.0~b4.orig.tar.bz2");
  299                 file.WriteLine("<p>+ Added warnings {0}</p>", added.Count);
  300                 file.WriteLine("<p>- Removed warnings {0}</p>", removed.Count);
  301 
  302                 file.WriteLine("<svg width=\"{0}\" height=\"{1}\">", 60 + Math.Max(added.Count, removed.Count) + 10, 40);
  303                 file.WriteLine("  <rect x=\"0\" y=\"0\" width=\"100%\" height=\"100%\" stroke=\"" + graphBorderColor + "\" fill=\"" + graphBackgroundColor + "\"/>");
  304                 file.WriteLine("  <text x=\"4\" y=\"15\" font-family=\"Verdana\" font-size=\"10\" fill=\"" + graphTextColor + "\">Added</text>");
  305                 file.WriteLine("  <text x=\"4\" y=\"30\" font-family=\"Verdana\" font-size=\"10\" fill=\"" + graphTextColor + "\">Removed</text>");
  306                 file.WriteLine("  <rect x=\"60\" y=\"8\" width=\"{0}\" height=\"8\" fill=\"#40ff40\" />", added.Count);
  307                 file.WriteLine("  <rect x=\"60\" y=\"23\" width=\"{0}\" height=\"8\" fill=\"#ff4040\" />", removed.Count);
  308                 file.WriteLine("</svg>");
  309 
  310                 string[] title = { "Added warnings", "Removed warnings" };
  311                 List<string>[] warnings = { added, removed };
  312                 for (int i = 0; i < 2; i++)
  313                 {
  314                     file.WriteLine("<h2>" + title[i] + "</h2>");
  315                     file.WriteLine("<pre>");
  316                     foreach (string w in warnings[i])
  317                     {
  318                         string filename = "code" + fileIndex.ToString() + ".html";
  319                         fileIndex++;
  320                         writeSourceFile(path, w, "html/" + filename);
  321                         file.WriteLine("<a href=\"{0}\">{1}</a>", filename, TextToHtml(w));
  322                     }
  323                     file.WriteLine("</pre>");
  324                 }
  325 
  326                 file.WriteLine("</body>");
  327                 file.WriteLine("</html>");
  328             }
  329         }
  330     }
  331 }