Created
May 6, 2018 19:25
-
-
Save kisssko/e05605607e7f99ffb237a17b0ac9e858 to your computer and use it in GitHub Desktop.
ReadLines iterator (cs)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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