Skip to content

Commit

Permalink
2024.46.1
Browse files Browse the repository at this point in the history
- IFileOutput now implements IDisposable
- OutFile now implements IDisposable
  • Loading branch information
simon-techkid committed May 22, 2024
1 parent c726835 commit 7291127
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
4 changes: 2 additions & 2 deletions SpotifyGPX/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion SpotifyGPX/Output/IFileOutput.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// SpotifyGPX by Simon Field

using System;

namespace SpotifyGPX.Output;

/// <summary>
/// Interfaces with file output classes, unifying all formats that pairs can be written out as.
/// </summary>
public interface IFileOutput
public interface IFileOutput : IDisposable
{
/// <summary>
/// The name of this document's format, used in exported files' extensions.
Expand Down
20 changes: 16 additions & 4 deletions SpotifyGPX/Output/OutputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -63,7 +69,7 @@ private static void LogExportResults(List<OutFile> files, Formats format)
/// <summary>
/// Represents an output file.
/// </summary>
private readonly struct OutFile
private readonly struct OutFile : IDisposable
{
/// <summary>
/// Stages an output file, containing the given pairs, in the given format, with the specified names.
Expand All @@ -78,22 +84,23 @@ public OutFile(IEnumerable<SongPoint> pairs, Formats format, string name, string
Handler = factory.CreateFileOutput(format, () => pairs, trackName);
SourceName = name;
TrackName = trackName;
ExportCount = Handler.Count;
OriginalCount = pairs.Count();
}

private IFileOutput Handler { get; }
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})";

public void Save(bool transform)
{
AttemptSave(transform, FinalName);
}

private void AttemptSave(bool transform, string fileName, int attempt = 0)
{
try
Expand Down Expand Up @@ -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();
}
}

/// <summary>
Expand Down
34 changes: 33 additions & 1 deletion SpotifyGPX/Output/SaveableAndTransformableBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected SaveableBase(Func<IEnumerable<SongPoint>> pairs, string? trackName)
/// <summary>
/// The document in format <typeparamref name="T"/> that will be serialized and saved to the disk.
/// </summary>
protected T Document { get; }
protected T Document { get; private set; }
public abstract int Count { get; }

/// <summary>
Expand Down Expand Up @@ -66,6 +66,18 @@ protected virtual void Save(string path, byte[] bytes)
/// </summary>
/// <returns>This <see cref="Document"/>, as <see langword="byte"/>[].</returns>
protected abstract byte[] ConvertToBytes();

/// <summary>
/// Clears the contents of the <see cref="Document"/> in preparation for disposal.
/// </summary>
/// <returns>A <typeparamref name="T"/> that has been cleared.</returns>
protected abstract T ClearDocument();

public virtual void Dispose()
{
Document = ClearDocument();
GC.SuppressFinalize(this);
}
}

/// <summary>
Expand Down Expand Up @@ -268,6 +280,11 @@ private static XElement JsonToXElement(JsonElement element)

return xElement;
}

protected override List<JsonDocument> ClearDocument()
{
return new();
}
}

/// <summary>
Expand All @@ -291,6 +308,11 @@ protected override XDocument TransformToXml()
{
return Document;
}

protected override XDocument ClearDocument()
{
return new XDocument();
}
}

/// <summary>
Expand Down Expand Up @@ -324,6 +346,11 @@ protected override XDocument TransformToXml()

return new XDocument(root);
}

protected override string[] ClearDocument()
{
return Array.Empty<string>();
}
}

/// <summary>
Expand All @@ -339,4 +366,9 @@ protected override byte[] ConvertToBytes()
{
return Document;
}

protected override byte[] ClearDocument()
{
return Array.Empty<byte>();
}
}
2 changes: 1 addition & 1 deletion SpotifyGPX/Output/Tsv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public sealed partial class Tsv : TxtSaveable
{
public override string FormatName => "tsv";
protected override DocumentAccessor SaveAction => GetDocument;

public Tsv(Func<IEnumerable<SongPoint>> pairs, string? trackName) : base(pairs, trackName)
{
}
Expand Down

0 comments on commit 7291127

Please sign in to comment.