Make report writing work with streams

This commit is contained in:
Matt Nadareski
2025-04-14 21:16:45 -04:00
parent 8897fe0f7a
commit d5fb8414d4
7 changed files with 57 additions and 82 deletions

View File

@@ -10,7 +10,6 @@ using SabreTools.Core.Tools;
using SabreTools.DatFiles;
using SabreTools.DatItems;
using SabreTools.Hashing;
using SabreTools.IO.Logging;
namespace SabreTools.Reports.Formats
{
@@ -30,21 +29,11 @@ namespace SabreTools.Reports.Formats
}
/// <inheritdoc/>
public override bool WriteToFile(string? outfile, bool baddumpCol, bool nodumpCol, bool throwOnError = false)
public override bool WriteToStream(Stream stream, bool baddumpCol, bool nodumpCol, bool throwOnError = false)
{
InternalStopwatch watch = new($"Writing statistics to '{outfile}");
try
{
// Try to create the output file
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;
}
XmlTextWriter xtw = new(fs, Encoding.UTF8)
XmlTextWriter xtw = new(stream, Encoding.UTF8)
{
Formatting = Formatting.Indented,
IndentChar = '\t',
@@ -85,17 +74,12 @@ namespace SabreTools.Reports.Formats
#if NET452_OR_GREATER
xtw.Dispose();
#endif
fs.Dispose();
}
catch (Exception ex) when (!throwOnError)
{
_logger.Error(ex);
return false;
}
finally
{
watch.Stop();
}
return true;
}