-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparsehelpers.cpp
More file actions
74 lines (62 loc) · 1.61 KB
/
parsehelpers.cpp
File metadata and controls
74 lines (62 loc) · 1.61 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
#include "parsehelpers.h"
#include <QFile>
QByteArray readFile(const QString &filename)
{
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
throw QString("read error, couldn't open file");
}
QByteArray data = file.readAll();
if (data.isNull())
{
throw QString("read error, couldn't read file");
}
return data;
}
int indexOrThrow(QByteArray const &data, const char *target, int from)
{
int result = data.indexOf(target, from);
if (result < 0)
throw QString("QByteArray: Target not found");
return result;
}
QString const &atOrThrow(QStringList const &data, int index)
{
if (index >= data.size() || index < 0)
throw QString("Index out of range");
return data.at(index);
}
int toIntOrThrow(QString const &str) {
bool ok = true;
int result = str.toInt(&ok);
if (!ok)
throw QString("Could not convert QString to integer:") + str;
return result;
}
double toDoubleOrThrow(QString const &str) {
bool ok = true;
double result = str.toDouble(&ok);
if (!ok)
throw QString("Could not convert QString to double:") + str;
return result;
}
int intFromList(QStringList const &data, int index)
{
return toIntOrThrow(atOrThrow(data, index));
}
double doubleFromList(QStringList const &data, int index)
{
return toDoubleOrThrow(atOrThrow(data, index));
}
QStringList splitFixedWidth(QString &str, QList<int> sizes)
{
QStringList result;
int last = 0;
for (int len: sizes)
{
result.push_back(str.mid(last, len));
last += len;
}
return result;
}