-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvrankingprinter.cpp
157 lines (137 loc) · 5.7 KB
/
csvrankingprinter.cpp
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
/*****************************************************************************
* Copyright (C) 2021 by Lorenzo Buzzi ([email protected]) *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
*****************************************************************************/
#include <QIODevice>
#include "csvrankingprinter.hpp"
#include "lbcrexception.hpp"
#include "crhelper.hpp"
void CSVRankingPrinter::init(QString *outFileName, QString const &title, QString const &subject)
{
Q_ASSERT(!csvFile.isOpen());
Q_UNUSED(title)
Q_UNUSED(subject)
if (outFileName == Q_NULLPTR) {
throw(ChronoRaceException(tr("Error: no file name supplied")));
}
// append the .csv extension if missing
checkOutFileNameExtension(*outFileName);
csvFile.setFileName(*outFileName);
if (!csvFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
throw(ChronoRaceException(tr("Error: cannot open %1").arg(*outFileName)));
}
csvStream.setDevice(&csvFile);
csvStream.setEncoding(CRLoader::getEncoding());
}
void CSVRankingPrinter::printStartList(QList<Competitor const *> const &startList)
{
if (!csvFile.isOpen()) {
throw(ChronoRaceException(tr("Error: writing attempt on closed file")));
}
int offset;
int i = 0;
QTime startTime = getRaceInfo()->getStartTime();
for (auto const *competitor : startList) {
i++;
csvStream << i << ",";
csvStream << competitor->getBib() << ",";
csvStream << competitor->getName() << ",";
csvStream << competitor->getTeam() << ",";
csvStream << competitor->getYear() << ",";
csvStream << CRHelper::toSexString(competitor->getSex()) << ",";
offset = competitor->getOffset();
if (offset >= 0) {
offset += (3600 * startTime.hour()) + (60 * startTime.minute()) + startTime.second();
csvStream << CRHelper::toOffsetString(offset);
} else if (CRLoader::getStartListLegs() == 1) {
offset = (3600 * startTime.hour()) + (60 * startTime.minute()) + startTime.second();
csvStream << CRHelper::toOffsetString(offset);
} else {
csvStream << qAbs(offset);
}
csvStream << Qt::endl;
}
csvStream << Qt::endl;
}
void CSVRankingPrinter::printRanking(Ranking const &categories, QList<ClassEntry const *> const &ranking)
{
static Position position;
if (!csvFile.isOpen()) {
throw(ChronoRaceException(tr("Error: writing attempt on closed file")));
}
int i = 0;
int prevPosNumber = 0;
int currPosNumber = 0;
QString currTime;
csvStream << categories.getShortDescription() << Qt::endl;
for (auto const c : ranking) {
i++;
currTime = c->getTotalTime(CRLoader::Format::CSV);
if ((currPosNumber = position.getCurrentPositionNumber(i, currTime)) == 0)
currPosNumber = prevPosNumber;
else
prevPosNumber = currPosNumber;
csvStream << currPosNumber << ",";
csvStream << c->getBib() << ",";
csvStream << c->getNames(CRLoader::Format::CSV) << ",";
if (CRLoader::getStartListLegs() > 1)
csvStream << c->getTimes(CRLoader::Format::CSV) << ",";
csvStream << currTime << Qt::endl;
}
csvStream << Qt::endl;
}
void CSVRankingPrinter::printRanking(Ranking const &categories, QList<TeamClassEntry const *> const &ranking)
{
if (!csvFile.isOpen()) {
throw(ChronoRaceException(tr("Error: writing attempt on closed file")));
}
int i = 0;
csvStream << categories.getShortDescription() << Qt::endl;
for (auto const r : ranking) {
i++;
for (int j = 0; j < r->getClassEntryCount(); j++) {
csvStream << i << ",";
csvStream << r->getClassEntry(j)->getBib() << ",";
csvStream << r->getClassEntry(j)->getNames(CRLoader::Format::CSV) << ",";
if (CRLoader::getStartListLegs() > 1)
csvStream << r->getClassEntry(j)->getTimes(CRLoader::Format::CSV) << ",";
csvStream << r->getClassEntry(j)->getTotalTime(CRLoader::Format::CSV) << Qt::endl;
}
}
csvStream << Qt::endl;
}
bool CSVRankingPrinter::finalize()
{
bool result = true;
if (QIODevice *device = csvStream.device(); device != Q_NULLPTR) {
csvStream.flush();
device->close();
} else {
result = false;
}
return result;
}
QString CSVRankingPrinter::getFileFilter()
{
return tr("CSV (*.csv)");
}
QString &CSVRankingPrinter::buildOutFileName(QString &outFileBaseName)
{
return outFileBaseName.append(".csv");
}
QString &CSVRankingPrinter::checkOutFileNameExtension(QString &outFileBaseName)
{
return (!outFileBaseName.endsWith(".csv", Qt::CaseInsensitive)) ? buildOutFileName(outFileBaseName) : outFileBaseName;
}