Reduce boilerplate for writing to file

This commit is contained in:
Matt Nadareski
2020-08-28 15:06:07 -07:00
parent d11209951b
commit f85fbd68ce
18 changed files with 395 additions and 816 deletions

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using SabreTools.Library.Data;
@@ -29,13 +30,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
protected override void ParseFile(
// Standard Dat parsing
string filename,
int indexId,
// Miscellaneous
bool keep)
protected override void ParseFile(string filename, int indexId, bool keep)
{
// Open a file reader
Encoding enc = FileExtensions.GetEncoding(filename);
@@ -117,34 +112,21 @@ namespace SabreTools.Library.DatFiles
// Use a sorted list of games to output
foreach (string key in Items.SortedKeys)
{
List<DatItem> roms = Items[key];
List<DatItem> datItems = Items.FilteredItems(key);
// Resolve the names in the block
roms = DatItem.ResolveNames(roms);
datItems = DatItem.ResolveNames(datItems);
for (int index = 0; index < roms.Count; index++)
for (int index = 0; index < datItems.Count; index++)
{
DatItem item = roms[index];
DatItem datItem = datItems[index];
// There are apparently times when a null rom can skip by, skip them
if (item.Name == null || item.Machine.Name == null)
{
Globals.Logger.Warning("Null rom found!");
continue;
}
// Check for a "null" item
datItem = ProcessNullifiedItem(datItem);
// If we have a "null" game (created by DATFromDir or something similar), log it to file
if (item.ItemType == ItemType.Rom
&& ((Rom)item).Size == -1
&& ((Rom)item).CRC == "null")
{
Globals.Logger.Verbose($"Empty folder found: {item.Machine.Name}");
item.Name = (item.Name == "null" ? "-" : item.Name);
((Rom)item).Size = Constants.SizeZero;
}
WriteDatItem(svw, item, ignoreblanks);
// Write out the item if we're not ignoring
if (!ShouldIgnore(datItem, ignoreblanks))
WriteDatItem(svw, datItem);
}
}
@@ -167,12 +149,8 @@ namespace SabreTools.Library.DatFiles
/// <param name="svw">SeparatedValueWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param>
/// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(SeparatedValueWriter svw, DatItem datItem, bool ignoreblanks = false)
private bool WriteDatItem(SeparatedValueWriter svw, DatItem datItem)
{
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
return true;
try
{
// No game should start with a path separator