-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumption.cpp
62 lines (47 loc) · 1.33 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
#include "consumption.h"
#include "app.h"
#include "args.h"
#include "common.h" // needed for fmt
#include <QFile>
#include <fmt/base.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;
while (!file.atEnd()) {
++lineno;
auto const line = file.readLine();
if (lineno <= _app.args().skip()) {
continue;
}
Record rec{lineno, line, _app.args().oldFormat()};
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