Files
SabreTools/SabreTools.Reports/Formats/SeparatedValue.cs

160 lines
5.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using SabreTools.IO.Writers;
using SabreTools.Logging;
2020-12-11 10:10:56 -08:00
namespace SabreTools.Reports.Formats
{
2019-02-08 20:53:13 -08:00
/// <summary>
/// Separated-Value report format
/// </summary>
internal class SeparatedValue : BaseReport
{
2020-12-08 13:23:59 -08:00
private readonly char _separator;
2019-02-08 20:53:13 -08:00
/// <summary>
/// Create a new report from the filename
2019-02-08 20:53:13 -08:00
/// </summary>
/// <param name="statsList">List of statistics objects to set</param>
2019-02-08 20:53:13 -08:00
/// <param name="separator">Separator character to use in output</param>
public SeparatedValue(List<DatStatistics> statsList, char separator)
: base(statsList)
2019-02-08 20:53:13 -08:00
{
_separator = separator;
}
/// <inheritdoc/>
2024-02-28 19:19:50 -05:00
public override bool WriteToFile(string? outfile, bool baddumpCol, bool nodumpCol, bool throwOnError = false)
2019-02-08 20:53:13 -08:00
{
InternalStopwatch watch = new($"Writing statistics to '{outfile}");
try
{
// Try to create the output file
2024-02-28 19:19:50 -05:00
FileStream fs = File.Create(outfile ?? string.Empty);
if (fs == null)
{
logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
return false;
}
SeparatedValueWriter svw = new(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;
2019-02-08 20:53:13 -08:00
}
2019-02-08 20:53:13 -08:00
/// <summary>
/// Write out the header to the stream, if any exists
2019-02-08 20:53:13 -08:00
/// </summary>
/// <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)
2019-02-08 20:53:13 -08:00
{
2024-02-28 19:19:50 -05:00
string[] headers =
[
"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,
2024-02-28 19:19:50 -05:00
];
svw.WriteHeader(headers);
svw.Flush();
2019-02-08 20:53:13 -08:00
}
2019-02-08 20:53:13 -08:00
/// <summary>
/// Write a single set of statistics
2019-02-08 20:53:13 -08:00
/// </summary>
/// <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)
2019-02-08 20:53:13 -08:00
{
2024-02-28 19:19:50 -05:00
string[] values =
[
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,
2024-02-28 19:19:50 -05:00
];
svw.WriteValues(values);
svw.Flush();
2019-02-08 20:53:13 -08:00
}
2019-02-08 20:53:13 -08:00
/// <summary>
/// Write out the footer-separator to the stream, if any exists
2019-02-08 20:53:13 -08:00
/// </summary>
/// <param name="svw">SeparatedValueWriter to write to</param>
private void WriteFooterSeparator(SeparatedValueWriter svw)
2019-02-08 20:53:13 -08:00
{
svw.WriteString("\n");
svw.Flush();
2019-02-08 20:53:13 -08:00
}
}
}