-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Workaround filed here: alexanderkozlenko/addax#21
- Loading branch information
1 parent
b8989a9
commit 45fecf7
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
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
66 changes: 66 additions & 0 deletions
66
NCsvPerf/CsvReadable/Implementations/Addax_Formats_Tabular.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using Addax.Formats.Tabular; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
|
||
namespace Knapcode.NCsvPerf.CsvReadable | ||
{ | ||
/// <summary> | ||
/// Package: https://www.nuget.org/packages/Addax.Formats.Tabular | ||
/// Source: https://github.com/alexanderkozlenko/addax | ||
/// </summary> | ||
public class Addax_Formats_Tabular : ICsvReader | ||
{ | ||
private readonly TabularDialect _dialect; | ||
|
||
public Addax_Formats_Tabular() | ||
{ | ||
_dialect = new TabularDialect("\r\n", ',', '\"'); | ||
} | ||
|
||
public List<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new() | ||
{ | ||
var allRecords = new List<T>(); | ||
|
||
using (var reader = new TabularReader(stream, _dialect)) | ||
{ | ||
while (reader.TryPickRecord()) | ||
{ | ||
// workaround for https://github.com/alexanderkozlenko/addax/issues/21 | ||
string firstValue; | ||
if (!reader.TryReadField() || !reader.TryGetString(out firstValue)) | ||
{ | ||
break; | ||
} | ||
|
||
string secondValue; | ||
if (!reader.TryReadField() || !reader.TryGetString(out secondValue)) | ||
{ | ||
break; | ||
} | ||
|
||
var record = new T(); | ||
record.Read(i => | ||
{ | ||
if (i == 0) | ||
{ | ||
return firstValue; | ||
} | ||
else if (i == 1) | ||
{ | ||
return secondValue; | ||
} | ||
else if (reader.TryReadField() && reader.TryGetString(out var value)) | ||
{ | ||
return value; | ||
} | ||
|
||
return null; | ||
}); | ||
allRecords.Add(record); | ||
} | ||
} | ||
|
||
return allRecords; | ||
} | ||
} | ||
} |
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