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

@@ -34,13 +34,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
/// <remarks>
/// </remarks>
protected override void ParseFile(
// Standard Dat parsing
string filename,
int indexId,
// Miscellaneous
bool keep)
protected override void ParseFile(string filename, int indexId, bool keep)
{
// Prepare all internal variables
XmlReader xtr = filename.GetXmlTextReader();
@@ -112,12 +106,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="reader">XmlReader representing a machine block</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
private void ReadMachine(
XmlReader reader,
// Standard Dat parsing
string filename,
int indexId)
private void ReadMachine(XmlReader reader, string filename, int indexId)
{
// If we have an empty machine, skip it
if (reader == null)
@@ -499,7 +488,7 @@ namespace SabreTools.Library.DatFiles
slot.Name = reader.GetAttribute("name");
// Now read the internal tags
ReadSlot(reader.ReadSubtree(), slot, machine);
ReadSlot(reader.ReadSubtree(), slot);
// Ensure the list exists
if (machine.Slots == null)
@@ -579,8 +568,7 @@ namespace SabreTools.Library.DatFiles
/// </summary>
/// <param name="reader">XmlReader representing a machine block</param>
/// <param name="slot">ListXmlSlot to populate</param>
/// <param name="machine">Machine information to pass to contained items</param>
private void ReadSlot(XmlReader reader, ListXmlSlot slot, Machine machine)
private void ReadSlot(XmlReader reader, ListXmlSlot slot)
{
// If we have an empty machine, skip it
if (reader == null)
@@ -981,46 +969,32 @@ 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 rom = roms[index];
// There are apparently times when a null rom can skip by, skip them
if (rom.Name == null || rom.Machine.Name == null)
{
Globals.Logger.Warning("Null rom found!");
continue;
}
DatItem datItem = datItems[index];
// If we have a different game and we're not at the start of the list, output the end of last item
if (lastgame != null && lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant())
WriteEndGame(xtw, rom);
if (lastgame != null && lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
WriteEndGame(xtw, datItem);
// If we have a new game, output the beginning of the new item
if (lastgame == null || lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant())
WriteStartGame(xtw, rom);
if (lastgame == null || lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
WriteStartGame(xtw, datItem);
// If we have a "null" game (created by DATFromDir or something similar), log it to file
if (rom.ItemType == ItemType.Rom
&& ((Rom)rom).Size == -1
&& ((Rom)rom).CRC == "null")
{
Globals.Logger.Verbose($"Empty folder found: {rom.Machine.Name}");
// Check for a "null" item
datItem = ProcessNullifiedItem(datItem);
lastgame = rom.Machine.Name;
continue;
}
// Now, output the rom data
WriteDatItem(xtw, rom, ignoreblanks);
// Write out the item if we're not ignoring
if (!ShouldIgnore(datItem, ignoreblanks))
WriteDatItem(xtw, datItem);
// Set the new data to compare against
lastgame = rom.Machine.Name;
lastgame = datItem.Machine.Name;
}
}
@@ -1514,14 +1488,9 @@ namespace SabreTools.Library.DatFiles
/// </summary>
/// <param name="xtw">XmlTextWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param>
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param>
/// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(XmlTextWriter xtw, DatItem datItem, bool ignoreblanks = false)
private bool WriteDatItem(XmlTextWriter xtw, 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
{
// Pre-process the item name