-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTsvFile.cpp
More file actions
228 lines (196 loc) · 4.64 KB
/
TsvFile.cpp
File metadata and controls
228 lines (196 loc) · 4.64 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
#include "TsvFile.h"
#include "Exceptions.h"
#include "Helper.h"
#include "VersatileTextStream.h"
TsvFile::TsvFile()
{
}
void TsvFile::addComment(const QString& comment)
{
if (!comment.startsWith("##"))
{
THROW(ProgrammingException, "TsvFile: comment must start with '##', but does not: " + comment);
}
if (comment.contains("\n"))
{
THROW(ProgrammingException, "TsvFile: comment must not contain newline, but does: " + comment);
}
comments_ << comment;
}
void TsvFile::setComments(const QStringList& comments)
{
comments_.clear();
foreach(const QString& comment, comments)
{
addComment(comment);
}
}
void TsvFile::addHeader(const QString& header)
{
if (header.isEmpty())
{
THROW(ProgrammingException, "TsvFile: header entry must not be empty, but is!");
}
if (headers_.isEmpty() && header.startsWith('#'))
{
THROW(ProgrammingException, "TsvFile: first header entry must not start with '#', but does: " + header);
}
if (header.contains("\t") || header.contains("\n"))
{
THROW(ProgrammingException, "TsvFile: header must not contain newline or tab, but does: " + header);
}
if (!rows_.isEmpty())
{
THROW(ProgrammingException, "TsvFile: cannot add header after row data was already added!");
}
headers_ << header;
}
const QStringList& TsvFile::headers() const
{
return headers_;
}
void TsvFile::addRow(const QStringList& row)
{
if (row.count()!=headers_.count())
{
THROW(ProgrammingException, "TsvFile: " + QString::number(headers_.count()) + " columns expected, but added row as " + QString::number(row.count()) + " columns:\n" + row.join("\t"));
}
foreach(const QString& entry, row)
{
if (entry.contains("\t") || entry.contains("\n"))
{
THROW(ProgrammingException, "TsvFile: row entry must not contain newline or tab, but does: " + entry);
}
}
rows_ << row;
}
const QStringList& TsvFile::operator[](int i) const
{
if (i<0 || i>=rows_.count())
{
THROW(ProgrammingException, "TsvFile: table has " + QString::number(rows_.count()) + " rows, but row with index " + QString::number(i) + " was requested.");
}
return rows_[i];
}
int TsvFile::columnIndex(const QString& column, bool throw_if_not_found) const
{
for (int c=0; c<headers_.count(); ++c)
{
if (headers_[c]==column) return c;
}
if (throw_if_not_found) THROW(ProgrammingException, "Column '" + column + "' not found in TsvFile!");
return -1;
}
QStringList TsvFile::extractColumn(int c)
{
if (c<0 || c>=headers_.count())
{
THROW(ProgrammingException, "TsvFile: table has " + QString::number(headers_.count()) + " columns, but column with index " + QString::number(c) + " was requested.");
}
QStringList output;
foreach(const QStringList& row, rows_)
{
output << row[c];
}
return output;
}
void TsvFile::removeColumn(int c)
{
if (c<0 || c>=headers_.count())
{
THROW(ProgrammingException, "TsvFile: table has " + QString::number(headers_.count()) + " columns, but column with index " + QString::number(c) + " was requested.");
}
headers_.removeAt(c);
for (int i=0; i<rows_.count(); ++i)
{
rows_[i].removeAt(c);
}
}
void TsvFile::load(QString filename, bool use_string_hash)
{
VersatileTextStream stream(filename);
while (!stream.atEnd())
{
QString line = stream.readLine();
//skip empty lines
if (line.isEmpty()) continue;
//header lines
if (line[0]=='#')
{
if (line.size()>1 && line[1]=='#') //comment
{
addComment(line);
}
else //header
{
QStringList parts = line.mid(1).split('\t');
foreach(QString part, parts)
{
addHeader(part);
}
}
continue;
}
//content lines
if (use_string_hash)
{
QStringList tmp;
foreach(QString entry, line.split('\t'))
{
if (!hash_.contains(entry)) hash_.insert(entry, entry);
tmp.append(hash_[entry]);
}
rows_ << tmp;
}
else
{
addRow(line.split('\t'));
}
}
hash_.clear();
filename_ = filename;
}
bool TsvFile::isValid() const
{
return filename_ != "";
}
void TsvFile::store(QString filename) const
{
auto file = Helper::openFileForWriting(filename);
QTextStream stream(file.data());
toStream(stream);
}
QString TsvFile::toString() const
{
QString output;
QTextStream stream(&output);
stream.setEncoding(QStringConverter::Utf8);
toStream(stream);
return output;
}
void TsvFile::toStream(QTextStream& stream) const
{
//comment
foreach(const QString& comment, comments_)
{
stream << comment << '\n';
}
//header
stream << '#';
for(int i=0; i<headers_.count(); ++i)
{
if (i!=0) stream << '\t';
stream << headers_[i];
}
stream << '\n';
//rows
foreach(const QStringList& row, rows_)
{
for(int i=0; i<row.count(); ++i)
{
if (i!=0) stream << '\t';
stream << row[i];
}
stream << '\n';
}
}