-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_slice_parser.cpp
More file actions
58 lines (50 loc) · 978 Bytes
/
file_slice_parser.cpp
File metadata and controls
58 lines (50 loc) · 978 Bytes
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
#include "file_slice_parser.h"
#include "slice_db.h"
#include <sstream>
#include <fcntl.h>
#include <unistd.h>
FileSliceParser::FileSliceParser(const std::string& file)
: m_file(file)
{
}
bool FileSliceParser::readFileIntoStream(std::stringstream* const str) {
bool ret = true;
char buf[8192];
int fd = open(m_file.c_str(), 0);
if (fd < 0)
return false;
for (;;) {
int rd = read(fd, buf, sizeof(buf));
if (rd < 0) {
ret = false;
break;
}
else if (rd == 0)
break;
else
str->write(buf, rd);
}
close(fd);
return ret;
}
bool FileSliceParser::parse(Slices* const slices) {
std::stringstream ss;
if (!readFileIntoStream(&ss))
return false;
slices->clear();
cereal::BinaryInputArchive iarchive(ss);
try {
for (;;) {
uint64_t time;
Slice slice;
iarchive(time);
iarchive(slice);
slices->insert(std::make_pair(time, slice));
}
}
catch (const cereal::Exception& e) {
// nothing
puts(e.what());
}
return true;
}