using System.Collections.Generic;
using SabreTools.DatFiles;
using SabreTools.Logging;
using SabreTools.Reports.Formats;
namespace SabreTools.Reports
{
///
/// Base class for a report output format
///
public abstract class BaseReport
{
#region Logging
///
/// Logging object
///
protected readonly Logger logger = new();
#endregion
public List Statistics { get; set; }
///
/// Create a new report from the filename
///
/// List of statistics objects to set
public BaseReport(List statsList)
{
Statistics = statsList;
}
///
/// Create a specific type of BaseReport to be used based on a format and user inputs
///
/// Format of the Statistics Report to be created
/// List of statistics objects to set
/// BaseReport of the specific internal type that corresponds to the inputs
public static BaseReport? Create(StatReportFormat statReportFormat, List statsList)
{
return statReportFormat switch
{
StatReportFormat.None => new Textfile(statsList, true),
StatReportFormat.Textfile => new Textfile(statsList, false),
StatReportFormat.CSV => new SeparatedValue(statsList, ','),
StatReportFormat.HTML => new Html(statsList),
StatReportFormat.SSV => new SeparatedValue(statsList, ';'),
StatReportFormat.TSV => new SeparatedValue(statsList, '\t'),
_ => null,
};
}
///
/// 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 abstract bool WriteToFile(string? outfile, bool baddumpCol, bool nodumpCol, bool throwOnError = false);
///
/// Returns the human-readable file size for an arbitrary, 64-bit file size
/// The default format is "0.### XB", e.g. "4.2 KB" or "1.434 GB"
///
///
/// Human-readable file size
/// http://www.somacon.com/p576.php
protected static string GetBytesReadable(long input)
{
// Get absolute value
long absolute_i = (input < 0 ? -input : input);
// Determine the suffix and readable value
string suffix;
double readable;
if (absolute_i >= 0x1000000000000000) // Exabyte
{
suffix = "EB";
readable = (input >> 50);
}
else if (absolute_i >= 0x4000000000000) // Petabyte
{
suffix = "PB";
readable = (input >> 40);
}
else if (absolute_i >= 0x10000000000) // Terabyte
{
suffix = "TB";
readable = (input >> 30);
}
else if (absolute_i >= 0x40000000) // Gigabyte
{
suffix = "GB";
readable = (input >> 20);
}
else if (absolute_i >= 0x100000) // Megabyte
{
suffix = "MB";
readable = (input >> 10);
}
else if (absolute_i >= 0x400) // Kilobyte
{
suffix = "KB";
readable = input;
}
else
{
return input.ToString("0 B"); // Byte
}
// Divide by 1024 to get fractional value
readable /= 1024;
// Return formatted number with suffix
return readable.ToString("0.### ") + suffix;
}
}
}