-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCsvReader.cs
284 lines (237 loc) · 6.19 KB
/
CsvReader.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
namespace Sylphe.Csv
{
/// <summary>
/// Reader for CSV files (comma separated values).
/// </summary>
/// <remarks>
/// To read a file, call <see cref="ReadRecord"/> until it returns
/// <c>false</c>; after each such call, <see cref="Values"/>
/// contains the current record's values.
/// <para/>
/// The <see cref="FieldSeparator"/> and <see cref="QuoteChar"/>
/// are configurable (they default to a comma and a double quote,
/// respectively). The record separator is always a newline, that
/// is, either CR, or LF, or CR immediately followed by LF.
/// <para/>
/// <see cref="RecordNumber"/> is the number of the record last read,
/// whereas <see cref="LineNumber"/> is the reader's current line.
/// Usually, LineNumber is one more than RecordNumber; it is larger
/// if newlines occur within fields.
/// <para/>
/// If <see cref="SkipBlankLines"/> is <c>true</c>, lines that are
/// empty or consist of white space only will be skipped.
/// If <see cref="SkipCommentLines"/> is <c>true</c>, lines that
/// begin with the <see cref="CommentChar"/> (optionally preceded
/// by blanks and tabs) will be skipped.
/// </remarks>
public class CsvReader : IDisposable
{
private TextReader _reader;
private readonly StringBuilder _buffer;
private int _fieldCountFirstRecord;
private const int CR = 13, LF = 10;
private const int CRLF = 0x10FFFF + 1; // max Unicode + 1
public CsvReader([NotNull] TextReader reader, char fieldSeparator = ',',
char quoteChar = '"')
{
_reader = reader ?? throw new ArgumentNullException(nameof(reader));
Values = new List<string>();
_buffer = new StringBuilder();
_fieldCountFirstRecord = 0;
QuoteChar = quoteChar;
FieldSeparator = fieldSeparator;
SkipBlankLines = false;
SkipCommentLines = false;
CommentChar = '#';
LineNumber = 1;
RecordNumber = 0;
}
public char QuoteChar { get; set; }
public char FieldSeparator { get; set; }
public bool SkipBlankLines { get; set; }
public bool SkipCommentLines { get; set; }
public char CommentChar { get; set; }
public int LineNumber { get; private set; }
public int RecordNumber { get; private set; }
[NotNull]
public IList<string> Values { get; }
/// <summary>
/// Read the next record. If there is a next record,
/// make its fields available through the <see cref="Values"/>
/// property and return <c>true</c>. Otherwise, clear
/// <see cref="Values"/> and return <c>false</c> (the reader
/// has reached the end of the input).
/// </summary>
public bool ReadRecord()
{
CheckDisposed();
Values.Clear();
_buffer.Length = 0;
int cc = Read();
while (true)
{
if (cc < 0)
{
return false;
}
cc = SkipBlanks(cc);
if (SkipCommentLines && cc == CommentChar)
{
cc = SkipLine(cc);
continue;
}
if (SkipBlankLines && IsEndOfLine(cc))
{
cc = Read();
continue;
}
nextField:
cc = ReadField(cc);
if (IsEndOfLine(cc))
{
if (_fieldCountFirstRecord <= 0)
{
_fieldCountFirstRecord = Values.Count;
}
else
{
while (Values.Count < _fieldCountFirstRecord)
{
Values.Add(string.Empty);
}
}
RecordNumber += 1;
return true;
}
if (cc == FieldSeparator)
{
cc = Read();
cc = SkipBlanks(cc);
goto nextField;
}
throw SyntaxError(LineNumber, "Unexpected trash after field value");
}
}
public void Dispose()
{
if (_reader != null)
{
_reader.Dispose();
_reader = null;
}
}
#region Private methods
private void CheckDisposed()
{
if (_reader == null)
{
throw new ObjectDisposedException(GetType().Name);
}
}
private int Read()
{
int cc = _reader.Read();
if (cc == CR || cc == LF)
{
if (cc == CR && _reader.Peek() == LF)
{
_reader.Read();
cc = CRLF;
}
LineNumber += 1;
}
return cc;
}
private int SkipBlanks(int cc)
{
// Avoid Char.IsWhiteSpace: it includes our record separator!
// Here we want: blank, horizontal tab, vertical tab, form feed
// (Char.IsWhiteSpace also includes 0x85 and 0xA0, besides CR and LF)
while (cc == 32 || cc == 9 || cc == 11 || cc == 12)
{
cc = Read();
}
return cc;
}
private int SkipLine(int cc)
{
while (! IsEndOfLine(cc))
{
cc = Read();
}
// Consume the EOL:
return cc < 0 ? cc : Read();
}
private int ReadField(int cc)
{
// Assume leading blanks have been skipped
_buffer.Length = 0; // clear
var fieldLength = 0; // w/o trailing blanks
while (cc != FieldSeparator && ! IsEndOfLine(cc))
{
if (cc == QuoteChar)
{
cc = ReadString((char) cc, _buffer);
fieldLength = _buffer.Length;
}
else
{
_buffer.Append((char) cc);
if (! char.IsWhiteSpace((char) cc))
{
fieldLength = _buffer.Length;
}
cc = Read();
}
}
Values.Add(_buffer.ToString(0, fieldLength));
return cc;
}
private int ReadString(char quote, StringBuilder buffer)
{
int cc;
while ((cc = Read()) >= 0)
{
if (cc == CRLF)
{
// Within a quoted string, CRLF
// is to be taken verbatim:
buffer.Append('\r');
buffer.Append('\n');
}
else if (cc == quote)
{
if (_reader.Peek() == quote)
{
buffer.Append(quote);
Read();
}
else
{
return Read();
}
}
else
{
buffer.Append((char) cc);
}
}
throw SyntaxError(LineNumber, "Unterminated string");
}
private static bool IsEndOfLine(int cc)
{
// End-of-input is also end-of-line:
return cc == CR || cc == LF || cc == CRLF || cc < 0;
}
private static Exception SyntaxError(int lineno, string message)
{
return new FormatException(string.Format("{0} (line {1})", message, lineno));
}
#endregion
}
}