Fix separated value read (and use for SMDB)

This commit is contained in:
Matt Nadareski
2020-09-21 13:20:22 -07:00
parent a04a3485ef
commit db0c50bd3a
2 changed files with 45 additions and 46 deletions

View File

@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using SabreTools.Library.Data;
@@ -33,17 +32,20 @@ namespace SabreTools.Library.DatFiles
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false)
{
// TODO: Use SeparatedValueReader
// Open a file reader
Encoding enc = FileExtensions.GetEncoding(filename);
StreamReader sr = new StreamReader(FileExtensions.TryOpenRead(filename), enc);
SeparatedValueReader svr = new SeparatedValueReader(FileExtensions.TryOpenRead(filename), enc)
{
Header = false,
Quotes = false,
Separator = '\t',
VerifyFieldCount = false,
};
while (!sr.EndOfStream)
while (!svr.EndOfStream)
{
try
{
string line = sr.ReadLine();
/*
The gameinfo order is as follows
0 - SHA-256
@@ -52,18 +54,17 @@ namespace SabreTools.Library.DatFiles
3 - MD5
4 - CRC32
*/
string[] gameinfo = line.Split('\t');
string[] fullname = gameinfo[1].Split('/');
string[] fullname = svr.Line[1].Split('/');
Rom rom = new Rom
{
Name = gameinfo[1].Substring(fullname[0].Length + 1),
Name = svr.Line[1].Substring(fullname[0].Length + 1),
Size = null, // No size provided, but we don't want the size being 0
CRC = gameinfo[4],
MD5 = gameinfo[3],
SHA1 = gameinfo[2],
SHA256 = gameinfo[0],
CRC = svr.Line[4],
MD5 = svr.Line[3],
SHA1 = svr.Line[2],
SHA256 = svr.Line[0],
ItemStatus = ItemStatus.None,
Machine = new Machine
@@ -84,17 +85,17 @@ namespace SabreTools.Library.DatFiles
}
catch (Exception ex)
{
string message = $"'{filename}' - There was an error parsing at position {sr.BaseStream.Position}";
string message = $"'{filename}' - There was an error parsing line {svr.LineNumber} '{svr.CurrentLine}'";
Globals.Logger.Error(ex, message);
if (throwOnError)
{
sr.Dispose();
svr.Dispose();
throw new Exception(message, ex);
}
}
}
sr.Dispose();
svr.Dispose();
}
/// <inheritdoc/>

View File

@@ -45,7 +45,7 @@ namespace SabreTools.Library.DatFiles
Header = true,
Quotes = true,
Separator = _delim,
VerifyFieldCount = true
VerifyFieldCount = true,
};
// If we're somehow at the end of the stream already, we can't do anything
@@ -62,8 +62,33 @@ namespace SabreTools.Library.DatFiles
{
// Get the current line, split and parse
svr.ReadNextLine();
// Create mapping dictionary
var mappings = new Dictionary<Field, string>();
// Now we loop through and get values for everything
for (int i = 0; i < svr.HeaderValues.Count; i++)
{
Field key = svr.HeaderValues[i].AsField();
string value = svr.Line[i];
mappings[key] = value;
}
// Set DatHeader fields
DatHeader header = new DatHeader();
header.SetFields(mappings);
Header.ConditionalCopy(header);
// Set Machine and DatItem fields
if (mappings.ContainsKey(Field.DatItem_Type))
{
DatItem datItem = DatItem.Create(mappings[Field.DatItem_Type].AsItemType());
datItem.SetFields(mappings);
datItem.Source = new Source(indexId, filename);
ParseAddHelper(datItem);
}
}
catch (InvalidDataException ex)
catch (Exception ex)
{
string message = $"'{filename}' - There was an error parsing line {svr.LineNumber} '{svr.CurrentLine}'";
Globals.Logger.Error(ex, message);
@@ -72,33 +97,6 @@ namespace SabreTools.Library.DatFiles
svr.Dispose();
throw new Exception(message, ex);
}
continue;
}
// Create mapping dictionary
var mappings = new Dictionary<Field, string>();
// Now we loop through and get values for everything
for (int i = 0; i < svr.HeaderValues.Count; i++)
{
Field key = svr.HeaderValues[i].AsField();
string value = svr.Line[i];
mappings[key] = value;
}
// Set DatHeader fields
DatHeader header = new DatHeader();
header.SetFields(mappings);
Header.ConditionalCopy(header);
// Set Machine and DatItem fields
if (mappings.ContainsKey(Field.DatItem_Type))
{
DatItem datItem = DatItem.Create(mappings[Field.DatItem_Type].AsItemType());
datItem.SetFields(mappings);
datItem.Source = new Source(indexId, filename);
ParseAddHelper(datItem);
}
}