-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericParser.cs
More file actions
2420 lines (2168 loc) · 103 KB
/
GenericParser.cs
File metadata and controls
2420 lines (2168 loc) · 103 KB
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// GenericParsing
// Copyright © 2010 Andrew Rissing
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#region Using Directives
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
#endregion Using Directives
namespace GenericParsing
{
/// <summary>
/// The <see cref="GenericParser"/> class is designed to be a flexible and efficient manner
/// of parsing various flat files formats.
/// </summary>
/// <threadsafety static="false" instance="false"/>
public class GenericParser : IDisposable
{
#region Constants
#region Default Values
/// <summary>
/// Defines the default max buffer size (4096).
/// </summary>
public const int DefaultMaxBufferSize = 4096;
/// <summary>
/// Defines the max rows value (0 = no limit).
/// </summary>
public const int DefaultMaxRows = 0;
/// <summary>
/// Defines the number of skip starting data rows (0).
/// </summary>
public const int DefaultSkipStartingDataRows = 0;
/// <summary>
/// Defines the number of expected columns (0 = no limit).
/// </summary>
public const int DefaultExpectedColumnCount = 0;
/// <summary>
/// Defines the default first row has a header (false).
/// </summary>
public const bool DefaultFirstRowHasHeader = false;
/// <summary>
/// Defines the default value for trim results (false).
/// </summary>
public const bool DefaultTrimResults = false;
/// <summary>
/// Defines the default value for stripping control characters (false).
/// </summary>
public const bool DefaulStripControlCharacters = false;
/// <summary>
/// Defines the default value for skipping empty rows (true).
/// </summary>
public const bool DefaulSkipEmptyRows = true;
/// <summary>
/// Defines the default value for text field type (Delimited).
/// </summary>
public const FieldType DefaultTextFieldType = FieldType.Delimited;
/// <summary>
/// Defines the default for first row sets the expected column count (false).
/// </summary>
public const bool DefaultFirstRowSetsExpectedColumnCount = false;
/// <summary>
/// Defines the default column delimiter (',').
/// </summary>
public const char DefaultColumnDelimiter = ',';
/// <summary>
/// Defines the default text qualifier ('\"').
/// </summary>
public const char DefaultTextQualifier = '\"';
/// <summary>
/// Defines the default comment row character ('#').
/// </summary>
public const char DefaultCommentCharacter = '#';
#endregion Default Values
/// <summary>
/// Indicates the current type of row being processed.
/// </summary>
private enum RowType
{
/// <summary>
/// The row type is unknown and needs to be determined.
/// </summary>
Unknown = 0,
/// <summary>
/// The row type is a comment row and can be ignored.
/// </summary>
CommentRow = 1,
/// <summary>
/// The row type is a header row to name the columns.
/// </summary>
HeaderRow = 2,
/// <summary>
/// The row type is a skipped row that is not intended to be extracted.
/// </summary>
SkippedRow = 3,
/// <summary>
/// The row type is data row that is intended to be extracted.
/// </summary>
DataRow = 4
}
#region XmlConfig Constants
private const string XML_ROOT_NODE = "GenericParser";
private const string XML_COLUMN_WIDTH = "ColumnWidth";
private const string XML_COLUMN_WIDTHS = "ColumnWidths";
private const string XML_MAX_BUFFER_SIZE = "MaxBufferSize";
private const string XML_MAX_ROWS = "MaxRows";
private const string XML_SKIP_STARTING_DATA_ROWS = "SkipStartingDataRows";
private const string XML_EXPECTED_COLUMN_COUNT = "ExpectedColumnCount";
private const string XML_FIRST_ROW_HAS_HEADER = "FirstRowHasHeader";
private const string XML_TRIM_RESULTS = "TrimResults";
private const string XML_STRIP_CONTROL_CHARS = "StripControlChars";
private const string XML_SKIP_EMPTY_ROWS = "SkipEmptyRows";
private const string XML_TEXT_FIELD_TYPE = "TextFieldType";
private const string XML_FIRST_ROW_SETS_EXPECTED_COLUMN_COUNT = "FirstRowSetsExpectedColumnCount";
private const string XML_COLUMN_DELIMITER = "ColumnDelimiter";
private const string XML_TEXT_QUALIFIER = "TextQualifier";
private const string XML_ESCAPE_CHARACTER = "EscapeCharacter";
private const string XML_COMMENT_CHARACTER = "CommentCharacter";
private const string XML_SAFE_STRING_DELIMITER = ",";
#endregion XmlConfig Constants
#endregion Constants
#region Static Code
/// <summary>
/// Clones the provided array in a type-friendly way.
/// </summary>
/// <typeparam name="T">The type of the array to clone.</typeparam>
/// <param name="array">The array to clone.</param>
/// <returns>The cloned version of the array.</returns>
private static T[] CloneArray<T>(T[] array)
{
T[] clone;
if (array != null)
{
clone = new T[array.Length];
for (int i = 0; i < array.Length; ++i)
clone[i] = array[i];
}
else
{
clone = null;
}
return clone;
}
#endregion Static Code
#region Constructors
/// <summary>
/// Constructs an instance of a <see cref="GenericParser"/> with the default settings.
/// </summary>
/// <remarks>
/// When using this constructor, the datasource must be set prior to using the parser
/// (using <see cref="GenericParser.SetDataSource(string)"/>), otherwise an exception will be thrown.
/// </remarks>
public GenericParser()
{
this.m_ParserState = ParserState.NoDataSource;
this.m_txtReader = null;
this.m_blnDisposed = false;
this.m_objLock = new object();
this._InitializeConfigurationVariables();
}
/// <summary>
/// Constructs an instance of a <see cref="GenericParser"/> and sets the initial datasource
/// as the file referenced by the string passed in.
/// </summary>
/// <param name="strFileName">The file name to set as the initial datasource.</param>
/// <exception cref="ArgumentNullException">Supplying <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">Supplying a filename to a file that does not exist.</exception>
/// <exception cref="InvalidOperationException">Attempting to modify the configuration, while parsing.</exception>
public GenericParser(string strFileName)
: this()
{
this.SetDataSource(strFileName);
}
/// <summary>
/// Constructs an instance of a <see cref="GenericParser"/> and sets the initial datasource
/// as the file referenced by the string passed in with the provided encoding.
/// </summary>
/// <param name="strFileName">The file name to set as the initial datasource.</param>
/// <param name="encoding">The <see cref="Encoding"/> of the file being referenced.</param>
/// <exception cref="ArgumentNullException">Supplying <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">Supplying a filename to a file that does not exist.</exception>
/// <exception cref="InvalidOperationException">Attempting to modify the configuration, while parsing.</exception>
public GenericParser(string strFileName, Encoding encoding)
: this()
{
this.SetDataSource(strFileName, encoding);
}
/// <summary>
/// Constructs an instance of a <see cref="GenericParser"/> and sets the initial datasource
/// as the <see cref="TextReader"/> passed in.
/// </summary>
/// <param name="txtReader">The <see cref="TextReader"/> containing the data to be parsed.</param>
/// <exception cref="ArgumentNullException">Supplying <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">Attempting to modify the configuration, while parsing.</exception>
public GenericParser(TextReader txtReader)
: this()
{
this.SetDataSource(txtReader);
}
#endregion Constructors
#region Public Code
/// <summary>
/// Gets whether or not the instance has been disposed of.
/// </summary>
/// <value>
/// <para>
/// <see langword="true"/> - Indicates the instance has be disposed of.
/// </para>
/// <para>
/// <see langword="false"/> - Indicates the instance has not be disposed of.
/// </para>
/// </value>
public bool IsDisposed
{
get
{
return this.m_blnDisposed;
}
}
/// <summary>
/// Gets or sets an integer array indicating the number of characters needed for each column.
/// </summary>
/// <value>An int[] containing the number of spaces for each column.</value>
/// <remarks>
/// <para>
/// If parsing has started, this value cannot be updated.
/// </para>
/// <para>
/// By setting this property, the <see cref="TextFieldType"/> and <see cref="ExpectedColumnCount"/> are automatically updated.
/// </para>
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">Passing in an empty array or an
/// array of values that have a number less than one.</exception>
/// <exception cref="InvalidOperationException">Attempting to modify the configuration, while parsing.</exception>
public int[] ColumnWidths
{
get
{
return GenericParser.CloneArray(this.m_iaColumnWidths);
}
set
{
if (this.m_ParserState == ParserState.Parsing)
throw new InvalidOperationException("Parsing has already begun, close the existing parse first.");
this.m_iaColumnWidths = GenericParser.CloneArray(value);
if (value == null)
{
this.m_textFieldType = FieldType.Delimited;
this.m_intExpectedColumnCount = 0;
}
else
{
if (this.m_iaColumnWidths.Length < 1)
throw new ArgumentOutOfRangeException("value", "ColumnWidths cannot be an empty array.");
// Make sure all of the ColumnWidths are valid.
for (int intColumnIndex = 0; intColumnIndex < this.m_iaColumnWidths.Length; ++intColumnIndex)
{
if (this.m_iaColumnWidths[intColumnIndex] < 1)
throw new ArgumentOutOfRangeException("value", "ColumnWidths cannot contain a number less than one.");
}
this.m_textFieldType = FieldType.FixedWidth;
this.m_intExpectedColumnCount = this.m_iaColumnWidths.Length;
}
}
}
/// <summary>
/// Gets or sets the maximum size of the internal buffer used to cache the data.
/// </summary>
/// <value>The maximum size of the internal buffer to cache data from the datasource.</value>
/// <remarks>
/// <para>
/// Maintaining the smallest number possible here improves memory usage, but
/// trades it off for higher CPU usage. The <see cref="MaxBufferSize"/> must
/// be at least the size of one column of data, plus the Max(column delimiter
/// width, row delimiter width).
/// </para>
/// <para>
/// Default: 4096
/// </para>
/// <para>
/// If parsing has started, this value cannot be updated.
/// </para>
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">Setting the value to something less than one.</exception>
/// <exception cref="InvalidOperationException">Attempting to modify the configuration, while parsing.</exception>
public int MaxBufferSize
{
get
{
return this.m_intMaxBufferSize;
}
set
{
if (this.m_ParserState == ParserState.Parsing)
throw new InvalidOperationException("Parsing has already begun, close the existing parse first.");
if (value > 0)
this.m_intMaxBufferSize = value;
else
throw new ArgumentOutOfRangeException("value", value, "The MaxBufferSize must be greater than 0.");
}
}
/// <summary>
/// Gets or sets the maximum number of rows to parse.
/// </summary>
/// <value>The maximum number of rows to parse.</value>
/// <remarks>
/// <para>
/// Setting the value to zero will cause all of the rows to be returned.
/// </para>
/// <para>
/// Default: 0
/// </para>
/// <para>
/// If parsing has started, this value cannot be updated.
/// </para>
/// </remarks>
/// <exception cref="InvalidOperationException">Attempting to modify the configuration, while parsing.</exception>
public int MaxRows
{
get
{
return this.m_intMaxRows;
}
set
{
if (this.m_ParserState == ParserState.Parsing)
throw new InvalidOperationException("Parsing has already begun, close the existing parse first.");
this.m_intMaxRows = value;
if (this.m_intMaxRows < 0)
this.m_intMaxRows = 0;
}
}
/// <summary>
/// Gets or sets the number of rows of data to ignore at the start of the file.
/// </summary>
/// <value>The number of data rows to initially skip in the datasource.</value>
/// <remarks>
/// <para>
/// The header row (if present) and comment rows will not be taken into account
/// when determining the number of rows to skip. Setting the value to zero will
/// cause no rows to be ignored.
/// </para>
/// <para>
/// Default: 0
/// </para>
/// <para>
/// If parsing has started, this value cannot be updated.
/// </para>
/// </remarks>
/// <exception cref="InvalidOperationException">Attempting to modify the configuration, while parsing.</exception>
public int SkipStartingDataRows
{
get
{
return this.m_intSkipStartingDataRows;
}
set
{
if (this.m_ParserState == ParserState.Parsing)
throw new InvalidOperationException("Parsing has already begun, close the existing parse first.");
this.m_intSkipStartingDataRows = value;
if (this.m_intSkipStartingDataRows < 0)
this.m_intSkipStartingDataRows = 0;
}
}
/// <summary>
/// Gets or sets the number of rows of data that have currently been parsed.
/// </summary>
/// <value>The number of rows of data that have been parsed.</value>
/// <remarks>The DataRowNumber property is read-only.</remarks>
public int DataRowNumber
{
get
{
return this.m_intDataRowNumber;
}
}
/// <summary>
/// Gets or sets how many rows in the file have been parsed.
/// </summary>
/// <value>The number of rows in the file that have been parsed.</value>
/// <remarks>The <see cref="FileRowNumber"/> property is read-only and includes all
/// rows possible (header, comment, and data).</remarks>
public int FileRowNumber
{
get
{
return this.m_intFileRowNumber;
}
}
/// <summary>
/// Gets or sets the expected number of columns to find in the data. If
/// the number of columns differs, an exception will be thrown.
/// </summary>
/// <value>The number of columns expected per row of data.</value>
/// <remarks>
/// <para>
/// Setting the value to zero will cause the <see cref="GenericParser"/> to ignore
/// the column count in case the number changes per row.
/// </para>
/// <para>
/// Default: 0
/// </para>
/// <para>
/// By setting this property, the <see cref="TextFieldType"/> and <see cref="ColumnWidths"/>
/// are automatically updated.
/// </para>
/// <para>
/// If parsing has started, this value cannot be updated.
/// </para>
/// </remarks>
/// <exception cref="InvalidOperationException">Attempting to modify the configuration, while parsing.</exception>
public int ExpectedColumnCount
{
get
{
return this.m_intExpectedColumnCount;
}
set
{
if (this.m_ParserState == ParserState.Parsing)
throw new InvalidOperationException("Parsing has already begun, close the existing parse first.");
this.m_intExpectedColumnCount = value;
if (this.m_intExpectedColumnCount < 0)
this.m_intExpectedColumnCount = 0;
// Make sure the ExpectedColumnCount matches the column width's
// supplied.
if ((this.m_textFieldType == FieldType.FixedWidth)
&& (this.m_iaColumnWidths != null)
&& (this.m_iaColumnWidths.Length != this.m_intExpectedColumnCount))
{
// Null it out to force the proper column width's to be supplied.
this.m_iaColumnWidths = null;
this.m_textFieldType = FieldType.Delimited;
}
}
}
/// <summary>
/// Gets or sets whether or not the first row of data in the file contains
/// the header information.
/// </summary>
/// <value>
/// <para>
/// <see langword="true"/> - Header found on first 'data row'.
/// </para>
/// <para>
/// <see langword="false"/> - Header row does not exist.
/// </para>
/// </value>
/// <remarks>
/// <para>
/// Default: <see langword="false"/>
/// </para>
/// <para>
/// If parsing has started, this value cannot be updated.
/// </para>
/// </remarks>
/// <exception cref="InvalidOperationException">Attempting to modify the configuration, while parsing.</exception>
public bool FirstRowHasHeader
{
get
{
return this.m_blnFirstRowHasHeader;
}
set
{
if (this.m_ParserState == ParserState.Parsing)
throw new InvalidOperationException("Parsing has already begun, close the existing parse first.");
this.m_blnFirstRowHasHeader = value;
}
}
/// <summary>
/// Gets or sets whether or not to trim the values for each column.
/// </summary>
/// <value>
/// <para>
/// <see langword="true"/> - Indicates to trim the resulting strings.
/// </para>
/// <para>
/// <see langword="false"/> - Indicates to not trim the resulting strings.
/// </para>
/// </value>
/// <remarks>
/// <para>
/// Trimming only occurs on the strings if they are not text qualified.
/// So by placing values in quotes, it preserves all whitespace within
/// quotes.
/// </para>
/// <para>
/// Default: <see langword="false"/>
/// </para>
/// <para>
/// If parsing has started, this value cannot be updated.
/// </para>
/// </remarks>
/// <exception cref="InvalidOperationException">Attempting to modify the configuration, while parsing.</exception>
public bool TrimResults
{
get
{
return this.m_blnTrimResults;
}
set
{
if (this.m_ParserState == ParserState.Parsing)
throw new InvalidOperationException("Parsing has already begun, close the existing parse first.");
this.m_blnTrimResults = value;
}
}
/// <summary>
/// Gets or sets whether or not to strip control characters out of the input.
/// </summary>
/// <value>
/// <para>
/// <see langword="true"/> - Indicates to remove control characters from the input.
/// </para>
/// <para>
/// <see langword="false"/> - Indicates to leave control characters in the input.
/// </para>
/// </value>
/// <remarks>
/// <para>
/// Setting this to <see langword="true"/> can cause a performance boost.
/// </para>
/// <para>
/// Default: <see langword="false"/>
/// </para>
/// <para>
/// If parsing has started, this value cannot be updated.
/// </para>
/// </remarks>
/// <exception cref="InvalidOperationException">Attempting to modify the configuration, while parsing.</exception>
public bool StripControlChars
{
get
{
return this.m_blnStripControlChars;
}
set
{
if (this.m_ParserState == ParserState.Parsing)
throw new InvalidOperationException("Parsing has already begun, close the existing parse first.");
this.m_blnStripControlChars = value;
}
}
/// <summary>
/// Gets or sets whether or not to skip empty rows in the input.
/// </summary>
/// <value>
/// <para>
/// <see langword="true"/> - Indicates to skip empty rows in the input.
/// </para>
/// <para>
/// <see langword="false"/> - Indicates to include empty rows in the input.
/// </para>
/// </value>
/// <remarks>
/// <para>
/// Default: <see langword="true"/>
/// </para>
/// <para>
/// If parsing has started, this value cannot be updated.
/// </para>
/// </remarks>
/// <exception cref="InvalidOperationException">Attempting to modify the configuration, while parsing.</exception>
public bool SkipEmptyRows
{
get
{
return this.m_blnSkipEmptyRows;
}
set
{
if (this.m_ParserState == ParserState.Parsing)
throw new InvalidOperationException("Parsing has already begun, close the existing parse first.");
this.m_blnSkipEmptyRows = value;
}
}
/// <summary>
/// Gets whether or not the current row is an empty row.
/// </summary>
public bool IsCurrentRowEmpty
{
get
{
return this.m_blnIsCurrentRowEmpty;
}
}
/// <summary>
/// Gets or sets the <see cref="FieldType"/> of the data encoded in the rows.
/// </summary>
/// <remarks>
/// <para>
/// By setting <see cref="ColumnWidths"/>, this property is automatically set.
/// </para>
/// <para>
/// Default: <see cref="FieldType.Delimited"/>
/// </para>
/// <para>
/// If parsing has started, this value cannot be updated.
/// </para>
/// </remarks>
/// <exception cref="InvalidOperationException">Attempting to modify the configuration, while parsing.</exception>
public FieldType TextFieldType
{
get
{
return this.m_textFieldType;
}
set
{
if (this.m_ParserState == ParserState.Parsing)
throw new InvalidOperationException("Parsing has already begun, close the existing parse first.");
this.m_textFieldType = value;
if (this.m_textFieldType == FieldType.FixedWidth)
{
this.m_chColumnDelimiter = null;
this.m_blnFirstRowSetsExpectedColumnCount = false;
}
else
{
this.m_iaColumnWidths = null;
}
}
}
/// <summary>
/// Gets or sets the number of columns in the header/first data row determines
/// the expected number of columns in the data.
/// </summary>
/// <value>
/// <para>
/// <see langword="true"/> - Indicates the data's column count should match the header/first data row's column count.
/// </para>
/// <para>
/// <see langword="false"/> - Indicates the data's column count does not necessarily match the header/first data row's column count.
/// </para>
/// </value>
/// <remarks>
/// <para>
/// If set to <see langword="true"/>, <see cref="FieldType"/> will automatically be set to <see langword="false"/>.
/// </para>
/// <para>
/// Default: <see langword="false"/>
/// </para>
/// <para>
/// If parsing has started, this value cannot be updated.
/// </para>
/// </remarks>
/// <exception cref="InvalidOperationException">Attempting to modify the configuration, while parsing.</exception>
public bool FirstRowSetsExpectedColumnCount
{
get
{
return this.m_blnFirstRowSetsExpectedColumnCount;
}
set
{
if (this.m_ParserState == ParserState.Parsing)
throw new InvalidOperationException("Parsing has already begun, close the existing parse first.");
this.m_blnFirstRowSetsExpectedColumnCount = value;
// If set to true, unset fixed width as it makes no sense.
if (value)
this.TextFieldType = FieldType.Delimited;
}
}
/// <summary>
/// Gets the <see cref="ParserState"/> value indicating the current
/// internal state of the parser.
/// </summary>
/// <value>The <see cref="State"/> property is read-only and is used to return
/// information about the internal state of the parser.</value>
public ParserState State
{
get
{
return this.m_ParserState;
}
}
/// <summary>
/// Gets or sets the character used to match the end of a column of data.
/// </summary>
/// <value>Contains the character used to delimit a column.</value>
/// <remarks>
/// <para>
/// By setting this property, the <see cref="TextFieldType"/> is automatically
/// updated. This is only meaningful when performing delimited parsing.
/// </para>
/// <para>
/// Default: ','
/// </para>
/// <para>
/// If parsing has started, this value cannot be updated.
/// </para>
/// </remarks>
/// <exception cref="InvalidOperationException">Attempting to modify the configuration, while parsing.</exception>
public char? ColumnDelimiter
{
get
{
return m_chColumnDelimiter;
}
set
{
if (this.m_ParserState == ParserState.Parsing)
throw new InvalidOperationException("Parsing has already begun, close the existing parse first.");
else
{
this.m_chColumnDelimiter = value;
this.m_textFieldType = (value == null) ? FieldType.FixedWidth : FieldType.Delimited;
}
}
}
/// <summary>
/// Gets or sets the character that is used to enclose a string that would otherwise
/// be potentially trimmed (Ex. " this ").
/// </summary>
/// <value>
/// The character used to enclose a string, so that row/column delimiters are ignored
/// and whitespace is preserved.
/// </value>
/// <remarks>
/// <para>
/// The Text Qualifiers must be present at the beginning and end of the column to
/// have them properly removed from the ends of the string. Furthermore, for a
/// string that has been enclosed with the text qualifier, if the text qualifier is
/// doubled up inside the string, the characters will be treated as an escape for
/// the literal character of the text qualifier (ie. "This""Test" will translate
/// with only one double quote inside the string).
/// </para>
/// <para>
/// Setting this to <see langword="null"/> can cause a performance boost, if none of the values are
/// expected to require escaping.
/// </para>
/// <para>
/// Default: '\"'
/// </para>
/// <para>
/// If parsing has started, this value cannot be updated.
/// </para>
/// </remarks>
/// <exception cref="InvalidOperationException">Attempting to modify the configuration, while parsing.</exception>
public char? TextQualifier
{
get
{
return this.m_chTextQualifier;
}
set
{
if (this.m_ParserState == ParserState.Parsing)
throw new InvalidOperationException("Parsing has already begun, close the existing parse first.");
this.m_chTextQualifier = value;
}
}
/// <summary>
/// Gets or sets the character that is used to escape a character (Ex. "\"This\"").
/// </summary>
/// <value>The character used to escape row/column delimiters and the text qualifier.</value>
/// <remarks>
/// <para>
/// Upon parsing the file, the escaped characters will be stripped out, leaving
/// the desired character in place. To produce the escaped character, use the
/// escaped character twice (Ex. \\). Text qualifiers are already assumed to be
/// escaped if used twice.
/// </para>
/// <para>
/// Setting this to <see langword="null"/> can cause a performance boost, if none of the values are
/// expected to require escaping.
/// </para>
/// <para>
/// Default: null
/// </para>
/// <para>
/// If parsing has started, this value cannot be updated.
/// </para>
/// </remarks>
/// <exception cref="InvalidOperationException">Attempting to modify the configuration, while parsing.</exception>
public char? EscapeCharacter
{
get
{
return this.m_chEscapeCharacter;
}
set
{
if (this.m_ParserState == ParserState.Parsing)
throw new InvalidOperationException("Parsing has already begun, close the existing parse first.");
this.m_chEscapeCharacter = value;
}
}
/// <summary>
/// Gets or sets the character that is used to mark the beginning of a row that contains
/// purely comments and that should not be parsed.
/// </summary>
/// <value>
/// The character used to indicate the current row is to be ignored as a comment.
/// </value>
/// <remarks>
/// <para>
/// Default: '#'
/// </para>
/// <para>
/// If parsing has started, this value cannot be updated.
/// </para>
/// </remarks>
/// <exception cref="InvalidOperationException">Attempting to modify the configuration, while parsing.</exception>
public char? CommentCharacter
{
get
{
return this.m_chCommentCharacter;
}
set
{
if (this.m_ParserState == ParserState.Parsing)
throw new InvalidOperationException("Parsing has already begun, close the existing parse first.");
this.m_chCommentCharacter = value;
}
}
/// <summary>
/// Gets the data found in the current row of data by the column index.
/// </summary>
/// <value>The value of the column at the given index.</value>
/// <param name="intColumnIndex">The index of the column to retreive.</param>
/// <remarks>
/// If the column is outside the bounds of the columns found or the column
/// does not possess a name, it will return <see langword="null"/>.
/// </remarks>
public string this[int intColumnIndex]
{
get
{
if ((intColumnIndex > -1) && (intColumnIndex < this.m_lstData.Count))
return this.m_lstData[intColumnIndex];
else
return null;
}
}
/// <summary>
/// Gets the data found in the current row of data by the column name.
/// </summary>
/// <value>The value of the column with the given column name.</value>
/// <param name="strColumnName">The name of the column to retreive.</param>
/// <remarks>
/// If the header has yet to be parsed (or no header exists), the property will
/// return <see langword="null"/>.
/// </remarks>
public string this[string strColumnName]
{
get
{
return this[this._GetColumnIndex(strColumnName)];
}
}
/// <summary>
/// Gets the number of columns found in the current row.
/// </summary>
/// <value>The number of data columns found in the current row.</value>
/// <remarks>The <see cref="ColumnCount"/> property is read-only. The number of columns per row can differ, if allowed.</remarks>
public int ColumnCount
{
get
{
return this.m_lstData.Count;
}
}
/// <summary>
/// Gets the largest column count found thusfar from parsing.
/// </summary>
/// <value>The largest column count found thusfar from parsing.</value>
/// <remarks>The <see cref="LargestColumnCount"/> property is read-only. The LargestColumnCount can increase due to rows with additional data.</remarks>
public int LargestColumnCount
{
get
{
return this.m_lstColumnNames.Count;
}
}
/// <summary>
/// Sets the file as the datasource.
/// </summary>
/// <remarks>
/// If the parser is currently parsing a file, all data associated
/// with the previous file is lost and the parser is reset back to
/// its initial values.
/// </remarks>
/// <param name="strFileName">The <see cref="string"/> containing the name of the file
/// to set as the data source.</param>
/// <example>
/// <code lang="C#" escaped="true">
/// using (GenericParser p = new GenericParser())
/// p.SetDataSource(@"C:\MyData.txt");
/// </code>
/// </example>
/// <exception cref="ArgumentNullException">Supplying <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">Supplying a filename to a file that does not exist.</exception>
/// <exception cref="InvalidOperationException">Attempting to modify the configuration, while parsing.</exception>
public void SetDataSource(string strFileName)
{
this.SetDataSource(strFileName, Encoding.UTF8);
}
/// <summary>
/// Sets the file as the datasource using the provided encoding.
/// </summary>
/// <remarks>
/// If the parser is currently parsing a file, all data associated
/// with the previous file is lost and the parser is reset back to
/// its initial values.
/// </remarks>
/// <param name="strFileName">The <see cref="string"/> containing the name of the file
/// to set as the data source.</param>
/// <param name="encoding">The <see cref="Encoding"/> of the file being referenced.</param>
/// <example>
/// <code lang="C#" escaped="true">
/// using (GenericParser p = new GenericParser())
/// p.SetDataSource(@"C:\MyData.txt", Encoding.ASCII);
/// </code>
/// </example>
/// <exception cref="ArgumentNullException">Supplying <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">Supplying a filename to a file that does not exist.</exception>
/// <exception cref="InvalidOperationException">Attempting to modify the configuration, while parsing.</exception>
public void SetDataSource(string strFileName, Encoding encoding)
{
if (this.m_ParserState == ParserState.Parsing)
throw new InvalidOperationException("Parsing has already begun, close the existing parse first.");
if (strFileName == null)
throw new ArgumentNullException("strFileName", "The filename cannot be a null value.");
if (!File.Exists(strFileName))
throw new ArgumentException(string.Format("File, {0}, does not exist.", strFileName), "strFileName");
if (encoding == null)
throw new ArgumentNullException("encoding", "The encoding cannot be a null value.");
// Clean up the existing text reader if it exists.
if (this.m_txtReader != null)
this.m_txtReader.Dispose();