-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTSVFileStream.cpp
More file actions
164 lines (142 loc) · 3.49 KB
/
TSVFileStream.cpp
File metadata and controls
164 lines (142 loc) · 3.49 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
#include "TSVFileStream.h"
#include "Helper.h"
TSVFileStream::TSVFileStream(QString filename, char separator, char comment)
: filename_(filename)
, separator_(separator)
, comment_(comment)
, double_comment_(2, comment)
{
reset();
}
void TSVFileStream::reset()
{
//init
line_ = 0;
comments_.clear();
header_.clear();
//set file
if (filename_.isEmpty())
{
file_ = QSharedPointer<VersatileFile>(new VersatileFile(filename_, stdin));
}
else
{
file_ = QSharedPointer<VersatileFile>(new VersatileFile(filename_));
}
//open
file_->open(QFile::ReadOnly | QFile::Text);
//read comments and headers
next_line_ = double_comment_;
while(next_line_.startsWith(comment_))
{
if (next_line_.startsWith(double_comment_))
{
if (next_line_.trimmed()!=double_comment_ && header_.isEmpty())
{
comments_ << next_line_;
}
}
else if (next_line_.startsWith(comment_))
{
header_ = next_line_.mid(1).split(separator_);
}
next_line_ = file_->readLine(true);
++line_;
}
//no first line
if (file_->atEnd() && next_line_.isEmpty()) next_line_ = QByteArray();
//determine number of columns if no header is present
if (header_.isEmpty())
{
for(int i=0; i<next_line_.split(separator_).count(); ++i)
{
header_.append("");
}
}
}
TSVFileStream::~TSVFileStream()
{
}
QByteArrayList TSVFileStream::readLine()
{
//handle first content line
if (!next_line_.isNull())
{
if (next_line_.isEmpty())
{
next_line_ = QByteArray();
return QByteArrayList();
}
QByteArrayList parts = next_line_.split(separator_);
if (parts.count()!=columns()) THROW(FileParseException, "Expected " + QString::number(columns()) + " columns, but got " + QString::number(parts.count()) + " columns in line 1: " + next_line_);
next_line_ = QByteArray();
return parts;
}
//handle second to last content line
QByteArray line = file_->readLine(true);
++line_;
if (line.isEmpty()) return QByteArrayList();
if (line.startsWith(double_comment_)) //comments between lines are ignored
{
return readLine();
}
QByteArrayList parts = line.split(separator_);
if (parts.count()!=columns()) THROW(FileParseException, "Expected " + QString::number(columns()) + " columns, but got " + QString::number(parts.count()) + " columns in line " + QString::number(line_) + ": " + line);
return parts;
}
int TSVFileStream::colIndex(QByteArray name, bool error_when_missing)
{
//find matching indices
QVector<int> hits;
for (int i=0; i<columns(); ++i)
{
if (header_[i]==name)
{
hits.append(i);
}
}
//error handling
if (hits.count()!=1)
{
if (error_when_missing)
{
if (hits.count()==0)
{
THROW(CommandLineParsingException, "Could not find column name '" + name + "' in column headers of '"+filename_+"!");
}
if (hits.count()>1)
{
THROW(CommandLineParsingException, "Found column name '" + name + "' more than once in column headers of '"+filename_+"!");
}
}
else
{
return -1;
}
}
return hits[0];
}
QVector<int> TSVFileStream::checkColumns(const QByteArrayList& col_names, bool numeric)
{
QVector<int> col_indices;
if (numeric)
{
foreach(const QByteArray& part, col_names)
{
int col = Helper::toInt(part, "column number");
if (col<1 || col>columns())
{
THROW(CommandLineParsingException, "1-based column number '" + part + "' out of range (max is " + QString::number(columns()) + ")!");
}
col_indices.append(col-1);
}
}
else
{
foreach(const QByteArray& part, col_names)
{
col_indices.append(colIndex(part, true));
}
}
return col_indices;
}