-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumption.cpp
81 lines (63 loc) · 1.83 KB
/
consumption.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
#include "consumption.h"
#include "app.h"
#include "args.h"
#include "common.h" // IWYU pragma: keep Needed for formatting Qt types
#include "header.h"
#include <QFile>
#include <fmt/format.h>
namespace El {
Consumption::Consumption(App const &app)
: _app(app)
{}
Consumption::~Consumption() = default;
auto Consumption::load(QString const &filename) -> bool
{
// open the input file
QFile file(filename);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
fmt::print(stderr, "CSV faili {} avamine ebaõnnestus: {}", filename, file.errorString());
return false;
}
// load all the records
int lineno = 0;
bool skip = true;
bool header = true;
Header hdr;
while (!file.atEnd()) {
++lineno;
auto const line = file.readLine().trimmed();
// skip the first lines until we reach the header
if (skip) {
// there is an empty line or a line with just two quotes between the beginning and header
skip = !line.isEmpty() && line != "\"\"";
continue;
}
// read the header
if (header) {
header = false;
hdr = Header(line);
if (!hdr.isValid()) {
return false;
}
continue;
}
Record rec{lineno, line, hdr};
if (!rec.isValid()) {
continue;
}
// verify time
if (rec.endTime() > _app.args().time()) {
break;
}
_records.append(std::move(rec));
}
// ensure that there is at least one record
if (_records.isEmpty()) {
fmt::print(stderr, "CSV fail ei sisalda ühtegi kirjet\n");
return false;
}
_first_record_time = _records.first().startTime();
_last_record_time = _records.last().startTime();
return true;
}
} // namespace El