[SabreTools, DatFile, Reports] Fix and upgrade stats output

This commit is contained in:
Matt Nadareski
2017-11-07 13:56:15 -08:00
parent c464b0c2ac
commit 4fad2b97bc
12 changed files with 612 additions and 337 deletions

View File

@@ -9,6 +9,7 @@ using System.Web;
using SabreTools.Library.Data;
using SabreTools.Library.FileTypes;
using SabreTools.Library.DatItems;
using SabreTools.Library.Reports;
using SabreTools.Library.Skippers;
using SabreTools.Library.Tools;
@@ -5399,14 +5400,11 @@ namespace SabreTools.Library.DatFiles
/// <summary>
/// Output the stats for the Dat in a human-readable format
/// </summary>
/// <param name="outputs">Dictionary representing the outputs</param>
/// <param name="statDatFormat">Set the statistics output format to use</param>
/// <param name="recalculate">True if numbers should be recalculated for the DAT, false otherwise (default)</param>
/// <param name="game">Number of games to use, -1 means recalculate games (default)</param>
/// <param name="baddumpCol">True if baddumps should be included in output, false otherwise (default)</param>
/// <param name="nodumpCol">True if nodumps should be included in output, false otherwise (default)</param>
public void OutputStats(Dictionary<StatDatFormat, StreamWriter> outputs, StatDatFormat statDatFormat,
bool recalculate = false, long game = -1, bool baddumpCol = false, bool nodumpCol = false)
public void WriteStatsToScreen(bool recalculate = false, long game = -1, bool baddumpCol = false, bool nodumpCol = false)
{
// If we're supposed to recalculate the statistics, do so
if (recalculate)
@@ -5447,115 +5445,6 @@ namespace SabreTools.Library.DatFiles
results += "\n\n";
Globals.Logger.User(results);
// Now write it out to file as well
string line = "";
if (outputs.ContainsKey(StatDatFormat.None))
{
line = @"'" + FileName + @"':
--------------------------------------------------
Uncompressed size: " + Style.GetBytesReadable(TotalSize) + @"
Games found: " + (game == -1 ? Keys.Count() : game) + @"
Roms found: " + RomCount + @"
Disks found: " + DiskCount + @"
Roms with CRC: " + CRCCount + @"
Roms with SHA-1: " + SHA1Count + @"
Roms with SHA-256: " + SHA256Count + @"
Roms with SHA-384: " + SHA384Count + @"
Roms with SHA-512: " + SHA512Count + "\n";
if (baddumpCol)
{
line += " Roms with BadDump status: " + BaddumpCount + "\n";
}
if (nodumpCol)
{
line += " Roms with Nodump status: " + NodumpCount + "\n";
}
// For spacing between DATs
line += "\n\n";
outputs[StatDatFormat.None].Write(line);
}
if (outputs.ContainsKey(StatDatFormat.CSV))
{
line = "\"" + FileName + "\","
+ "\"" + TotalSize + "\","
+ "\"" + (game == -1 ? Keys.Count() : game) + "\","
+ "\"" + RomCount + "\","
+ "\"" + DiskCount + "\","
+ "\"" + CRCCount + "\","
+ "\"" + MD5Count + "\","
+ "\"" + SHA1Count + "\","
+ "\"" + SHA256Count + "\","
+ "\"" + SHA384Count + "\","
+ "\"" + SHA512Count + "\"";
if (baddumpCol)
{
line += ",\"" + BaddumpCount + "\"";
}
if (nodumpCol)
{
line += ",\"" + NodumpCount + "\"";
}
line += "\n";
outputs[StatDatFormat.CSV].Write(line);
}
if (outputs.ContainsKey(StatDatFormat.HTML))
{
line = "\t\t\t<tr" + (FileName.StartsWith("DIR: ")
? " class=\"dir\"><td>" + HttpUtility.HtmlEncode(FileName.Remove(0, 5))
: "><td>" + HttpUtility.HtmlEncode(FileName)) + "</td>"
+ "<td align=\"right\">" + Style.GetBytesReadable(TotalSize) + "</td>"
+ "<td align=\"right\">" + (game == -1 ? Keys.Count() : game) + "</td>"
+ "<td align=\"right\">" + RomCount + "</td>"
+ "<td align=\"right\">" + DiskCount + "</td>"
+ "<td align=\"right\">" + CRCCount + "</td>"
+ "<td align=\"right\">" + MD5Count + "</td>"
+ "<td align=\"right\">" + SHA1Count + "</td>"
+ "<td align=\"right\">" + SHA256Count + "</td>";
if (baddumpCol)
{
line += "<td align=\"right\">" + BaddumpCount + "</td>";
}
if (nodumpCol)
{
line += "<td align=\"right\">" + NodumpCount + "</td>";
}
line += "</tr>\n";
outputs[StatDatFormat.HTML].Write(line);
}
if (outputs.ContainsKey(StatDatFormat.TSV))
{
line = "\"" + FileName + "\"\t"
+ "\"" + TotalSize + "\"\t"
+ "\"" + (game == -1 ? Keys.Count() : game) + "\"\t"
+ "\"" + RomCount + "\"\t"
+ "\"" + DiskCount + "\"\t"
+ "\"" + CRCCount + "\"\t"
+ "\"" + MD5Count + "\"\t"
+ "\"" + SHA1Count + "\"\t"
+ "\"" + SHA256Count + "\"\t"
+ "\"" + SHA384Count + "\"\t"
+ "\"" + SHA512Count + "\"";
if (baddumpCol)
{
line += "\t\"" + BaddumpCount + "\"";
}
if (nodumpCol)
{
line += "\t\"" + NodumpCount + "\"";
}
line += "\n";
outputs[StatDatFormat.TSV].Write(line);
}
}
/// <summary>
@@ -5664,8 +5553,7 @@ namespace SabreTools.Library.DatFiles
// Output initial statistics, for kicks
if (stats)
{
OutputStats(new Dictionary<StatDatFormat, StreamWriter>(), StatDatFormat.None,
recalculate: (RomCount + DiskCount == 0), baddumpCol: true, nodumpCol: true);
WriteStatsToScreen(recalculate: (RomCount + DiskCount == 0), baddumpCol: true, nodumpCol: true);
}
// Bucket and dedupe according to the flag
@@ -5802,12 +5690,12 @@ namespace SabreTools.Library.DatFiles
/// <param name="nodumpCol">True if nodumps should be included in output, false otherwise</param>
/// <param name="statDatFormat" > Set the statistics output format to use</param>
public static void OutputStats(List<string> inputs, string reportName, string outDir, bool single,
bool baddumpCol, bool nodumpCol, StatDatFormat statDatFormat)
bool baddumpCol, bool nodumpCol, StatReportFormat statDatFormat)
{
// If there's no output format, set the default
if (statDatFormat == 0x0)
{
statDatFormat = StatDatFormat.None;
statDatFormat = StatReportFormat.None;
}
// Get the proper output file name
@@ -5818,7 +5706,7 @@ namespace SabreTools.Library.DatFiles
outDir = Path.GetFullPath(outDir);
// Get the dictionary of desired output report names
Dictionary<StatDatFormat, string> outputs = Style.CreateOutStatsNames(outDir, statDatFormat, reportName);
Dictionary<StatReportFormat, string> outputs = Style.CreateOutStatsNames(outDir, statDatFormat, reportName);
// Make sure we have all files and then order them
List<string> files = FileTools.GetOnlyFilesFromInputs(inputs);
@@ -5827,19 +5715,35 @@ namespace SabreTools.Library.DatFiles
.ThenBy(i => Path.GetFileName(i))
.ToList();
// Create output writers based on filenames
Dictionary<StatDatFormat, StreamWriter> writers = new Dictionary<StatDatFormat, StreamWriter>();
foreach (KeyValuePair<StatDatFormat, string> kvp in outputs)
// Get all of the writers that we need
List<BaseReport> reports = new List<BaseReport>();
// Loop through and output based on the inputs
foreach (KeyValuePair<StatReportFormat, string> kvp in outputs)
{
FileStream fs = FileTools.TryCreate(kvp.Value);
if (fs != null)
// Create the proper report for this format
BaseReport report = null;
switch (kvp.Key)
{
writers.Add(kvp.Key, new StreamWriter(fs));
case StatReportFormat.None:
report = new Textfile(null, kvp.Value, baddumpCol, nodumpCol);
break;
case StatReportFormat.CSV:
report = new Reports.SeparatedValue(null, kvp.Value, ',', baddumpCol, nodumpCol);
break;
case StatReportFormat.HTML:
report = new Html(null, kvp.Value, baddumpCol, nodumpCol);
break;
case StatReportFormat.TSV:
report = new Reports.SeparatedValue(null, kvp.Value, '\t', baddumpCol, nodumpCol);
break;
}
reports.Add(report);
}
// Write the header, if any
WriteStatsHeader(writers, statDatFormat, baddumpCol, nodumpCol);
reports.ForEach(report => report.WriteStatsHeader());
// Init all total variables
DatStats totalStats = new DatStats();
@@ -5860,7 +5764,7 @@ namespace SabreTools.Library.DatFiles
if (lastdir != null && thisdir != lastdir)
{
// Output separator if needed
WriteStatsMidSeparator(writers, statDatFormat, baddumpCol, nodumpCol);
reports.ForEach(report => report.WriteStatsMidSeparator());
DatFile lastdirdat = new DatFile
{
@@ -5868,13 +5772,15 @@ namespace SabreTools.Library.DatFiles
_datStats = dirStats,
};
lastdirdat.OutputStats(writers, statDatFormat, game: dirStats.GameCount, baddumpCol: baddumpCol, nodumpCol: nodumpCol);
lastdirdat.WriteStatsToScreen(recalculate: false, game: dirStats.GameCount, baddumpCol: baddumpCol, nodumpCol: nodumpCol);
reports.ForEach(report => report.ReplaceDatFile(lastdirdat));
reports.ForEach(report => report.Write(game: dirStats.GameCount));
// Write the mid-footer, if any
WriteStatsFooterSeparator(writers, statDatFormat, baddumpCol, nodumpCol);
reports.ForEach(report => report.WriteStatsFooterSeparator());
// Write the header, if any
WriteStatsMidHeader(writers, statDatFormat, baddumpCol, nodumpCol);
reports.ForEach(report => report.WriteStatsMidHeader());
// Reset the directory stats
dirStats.Reset();
@@ -5890,8 +5796,9 @@ namespace SabreTools.Library.DatFiles
Globals.Logger.User("Adding stats for file '{0}'\n", false, file);
if (single)
{
datdata.OutputStats(writers, statDatFormat,
baddumpCol: baddumpCol, nodumpCol: nodumpCol);
datdata.WriteStatsToScreen(recalculate: false, baddumpCol: baddumpCol, nodumpCol: nodumpCol);
reports.ForEach(report => report.ReplaceDatFile(datdata));
reports.ForEach(report => report.Write());
}
// Add single DAT stats to dir
@@ -5907,7 +5814,7 @@ namespace SabreTools.Library.DatFiles
}
// Output the directory stats one last time
WriteStatsMidSeparator(writers, statDatFormat, baddumpCol, nodumpCol);
reports.ForEach(report => report.WriteStatsMidSeparator());
if (single)
{
@@ -5917,14 +5824,16 @@ namespace SabreTools.Library.DatFiles
_datStats = dirStats,
};
dirdat.OutputStats(writers, statDatFormat, game: dirStats.GameCount, baddumpCol: baddumpCol, nodumpCol: nodumpCol);
dirdat.WriteStatsToScreen(recalculate: false, game: dirStats.GameCount, baddumpCol: baddumpCol, nodumpCol: nodumpCol);
reports.ForEach(report => report.ReplaceDatFile(dirdat));
reports.ForEach(report => report.Write(dirStats.GameCount));
}
// Write the mid-footer, if any
WriteStatsFooterSeparator(writers, statDatFormat, baddumpCol, nodumpCol);
reports.ForEach(report => report.WriteStatsFooterSeparator());
// Write the header, if any
WriteStatsMidHeader(writers, statDatFormat, baddumpCol, nodumpCol);
reports.ForEach(report => report.WriteStatsMidHeader());
// Reset the directory stats
dirStats.Reset();
@@ -5936,198 +5845,17 @@ namespace SabreTools.Library.DatFiles
_datStats = totalStats,
};
totaldata.OutputStats(writers, statDatFormat, game: totalStats.GameCount, baddumpCol: baddumpCol, nodumpCol: nodumpCol);
totaldata.WriteStatsToScreen(recalculate: false, game: totalStats.GameCount, baddumpCol: baddumpCol, nodumpCol: nodumpCol);
reports.ForEach(report => report.ReplaceDatFile(totaldata));
reports.ForEach(report => report.Write(totalStats.GameCount));
// Output footer if needed
WriteStatsFooter(writers, statDatFormat);
// Flush and dispose of the stream writers
foreach (StatDatFormat format in outputs.Keys)
{
writers[format].Flush();
writers[format].Dispose();
}
reports.ForEach(report => report.WriteStatsFooter());
Globals.Logger.User(@"
Please check the log folder if the stats scrolled offscreen", false);
}
/// <summary>
/// Write out the header to the stream, if any exists
/// </summary>
/// <param name="outputs">Dictionary representing the outputs</param>
/// <param name="statDatFormat">StatDatFormat representing output format</param>
/// <param name="baddumpCol">True if baddumps should be included in output, false otherwise</param>
/// <param name="nodumpCol">True if nodumps should be included in output, false otherwise</param>
private static void WriteStatsHeader(Dictionary<StatDatFormat, StreamWriter> outputs, StatDatFormat statDatFormat, bool baddumpCol, bool nodumpCol)
{
if (outputs.ContainsKey(StatDatFormat.None))
{
// Nothing
}
if (outputs.ContainsKey(StatDatFormat.CSV))
{
outputs[StatDatFormat.CSV].Write("\"File Name\",\"Total Size\",\"Games\",\"Roms\",\"Disks\",\"# with CRC\",\"# with MD5\",\"# with SHA-1\",\"# with SHA-256\""
+ (baddumpCol ? ",\"BadDumps\"" : "") + (nodumpCol ? ",\"Nodumps\"" : "") + "\n");
}
if (outputs.ContainsKey(StatDatFormat.HTML))
{
outputs[StatDatFormat.HTML].Write(@"<!DOCTYPE html>
<html>
<header>
<title>DAT Statistics Report</title>
<style>
body {
background-color: lightgray;
}
.dir {
color: #0088FF;
}
.right {
align: right;
}
</style>
</header>
<body>
<h2>DAT Statistics Report (" + DateTime.Now.ToShortDateString() + @")</h2>
<table border=""1"" cellpadding=""5"" cellspacing=""0"">
");
}
if (outputs.ContainsKey(StatDatFormat.TSV))
{
outputs[StatDatFormat.TSV].Write("\"File Name\"\t\"Total Size\"\t\"Games\"\t\"Roms\"\t\"Disks\"\t\"# with CRC\"\t\"# with MD5\"\t\"# with SHA-1\"\t\"# with SHA-256\""
+ (baddumpCol ? "\t\"BadDumps\"" : "") + (nodumpCol ? "\t\"Nodumps\"" : "") + "\n");
}
// Now write the mid header for those who need it
WriteStatsMidHeader(outputs, statDatFormat, baddumpCol, nodumpCol);
}
/// <summary>
/// Write out the mid-header to the stream, if any exists
/// </summary>
/// <param name="outputs">Dictionary representing the outputs</param>
/// <param name="statDatFormat">StatDatFormat representing output format</param>
/// <param name="baddumpCol">True if baddumps should be included in output, false otherwise</param>
/// <param name="nodumpCol">True if nodumps should be included in output, false otherwise</param>
private static void WriteStatsMidHeader(Dictionary<StatDatFormat, StreamWriter> outputs, StatDatFormat statDatFormat, bool baddumpCol, bool nodumpCol)
{
if (outputs.ContainsKey(StatDatFormat.None))
{
// Nothing
}
if (outputs.ContainsKey(StatDatFormat.CSV))
{
// Nothing
}
if (outputs.ContainsKey(StatDatFormat.HTML))
{
outputs[StatDatFormat.HTML].Write(@" <tr bgcolor=""gray""><th>File Name</th><th align=""right"">Total Size</th><th align=""right"">Games</th><th align=""right"">Roms</th>"
+ @"<th align=""right"">Disks</th><th align=""right"">&#35; with CRC</th><th align=""right"">&#35; with MD5</th><th align=""right"">&#35; with SHA-1</th><th align=""right"">&#35; with SHA-256</th>"
+ (baddumpCol ? "<th class=\".right\">Baddumps</th>" : "") + (nodumpCol ? "<th class=\".right\">Nodumps</th>" : "") + "</tr>\n");
}
if (outputs.ContainsKey(StatDatFormat.TSV))
{
// Nothing
}
}
/// <summary>
/// Write out the separator to the stream, if any exists
/// </summary>
/// <param name="outputs">Dictionary representing the outputs</param>
/// <param name="statDatFormat">StatDatFormat representing output format</param>
/// <param name="baddumpCol">True if baddumps should be included in output, false otherwise</param>
/// <param name="nodumpCol">True if nodumps should be included in output, false otherwise</param>
private static void WriteStatsMidSeparator(Dictionary<StatDatFormat, StreamWriter> outputs, StatDatFormat statDatFormat, bool baddumpCol, bool nodumpCol)
{
if (outputs.ContainsKey(StatDatFormat.None))
{
// Nothing
}
if (outputs.ContainsKey(StatDatFormat.CSV))
{
// Nothing
}
if (outputs.ContainsKey(StatDatFormat.HTML))
{
outputs[StatDatFormat.HTML].Write("<tr><td colspan=\""
+ (baddumpCol && nodumpCol
? "12"
: (baddumpCol ^ nodumpCol
? "11"
: "10")
)
+ "\"></td></tr>\n");
}
if (outputs.ContainsKey(StatDatFormat.TSV))
{
// Nothing
}
}
/// <summary>
/// Write out the footer-separator to the stream, if any exists
/// </summary>
/// <param name="outputs">Dictionary representing the outputs</param>
/// <param name="statDatFormat">StatDatFormat representing output format</param>
/// <param name="baddumpCol">True if baddumps should be included in output, false otherwise</param>
/// <param name="nodumpCol">True if nodumps should be included in output, false otherwise</param>
private static void WriteStatsFooterSeparator(Dictionary<StatDatFormat, StreamWriter> outputs, StatDatFormat statDatFormat, bool baddumpCol, bool nodumpCol)
{
if (outputs.ContainsKey(StatDatFormat.None))
{
outputs[StatDatFormat.None].Write("\n");
}
if (outputs.ContainsKey(StatDatFormat.CSV))
{
outputs[StatDatFormat.CSV].Write("\n");
}
if (outputs.ContainsKey(StatDatFormat.HTML))
{
outputs[StatDatFormat.HTML].Write("<tr border=\"0\"><td colspan=\""
+ (baddumpCol && nodumpCol
? "12"
: (baddumpCol ^ nodumpCol
? "11"
: "10")
)
+ "\"></td></tr>\n");
}
if (outputs.ContainsKey(StatDatFormat.TSV))
{
outputs[StatDatFormat.TSV].Write("\n");
}
}
/// <summary>
/// Write out the footer to the stream, if any exists
/// </summary>
/// <param name="sw">StreamWriter representing the output</param>
/// <param name="statDatFormat">StatDatFormat representing output format</param>
private static void WriteStatsFooter(Dictionary<StatDatFormat, StreamWriter> outputs, StatDatFormat statDatFormat)
{
if (outputs.ContainsKey(StatDatFormat.None))
{
// Nothing
}
if (outputs.ContainsKey(StatDatFormat.CSV))
{
// Nothing
}
if (outputs.ContainsKey(StatDatFormat.HTML))
{
outputs[StatDatFormat.HTML].Write(@" </table>
</body>
</html>
");
}
if (outputs.ContainsKey(StatDatFormat.TSV))
{
// Nothing
}
}
#endregion
#endregion // Static Methods

View File

@@ -246,12 +246,14 @@ namespace SabreTools.Library.Data
/// Determine which format to output Stats to
/// </summary>
/// [Flags]
public enum StatDatFormat
public enum StatReportFormat
{
None = 0x01,
HTML = None << 1,
CSV = HTML << 1,
TSV = CSV << 1,
All = None | HTML | CSV | TSV,
}
/// <summary>

View File

@@ -810,6 +810,9 @@ Options:
- Roms that include a SHA-1
- Roms with Nodump status
-as, --all-stats Write all statistics to all available formats
Output all rom information to all available formats
-bc, --baddump-col Add statistics for baddumps to output
Add a new column or field for counting the number of baddumps in the
DAT
@@ -839,6 +842,10 @@ Options:
-tsv, --tsv Output in Tab-Separated Value format
Output all rom information in standardized TSV format
-txt, --text Output in generic text format
Output all rom information in generic text format. If no other format
flags are enabled, this is the default output.
-ts, --type-split Split DAT(s) or folder by file types (rom/disk)
For a DAT, or set of DATs, allow for splitting based on the types of the

View File

@@ -0,0 +1,108 @@
using System;
using SabreTools.Library.DatFiles;
using SabreTools.Library.Tools;
#if MONO
using System.IO;
#else
using Alphaleonis.Win32.Filesystem;
using FileStream = System.IO.FileStream;
using IOException = System.IO.IOException;
using MemoryStream = System.IO.MemoryStream;
using SearchOption = System.IO.SearchOption;
using SeekOrigin = System.IO.SeekOrigin;
using Stream = System.IO.Stream;
using StreamWriter = System.IO.StreamWriter;
#endif
namespace SabreTools.Library.Reports
{
/// <summary>
/// Base class for a report output format
/// </summary>
public abstract class BaseReport
{
protected DatFile _datFile;
protected StreamWriter _writer;
protected bool _baddumpCol;
protected bool _nodumpCol;
/// <summary>
/// Create a new report from the input DatFile and the filename
/// </summary>
/// <param name="datfile">DatFile to write out statistics for</param>
/// <param name="filename">Name of the file to write out to</param>
/// <param name="baddumpCol">True if baddumps should be included in output, false otherwise</param>
/// <param name="nodumpCol">True if nodumps should be included in output, false otherwise</param>
public BaseReport(DatFile datfile, string filename, bool baddumpCol = false, bool nodumpCol = false)
{
_datFile = datfile;
_writer = new StreamWriter(FileTools.TryCreate(filename));
_baddumpCol = baddumpCol;
_nodumpCol = nodumpCol;
}
/// <summary>
/// Create a new report from the input DatFile and the stream
/// </summary>
/// <param name="datfile">DatFile to write out statistics for</param>
/// <param name="stream">Output stream to write to</param>
/// <param name="baddumpCol">True if baddumps should be included in output, false otherwise</param>
/// <param name="nodumpCol">True if nodumps should be included in output, false otherwise</param>
public BaseReport(DatFile datfile, Stream stream, bool baddumpCol = false, bool nodumpCol = false)
{
_datFile = datfile;
if (!stream.CanWrite)
{
throw new ArgumentException(nameof(stream));
}
_writer = new StreamWriter(stream);
_baddumpCol = baddumpCol;
_nodumpCol = nodumpCol;
}
/// <summary>
/// Replace the DatFile that is being output
/// </summary>
/// <param name="datfile"></param>
public void ReplaceDatFile(DatFile datfile)
{
_datFile = datfile;
}
/// <summary>
/// Write the report to the output stream
/// </summary>
/// <param name="game">Number of games to use, -1 means use the number of keys</param>
public abstract void Write(long game = -1);
/// <summary>
/// Write out the header to the stream, if any exists
/// </summary>
public abstract void WriteStatsHeader();
/// <summary>
/// Write out the mid-header to the stream, if any exists
/// </summary>
public abstract void WriteStatsMidHeader();
/// <summary>
/// Write out the separator to the stream, if any exists
/// </summary>
public abstract void WriteStatsMidSeparator();
/// <summary>
/// Write out the footer-separator to the stream, if any exists
/// </summary>
public abstract void WriteStatsFooterSeparator();
/// <summary>
/// Write out the footer to the stream, if any exists
/// </summary>
public abstract void WriteStatsFooter();
}
}

View File

@@ -0,0 +1,163 @@
using System;
using System.Linq;
using System.Web;
using SabreTools.Library.DatFiles;
using SabreTools.Library.Tools;
#if MONO
using System.IO;
#else
using Alphaleonis.Win32.Filesystem;
using FileStream = System.IO.FileStream;
using IOException = System.IO.IOException;
using MemoryStream = System.IO.MemoryStream;
using SearchOption = System.IO.SearchOption;
using SeekOrigin = System.IO.SeekOrigin;
using Stream = System.IO.Stream;
using StreamWriter = System.IO.StreamWriter;
#endif
namespace SabreTools.Library.Reports
{
/// <summary>
/// HTML report format
/// </summary>
public class Html : BaseReport
{
/// <summary>
/// Create a new report from the input DatFile and the filename
/// </summary>
/// <param name="datfile">DatFile to write out statistics for</param>
/// <param name="filename">Name of the file to write out to</param>
/// <param name="baddumpCol">True if baddumps should be included in output, false otherwise</param>
/// <param name="nodumpCol">True if nodumps should be included in output, false otherwise</param>
public Html(DatFile datfile, string filename, bool baddumpCol = false, bool nodumpCol = false)
: base(datfile, filename, baddumpCol, nodumpCol)
{
}
/// <summary>
/// Create a new report from the input DatFile and the stream
/// </summary>
/// <param name="datfile">DatFile to write out statistics for</param>
/// <param name="stream">Output stream to write to</param>
/// <param name="baddumpCol">True if baddumps should be included in output, false otherwise</param>
/// <param name="nodumpCol">True if nodumps should be included in output, false otherwise</param>
public Html(DatFile datfile, Stream stream, bool baddumpCol = false, bool nodumpCol = false)
: base(datfile, stream, baddumpCol, nodumpCol)
{
}
/// <summary>
/// Write the report to file
/// </summary>
/// <param name="game">Number of games to use, -1 means use the number of keys</param>
public override void Write(long game = -1)
{
string line = "\t\t\t<tr" + (_datFile.FileName.StartsWith("DIR: ")
? " class=\"dir\"><td>" + HttpUtility.HtmlEncode(_datFile.FileName.Remove(0, 5))
: "><td>" + HttpUtility.HtmlEncode(_datFile.FileName)) + "</td>"
+ "<td align=\"right\">" + Style.GetBytesReadable(_datFile.TotalSize) + "</td>"
+ "<td align=\"right\">" + (game == -1 ? _datFile.Keys.Count() : game) + "</td>"
+ "<td align=\"right\">" + _datFile.RomCount + "</td>"
+ "<td align=\"right\">" + _datFile.DiskCount + "</td>"
+ "<td align=\"right\">" + _datFile.CRCCount + "</td>"
+ "<td align=\"right\">" + _datFile.MD5Count + "</td>"
+ "<td align=\"right\">" + _datFile.SHA1Count + "</td>"
+ "<td align=\"right\">" + _datFile.SHA256Count + "</td>"
+ (_baddumpCol ? "<td align=\"right\">" + _datFile.BaddumpCount + "</td>" : "")
+ (_nodumpCol ? "<td align=\"right\">" + _datFile.NodumpCount + "</td>" : "")
+ "</tr>\n";
_writer.Write(line);
_writer.Flush();
}
/// <summary>
/// Write out the header to the stream, if any exists
/// </summary>
public override void WriteStatsHeader()
{
_writer.Write(@"<!DOCTYPE html>
<html>
<header>
<title>DAT Statistics Report</title>
<style>
body {
background-color: lightgray;
}
.dir {
color: #0088FF;
}
.right {
align: right;
}
</style>
</header>
<body>
<h2>DAT Statistics Report (" + DateTime.Now.ToShortDateString() + @")</h2>
<table border=""1"" cellpadding=""5"" cellspacing=""0"">
");
_writer.Flush();
// Now write the mid header for those who need it
WriteStatsMidHeader();
}
/// <summary>
/// Write out the mid-header to the stream, if any exists
/// </summary>
public override void WriteStatsMidHeader()
{
_writer.Write(@" <tr bgcolor=""gray""><th>File Name</th><th align=""right"">Total Size</th><th align=""right"">Games</th><th align=""right"">Roms</th>"
+ @"<th align=""right"">Disks</th><th align=""right"">&#35; with CRC</th><th align=""right"">&#35; with MD5</th><th align=""right"">&#35; with SHA-1</th><th align=""right"">&#35; with SHA-256</th>"
+ (_baddumpCol ? "<th class=\".right\">Baddumps</th>" : "") + (_nodumpCol ? "<th class=\".right\">Nodumps</th>" : "") + "</tr>\n");
_writer.Flush();
}
/// <summary>
/// Write out the separator to the stream, if any exists
/// </summary>
public override void WriteStatsMidSeparator()
{
_writer.Write("<tr><td colspan=\""
+ (_baddumpCol && _nodumpCol
? "12"
: (_baddumpCol ^ _nodumpCol
? "11"
: "10")
)
+ "\"></td></tr>\n");
_writer.Flush();
}
/// <summary>
/// Write out the footer-separator to the stream, if any exists
/// </summary>
public override void WriteStatsFooterSeparator()
{
_writer.Write("<tr border=\"0\"><td colspan=\""
+ (_baddumpCol && _nodumpCol
? "12"
: (_baddumpCol ^ _nodumpCol
? "11"
: "10")
)
+ "\"></td></tr>\n");
_writer.Flush();
}
/// <summary>
/// Write out the footer to the stream, if any exists
/// </summary>
public override void WriteStatsFooter()
{
_writer.Write(@" </table>
</body>
</html>
");
_writer.Flush();
}
}
}

View File

@@ -0,0 +1,124 @@
using System.Linq;
using SabreTools.Library.DatFiles;
#if MONO
using System.IO;
#else
using Alphaleonis.Win32.Filesystem;
using FileStream = System.IO.FileStream;
using IOException = System.IO.IOException;
using MemoryStream = System.IO.MemoryStream;
using SearchOption = System.IO.SearchOption;
using SeekOrigin = System.IO.SeekOrigin;
using Stream = System.IO.Stream;
using StreamWriter = System.IO.StreamWriter;
#endif
namespace SabreTools.Library.Reports
{
/// <summary>
/// Separated-Value report format
/// </summary>
public class SeparatedValue : BaseReport
{
private char _separator;
/// <summary>
/// Create a new report from the input DatFile and the filename
/// </summary>
/// <param name="datfile">DatFile to write out statistics for</param>
/// <param name="filename">Name of the file to write out to</param>
/// <param name="separator">Separator character to use in output</param>
/// <param name="baddumpCol">True if baddumps should be included in output, false otherwise</param>
/// <param name="nodumpCol">True if nodumps should be included in output, false otherwise</param>
public SeparatedValue(DatFile datfile, string filename, char separator, bool baddumpCol = false, bool nodumpCol = false)
: base(datfile, filename, baddumpCol, nodumpCol)
{
_separator = separator;
}
/// <summary>
/// Create a new report from the input DatFile and the stream
/// </summary>
/// <param name="datfile">DatFile to write out statistics for</param>
/// <param name="stream">Output stream to write to</param>
/// <param name="separator">Separator character to use in output</param>
/// <param name="baddumpCol">True if baddumps should be included in output, false otherwise</param>
/// <param name="nodumpCol">True if nodumps should be included in output, false otherwise</param>
public SeparatedValue(DatFile datfile, Stream stream, char separator, bool baddumpCol = false, bool nodumpCol = false)
: base(datfile, stream, baddumpCol, nodumpCol)
{
_separator = separator;
}
/// <summary>
/// Write the report to file
/// </summary>
/// <param name="game">Number of games to use, -1 means use the number of keys</param>
public override void Write(long game = -1)
{
string line = string.Format("\"" + _datFile.FileName + "\"{0}"
+ "\"" + _datFile.TotalSize + "\"{0}"
+ "\"" + (game == -1 ? _datFile.Keys.Count() : game) + "\"{0}"
+ "\"" + _datFile.RomCount + "\"{0}"
+ "\"" + _datFile.DiskCount + "\"{0}"
+ "\"" + _datFile.CRCCount + "\"{0}"
+ "\"" + _datFile.MD5Count + "\"{0}"
+ "\"" + _datFile.SHA1Count + "\"{0}"
+ "\"" + _datFile.SHA256Count + "\"{0}"
+ "\"" + _datFile.SHA384Count + "\"{0}"
+ "\"" + _datFile.SHA512Count + "\""
+ (_baddumpCol ? "{0}\"" + _datFile.BaddumpCount + "\"" : "")
+ (_nodumpCol ? "{0}\"" + _datFile.BaddumpCount + "\"" : "")
+ "\n", _separator);
_writer.Write(line);
_writer.Flush();
}
/// <summary>
/// Write out the header to the stream, if any exists
/// </summary>
public override void WriteStatsHeader()
{
_writer.Write(string.Format("\"File Name\"{0}\"Total Size\"{0}\"Games\"{0}\"Roms\"{0}\"Disks\"{0}\"# with CRC\"{0}\"# with MD5\"{0}\"# with SHA-1\"{0}\"# with SHA-256\""
+ (_baddumpCol ? "{0}\"BadDumps\"" : "") + (_nodumpCol ? "{0}\"Nodumps\"" : "") + "\n", _separator));
_writer.Flush();
}
/// <summary>
/// Write out the mid-header to the stream, if any exists
/// </summary>
public override void WriteStatsMidHeader()
{
// This call is a no-op for separated value formats
}
/// <summary>
/// Write out the separator to the stream, if any exists
/// </summary>
public override void WriteStatsMidSeparator()
{
// This call is a no-op for separated value formats
}
/// <summary>
/// Write out the footer-separator to the stream, if any exists
/// </summary>
public override void WriteStatsFooterSeparator()
{
_writer.Write("\n");
_writer.Flush();
}
/// <summary>
/// Write out the footer to the stream, if any exists
/// </summary>
public override void WriteStatsFooter()
{
// This call is a no-op for separated value formats
}
}
}

View File

@@ -0,0 +1,126 @@
using System.Linq;
using SabreTools.Library.DatFiles;
using SabreTools.Library.Tools;
#if MONO
using System.IO;
#else
using Alphaleonis.Win32.Filesystem;
using FileStream = System.IO.FileStream;
using IOException = System.IO.IOException;
using MemoryStream = System.IO.MemoryStream;
using SearchOption = System.IO.SearchOption;
using SeekOrigin = System.IO.SeekOrigin;
using Stream = System.IO.Stream;
using StreamWriter = System.IO.StreamWriter;
#endif
namespace SabreTools.Library.Reports
{
/// <summary>
/// Textfile report format
/// </summary>
public class Textfile : BaseReport
{
/// <summary>
/// Create a new report from the input DatFile and the filename
/// </summary>
/// <param name="datfile">DatFile to write out statistics for</param>
/// <param name="filename">Name of the file to write out to</param>
/// <param name="baddumpCol">True if baddumps should be included in output, false otherwise</param>
/// <param name="nodumpCol">True if nodumps should be included in output, false otherwise</param>
public Textfile(DatFile datfile, string filename, bool baddumpCol = false, bool nodumpCol = false)
: base(datfile, filename, baddumpCol, nodumpCol)
{
}
/// <summary>
/// Create a new report from the input DatFile and the stream
/// </summary>
/// <param name="datfile">DatFile to write out statistics for</param>
/// <param name="stream">Output stream to write to</param>
/// <param name="baddumpCol">True if baddumps should be included in output, false otherwise</param>
/// <param name="nodumpCol">True if nodumps should be included in output, false otherwise</param>
public Textfile(DatFile datfile, Stream stream, bool baddumpCol = false, bool nodumpCol = false)
: base(datfile, stream, baddumpCol, nodumpCol)
{
}
/// <summary>
/// Write the report to file
/// </summary>
/// <param name="game">Number of games to use, -1 means use the number of keys</param>
public override void Write(long game = -1)
{
string line = @"'" + _datFile.FileName + @"':
--------------------------------------------------
Uncompressed size: " + Style.GetBytesReadable(_datFile.TotalSize) + @"
Games found: " + (game == -1 ? _datFile.Keys.Count() : game) + @"
Roms found: " + _datFile.RomCount + @"
Disks found: " + _datFile.DiskCount + @"
Roms with CRC: " + _datFile.CRCCount + @"
Roms with SHA-1: " + _datFile.SHA1Count + @"
Roms with SHA-256: " + _datFile.SHA256Count + @"
Roms with SHA-384: " + _datFile.SHA384Count + @"
Roms with SHA-512: " + _datFile.SHA512Count + "\n";
if (_baddumpCol)
{
line += " Roms with BadDump status: " + _datFile.BaddumpCount + "\n";
}
if (_nodumpCol)
{
line += " Roms with Nodump status: " + _datFile.NodumpCount + "\n";
}
// For spacing between DATs
line += "\n\n";
_writer.Write(line);
_writer.Flush();
}
/// <summary>
/// Write out the header to the stream, if any exists
/// </summary>
public override void WriteStatsHeader()
{
// This call is a no-op for textfile output
}
/// <summary>
/// Write out the mid-header to the stream, if any exists
/// </summary>
public override void WriteStatsMidHeader()
{
// This call is a no-op for textfile output
}
/// <summary>
/// Write out the separator to the stream, if any exists
/// </summary>
public override void WriteStatsMidSeparator()
{
// This call is a no-op for textfile output
}
/// <summary>
/// Write out the footer-separator to the stream, if any exists
/// </summary>
public override void WriteStatsFooterSeparator()
{
_writer.Write("\n");
_writer.Flush();
}
/// <summary>
/// Write out the footer to the stream, if any exists
/// </summary>
public override void WriteStatsFooter()
{
// This call is a no-op for textfile output
}
}
}

View File

@@ -175,6 +175,10 @@
<Compile Include="External\SupportedFiles\ZipFile.cs" />
<Compile Include="Help\Feature.cs" />
<Compile Include="Help\Help.cs" />
<Compile Include="Reports\BaseReport.cs" />
<Compile Include="Reports\Html.cs" />
<Compile Include="Reports\SeparatedValue.cs" />
<Compile Include="Reports\Textfile.cs" />
<Compile Include="Skippers\Skipper.cs" />
<Compile Include="Skippers\SkipperRule.cs" />
<Compile Include="Tools\InternalStopwatch.cs" />

View File

@@ -331,9 +331,9 @@ namespace SabreTools.Library.Tools
/// <param name="statDatFormat">StatDatFormat to get the extension for</param>
/// <param name="reportName">Name of the input file to use</param>
/// <returns>Dictionary of output formats mapped to file names</returns>
public static Dictionary<StatDatFormat, string> CreateOutStatsNames(string outDir, StatDatFormat statDatFormat, string reportName, bool overwrite = true)
public static Dictionary<StatReportFormat, string> CreateOutStatsNames(string outDir, StatReportFormat statDatFormat, string reportName, bool overwrite = true)
{
Dictionary<StatDatFormat, string> output = new Dictionary<StatDatFormat, string>();
Dictionary<StatReportFormat, string> output = new Dictionary<StatReportFormat, string>();
// First try to create the output directory if we need to
if (!Directory.Exists(outDir))
@@ -348,21 +348,21 @@ namespace SabreTools.Library.Tools
}
// For each output format, get the appropriate stream writer
if ((statDatFormat & StatDatFormat.None) != 0)
if ((statDatFormat & StatReportFormat.None) != 0)
{
output.Add(StatDatFormat.None, CreateOutStatsNamesHelper(outDir, ".txt", reportName, overwrite));
output.Add(StatReportFormat.None, CreateOutStatsNamesHelper(outDir, ".txt", reportName, overwrite));
}
if ((statDatFormat & StatDatFormat.CSV) != 0)
if ((statDatFormat & StatReportFormat.CSV) != 0)
{
output.Add(StatDatFormat.CSV, CreateOutStatsNamesHelper(outDir, ".csv", reportName, overwrite));
output.Add(StatReportFormat.CSV, CreateOutStatsNamesHelper(outDir, ".csv", reportName, overwrite));
}
if ((statDatFormat & StatDatFormat.HTML) != 0)
if ((statDatFormat & StatReportFormat.HTML) != 0)
{
output.Add(StatDatFormat.HTML, CreateOutStatsNamesHelper(outDir, ".html", reportName, overwrite));
output.Add(StatReportFormat.HTML, CreateOutStatsNamesHelper(outDir, ".html", reportName, overwrite));
}
if ((statDatFormat & StatDatFormat.TSV) != 0)
if ((statDatFormat & StatReportFormat.TSV) != 0)
{
output.Add(StatDatFormat.TSV, CreateOutStatsNamesHelper(outDir, ".tsv", reportName, overwrite));
output.Add(StatReportFormat.TSV, CreateOutStatsNamesHelper(outDir, ".tsv", reportName, overwrite));
}
return output;

View File

@@ -738,10 +738,15 @@ namespace SabreTools
FeatureType.Flag,
null));
stats.AddFeature("tsv", new Feature(
new List<string>() { "-tsv", "--csv" },
new List<string>() { "-tsv", "--tsv" },
"Output in Tab-Separated Value format",
FeatureType.Flag,
null));
stats.AddFeature("text", new Feature(
new List<string>() { "-txt", "--text" },
"Output in generic text format",
FeatureType.Flag,
null));
// Create the Type Split feature
Feature typeSplit = new Feature(

View File

@@ -404,7 +404,7 @@ namespace SabreTools
/// <param name="nodumpCol">True if nodumps should be included in output, false otherwise</param>
/// <param name="statDatFormat">Set the statistics output format to use</param>
private static void InitStats(List<string> inputs, string filename, string outDir, bool single, bool baddumpCol, bool nodumpCol,
StatDatFormat statDatFormat)
StatReportFormat statDatFormat)
{
DatFile.OutputStats(inputs, filename, outDir, single, baddumpCol, nodumpCol, statDatFormat);
}

View File

@@ -128,7 +128,7 @@ namespace SabreTools
OutputFormat outputFormat = OutputFormat.Folder;
SkipFileType skipFileType = SkipFileType.None;
SplitType splitType = SplitType.None;
StatDatFormat statDatFormat = 0x0;
StatReportFormat statDatFormat = 0x0;
UpdateMode updateMode = UpdateMode.None;
// User inputs
@@ -293,6 +293,10 @@ namespace SabreTools
case "--against":
updateMode |= UpdateMode.DiffAgainst;
break;
case "-as":
case "--all-stats":
statDatFormat = StatReportFormat.All;
break;
case "-b":
case "--bare":
removeDateFromAutomaticName = true;
@@ -323,7 +327,7 @@ namespace SabreTools
break;
case "-csv":
case "--csv":
statDatFormat |= StatDatFormat.CSV;
statDatFormat |= StatReportFormat.CSV;
break;
case "-dan":
case "--desc-name":
@@ -391,7 +395,7 @@ namespace SabreTools
break;
case "-html":
case "--html":
statDatFormat |= StatDatFormat.HTML;
statDatFormat |= StatReportFormat.HTML;
break;
case "-ic":
case "--ignore-chd":
@@ -631,12 +635,16 @@ namespace SabreTools
break;
case "-tsv":
case "--tsv":
statDatFormat |= StatDatFormat.TSV;
statDatFormat |= StatReportFormat.TSV;
break;
case "-txz":
case "--txz":
outputFormat = OutputFormat.TorrentXZ;
break;
case "-txt":
case "--text":
statDatFormat |= StatReportFormat.None;
break;
case "-tzip":
case "--tzip":
outputFormat = OutputFormat.TorrentZip;