2019-07-21 12:11:00 +00:00
|
|
|
|
// <copyright file="Lines.cs" company="Adrian Conlon">
|
|
|
|
|
// Copyright (c) Adrian Conlon. All rights reserved.
|
|
|
|
|
// </copyright>
|
|
|
|
|
namespace Fuse
|
2019-07-20 18:17:08 +00:00
|
|
|
|
{
|
2019-07-21 08:10:45 +00:00
|
|
|
|
using System;
|
2019-07-20 18:17:08 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
public class Lines
|
|
|
|
|
{
|
|
|
|
|
private readonly string path;
|
|
|
|
|
private readonly List<string> lines = new List<string>();
|
|
|
|
|
private int position = -1;
|
|
|
|
|
|
|
|
|
|
public Lines(string path) => this.path = path;
|
|
|
|
|
|
|
|
|
|
public bool EndOfFile => this.position == this.lines.Count;
|
|
|
|
|
|
|
|
|
|
public void Read()
|
|
|
|
|
{
|
2019-07-21 08:10:45 +00:00
|
|
|
|
using (var reader = File.OpenText(this.path))
|
2019-07-20 18:17:08 +00:00
|
|
|
|
{
|
|
|
|
|
while (!reader.EndOfStream)
|
|
|
|
|
{
|
|
|
|
|
var line = reader.ReadLine();
|
2019-07-21 08:10:45 +00:00
|
|
|
|
var ignored = line.StartsWith(";", StringComparison.OrdinalIgnoreCase);
|
2019-07-20 18:17:08 +00:00
|
|
|
|
if (!ignored)
|
|
|
|
|
{
|
|
|
|
|
this.lines.Add(line);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-21 08:10:45 +00:00
|
|
|
|
|
|
|
|
|
// Users should check EndOfFile before using a bad position...
|
|
|
|
|
this.position = 0;
|
2019-07-20 18:17:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string ReadLine()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return this.PeekLine();
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
this.Increment();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UnreadLine() => this.Decrement();
|
|
|
|
|
|
|
|
|
|
public string PeekLine() => this.lines[this.position];
|
|
|
|
|
|
|
|
|
|
private void Increment() => ++this.position;
|
|
|
|
|
|
|
|
|
|
private void Decrement() => --this.position;
|
|
|
|
|
}
|
|
|
|
|
}
|