Skip to content

Instantly share code, notes, and snippets.

@dmimat
Last active December 21, 2015 02:59
Show Gist options
  • Save dmimat/6239373 to your computer and use it in GitHub Desktop.
Save dmimat/6239373 to your computer and use it in GitHub Desktop.
// Converts the output of dupFInder.exe to an HTML report.
// Usage: DupTransfromer.exe inputReport.xml outputReport.html
static void Main(string[] args)
{
var dupRebport = XDocument.Load(@args[0]);
using (var writer = XmlWriter.Create(@args[1], new XmlWriterSettings { Indent = true }))
{
writer.WriteStartElement("html");
// Write out statistics
writer.WriteElementString("h1", "Statistics");
writer.WriteElementString("p", "Total codebase size: " +
dupRebport.Descendants("CodebaseCost").FirstOrDefault().Value);
writer.WriteElementString("p", "Code to analyze: " +
dupRebport.Descendants("TotalDuplicatesCost").FirstOrDefault().Value);
writer.WriteElementString("p", "Total size of duplicated fragments: " +
dupRebport.Descendants("TotalFragmentsCost").FirstOrDefault().Value);
// Sort duplicates by size
var sortedDuplicates = from node in dupRebport.Root.Element("Duplicates").Elements()
orderby Int32.Parse(node.Attribute("Cost").Value) descending
select node;
// Write out duplicates
writer.WriteElementString("h1", "Detected Duplicates");
foreach (var duplicate in sortedDuplicates)
{
writer.WriteElementString("h2", "Duplicated Code. Size: " + duplicate.Attribute("Cost").Value);
writer.WriteElementString("h3", "Duplicated Fragments: ");
// Get all duplicated fragments
int dupCount = 1;
foreach (var fragment in duplicate.Elements())
{
var fragmentFile = fragment.Element("FileName").Value;
// Read duplicated fragment by the offset range
var fileOffset = Int32.Parse(fragment.Element("OffsetRange").Attribute("Start").Value);
var bytesToRead = Int32.Parse(fragment.Element("OffsetRange").Attribute("End").Value) - fileOffset;
var dupByteRange = File.ReadAllBytes(fragmentFile).Skip(fileOffset).Take(bytesToRead).ToArray();
string dupFragment = Encoding.UTF8.GetString(dupByteRange).Replace("\u0000", "");
// Write out duplicated fragment
writer.WriteElementString("p", String.Format("Fragment {0} in file {1}", dupCount, fragmentFile));
writer.WriteElementString("pre", dupFragment);
dupCount ++;
}
}
writer.WriteEndElement();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment