-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump.cc
98 lines (78 loc) · 3.17 KB
/
dump.cc
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
/*
Copyright 2021 Simon (psyomn) Symeonidis
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "dump.h"
#include "utils.h"
#include <cstdint>
#include <ctime>
#include <functional>
namespace psy::psycal /* ::Dump // TODO */ {
std::vector<Event> ReadEventsFrom(std::istream& is) {
std::vector<Event> ret;
while (!is.eof() && is.peek() != EOF) {
std::uint64_t timestamp_u64 = 0;
is.read(reinterpret_cast<char*>(×tamp_u64), sizeof(timestamp_u64));
size_t message_size = 0;
is.read(reinterpret_cast<char*>(&message_size), sizeof(message_size));
char *buffer = new char[message_size];
is.read(reinterpret_cast<char*>(buffer),
static_cast<std::streamsize>(message_size));
const time_t timestamp = time_t(timestamp_u64);
std::tm result = {0};
(void) localtime_r(×tamp, &result);
ret.push_back(
Event(std::move(result), std::move(std::string(buffer)))
);
delete[] buffer;
}
return ret;
}
void SaveOld(const std::vector<Event>& olds) {
std::ofstream of(
Utils::HistoryFilePath(),
std::ofstream::binary | std::ofstream::app | std::ofstream::out);
of.imbue(std::locale::classic());
for (const auto& el : olds) {
const std::uint64_t timestamp = el.GetUnixTimestamp();
of.write(reinterpret_cast<const char*>(×tamp), sizeof(timestamp));
const std::uint64_t message_size = el.GetMessage().size();
of.write(reinterpret_cast<const char*>(&message_size), sizeof(message_size));
of.write(reinterpret_cast<const char*>(el.GetMessage().data()),
static_cast<std::streamsize>(el.GetMessage().size()));
}
of.close();
}
void SaveCurrentEvents(const std::priority_queue<Event,
std::vector<Event>,
Event::Comparator>& events) {
std::ofstream of(
Utils::EventsFilePath(),
std::ios::binary|std::ios::out);
of.imbue(std::locale::classic());
/* priority queue doesn't allow for iteration, so I can't quite
dump this directly onto disk. */
std::priority_queue<
Event,
std::vector<Event>,
Event::Comparator> copy = events;
while (copy.size() > 0) {
auto element = copy.top();
const std::uint64_t timestamp = element.GetUnixTimestamp();
of.write(reinterpret_cast<const char*>(×tamp), sizeof(timestamp));
const size_t message_size = element.GetMessage().size();
of.write(reinterpret_cast<const char*>(&message_size), sizeof(message_size));
of.write(element.GetMessage().c_str(),
static_cast<std::streamsize>(element.GetMessage().size()));
copy.pop();
}
}
}