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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Collections.Concurrent;
using System.Threading.Tasks;
namespace Tango.CSV
{
/// <summary>
/// Represents a CSV file reader.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <seealso cref="Tango.CSV.CsvFile" />
/// <seealso cref="System.Collections.Generic.IEnumerable{T}" />
/// <seealso cref="System.Collections.Generic.IEnumerator{T}" />
internal class CsvFileReader<T> : CsvFile, IEnumerable<T>, IEnumerator<T>
where T : new()
{
private readonly Dictionary<Type, List<Action<T, String>>> allSetters = new Dictionary<Type, List<Action<T, String>>>();
private string[] columns;
private char curChar;
private int len;
private string line;
private int pos;
private T record;
private readonly char fieldSeparator;
private readonly TextReader textReader;
private readonly char textQualifier;
private readonly StringBuilder parseFieldResult = new StringBuilder();
/// <summary>
/// Initializes a new instance of the <see cref="CsvFileReader{T}"/> class.
/// </summary>
/// <param name="csvSource">The CSV source.</param>
public CsvFileReader(CsvSource csvSource)
: this(csvSource, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="CsvFileReader{T}"/> class.
/// </summary>
/// <param name="csvSource">The CSV source.</param>
/// <param name="csvDefinition">The CSV definition.</param>
public CsvFileReader(CsvSource csvSource, CsvDefinition csvDefinition)
{
var streamReader = csvSource.TextReader as StreamReader;
if (streamReader != null)
this.BaseStream = streamReader.BaseStream;
if (csvDefinition == null)
csvDefinition = DefaultCsvDefinition;
this.fieldSeparator = csvDefinition.FieldSeparator;
this.textQualifier = csvDefinition.TextQualifier;
this.textReader = csvSource.TextReader;// new FileStream(csvSource.TextReader, FileMode.Open);
this.ReadHeader(csvDefinition.Header);
}
/// <summary>
/// Gets the element in the collection at the current position of the enumerator.
/// </summary>
public T Current
{
get { return this.record; }
}
/// <summary>
/// Gets a value indicating whether this <see cref="CsvFileReader{T}"/> is EOF.
/// </summary>
public bool Eof
{
get { return this.line == null; }
}
/// <summary>
/// Gets the element in the collection at the current position of the enumerator.
/// </summary>
object IEnumerator.Current
{
get { return this.Current; }
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
// free managed resources
this.textReader.Dispose();
}
}
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator" /> object that can be used to iterate through the collection.
/// </returns>
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>
/// An enumerator that can be used to iterate through the collection.
/// </returns>
public IEnumerator<T> GetEnumerator()
{
return this;
}
/// <summary>
/// Advances the enumerator to the next element of the collection.
/// </summary>
/// <returns>
/// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
/// </returns>
public bool MoveNext()
{
this.ReadNextLine();
if (this.line == null && (this.line = this.textReader.ReadLine()) == null)
{
this.record = default(T);
}
else
{
this.record = new T();
Type recordType = typeof(T);
List<Action<T, String>> setters;
if (!this.allSetters.TryGetValue(recordType, out setters))
{
setters = this.CreateSetters();
this.allSetters[recordType] = setters;
}
var fieldValues = new string[setters.Count];
for (int i = 0; i < setters.Count; i++)
{
fieldValues[i] = this.ParseField();
if (this.curChar == this.fieldSeparator)
this.NextChar();
else
break;
}
for (int i = 0; i < setters.Count; i++)
{
var setter = setters[i];
if (setter != null)
{
setter(this.record, fieldValues[i]);
}
}
}
return (this.record != null);
}
/// <summary>
/// Sets the enumerator to its initial position, which is before the first element in the collection.
/// </summary>
/// <exception cref="System.NotImplementedException">Cannot reset CsvFileReader enumeration.</exception>
public void Reset()
{
throw new NotImplementedException("Cannot reset CsvFileReader enumeration.");
}
/// <summary>
/// Emits the set value action.
/// </summary>
/// <param name="mi">The mi.</param>
/// <param name="func">The function.</param>
/// <returns></returns>
/// <exception cref="System.NotImplementedException"></exception>
private static Action<T, string> EmitSetValueAction(MemberInfo mi, Func<string, object> func)
{
ParameterExpression paramExpObj = Expression.Parameter(typeof(object), "obj");
ParameterExpression paramExpT = Expression.Parameter(typeof(T), "instance");
{
var pi = mi as PropertyInfo;
if (pi != null)
{
if (CsvFile.UseLambdas)
{
var callExpr = Expression.Call(
paramExpT,
pi.GetSetMethod(),
Expression.ConvertChecked(paramExpObj, pi.PropertyType));
var setter = Expression.Lambda<Action<T, object>>(
callExpr,
paramExpT,
paramExpObj).Compile();
return (o, s) => setter(o, func(s));
}
return (o, v) => pi.SetValue(o, (object)func(v), null);
}
}
{
var fi = mi as FieldInfo;
if (fi != null)
{
if (CsvFile.UseLambdas)
{
//ParameterExpression valueExp = Expression.Parameter(typeof(string), "value");
var valueExp = Expression.ConvertChecked(paramExpObj, fi.FieldType);
// Expression.Property can be used here as well
MemberExpression fieldExp = Expression.Field(paramExpT, fi);
BinaryExpression assignExp = Expression.Assign(fieldExp, valueExp);
var setter = Expression.Lambda<Action<T, object>>
(assignExp, paramExpT, paramExpObj).Compile();
return (o, s) => setter(o, func(s));
}
return ((o, v) => fi.SetValue(o, func(v)));
}
}
throw new NotImplementedException();
}
/// <summary>
/// Finds the setter.
/// </summary>
/// <param name="c">The c.</param>
/// <param name="staticMember">if set to <c>true</c> [static member].</param>
/// <returns></returns>
private static Action<T, string> FindSetter(string c, bool staticMember, int? index = null)
{
var flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.IgnoreCase | (staticMember ? BindingFlags.Static : BindingFlags.Instance);
Action<T, string> action = null;
PropertyInfo pi = typeof(T).GetProperty(c, flags);
if (pi != null)
{
var pFunc = StringToObject(pi.PropertyType);
action = EmitSetValueAction(pi, pFunc);
}
if (action == null)
{
FieldInfo fi = typeof(T).GetField(c, flags);
if (fi != null)
{
var fFunc = StringToObject(fi.FieldType);
action = EmitSetValueAction(fi, fFunc);
}
}
if (action == null && index != null)
{
var propByIndex = typeof(T).GetProperties(flags).SingleOrDefault(x => x.GetCustomAttribute<CsvOrderAttribute>() != null && x.GetCustomAttribute<CsvOrderAttribute>().Index == index);
if (propByIndex != null)
{
var pFunc = StringToObject(propByIndex.PropertyType);
action = EmitSetValueAction(propByIndex, pFunc);
}
}
return action;
}
/// <summary>
/// Strings to object.
/// </summary>
/// <param name="propertyType">Type of the property.</param>
/// <returns></returns>
/// <exception cref="System.NotImplementedException"></exception>
private static Func<string, object> StringToObject(Type propertyType)
{
if (propertyType == typeof(string))
return (s) => s ?? String.Empty;
else if (propertyType == typeof(Int32))
return (s) => String.IsNullOrEmpty(s) ? 0 : Int32.Parse(s);
if (propertyType == typeof(DateTime))
return (s) => String.IsNullOrEmpty(s) ? DateTimeZero : DateTime.Parse(s);
else if (propertyType == typeof(Double))
return (s) => String.IsNullOrEmpty(s) ? 0.0 : Double.Parse(s);
else
throw new NotImplementedException();
}
/// <summary>
/// Creates the setters.
/// </summary>
/// <returns></returns>
private List<Action<T, string>> CreateSetters()
{
var list = new List<Action<T, string>>();
for (int i = 0; i < this.columns.Length; i++)
{
string columnName = this.columns[i];
Action<T, string> action = null;
if (columnName.IndexOf(' ') >= 0)
columnName = columnName.Replace(" ", "");
action = FindSetter(columnName, false, i) ?? FindSetter(columnName, true, i);
list.Add(action);
}
return list;
}
/// <summary>
/// Next character.
/// </summary>
private void NextChar()
{
if (this.pos < this.len)
{
this.pos++;
this.curChar = this.pos < this.len ? this.line[this.pos] : '\0';
}
}
/// <summary>
/// Parses the end of line.
/// </summary>
/// <exception cref="System.NotImplementedException"></exception>
private void ParseEndOfLine()
{
throw new NotImplementedException();
}
/// <summary>
/// Parses the field.
/// </summary>
/// <returns></returns>
private string ParseField()
{
parseFieldResult.Length = 0;
if (this.line == null || this.pos >= this.len)
return null;
while (this.curChar == ' ' || this.curChar == '\t')
{
this.NextChar();
}
if (this.curChar == this.textQualifier)
{
this.NextChar();
while (this.curChar != 0)
{
if (this.curChar == this.textQualifier)
{
this.NextChar();
if (this.curChar == this.textQualifier)
{
this.NextChar();
parseFieldResult.Append(this.textQualifier);
}
else
return parseFieldResult.ToString();
}
else if (this.curChar == '\0')
{
if (this.line == null)
return parseFieldResult.ToString();
this.ReadNextLine();
}
else
{
parseFieldResult.Append(this.curChar);
this.NextChar();
}
}
}
else
{
while (this.curChar != 0 && this.curChar != this.fieldSeparator && this.curChar != '\r' && this.curChar != '\n')
{
parseFieldResult.Append(this.curChar);
this.NextChar();
}
}
return parseFieldResult.ToString();
}
/// <summary>
/// Reads the header.
/// </summary>
/// <param name="header">The header.</param>
private void ReadHeader(string header)
{
if (header == null)
{
this.ReadNextLine();
}
else
{
// we read the first line from the given header
this.line = header;
this.pos = -1;
this.len = this.line.Length;
this.NextChar();
}
var readColumns = new List<string>();
string columnName;
while ((columnName = this.ParseField()) != null)
{
readColumns.Add(columnName);
if (this.curChar == this.fieldSeparator)
this.NextChar();
else
break;
}
this.columns = readColumns.ToArray();
}
/// <summary>
/// Reads the next line.
/// </summary>
private void ReadNextLine()
{
this.line = this.textReader.ReadLine();
this.pos = -1;
if (this.line == null)
{
this.len = 0;
this.curChar = '\0';
}
else
{
this.len = this.line.Length;
this.NextChar();
}
}
}
}
|