Add flag for long/short SeparatedValue writing

This commit is contained in:
Matt Nadareski
2024-04-03 22:26:23 -04:00
parent 8f731cebc8
commit 46ad76c5d2

View File

@@ -14,23 +14,23 @@ namespace SabreTools.Serialization.Serializers
#region IFileSerializer
/// <inheritdoc cref="IFileSerializer.Serialize(T?, string?)"/>
public static bool SerializeFile(MetadataFile? obj, string? path, char delim = ',')
public static bool SerializeFile(MetadataFile? obj, string? path, char delim = ',', bool longHeader = false)
{
var serializer = new SeparatedValue();
return serializer.Serialize(obj, path, delim);
return serializer.Serialize(obj, path, delim, longHeader);
}
/// <inheritdoc/>
public bool Serialize(MetadataFile? obj, string? path)
=> Serialize(obj, path, ',');
/// <inheritdoc/>
public bool Serialize(MetadataFile? obj, string? path, char delim)
public bool Serialize(MetadataFile? obj, string? path)
=> Serialize(obj, path, ',', false);
/// <inheritdoc/>
public bool Serialize(MetadataFile? obj, string? path, char delim, bool longHeader)
{
if (string.IsNullOrEmpty(path))
return false;
using var stream = SerializeStream(obj, delim);
using var stream = SerializeStream(obj, delim, longHeader);
if (stream == null)
return false;
@@ -44,25 +44,18 @@ namespace SabreTools.Serialization.Serializers
#region IStreamSerializer
/// <inheritdoc cref="IStreamSerializer.Serialize(T?)"/>
public static Stream? SerializeStream(MetadataFile? obj)
public static Stream? SerializeStream(MetadataFile? obj, char delim = ',', bool longHeader = false)
{
var serializer = new SeparatedValue();
return serializer.Serialize(obj);
return serializer.Serialize(obj, delim, longHeader);
}
/// <inheritdoc cref="IStreamSerializer.Serialize(T?)"/>
public static Stream? SerializeStream(MetadataFile? obj, char delim)
{
var serializer = new SeparatedValue();
return serializer.Serialize(obj, delim);
}
/// <inheritdoc/>
public Stream? Serialize(MetadataFile? obj)
=> Serialize(obj, ',');
=> Serialize(obj, ',', false);
/// <inheritdoc cref="Serialize(MetadataFile)"/>
public Stream? Serialize(MetadataFile? obj, char delim)
public Stream? Serialize(MetadataFile? obj, char delim, bool longHeader)
{
// If the metadata file is null
if (obj == null)
@@ -72,12 +65,11 @@ namespace SabreTools.Serialization.Serializers
var stream = new MemoryStream();
var writer = new SeparatedValueWriter(stream, Encoding.UTF8) { Separator = delim, Quotes = true };
// TODO: Include flag to write out long or short header
// Write the short header
WriteHeader(writer);
// Write the header
WriteHeader(writer, longHeader);
// Write out the rows, if they exist
WriteRows(obj.Row, writer);
WriteRows(obj.Row, writer, longHeader);
// Return the stream
stream.Seek(0, SeekOrigin.Begin);
@@ -88,28 +80,53 @@ namespace SabreTools.Serialization.Serializers
/// Write header information to the current writer
/// </summary>
/// <param name="writer">SeparatedValueWriter representing the output</param>
private static void WriteHeader(SeparatedValueWriter writer)
/// <param name="longHeader">True if the long variant of the row should be written, false otherwise</param>
private static void WriteHeader(SeparatedValueWriter writer, bool longHeader)
{
var headerArray = new string?[]
string?[] headerArray;
if (longHeader)
{
"File Name",
"Internal Name",
"Description",
"Game Name",
"Game Description",
"Type",
"Rom Name",
"Disk Name",
"Size",
"CRC",
"MD5",
"SHA1",
"SHA256",
//"SHA384",
//"SHA512",
//"SpamSum",
"Status",
};
headerArray =
[
"File Name",
"Internal Name",
"Description",
"Game Name",
"Game Description",
"Type",
"Rom Name",
"Disk Name",
"Size",
"CRC",
"MD5",
"SHA1",
"SHA256",
"SHA384",
"SHA512",
"SpamSum",
"Status",
];
}
else
{
headerArray =
[
"File Name",
"Internal Name",
"Description",
"Game Name",
"Game Description",
"Type",
"Rom Name",
"Disk Name",
"Size",
"CRC",
"MD5",
"SHA1",
"SHA256",
"Status",
];
}
writer.WriteHeader(headerArray);
writer.Flush();
@@ -120,7 +137,8 @@ namespace SabreTools.Serialization.Serializers
/// </summary>
/// <param name="rows">Array of Row objects representing the rows information</param>
/// <param name="writer">SeparatedValueWriter representing the output</param>
private static void WriteRows(Row[]? rows, SeparatedValueWriter writer)
/// <param name="longHeader">True if the long variant of the row should be written, false otherwise</param>
private static void WriteRows(Row[]? rows, SeparatedValueWriter writer, bool longHeader)
{
// If the games information is missing, we can't do anything
if (rows == null || !rows.Any())
@@ -129,26 +147,50 @@ namespace SabreTools.Serialization.Serializers
// Loop through and write out the rows
foreach (var row in rows)
{
var rowArray = new string?[]
string?[] rowArray;
if (longHeader)
{
row.FileName,
row.InternalName,
row.Description,
row.GameName,
row.GameDescription,
row.Type,
row.RomName,
row.DiskName,
row.Size,
row.CRC,
row.MD5,
row.SHA1,
row.SHA256,
//row.SHA384,
//row.SHA512,
//row.SpamSum,
row.Status,
};
rowArray =
[
row.FileName,
row.InternalName,
row.Description,
row.GameName,
row.GameDescription,
row.Type,
row.RomName,
row.DiskName,
row.Size,
row.CRC,
row.MD5,
row.SHA1,
row.SHA256,
row.SHA384,
row.SHA512,
row.SpamSum,
row.Status,
];
}
else
{
rowArray =
[
row.FileName,
row.InternalName,
row.Description,
row.GameName,
row.GameDescription,
row.Type,
row.RomName,
row.DiskName,
row.Size,
row.CRC,
row.MD5,
row.SHA1,
row.SHA256,
row.Status,
];
}
writer.WriteValues(rowArray);
writer.Flush();