33using System . Collections . Generic ;
44using System . Diagnostics . Contracts ;
55using System . IO ;
6+ using System . Threading . Tasks ;
67
78namespace Open . Text . CSV
89{
910 public class CsvReader : DisposableBase
1011 {
1112 public CsvReader ( StreamReader source )
1213 {
13- _source = source ;
14+ _source = source ?? throw new ArgumentNullException ( nameof ( source ) ) ;
1415 }
1516
1617 StreamReader _source ;
1718
18- int _maxColumns ;
19- public int MaxColumns => _maxColumns ;
2019
2120 protected override void OnDispose ( bool calledExplicitly )
2221 {
2322 _source = null ; // The intention here is if this object is disposed, then prevent further reading.
2423 }
2524
26- public string [ ] ReadRow ( )
27- {
28- return GetRow ( _source , ref _maxColumns ) ;
29- }
25+ public IEnumerable < string > ReadNextRow ( )
26+ => GetNextRow ( _source ) ;
27+
28+ public ValueTask < IEnumerable < string > > ReadNextRowAsync ( )
29+ => GetNextRowAsync ( _source ) ;
3030
31- public string [ ] [ ] ReadRows ( )
31+ public IEnumerable < IEnumerable < string > > ReadRows ( )
3232 {
33- var rows = GetRows ( _source , out var maxColumns ) ;
34- if ( maxColumns > _maxColumns ) _maxColumns = maxColumns ;
35- return rows ;
33+ var s = _source ;
34+ if ( s == null )
35+ throw new ObjectDisposedException ( GetType ( ) . ToString ( ) ) ;
36+ Contract . EndContractBlock ( ) ;
37+
38+ while ( TryGetNextRow ( s , out var row ) )
39+ yield return row ;
3640 }
3741
38- public static bool TryGetRow ( StreamReader source , out string [ ] row , ref int maxColumns )
42+ public static bool TryGetNextRow ( StreamReader source , out IEnumerable < string > row )
3943 {
4044 if ( source == null )
4145 throw new ArgumentNullException ( nameof ( source ) ) ;
4246 Contract . EndContractBlock ( ) ;
4347
4448 if ( ! source . EndOfStream )
4549 {
46- row = CsvUtility . GetLine ( source . ReadLine ( ) , ref maxColumns ) ;
50+ row = CsvUtility . GetLine ( source . ReadLine ( ) ) ;
4751 return true ;
4852 }
4953
5054 row = null ;
5155 return false ;
5256 }
5357
54- public static string [ ] GetRow ( StreamReader source , ref int maxColumns )
58+ public static ValueTask < IEnumerable < string > > GetNextRowAsync ( StreamReader source )
59+ {
60+ if ( source == null )
61+ throw new ArgumentNullException ( nameof ( source ) ) ;
62+ Contract . EndContractBlock ( ) ;
63+
64+ if ( source . EndOfStream )
65+ return new ValueTask < IEnumerable < string > > ( default ( IEnumerable < string > ) ) ;
66+
67+ return GetNextRowAsyncCore ( source ) ;
68+ }
69+
70+ static async ValueTask < IEnumerable < string > > GetNextRowAsyncCore ( StreamReader source )
71+ => source . EndOfStream ? null : CsvUtility . GetLine ( await source . ReadLineAsync ( ) ) ;
72+
73+ public static IEnumerable < string > GetNextRow ( StreamReader source )
5574 {
56- TryGetRow ( source , out var row , ref maxColumns ) ;
75+ TryGetNextRow ( source , out var row ) ;
5776 return row ;
5877 }
5978
60- public static string [ ] [ ] GetRows ( StreamReader source , out int maxColumns )
79+ public static IEnumerable < IEnumerable < string > > GetRows ( StreamReader source )
6180 {
6281 if ( source == null )
6382 throw new ArgumentNullException ( nameof ( source ) ) ;
6483 Contract . EndContractBlock ( ) ;
6584
66- maxColumns = 0 ;
67- var lines = new List < string [ ] > ( ) ;
68- while ( TryGetRow ( source , out var row , ref maxColumns ) )
69- lines . Add ( row ) ;
70-
71- return lines . ToArray ( ) ;
85+ while ( TryGetNextRow ( source , out var row ) )
86+ yield return row ;
7287 }
7388
74- public static string [ ] [ ] GetRowsFromFile ( string filepath , out int maxColumns )
89+ public static IEnumerable < IEnumerable < string > > GetRowsFromFile ( string filepath )
7590 {
7691 if ( filepath == null )
7792 throw new ArgumentNullException ( nameof ( filepath ) ) ;
@@ -80,11 +95,11 @@ public static string[][] GetRowsFromFile(string filepath, out int maxColumns)
8095 Contract . EndContractBlock ( ) ;
8196
8297 if ( File . Exists ( filepath ) )
98+ {
8399 using ( var s = new StreamReader ( ( new FileInfo ( filepath ) ) . OpenRead ( ) ) )
84- return GetRows ( s , out maxColumns ) ;
85-
86- maxColumns = 0 ;
87- return new string [ 0 ] [ ] ;
100+ foreach ( var line in GetRows ( s ) )
101+ yield return line ;
102+ }
88103 }
89104 }
90105}
0 commit comments