Files
SabreTools/SabreTools.Reports/BaseReport.cs

89 lines
3.3 KiB
C#
Raw Normal View History

2025-04-14 21:16:45 -04:00
using System;
using System.Collections.Generic;
using System.IO;
using SabreTools.DatFiles;
2024-10-24 00:36:44 -04:00
using SabreTools.IO.Logging;
2020-12-11 10:10:56 -08:00
namespace SabreTools.Reports
{
2019-02-08 20:53:13 -08:00
/// <summary>
/// Base class for a report output format
/// </summary>
public abstract class BaseReport
{
#region Logging
2019-02-08 20:53:13 -08:00
/// <summary>
/// Logging object
2019-02-08 20:53:13 -08:00
/// </summary>
2025-01-08 16:59:44 -05:00
protected readonly Logger _logger = new();
2020-07-23 11:40:45 -07:00
#endregion
/// <summary>
/// Set of DatStatistics objects to use for formatting
/// </summary>
protected List<DatStatistics> _statistics;
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>
public BaseReport(List<DatStatistics> statsList)
2019-02-08 20:53:13 -08:00
{
_statistics = statsList;
2019-02-08 20:53:13 -08:00
}
/// <summary>
/// Create and open an output file for writing direct from a set of statistics
2019-02-08 20:53:13 -08:00
/// </summary>
/// <param name="outfile">Name of the file 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>
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
/// <returns>True if the report was written correctly, false otherwise</returns>
2025-04-14 21:16:45 -04:00
public bool WriteToFile(string? outfile, bool baddumpCol, bool nodumpCol, bool throwOnError = false)
{
InternalStopwatch watch = new($"Writing statistics to '{outfile}");
try
{
// Try to create the output file
FileStream stream = File.Create(outfile ?? string.Empty);
if (stream == null)
{
_logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
return false;
}
// Write to the stream
bool result = WriteToStream(stream, baddumpCol, nodumpCol, throwOnError);
// Dispose of the stream
stream.Dispose();
}
catch (Exception ex) when (!throwOnError)
{
_logger.Error(ex);
return false;
}
finally
{
watch.Stop();
}
return true;
}
/// <summary>
/// Write a set of statistics to an input stream
/// </summary>
/// <param name="stream">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>
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
/// <returns>True if the report was written correctly, false otherwise</returns>
public abstract bool WriteToStream(Stream stream, bool baddumpCol, bool nodumpCol, bool throwOnError = false);
2019-02-08 20:53:13 -08:00
}
}