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
|
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
namespace Tango.CSV
{
/// <summary>
/// CSV reader that supports unknown schemas and typed access with defaults.
/// - Case-insensitive column lookup
/// - Robust CSV parsing (RFC-4180 style: quotes, commas, escaped quotes)
/// - Typed Read with defaults: string, int, double, bool, and enums
/// </summary>
public sealed class CsvDynamicReader
{
private readonly Dictionary<string, int> _colIndex;
private readonly List<Row> _rows;
public IReadOnlyList<Row> Rows => _rows;
public char Delimiter { get; }
public static CsvDynamicReader FromString(String csv)
{
return new CsvDynamicReader(csv);
}
private CsvDynamicReader(string csvContent)
{
Delimiter = ',';
_colIndex = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
_rows = new List<Row>();
using (var sr = new StringReader(csvContent))
{
// Read to first non-empty line for headers
string headerLine;
do
{
headerLine = sr.ReadLine();
if (headerLine == null)
throw new InvalidDataException("CSV file has no header row.");
} while (string.IsNullOrWhiteSpace(headerLine));
var headers = ParseCsvLine(headerLine, Delimiter);
for (int i = 0; i < headers.Count; i++)
{
var clean = CleanHeader(headers[i]);
if (!_colIndex.ContainsKey(clean))
_colIndex.Add(clean, i);
// If duplicate header name appears, first one wins.
}
// Read all rows
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Length == 0) continue; // skip empty
var fields = ParseCsvLine(line, Delimiter).ToArray();
_rows.Add(new Row(this, fields));
}
}
}
public CsvDynamicReader(string path, char delimiter = ',')
{
if (path == null) throw new ArgumentNullException(nameof(path));
if (!File.Exists(path)) throw new FileNotFoundException("CSV file not found.", path);
Delimiter = delimiter;
_colIndex = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
_rows = new List<Row>();
using (var sr = new StreamReader(path))
{
// Read to first non-empty line for headers
string headerLine;
do
{
headerLine = sr.ReadLine();
if (headerLine == null)
throw new InvalidDataException("CSV file has no header row.");
} while (string.IsNullOrWhiteSpace(headerLine));
var headers = ParseCsvLine(headerLine, Delimiter);
for (int i = 0; i < headers.Count; i++)
{
var clean = CleanHeader(headers[i]);
if (!_colIndex.ContainsKey(clean))
_colIndex.Add(clean, i);
// If duplicate header name appears, first one wins.
}
// Read all rows
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Length == 0) continue; // skip empty
var fields = ParseCsvLine(line, Delimiter).ToArray();
_rows.Add(new Row(this, fields));
}
}
}
internal bool TryGetIndex(string columnName, out int index)
{
if (columnName == null) { index = -1; return false; }
return _colIndex.TryGetValue(columnName.Trim(), out index);
}
private static string CleanHeader(string s)
{
if (string.IsNullOrEmpty(s)) return string.Empty;
// Trim quotes if header was quoted
var t = s.Trim();
if (t.Length >= 2 && t[0] == '"' && t[t.Length - 1] == '"')
{
t = t.Substring(1, t.Length - 2).Replace("\"\"", "\"");
}
// Remove BOM if present
t = t.Trim('\uFEFF').Trim();
return t;
}
/// <summary>
/// RFC-4180-ish CSV line parser: supports quoted fields, commas, escaped quotes ("").
/// </summary>
private static List<string> ParseCsvLine(string line, char delimiter)
{
var result = new List<string>();
if (line == null)
{
result.Add(string.Empty);
return result;
}
var sb = new System.Text.StringBuilder(line.Length);
bool inQuotes = false;
for (int i = 0; i < line.Length; i++)
{
var c = line[i];
if (inQuotes)
{
if (c == '"')
{
// Escaped quote?
if (i + 1 < line.Length && line[i + 1] == '"')
{
sb.Append('"');
i++; // skip next
}
else
{
inQuotes = false;
}
}
else
{
sb.Append(c);
}
}
else
{
if (c == '"')
{
inQuotes = true;
}
else if (c == delimiter)
{
result.Add(sb.ToString());
sb.Clear();
}
else
{
sb.Append(c);
}
}
}
result.Add(sb.ToString());
return result;
}
// ------- Row --------
public sealed class Row
{
private readonly CsvDynamicReader _reader;
private readonly string[] _values;
internal Row(CsvDynamicReader reader, string[] values)
{
_reader = reader;
_values = values ?? new string[0];
}
public bool Exists(string columnName)
{
int idx;
return _reader.TryGetIndex(columnName, out idx);
}
/// <summary>
/// Typed read with a default fallback. Works for string, int, double, bool, and enums.
/// Usage: var v = row.Read("Col", 0); var s = row.Read("Col","def"); var b = row.Read("Flag", false);
/// </summary>
public T Read<T>(string columnName, T defaultValue)
{
int idx;
if (!_reader.TryGetIndex(columnName, out idx))
return defaultValue;
var raw = (idx >= 0 && idx < _values.Length) ? _values[idx] : null;
return ConvertValue(raw, defaultValue);
}
// -------- Conversion helpers --------
private static T ConvertValue<T>(string raw, T defaultValue)
{
if (typeof(T) == typeof(string))
{
// For strings return raw as-is (trim optional)
object s = raw ?? (object)defaultValue ?? string.Empty;
return (T)s;
}
if (string.IsNullOrWhiteSpace(raw))
return defaultValue;
var trimmed = raw.Trim();
// int
if (typeof(T) == typeof(int))
{
int v;
if (int.TryParse(trimmed, NumberStyles.Integer, CultureInfo.InvariantCulture, out v))
return (T)(object)v;
return defaultValue;
}
// double
if (typeof(T) == typeof(double))
{
double v;
if (double.TryParse(trimmed, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out v))
return (T)(object)v;
return defaultValue;
}
// bool (accepts true/false, 1/0, yes/no, y/n)
if (typeof(T) == typeof(bool))
{
bool bv;
if (TryParseBool(trimmed, out bv))
return (T)(object)bv;
return defaultValue;
}
// Enums (case-insensitive; also accept underlying numeric)
var t = typeof(T);
if (t.IsEnum)
{
try
{
// Numeric?
var underlying = Enum.GetUnderlyingType(t);
object numericObj;
if (TryParseNumeric(trimmed, underlying, out numericObj))
{
var boxed = Enum.ToObject(t, numericObj);
return (T)boxed;
}
T parsed;
if (TryParseEnum(trimmed, out parsed))
return parsed;
}
catch { /* fall through to default */ }
return defaultValue;
}
// Fallback: try ChangeType
try
{
object any = System.Convert.ChangeType(trimmed, typeof(T), CultureInfo.InvariantCulture);
return (T)any;
}
catch
{
return defaultValue;
}
}
private static bool TryParseBool(string s, out bool value)
{
// Standard
if (bool.TryParse(s, out value)) return true;
// Common variants
switch (s.Trim().ToLowerInvariant())
{
case "1":
case "yes":
case "y":
case "true":
case "t":
value = true; return true;
case "0":
case "no":
case "n":
case "false":
case "f":
value = false; return true;
}
value = false; return false;
}
private static bool TryParseNumeric(string s, Type numericType, out object boxed)
{
if (numericType == typeof(byte)) { byte v; if (byte.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out v)) { boxed = v; return true; } }
if (numericType == typeof(sbyte)) { sbyte v; if (sbyte.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out v)) { boxed = v; return true; } }
if (numericType == typeof(short)) { short v; if (short.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out v)) { boxed = v; return true; } }
if (numericType == typeof(ushort)) { ushort v; if (ushort.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out v)) { boxed = v; return true; } }
if (numericType == typeof(int)) { int v; if (int.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out v)) { boxed = v; return true; } }
if (numericType == typeof(uint)) { uint v; if (uint.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out v)) { boxed = v; return true; } }
if (numericType == typeof(long)) { long v; if (long.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out v)) { boxed = v; return true; } }
if (numericType == typeof(ulong)) { ulong v; if (ulong.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out v)) { boxed = v; return true; } }
boxed = null;
return false;
}
private static bool TryParseEnum<T>(string s, out T value)
{
try
{
value = (T)Enum.Parse(typeof(T), s, ignoreCase: true);
return true;
}
catch
{
value = default(T);
return false;
}
}
}
}
}
|