mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
[SabreTools, DatFile, Reports] Fix and upgrade stats output
This commit is contained in:
108
SabreTools.Library/Reports/BaseReport.cs
Normal file
108
SabreTools.Library/Reports/BaseReport.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
163
SabreTools.Library/Reports/Html.cs
Normal file
163
SabreTools.Library/Reports/Html.cs
Normal 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""># with CRC</th><th align=""right""># with MD5</th><th align=""right""># with SHA-1</th><th align=""right""># 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
124
SabreTools.Library/Reports/SeparatedValue.cs
Normal file
124
SabreTools.Library/Reports/SeparatedValue.cs
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
126
SabreTools.Library/Reports/Textfile.cs
Normal file
126
SabreTools.Library/Reports/Textfile.cs
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user