-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDelimitedTableFileReader.cs
61 lines (54 loc) · 1.49 KB
/
DelimitedTableFileReader.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Personal_Genome_Explorer
{
/** Reads a table from a text file with one row per line, and columns separated by a delimiting character. */
class DelimitedTableFileReader
{
StreamReader parentReader;
char commentPrefix;
char columnDelimiter;
/** A delegate that defines how each table row is processed. */
public delegate void ProcessRowDelegate(string[] columns);
/** Initialization constructor. */
public DelimitedTableFileReader(StreamReader inParentReader,char inCommentPrefix,char inColumnDelimiter)
{
parentReader = inParentReader;
commentPrefix = inCommentPrefix;
columnDelimiter = inColumnDelimiter;
}
/** Reads the table's rows. */
public void Read(ProcessRowDelegate processRowDelegate)
{
while (true)
{
string[] columns = ReadNextRow();
if (columns != null)
{
processRowDelegate(columns);
}
else
{
break;
}
};
}
/** Reads the next row of the table. */
string[] ReadNextRow()
{
while(!parentReader.EndOfStream)
{
string rowString = parentReader.ReadLine();
// Skip blank or comment lines.
if (rowString.Length > 0 && rowString[0] != commentPrefix)
{
return rowString.Split(columnDelimiter);
}
};
return null;
}
}
}