2021-02-18 11:13:11 -08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Text;
|
2024-03-13 01:22:59 -04:00
|
|
|
|
using SabreTools.DatFiles;
|
2024-03-13 00:02:19 -04:00
|
|
|
|
using SabreTools.DatItems;
|
2024-03-04 23:56:05 -05:00
|
|
|
|
using SabreTools.Hashing;
|
2024-10-24 00:36:44 -04:00
|
|
|
|
using SabreTools.IO.Logging;
|
2021-02-18 11:13:11 -08:00
|
|
|
|
using SabreTools.IO.Writers;
|
2017-11-07 13:56:15 -08:00
|
|
|
|
|
2020-12-11 10:10:56 -08:00
|
|
|
|
namespace SabreTools.Reports.Formats
|
2017-11-07 13:56:15 -08:00
|
|
|
|
{
|
2019-02-08 20:53:13 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Separated-Value report format
|
|
|
|
|
|
/// </summary>
|
2025-02-19 13:24:12 -05:00
|
|
|
|
public abstract class SeparatedValue : BaseReport
|
2019-02-08 20:53:13 -08:00
|
|
|
|
{
|
2025-02-19 13:28:28 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Represents the delimiter between fields
|
|
|
|
|
|
/// </summary>
|
2024-10-30 13:39:31 -04:00
|
|
|
|
protected char _delim;
|
2017-11-07 13:56:15 -08:00
|
|
|
|
|
2019-02-08 20:53:13 -08:00
|
|
|
|
/// <summary>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// Create a new report from the filename
|
2019-02-08 20:53:13 -08:00
|
|
|
|
/// </summary>
|
2021-02-18 11:13:11 -08:00
|
|
|
|
/// <param name="statsList">List of statistics objects to set</param>
|
2024-10-30 13:39:31 -04:00
|
|
|
|
public SeparatedValue(List<DatStatistics> statsList)
|
2021-02-18 11:13:11 -08:00
|
|
|
|
: base(statsList)
|
2019-02-08 20:53:13 -08:00
|
|
|
|
{
|
|
|
|
|
|
}
|
2017-11-07 13:56:15 -08:00
|
|
|
|
|
2021-02-18 11:13:11 -08:00
|
|
|
|
/// <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
|
|
|
|
{
|
2023-04-19 16:39:58 -04:00
|
|
|
|
InternalStopwatch watch = new($"Writing statistics to '{outfile}");
|
2017-11-07 13:56:15 -08:00
|
|
|
|
|
2021-02-18 11:13:11 -08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// Try to create the output file
|
2024-02-28 19:19:50 -05:00
|
|
|
|
FileStream fs = File.Create(outfile ?? string.Empty);
|
2021-02-18 11:13:11 -08:00
|
|
|
|
if (fs == null)
|
|
|
|
|
|
{
|
2025-01-08 16:59:44 -05:00
|
|
|
|
_logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
|
2021-02-18 11:13:11 -08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2017-11-07 13:56:15 -08:00
|
|
|
|
|
2023-04-19 16:39:58 -04:00
|
|
|
|
SeparatedValueWriter svw = new(fs, Encoding.UTF8)
|
2021-02-18 11:13:11 -08:00
|
|
|
|
{
|
2024-10-30 13:39:31 -04:00
|
|
|
|
Separator = _delim,
|
2021-02-18 11:13:11 -08:00
|
|
|
|
Quotes = true,
|
|
|
|
|
|
};
|
2017-11-07 13:56:15 -08:00
|
|
|
|
|
2021-02-18 11:13:11 -08:00
|
|
|
|
// Write out the header
|
|
|
|
|
|
WriteHeader(svw, baddumpCol, nodumpCol);
|
|
|
|
|
|
|
|
|
|
|
|
// Now process each of the statistics
|
2025-02-19 13:26:53 -05:00
|
|
|
|
for (int i = 0; i < _statistics.Count; i++)
|
2021-02-18 11:13:11 -08:00
|
|
|
|
{
|
|
|
|
|
|
// Get the current statistic
|
2025-02-19 13:26:53 -05:00
|
|
|
|
DatStatistics stat = _statistics[i];
|
2021-02-18 11:13:11 -08:00
|
|
|
|
|
|
|
|
|
|
// 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
|
2025-02-19 13:26:53 -05:00
|
|
|
|
if (i < _statistics.Count - 1)
|
2021-02-18 11:13:11 -08:00
|
|
|
|
WriteFooterSeparator(svw);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If we have a normal statistic
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
WriteIndividual(svw, stat, baddumpCol, nodumpCol);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
svw.Dispose();
|
|
|
|
|
|
fs.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex) when (!throwOnError)
|
|
|
|
|
|
{
|
2025-01-08 16:59:44 -05:00
|
|
|
|
_logger.Error(ex);
|
2021-02-18 11:13:11 -08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
watch.Stop();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
2019-02-08 20:53:13 -08:00
|
|
|
|
}
|
2017-11-07 13:56:15 -08:00
|
|
|
|
|
2019-02-08 20:53:13 -08:00
|
|
|
|
/// <summary>
|
2021-02-18 11:13:11 -08:00
|
|
|
|
/// Write out the header to the stream, if any exists
|
2019-02-08 20:53:13 -08:00
|
|
|
|
/// </summary>
|
2021-02-18 11:13:11 -08:00
|
|
|
|
/// <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>
|
2025-02-14 20:45:47 -05:00
|
|
|
|
private static 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 =
|
|
|
|
|
|
[
|
2021-02-18 11:13:11 -08:00
|
|
|
|
"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
|
|
|
|
];
|
2021-02-18 11:13:11 -08:00
|
|
|
|
svw.WriteHeader(headers);
|
|
|
|
|
|
svw.Flush();
|
2019-02-08 20:53:13 -08:00
|
|
|
|
}
|
2017-11-07 13:56:15 -08:00
|
|
|
|
|
2019-02-08 20:53:13 -08:00
|
|
|
|
/// <summary>
|
2021-02-18 11:13:11 -08:00
|
|
|
|
/// Write a single set of statistics
|
2019-02-08 20:53:13 -08:00
|
|
|
|
/// </summary>
|
2021-02-18 11:13:11 -08:00
|
|
|
|
/// <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>
|
2025-02-14 20:45:47 -05:00
|
|
|
|
private static 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!,
|
2024-03-13 01:22:59 -04:00
|
|
|
|
stat.TotalSize.ToString(),
|
2021-02-18 11:13:11 -08:00
|
|
|
|
stat.MachineCount.ToString(),
|
2024-03-13 01:22:59 -04:00
|
|
|
|
stat.GetItemCount(ItemType.Rom).ToString(),
|
|
|
|
|
|
stat.GetItemCount(ItemType.Disk).ToString(),
|
|
|
|
|
|
stat.GetHashCount(HashType.CRC32).ToString(),
|
|
|
|
|
|
stat.GetHashCount(HashType.MD5).ToString(),
|
|
|
|
|
|
stat.GetHashCount(HashType.SHA1).ToString(),
|
|
|
|
|
|
stat.GetHashCount(HashType.SHA256).ToString(),
|
|
|
|
|
|
stat.GetHashCount(HashType.SHA384).ToString(),
|
|
|
|
|
|
stat.GetHashCount(HashType.SHA512).ToString(),
|
|
|
|
|
|
baddumpCol ? stat.GetStatusCount(ItemStatus.BadDump).ToString() : string.Empty,
|
|
|
|
|
|
nodumpCol ? stat.GetStatusCount(ItemStatus.Nodump).ToString() : string.Empty,
|
2024-02-28 19:19:50 -05:00
|
|
|
|
];
|
2021-02-18 11:13:11 -08:00
|
|
|
|
svw.WriteValues(values);
|
|
|
|
|
|
svw.Flush();
|
2019-02-08 20:53:13 -08:00
|
|
|
|
}
|
2017-11-07 13:56:15 -08:00
|
|
|
|
|
2019-02-08 20:53:13 -08:00
|
|
|
|
/// <summary>
|
2021-02-18 11:13:11 -08:00
|
|
|
|
/// Write out the footer-separator to the stream, if any exists
|
2019-02-08 20:53:13 -08:00
|
|
|
|
/// </summary>
|
2021-02-18 11:13:11 -08:00
|
|
|
|
/// <param name="svw">SeparatedValueWriter to write to</param>
|
2025-02-14 20:45:47 -05:00
|
|
|
|
private static void WriteFooterSeparator(SeparatedValueWriter svw)
|
2019-02-08 20:53:13 -08:00
|
|
|
|
{
|
2021-02-18 11:13:11 -08:00
|
|
|
|
svw.WriteString("\n");
|
|
|
|
|
|
svw.Flush();
|
2019-02-08 20:53:13 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-10-30 13:39:31 -04:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Represents a comma-separated value file
|
|
|
|
|
|
/// </summary>
|
2025-02-19 13:24:12 -05:00
|
|
|
|
public sealed class CommaSeparatedValue : SeparatedValue
|
2024-10-30 13:39:31 -04:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a new report from the filename
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="statsList">List of statistics objects to set</param>
|
|
|
|
|
|
public CommaSeparatedValue(List<DatStatistics> statsList) : base(statsList)
|
|
|
|
|
|
{
|
|
|
|
|
|
_delim = ',';
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Represents a semicolon-separated value file
|
|
|
|
|
|
/// </summary>
|
2025-02-19 13:24:12 -05:00
|
|
|
|
public sealed class SemicolonSeparatedValue : SeparatedValue
|
2024-10-30 13:39:31 -04:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a new report from the filename
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="statsList">List of statistics objects to set</param>
|
|
|
|
|
|
public SemicolonSeparatedValue(List<DatStatistics> statsList) : base(statsList)
|
|
|
|
|
|
{
|
|
|
|
|
|
_delim = ';';
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Represents a tab-separated value file
|
|
|
|
|
|
/// </summary>
|
2025-02-19 13:24:12 -05:00
|
|
|
|
public sealed class TabSeparatedValue : SeparatedValue
|
2024-10-30 13:39:31 -04:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a new report from the filename
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="statsList">List of statistics objects to set</param>
|
|
|
|
|
|
public TabSeparatedValue(List<DatStatistics> statsList) : base(statsList)
|
|
|
|
|
|
{
|
|
|
|
|
|
_delim = '\t';
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-11-07 13:56:15 -08:00
|
|
|
|
}
|