diff --git a/SpotifyGPX/Options.cs b/SpotifyGPX/Options.cs index b0d6d0e..a94f2c2 100644 --- a/SpotifyGPX/Options.cs +++ b/SpotifyGPX/Options.cs @@ -302,7 +302,7 @@ public partial class PointTest private const double CenterLat = 44.918516; private const double CenterLon = -0.245090; private const int CenterRadius = 20; // Radius from center in kilometers - + // Point placement intervals private const int MinPlacementSecs = 15; // Minimum GPS point placement interval in seconds private const int MaxPlacementSecs = 120; // Maximum GPS point placement interval in seconds @@ -338,7 +338,7 @@ public partial class SongTest // Number of unique artists and songs to generate private const int ArtistsCount = 10; private const int SongsCount = 100; - + // Song playback start (randomness floor) time (currently 07:00) - start playing at 07:00 at the earliest private const int PlaybackMinStartHour = 5; private const int PlaybackMinStartMinute = 0; diff --git a/SpotifyGPX/Output/IFileOutput.cs b/SpotifyGPX/Output/IFileOutput.cs index 7f1c17e..09194cf 100644 --- a/SpotifyGPX/Output/IFileOutput.cs +++ b/SpotifyGPX/Output/IFileOutput.cs @@ -1,11 +1,13 @@ // SpotifyGPX by Simon Field +using System; + namespace SpotifyGPX.Output; /// /// Interfaces with file output classes, unifying all formats that pairs can be written out as. /// -public interface IFileOutput +public interface IFileOutput : IDisposable { /// /// The name of this document's format, used in exported files' extensions. diff --git a/SpotifyGPX/Output/OutputHandler.cs b/SpotifyGPX/Output/OutputHandler.cs index 5b6cb8b..b688ca9 100644 --- a/SpotifyGPX/Output/OutputHandler.cs +++ b/SpotifyGPX/Output/OutputHandler.cs @@ -46,7 +46,13 @@ public void Save(Formats format, string name, bool transform) .Select(track => new OutFile(track, format, name, track.Key.ToString()))); } - files.ForEach(file => file.Save(transform)); // Save each file to the disk + foreach (var file in files) + { + using (file) + { + file.Save(transform); // Save each file to the disk + } + } // Print the individual track results (number of pairs): LogExportResults(files, format); @@ -63,7 +69,7 @@ private static void LogExportResults(List files, Formats format) /// /// Represents an output file. /// - private readonly struct OutFile + private readonly struct OutFile : IDisposable { /// /// Stages an output file, containing the given pairs, in the given format, with the specified names. @@ -78,6 +84,7 @@ public OutFile(IEnumerable pairs, Formats format, string name, string Handler = factory.CreateFileOutput(format, () => pairs, trackName); SourceName = name; TrackName = trackName; + ExportCount = Handler.Count; OriginalCount = pairs.Count(); } @@ -85,7 +92,7 @@ public OutFile(IEnumerable pairs, Formats format, string name, string private string SourceName { get; } private string TrackName { get; } public int OriginalCount { get; } - public int ExportCount => Handler.Count; + public int ExportCount { get; } private string FinalName => $"{SourceName}_{TrackName}.{Handler.FormatName}"; public string Result => $"{ExportCount}/{OriginalCount} ({TrackName})"; @@ -93,7 +100,7 @@ public void Save(bool transform) { AttemptSave(transform, FinalName); } - + private void AttemptSave(bool transform, string fileName, int attempt = 0) { try @@ -130,6 +137,11 @@ private void AttemptTransform(bool transform, string fileName) throw new Exception($"Error transforming {fileName} to XML: {ex}"); } } + + public void Dispose() + { + Handler.Dispose(); + } } /// diff --git a/SpotifyGPX/Output/SaveableAndTransformableBase.cs b/SpotifyGPX/Output/SaveableAndTransformableBase.cs index 8119b3b..7ae1654 100644 --- a/SpotifyGPX/Output/SaveableAndTransformableBase.cs +++ b/SpotifyGPX/Output/SaveableAndTransformableBase.cs @@ -30,7 +30,7 @@ protected SaveableBase(Func> pairs, string? trackName) /// /// The document in format that will be serialized and saved to the disk. /// - protected T Document { get; } + protected T Document { get; private set; } public abstract int Count { get; } /// @@ -66,6 +66,18 @@ protected virtual void Save(string path, byte[] bytes) /// /// This , as []. protected abstract byte[] ConvertToBytes(); + + /// + /// Clears the contents of the in preparation for disposal. + /// + /// A that has been cleared. + protected abstract T ClearDocument(); + + public virtual void Dispose() + { + Document = ClearDocument(); + GC.SuppressFinalize(this); + } } /// @@ -268,6 +280,11 @@ private static XElement JsonToXElement(JsonElement element) return xElement; } + + protected override List ClearDocument() + { + return new(); + } } /// @@ -291,6 +308,11 @@ protected override XDocument TransformToXml() { return Document; } + + protected override XDocument ClearDocument() + { + return new XDocument(); + } } /// @@ -324,6 +346,11 @@ protected override XDocument TransformToXml() return new XDocument(root); } + + protected override string[] ClearDocument() + { + return Array.Empty(); + } } /// @@ -339,4 +366,9 @@ protected override byte[] ConvertToBytes() { return Document; } + + protected override byte[] ClearDocument() + { + return Array.Empty(); + } } diff --git a/SpotifyGPX/Output/Tsv.cs b/SpotifyGPX/Output/Tsv.cs index e035a8c..283b143 100644 --- a/SpotifyGPX/Output/Tsv.cs +++ b/SpotifyGPX/Output/Tsv.cs @@ -10,7 +10,7 @@ public sealed partial class Tsv : TxtSaveable { public override string FormatName => "tsv"; protected override DocumentAccessor SaveAction => GetDocument; - + public Tsv(Func> pairs, string? trackName) : base(pairs, trackName) { }