-
Notifications
You must be signed in to change notification settings - Fork 1
/
CsvParser.cs
46 lines (39 loc) · 1.34 KB
/
CsvParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LogViewer
{
class CsvParser
{
private TextReader reader;
private CsvFile file;
public static CsvFile Parse(TextReader reader)
{
CsvParser parser = new CsvParser(reader);
parser.Parse();
return parser.file;
}
private CsvParser(TextReader reader)
{
this.reader = reader;
}
private void Parse()
{
string headerString = this.reader.ReadLine();
string[] headers = headerString.Split(',');
string rowString;
List<ReadOnlyList<string>> rows = new List<ReadOnlyList<string>>();
while ((rowString = this.reader.ReadLine()) != null)
{
string[] rowValues = rowString.Split(',');
ReadOnlyList<string> row = new ReadOnlyList<string>(rowValues);
rows.Add(row);
}
ReadOnlyList<string> readOnlyHeaders = new ReadOnlyList<string>(headers);
ReadOnlyList<ReadOnlyList<string>> readOnlyRows = new ReadOnlyList<ReadOnlyList<string>>(rows);
this.file = new CsvFile(readOnlyHeaders, readOnlyRows);
}
}
}