using System;
using System.Collections.Generic;
using System.IO;
using SabreTools.DatFiles;
using SabreTools.IO.Logging;
namespace SabreTools.Reports
{
///
/// Base class for a report output format
///
public abstract class BaseReport
{
#region Logging
///
/// Logging object
///
protected readonly Logger _logger = new();
#endregion
///
/// Set of DatStatistics objects to use for formatting
///
protected List _statistics;
///
/// Create a new report from the filename
///
/// List of statistics objects to set
public BaseReport(List statsList)
{
_statistics = statsList;
}
///
/// Create and open an output file for writing direct from a set of statistics
///
/// Name of the file to write to
/// True if baddumps should be included in output, false otherwise
/// True if nodumps should be included in output, false otherwise
/// True if the error that is thrown should be thrown back to the caller, false otherwise
/// True if the report was written correctly, false otherwise
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;
}
///
/// Write a set of statistics to an input stream
///
/// Stream to write to
/// True if baddumps should be included in output, false otherwise
/// True if nodumps should be included in output, false otherwise
/// True if the error that is thrown should be thrown back to the caller, false otherwise
/// True if the report was written correctly, false otherwise
public abstract bool WriteToStream(Stream stream, bool baddumpCol, bool nodumpCol, bool throwOnError = false);
}
}