-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblob.cpp
35 lines (26 loc) · 969 Bytes
/
blob.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
#include "blob.h"
#include <filesystem>
#include <fstream>
#include <iterator>
namespace fs = std::filesystem;
namespace tog {
Blob::Blob(const fs::path& path) {
// Read file in binary mode, without skipping white space
std::ifstream file(path, std::ios::binary);
file.unsetf(std::ios::skipws);
// Get file size by seeking to the end of the stream and getting the cursor
// position
file.seekg(0, std::ios::end);
auto file_size = file.tellg();
// Seek back to the beginning of the stream
file.seekg(0, std::ios::beg);
_data.reserve(file_size);
_data.insert(_data.begin(), std::istream_iterator<unsigned char>(file),
std::istream_iterator<unsigned char>());
}
const std::vector<unsigned char>& Blob::serialize() {
// as of now, the serialization is just the raw data (i.e. no custom binary
// layout is used, like in git). this may change in the future
return _data;
}
} // namespace tog