Make the intel hex file format parser easier to use.

This commit is contained in:
Adrian Conlon
2024-10-07 09:40:26 +01:00
parent b6043c3659
commit 297f3b8dff
2 changed files with 6 additions and 25 deletions

View File

@@ -122,7 +122,7 @@ namespace EightBit
protected void LoadHexFile(string path)
{
using var file = new IntelHexFile(path);
var file = new IntelHexFile(path);
foreach (var (address, content) in file.Parse())
{
var mapped = this.Mapping(address);

View File

@@ -8,24 +8,18 @@ namespace EightBit
using System.Collections.Generic;
using System.IO;
public class IntelHexFile(string path) : IDisposable
public class IntelHexFile(string path)
{
private readonly StreamReader reader = File.OpenText(path);
private readonly string _path = path;
private bool eof;
private bool disposed = false;
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
public IEnumerable<Tuple<ushort, byte[]>> Parse()
{
this.eof = false;
while (!this.reader.EndOfStream && !this.eof)
using var reader = File.OpenText(this._path);
while (!reader.EndOfStream && !this.eof)
{
var line = this.reader.ReadLine() ?? throw new InvalidOperationException("Early EOF detected");
var line = reader.ReadLine() ?? throw new InvalidOperationException("Early EOF detected");
var parsed = this.Parse(line);
if (parsed != null)
{
@@ -39,19 +33,6 @@ namespace EightBit
}
}
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
this.reader.Dispose();
}
this.disposed = true;
}
}
private static byte[] ParseDataRecord(string line, byte count)
{
ArgumentNullException.ThrowIfNullOrEmpty(line);