-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCsv.cs
168 lines (144 loc) · 5.11 KB
/
Csv.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
namespace Standard
{
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text;
internal static class Csv
{
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static string Escape(string str)
{
if (string.IsNullOrEmpty(str))
{
return "";
}
if (str.Contains("\""))
{
str = str.Replace("\"", "\"\"");
}
if (str.IndexOfAny(new char[] { ',', '"', '\n' }) > -1)
{
str = '\"' + str + '\"';
}
return str;
}
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static string MakeLine(string[] data)
{
var sb = new StringBuilder();
foreach (var cell in data)
{
sb.Append(Escape(cell));
sb.Append(',');
}
return sb.ToString(0, sb.Length - 1);
}
// Read a logical line from the stream. It may contain embedded newlines if it was CSV-style escaped.
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
private static string _ReadLine(StreamReader reader)
{
int i = 0;
bool inEscape = false;
bool lastChance = false;
var sb = new StringBuilder(reader.ReadLine());
while (true)
{
for (; i < sb.Length; ++i)
{
if (sb[i] == '\"')
{
inEscape = !inEscape;
}
}
if (!inEscape)
{
return sb.ToString();
}
sb.Append("\n");
if (lastChance)
{
throw new ArgumentException("Invalid CSV data.");
}
sb.Append(reader.ReadLine());
lastChance = reader.EndOfStream;
}
}
private static readonly string bellString = Encoding.ASCII.GetString(new byte[] { 7 });
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
private static List<string> _ParseLine(string row)
{
if (!row.Contains('\"'))
{
return new List<string>(row.Split(','));
}
// Make it simpler to not have to look for escaped quotes.
row = row.Replace("\"\"", bellString);
var ret = new List<string>();
int startIndex = 0;
for (int i = 0; i < row.Length; ++i)
{
if (row[i] == '\"')
{
// Skip the opening quote
startIndex = i + 1;
while (row[++i] != '\"')
{ }
// Remove the trailing quote, and replace back any embedded quotes.
ret.Add(row.Substring(startIndex, i - startIndex).Replace((char)7, '\"'));
++i;
if (i < row.Length && row[i] != ',')
{
throw new ArgumentException("Malformed data.");
}
startIndex = i + 1;
}
else
{
if (i >= row.Length - 1 || row[i] == ',')
{
ret.Add("");
startIndex = i + 1;
continue;
}
while (i < row.Length-1 && row[++i] != ',')
{ }
ret.Add(row.Substring(startIndex, i - startIndex));
if (i < row.Length-1 && row[i] != ',')
{
throw new ArgumentException("Malformed data.");
}
startIndex = i + 1;
}
}
if (row.EndsWith(",", StringComparison.Ordinal))
{
ret.Add("");
}
return ret;
}
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static List<Dictionary<string, string>> ReadDocument(StreamReader reader)
{
List<string> headers = _ParseLine(_ReadLine(reader));
var ret = new List<Dictionary<string, string>>();
while (!reader.EndOfStream)
{
var rowData = new Dictionary<string,string>();
List<string> cells = _ParseLine(_ReadLine(reader));
if (cells.Count != headers.Count)
{
throw new ArgumentException("Bad CSV file.");
}
for (int i = 0; i < headers.Count; ++i)
{
rowData.Add(headers[i], cells[i]);
}
ret.Add(rowData);
}
return ret;
}
}
}