-
Notifications
You must be signed in to change notification settings - Fork 2
/
InputReader.cs
87 lines (77 loc) · 3 KB
/
InputReader.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
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
namespace OCDataImporter
{
class InputReader
{
public int sepcount { get; private set; }
public char delimiter {get; private set;}
public ArrayList dataFileItems { get; private set;}
private String inputFilePath;
private const char tab = '\u0009';
/**
* Constructor
* @param name="inputFilePath" the full qualified path to the input file
*/
public InputReader(String inputFilePath)
{
this.inputFilePath = inputFilePath;
delimiter = ';';
dataFileItems = new ArrayList();
}
/// <summary>
/// Resets the input reader
/// </summary>
public void reset() {
dataFileItems.Clear();
}
/// <summary>
/// Reads the input file. The lines can be retrieved by the method
/// </summary>
public void Get_DataFileItems_FromInput()
{
// Find out how many data items are present per line and build array of data item names for using in data grid
dataFileItems.Clear();
int linelen = 0;
using (StreamReader sr = new StreamReader(inputFilePath))
{
String line;
while ((line = sr.ReadLine()) != null)
{
line = line.Trim(); // 1.1b
if (line.Length == 0) continue;
linelen = line.Length;
if (line.IndexOf(tab) > 0) delimiter = tab;
if (line.IndexOf(';') > 0) delimiter = ';';
for (int i = 0; i < line.Length; i++) if (line[i] == delimiter) sepcount++;
string[] spfirst = line.Split(delimiter);
foreach (string one in spfirst) dataFileItems.Add(one);
break;
}
}
}
/// <summary>
/// Creates a string with information on the input file with the delimiter and file path
/// </summary>
/// <param name="workdir">the work directory in which the input file is found</param>
/// <returns>a non-empty string</returns>
public String makeInformationString(String workdir)
{
String ret = "";
if (delimiter != tab)
{
ret += "\r\nData file is: " + inputFilePath + ", delimited by: " + delimiter + " Number of items per line: " + sepcount + "\r\n";
}
else
{
ret += "\r\nData file is: " + inputFilePath + ", delimited by: tab, Number of items per line: " + sepcount + "\r\n";
}
ret += "Started in directory " + workdir + ". This may take several minutes...\r\n";
return ret;
}
}
}