Skip to content

Instantly share code, notes, and snippets.

@kisssko
Created May 6, 2018 19:25
Show Gist options
  • Save kisssko/e05605607e7f99ffb237a17b0ac9e858 to your computer and use it in GitHub Desktop.
Save kisssko/e05605607e7f99ffb237a17b0ac9e858 to your computer and use it in GitHub Desktop.
ReadLines iterator (cs)
using System;
using System.Text;
namespace System.IO
{
internal class ReadLinesIterator : Iterator<string>
{
private readonly string _path;
private readonly Encoding _encoding;
private StreamReader _reader;
private ReadLinesIterator(string path, Encoding encoding, StreamReader reader)
{
this._path = path;
this._encoding = encoding;
this._reader = reader;
}
public override bool MoveNext()
{
if (this._reader != null)
{
this.current = this._reader.ReadLine();
if (this.current != null)
{
return true;
}
base.Dispose();
}
return false;
}
protected override Iterator<string> Clone()
{
return ReadLinesIterator.CreateIterator(this._path, this._encoding, this._reader);
}
protected override void Dispose(bool disposing)
{
try
{
if (disposing && this._reader != null)
{
this._reader.Dispose();
}
}
finally
{
this._reader = null;
base.Dispose(disposing);
}
}
internal static ReadLinesIterator CreateIterator(string path, Encoding encoding)
{
return ReadLinesIterator.CreateIterator(path, encoding, null);
}
private static ReadLinesIterator CreateIterator(string path, Encoding encoding, StreamReader reader)
{
return new ReadLinesIterator(path, encoding, reader ?? new StreamReader(path, encoding));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment