-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBFReader.cs
476 lines (400 loc) · 17.5 KB
/
DBFReader.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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
DBFReader
Class for reading the records assuming that the given
InputStream comtains DBF data.
This file is part of DotNetDBF packege.
original author (javadbf): anil@linuxense.com 2004/03/31
License: LGPL (http://www.gnu.org/copyleft/lesser.html)
ported to C# (DotNetDBF): Jay Tuley <jay+dotnetdbf@tuley.name> 6/28/2007
*/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Linq;
namespace LinqDBF
{
public class DBFReader : DBFBase, IDisposable
{
protected Stream _BaseStream { get; set; }
protected BinaryReader _BinaryRead { get; set; }
protected DBFHeader _Header { get; set; }
protected Lazy<Stream> _StreamMemo { get; set; }
// protected string streamMemoLoc { get; set; }
protected int[] _SelectedFields = new int[] { };
protected int[] _OrderedSelectedFields = new int[] { };
/* Class specific variables */
protected bool _IsClosed = true;
/**
Returns the number of records in the DBF.
*/
public int RecordCount => _Header.NumberOfRecords;
public Stream DataMemo
{
get { return _StreamMemo?.Value; }
set { _StreamMemo = new Lazy<Stream>(() => value); }
}
public Stream BaseStream => _BaseStream;
/**
Returns the asked Field. In case of an invalid index,
it returns a ArrayIndexOutofboundsException.
@param index. Index of the field. Index of the first field is zero.
*/
public DBFField[] Fields => _Header.FieldArray;
protected static Lazy<Stream> GetMemoStreamByPath(string path, FileShare fileShare)
{
return new Lazy<Stream>(() =>
{
var dbtPath = Path.ChangeExtension(path, "dbt");
if (File.Exists(dbtPath))
{
return new FileStream(dbtPath, FileMode.Open, FileAccess.Read, fileShare);
}
return null;
});
}
/**
Initializes a DBFReader object.
When this constructor returns the object
will have completed reading the hader (meta date) and
header information can be quried there on. And it will
be ready to return the first row.
@param InputStream where the data is read from.
*/
public DBFReader(string path)
: this(path, FileShare.ReadWrite)
{
}
public DBFReader(string path, FileShare fileShare)
: this(new FileStream(path, FileMode.Open, FileAccess.Read, fileShare), GetMemoStreamByPath(path, fileShare))
{
}
public DBFReader(Stream streamWithReadAccess, Lazy<Stream> streamForMemo)
{
try
{
_BaseStream = streamWithReadAccess;
_BinaryRead = new BinaryReader(_BaseStream);
_StreamMemo = streamForMemo;
_IsClosed = false;
_Header = new DBFHeader();
_Header.Read(_BinaryRead);
/* it might be required to leap to the start of records at times */
var dataStartIndex = _Header.HeaderLength
- (32 + (32 * _Header.FieldArray.Length))
- 1;
if (dataStartIndex > 0)
{
_BinaryRead.ReadBytes((dataStartIndex));
}
}
catch (IOException e)
{
throw new DBFException("Failed To Read DBF", e);
}
}
public void SetSelectFields(params string[] fields)
{
_SelectedFields =
fields.Select(
field => Array.FindIndex(_Header.FieldArray, headerField => headerField.Name.Equals(field, StringComparison.OrdinalIgnoreCase))
).ToArray();
_OrderedSelectedFields = _SelectedFields.OrderBy(field => field).ToArray();
}
public DBFField[] GetSelectFields()
{
return _SelectedFields.Length > 0
? _SelectedFields.Select(field => _Header.FieldArray[field]).ToArray()
: _Header.FieldArray;
}
#region IDisposable Members
/// <summary>Performs application-defined tasks associated with freeing, releasing,
/// or resetting unmanaged resources.</summary>
/// <filterpriority>2</filterpriority>
public virtual void Dispose()
{
Close();
}
#endregion
//#if NET35
// [Obsolete("Will need to open your own stream and use DataMemo property in later versions of .Net Framework")]
// public string DataMemoLoc
// {
// get { return streamMemoLoc; }
// set { streamMemoLoc = value; }
// }
//#endif
public override String ToString()
{
var sb =
new StringBuilder().Append(_Header.Year.ToString()).Append("/")
.Append(_Header.Month.ToString()).Append("/")
.AppendLine(_Header.Day.ToString())
.Append("Total records: ").AppendLine(_Header.NumberOfRecords.ToString())
.Append("Header length: ").Append(_Header.HeaderLength);
for (var i = 0; i < _Header.FieldArray.Length; i++)
{
sb.AppendLine(_Header.FieldArray[i].Name);
}
return sb.ToString();
}
public virtual void Close()
{
//#if NET35
// _loadedStream?.Close();
// streamMemo?.Close();
// _dataInputStream.Close();
//#else
if (_StreamMemo.IsValueCreated)
{
_StreamMemo.Value?.Dispose();
}
//_dataInputStream.Dispose();
//#endif
_BaseStream?.Dispose();
_IsClosed = true;
}
/**
Reads the returns the next row in the DBF stream.
@returns The next row as an Object array. Types of the elements
these arrays follow the convention mentioned in the class description.
*/
public (Object[], long) NextRecord()
{
return NextRecord(_SelectedFields, _OrderedSelectedFields);
}
internal (Object[], long) NextRecord(IEnumerable<int> selectIndexes, IList<int> sortedIndexes)
{
if (_IsClosed)
{
throw new DBFException("Source is not open");
}
var position = _BaseStream.Position;
var tOrderdSelectIndexes = sortedIndexes;
var recordObjects = new Object[_Header.FieldArray.Length];
try
{
var isDeleted = false;
do
{
if (isDeleted)
{
position += _BinaryRead.ReadBytes(_Header.RecordLength - 1).Length;
}
int t_byte = _BinaryRead.ReadByte();
if (t_byte == DBFFieldType.EndOfData)
{
return (null, position);
}
position++;
isDeleted = (t_byte == '*');
} while (isDeleted);
position = position - 1;
var j = 0;
var k = -1;
for (var i = 0; i < _Header.FieldArray.Length; i++)
{
if (tOrderdSelectIndexes.Count == j && j != 0
||
(tOrderdSelectIndexes.Count > j && tOrderdSelectIndexes[j] > i && tOrderdSelectIndexes[j] != k))
{
_BaseStream.Seek(_Header.FieldArray[i].FieldLength, SeekOrigin.Current);
continue;
}
if (tOrderdSelectIndexes.Count > j)
k = tOrderdSelectIndexes[j];
j++;
switch (_Header.FieldArray[i].DataType)
{
case NativeDbType.UnicodeChar:
var b_arrayU = new byte[
_Header.FieldArray[i].FieldLength
];
_BinaryRead.Read(b_arrayU, 0, b_arrayU.Length);
recordObjects[i] = _UCharEncoding.GetString(b_arrayU).TrimEnd('\0', ' ');
break;
case NativeDbType.Char:
var b_array = new byte[
_Header.FieldArray[i].FieldLength
];
_BinaryRead.Read(b_array, 0, b_array.Length);
recordObjects[i] = CharEncoding.GetString(b_array).TrimEnd();
break;
case NativeDbType.Date:
var t_byte_year = new byte[4];
_BinaryRead.Read(t_byte_year,
0,
t_byte_year.Length);
var t_byte_month = new byte[2];
_BinaryRead.Read(t_byte_month,
0,
t_byte_month.Length);
var t_byte_day = new byte[2];
_BinaryRead.Read(t_byte_day,
0,
t_byte_day.Length);
try
{
var tYear = CharEncoding.GetString(t_byte_year);
var tMonth = CharEncoding.GetString(t_byte_month);
var tDay = CharEncoding.GetString(t_byte_day);
int tIntYear, tIntMonth, tIntDay;
if (int.TryParse(tYear, out tIntYear) &&
int.TryParse(tMonth, out tIntMonth) &&
int.TryParse(tDay, out tIntDay))
{
recordObjects[i] = new DateTime(
tIntYear,
tIntMonth,
tIntDay);
}
else
{
recordObjects[i] = null;
}
}
catch (ArgumentOutOfRangeException)
{
/* this field may be empty or may have improper value set */
recordObjects[i] = null;
}
break;
case NativeDbType.Float:
try
{
var t_float = new byte[
_Header.FieldArray[i].FieldLength
];
_BinaryRead.Read(t_float, 0, t_float.Length);
var tParsed = CharEncoding.GetString(t_float);
var tLast = tParsed.Substring(tParsed.Length - 1);
if (tParsed.Length > 0
&& tLast != " "
&& tLast != NullSymbol)
{
recordObjects[i] = double.Parse(tParsed,
NumberStyles.Float | NumberStyles.AllowLeadingWhite,
NumberFormatInfo.InvariantInfo);
}
else
{
recordObjects[i] = null;
}
}
catch (FormatException e)
{
throw new DBFException("Failed to parse Float",
e);
}
break;
case NativeDbType.Numeric:
try
{
var t_numeric = new byte[
_Header.FieldArray[i].FieldLength
];
_BinaryRead.Read(t_numeric,
0,
t_numeric.Length);
var tParsed =
CharEncoding.GetString(t_numeric);
var tLast = tParsed.Substring(tParsed.Length - 1);
if (tParsed.Length > 0
&& tLast != " "
&& tLast != NullSymbol)
{
recordObjects[i] = Decimal.Parse(tParsed,
NumberStyles.Float | NumberStyles.AllowLeadingWhite,
NumberFormatInfo.InvariantInfo);
}
else
{
recordObjects[i] = null;
}
}
catch (FormatException e)
{
throw new DBFException(
"Failed to parse Number", e);
}
break;
case NativeDbType.Logical:
var t_logical = _BinaryRead.ReadByte();
//todo find out whats really valid
if (t_logical == 'Y' || t_logical == 't'
|| t_logical == 'T'
|| t_logical == 't')
{
recordObjects[i] = true;
}
else if (t_logical == DBFFieldType.UnknownByte)
{
recordObjects[i] = DBNull.Value;
}
else
{
recordObjects[i] = false;
}
break;
case NativeDbType.Memo:
//if (String.IsNullOrEmpty(streamMemoLoc) && streamMemo == null)
if (_StreamMemo == null)
throw new Exception("Memo Location Not Set");
var tRawMemoPointer = _BinaryRead.ReadBytes(_Header.FieldArray[i].FieldLength);
var tMemoPoiner = CharEncoding.GetString(tRawMemoPointer);
if (string.IsNullOrEmpty(tMemoPoiner))
{
recordObjects[i] = DBNull.Value;
break;
}
long tBlock;
if (!long.TryParse(tMemoPoiner, out tBlock))
{
//Because Memo files can vary and are often the least importat data,
//we will return null when it doesn't match our format.
recordObjects[i] = DBNull.Value;
break;
}
//recordObjects[i] = new MemoValue(tBlock, this, streamMemoLoc, GetLazyStreamFromLocation());
recordObjects[i] = new MemoValue(tBlock, this, _StreamMemo);
break;
default:
_BinaryRead.ReadBytes(_Header.FieldArray[i].FieldLength);
recordObjects[i] = DBNull.Value;
break;
}
}
}
catch (EndOfStreamException)
{
return (null, position);
}
catch (IOException e)
{
throw new DBFException("Problem Reading File", e);
}
return (selectIndexes.Any() ? selectIndexes.Select(it => recordObjects[it]).ToArray() : recordObjects, position);
}
public IEnumerable<DBFRecord> AsEnumerable()
{
return AsEnumerable(null);
}
public IEnumerable<DBFRecord> AsEnumerable(Func<DBFRecord, bool> filter)
{
var lookupFieldName = GetSelectFields()
.Select((f, i) => new { f.Name, Index = i })
.ToDictionary(f => f.Name, f => f.Index);
var (objects, position) = NextRecord();
while (objects != null)
{
var record = new DBFRecord(lookupFieldName, objects, position);
if (record != null || filter(record) == true)
{
yield return record;
}
(objects, position) = NextRecord();
}
}
}
}