mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Statistics Collection / Writing Overhaul (#35)
* Add DatStatistics class * Add isDirectory setting * Add CalculateStatistics method (nw) * Add separate stats writing * Use new methods * Rename Write -> WriteIndividual * Naive implementation of new writing (nw) * Remove unncessary calls * Make writing more DatFile-like * Add console flag to constructor * Remove unused stream constructors * Move to local writers * Remove inherent filename * Fix invocation * Use SeparatedValueWriter * Fix final directory stats output * Use XmlTextWriter for HTML * Don't output separator on last stat output * Remove now-completed TODOs * Remove unused using
This commit is contained in:
@@ -1,4 +1,10 @@
|
||||
using System.IO;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
using SabreTools.IO.Writers;
|
||||
using SabreTools.Logging;
|
||||
|
||||
namespace SabreTools.Reports.Formats
|
||||
{
|
||||
@@ -12,94 +18,142 @@ namespace SabreTools.Reports.Formats
|
||||
/// <summary>
|
||||
/// Create a new report from the filename
|
||||
/// </summary>
|
||||
/// <param name="filename">Name of the file to write out to</param>
|
||||
/// <param name="statsList">List of statistics objects to set</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(string filename, char separator, bool baddumpCol = false, bool nodumpCol = false)
|
||||
: base(filename, baddumpCol, nodumpCol)
|
||||
public SeparatedValue(List<DatStatistics> statsList, char separator)
|
||||
: base(statsList)
|
||||
{
|
||||
_separator = separator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new report from the input DatFile and the stream
|
||||
/// </summary>
|
||||
/// <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(Stream stream, char separator, bool baddumpCol = false, bool nodumpCol = false)
|
||||
: base(stream, baddumpCol, nodumpCol)
|
||||
/// <inheritdoc/>
|
||||
public override bool WriteToFile(string outfile, bool baddumpCol, bool nodumpCol, bool throwOnError = false)
|
||||
{
|
||||
_separator = separator;
|
||||
}
|
||||
InternalStopwatch watch = new InternalStopwatch($"Writing statistics to '{outfile}");
|
||||
|
||||
/// <summary>
|
||||
/// Write the report to file
|
||||
/// </summary>
|
||||
public override void Write()
|
||||
{
|
||||
string line = string.Format("\"" + _name + "\"{0}"
|
||||
+ "\"" + _stats.TotalSize + "\"{0}"
|
||||
+ "\"" + _machineCount + "\"{0}"
|
||||
+ "\"" + _stats.RomCount + "\"{0}"
|
||||
+ "\"" + _stats.DiskCount + "\"{0}"
|
||||
+ "\"" + _stats.CRCCount + "\"{0}"
|
||||
+ "\"" + _stats.MD5Count + "\"{0}"
|
||||
+ "\"" + _stats.SHA1Count + "\"{0}"
|
||||
+ "\"" + _stats.SHA256Count + "\"{0}"
|
||||
+ "\"" + _stats.SHA384Count + "\"{0}"
|
||||
+ "\"" + _stats.SHA512Count + "\""
|
||||
+ (_baddumpCol ? "{0}\"" + _stats.BaddumpCount + "\"" : string.Empty)
|
||||
+ (_nodumpCol ? "{0}\"" + _stats.NodumpCount + "\"" : string.Empty)
|
||||
+ "\n", _separator);
|
||||
try
|
||||
{
|
||||
// Try to create the output file
|
||||
FileStream fs = File.Create(outfile);
|
||||
if (fs == null)
|
||||
{
|
||||
logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
|
||||
return false;
|
||||
}
|
||||
|
||||
_writer.Write(line);
|
||||
_writer.Flush();
|
||||
SeparatedValueWriter svw = new SeparatedValueWriter(fs, Encoding.UTF8)
|
||||
{
|
||||
Separator = _separator,
|
||||
Quotes = true,
|
||||
};
|
||||
|
||||
// Write out the header
|
||||
WriteHeader(svw, baddumpCol, nodumpCol);
|
||||
|
||||
// Now process each of the statistics
|
||||
for (int i = 0; i < Statistics.Count; i++)
|
||||
{
|
||||
// Get the current statistic
|
||||
DatStatistics stat = Statistics[i];
|
||||
|
||||
// If we have a directory statistic
|
||||
if (stat.IsDirectory)
|
||||
{
|
||||
WriteIndividual(svw, stat, baddumpCol, nodumpCol);
|
||||
|
||||
// If we have anything but the last value, write the separator
|
||||
if (i < Statistics.Count - 1)
|
||||
WriteFooterSeparator(svw);
|
||||
}
|
||||
|
||||
// If we have a normal statistic
|
||||
else
|
||||
{
|
||||
WriteIndividual(svw, stat, baddumpCol, nodumpCol);
|
||||
}
|
||||
}
|
||||
|
||||
svw.Dispose();
|
||||
fs.Dispose();
|
||||
}
|
||||
catch (Exception ex) when (!throwOnError)
|
||||
{
|
||||
logger.Error(ex);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
watch.Stop();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write out the header to the stream, if any exists
|
||||
/// </summary>
|
||||
public override void WriteHeader()
|
||||
/// <param name="svw">SeparatedValueWriter 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>
|
||||
private void WriteHeader(SeparatedValueWriter svw, bool baddumpCol, bool nodumpCol)
|
||||
{
|
||||
_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\"" : string.Empty) + (_nodumpCol ? "{0}\"Nodumps\"" : string.Empty) + "\n", _separator));
|
||||
_writer.Flush();
|
||||
string[] headers = new string[]
|
||||
{
|
||||
"File Name",
|
||||
"Total Size",
|
||||
"Games",
|
||||
"Roms",
|
||||
"Disks",
|
||||
"# with CRC",
|
||||
"# with MD5",
|
||||
"# with SHA-1",
|
||||
"# with SHA-256",
|
||||
"# with SHA-384",
|
||||
"# with SHA-512",
|
||||
baddumpCol ? "BadDumps" : string.Empty,
|
||||
nodumpCol ? "Nodumps" : string.Empty,
|
||||
};
|
||||
svw.WriteHeader(headers);
|
||||
svw.Flush();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write out the mid-header to the stream, if any exists
|
||||
/// Write a single set of statistics
|
||||
/// </summary>
|
||||
public override void WriteMidHeader()
|
||||
/// <param name="svw">SeparatedValueWriter to write to</param>
|
||||
/// <param name="stat">DatStatistics object to write out</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 void WriteIndividual(SeparatedValueWriter svw, DatStatistics stat, bool baddumpCol, bool nodumpCol)
|
||||
{
|
||||
// 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 WriteMidSeparator()
|
||||
{
|
||||
// This call is a no-op for separated value formats
|
||||
string[] values = new string[]
|
||||
{
|
||||
stat.DisplayName,
|
||||
stat.Statistics.TotalSize.ToString(),
|
||||
stat.MachineCount.ToString(),
|
||||
stat.Statistics.RomCount.ToString(),
|
||||
stat.Statistics.DiskCount.ToString(),
|
||||
stat.Statistics.CRCCount.ToString(),
|
||||
stat.Statistics.MD5Count.ToString(),
|
||||
stat.Statistics.SHA1Count.ToString(),
|
||||
stat.Statistics.SHA256Count.ToString(),
|
||||
stat.Statistics.SHA384Count.ToString(),
|
||||
stat.Statistics.SHA512Count.ToString(),
|
||||
baddumpCol ? stat.Statistics.BaddumpCount.ToString() : string.Empty,
|
||||
nodumpCol ? stat.Statistics.NodumpCount.ToString() : string.Empty,
|
||||
};
|
||||
svw.WriteValues(values);
|
||||
svw.Flush();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write out the footer-separator to the stream, if any exists
|
||||
/// </summary>
|
||||
public override void WriteFooterSeparator()
|
||||
/// <param name="svw">SeparatedValueWriter to write to</param>
|
||||
private void WriteFooterSeparator(SeparatedValueWriter svw)
|
||||
{
|
||||
_writer.Write("\n");
|
||||
_writer.Flush();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write out the footer to the stream, if any exists
|
||||
/// </summary>
|
||||
public override void WriteFooter()
|
||||
{
|
||||
// This call is a no-op for separated value formats
|
||||
svw.WriteString("\n");
|
||||
svw.Flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user