aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.CSV/CsvDynamicReader.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Software/Visual_Studio/Tango.CSV/CsvDynamicReader.cs')
-rw-r--r--Software/Visual_Studio/Tango.CSV/CsvDynamicReader.cs42
1 files changed, 42 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.CSV/CsvDynamicReader.cs b/Software/Visual_Studio/Tango.CSV/CsvDynamicReader.cs
index 33ba859eb..d43b9ba3d 100644
--- a/Software/Visual_Studio/Tango.CSV/CsvDynamicReader.cs
+++ b/Software/Visual_Studio/Tango.CSV/CsvDynamicReader.cs
@@ -20,6 +20,48 @@ namespace Tango.CSV
public char Delimiter { get; }
+ public static CsvDynamicReader FromString(String csv)
+ {
+ return new CsvDynamicReader(csv);
+ }
+
+ private CsvDynamicReader(string csvContent)
+ {
+ Delimiter = ',';
+ _colIndex = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
+ _rows = new List<Row>();
+
+ using (var sr = new StringReader(csvContent))
+ {
+ // Read to first non-empty line for headers
+ string headerLine;
+ do
+ {
+ headerLine = sr.ReadLine();
+ if (headerLine == null)
+ throw new InvalidDataException("CSV file has no header row.");
+ } while (string.IsNullOrWhiteSpace(headerLine));
+
+ var headers = ParseCsvLine(headerLine, Delimiter);
+ for (int i = 0; i < headers.Count; i++)
+ {
+ var clean = CleanHeader(headers[i]);
+ if (!_colIndex.ContainsKey(clean))
+ _colIndex.Add(clean, i);
+ // If duplicate header name appears, first one wins.
+ }
+
+ // Read all rows
+ string line;
+ while ((line = sr.ReadLine()) != null)
+ {
+ if (line.Length == 0) continue; // skip empty
+ var fields = ParseCsvLine(line, Delimiter).ToArray();
+ _rows.Add(new Row(this, fields));
+ }
+ }
+ }
+
public CsvDynamicReader(string path, char delimiter = ',')
{
if (path == null) throw new ArgumentNullException(nameof(path));