-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCsvParser.cs
140 lines (121 loc) · 4.46 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* License
* ------------------------------------------------------------------------------
* Copyright (c) 2019 UX Digital Systems Ltd
*
* Permission is hereby granted, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software
* for the continued use and development of the system on which it was installed,
* and to permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* Any persons obtaining the software have no rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software without
* written persmission from UX Digital Systems Ltd, if it is not for use on the
* system on which it was originally installed.
* ------------------------------------------------------------------------------
* UX.Digital
* ----------
* http://ux.digital
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Crestron.SimplSharp;
using Crestron.SimplSharp.CrestronIO;
namespace UX.Lib2
{
public class CsvParser
{
#region Fields
private readonly StreamReader _reader;
private ReadOnlyDictionary<int, string> _columnTitles;
private List<ReadOnlyDictionary<string, string>> _results;
private Dictionary<int, int> _columnWidth;
#endregion
#region Constructors
public CsvParser(Stream stream)
{
_reader = new StreamReader(stream);
}
#endregion
#region Finalizers
#endregion
#region Events
#endregion
#region Delegates
#endregion
#region Properties
public List<string> ColumnTitles
{
get { return _columnTitles.Values.ToList(); }
}
public List<ReadOnlyDictionary<string, string>> Results
{
get { return _results; }
}
#endregion
#region Methods
public void Parse()
{
var titles = new Dictionary<int, string>();
_columnWidth = new Dictionary<int, int>();
try
{
var line = _reader.ReadLine();
var columnIndex = 0;
foreach (var title in Regex.Split(line, @",(?=(?:[^\""]*\""[^\""]*\"")*(?![^\""]*\""))"))
{
titles[columnIndex] = title;
_columnWidth[columnIndex] = title.Length;
columnIndex ++;
}
_columnTitles = new ReadOnlyDictionary<int, string>(titles);
}
catch (Exception e)
{
throw new Exception(string.Format("Could not parse header line, {0}", e.Message));
}
_results = new List<ReadOnlyDictionary<string, string>>();
var lineCount = 0;
while (_reader.Peek() >= 0)
{
lineCount ++;
try
{
var line = _reader.ReadLine();
var columnIndex = 0;
var result = new Dictionary<string, string>();
foreach (var value in Regex.Split(line, @",(?=(?:[^\""]*\""[^\""]*\"")*(?![^\""]*\""))"))
{
var newValue = value;
if (value.Length > 1 && value.StartsWith("\"") && value.EndsWith("\""))
{
newValue = value.Substring(1, value.Length - 2);
}
result[_columnTitles[columnIndex]] = newValue;
if (newValue.Length > _columnWidth[columnIndex])
{
_columnWidth[columnIndex] = newValue.Length;
}
columnIndex++;
}
_results.Add(new ReadOnlyDictionary<string, string>(result));
}
catch (Exception e)
{
throw new Exception(string.Format("Error parsing csv on line {0}, {1}", lineCount, e.Message));
}
}
}
public int GetColumnWidth(int columnIndex)
{
return _columnWidth[columnIndex];
}
#endregion
}
}