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;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
using SabreTools.Library.Data; 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> /// <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) protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false)
{ {
// TODO: Use SeparatedValueReader
// Open a file reader // Open a file reader
Encoding enc = FileExtensions.GetEncoding(filename); 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 try
{ {
string line = sr.ReadLine();
/* /*
The gameinfo order is as follows The gameinfo order is as follows
0 - SHA-256 0 - SHA-256
@@ -53,17 +55,16 @@ namespace SabreTools.Library.DatFiles
4 - CRC32 4 - CRC32
*/ */
string[] gameinfo = line.Split('\t'); string[] fullname = svr.Line[1].Split('/');
string[] fullname = gameinfo[1].Split('/');
Rom rom = new Rom 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 Size = null, // No size provided, but we don't want the size being 0
CRC = gameinfo[4], CRC = svr.Line[4],
MD5 = gameinfo[3], MD5 = svr.Line[3],
SHA1 = gameinfo[2], SHA1 = svr.Line[2],
SHA256 = gameinfo[0], SHA256 = svr.Line[0],
ItemStatus = ItemStatus.None, ItemStatus = ItemStatus.None,
Machine = new Machine Machine = new Machine
@@ -84,17 +85,17 @@ namespace SabreTools.Library.DatFiles
} }
catch (Exception ex) 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); Globals.Logger.Error(ex, message);
if (throwOnError) if (throwOnError)
{ {
sr.Dispose(); svr.Dispose();
throw new Exception(message, ex); throw new Exception(message, ex);
} }
} }
} }
sr.Dispose(); svr.Dispose();
} }
/// <inheritdoc/> /// <inheritdoc/>

View File

@@ -45,7 +45,7 @@ namespace SabreTools.Library.DatFiles
Header = true, Header = true,
Quotes = true, Quotes = true,
Separator = _delim, Separator = _delim,
VerifyFieldCount = true VerifyFieldCount = true,
}; };
// If we're somehow at the end of the stream already, we can't do anything // If we're somehow at the end of the stream already, we can't do anything
@@ -62,19 +62,6 @@ namespace SabreTools.Library.DatFiles
{ {
// Get the current line, split and parse // Get the current line, split and parse
svr.ReadNextLine(); svr.ReadNextLine();
}
catch (InvalidDataException ex)
{
string message = $"'{filename}' - There was an error parsing line {svr.LineNumber} '{svr.CurrentLine}'";
Globals.Logger.Error(ex, message);
if (throwOnError)
{
svr.Dispose();
throw new Exception(message, ex);
}
continue;
}
// Create mapping dictionary // Create mapping dictionary
var mappings = new Dictionary<Field, string>(); var mappings = new Dictionary<Field, string>();
@@ -101,6 +88,17 @@ namespace SabreTools.Library.DatFiles
ParseAddHelper(datItem); ParseAddHelper(datItem);
} }
} }
catch (Exception ex)
{
string message = $"'{filename}' - There was an error parsing line {svr.LineNumber} '{svr.CurrentLine}'";
Globals.Logger.Error(ex, message);
if (throwOnError)
{
svr.Dispose();
throw new Exception(message, ex);
}
}
}
svr.Dispose(); svr.Dispose();
} }