-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBinaryReader.h
45 lines (41 loc) · 882 Bytes
/
BinaryReader.h
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
#pragma once
#include <fstream>
#include <algorithm>
class CBinaryReader : public std::ifstream
{
public:
CBinaryReader(const char* file) : std::ifstream(file, std::ios::binary){};
~CBinaryReader() = default;
template <class T>
T Read()
{
T res;
read(reinterpret_cast<char *>(&res), sizeof(T));
return res;
}
template <class T, class TS>
static T Read(TS* stream)
{
T res;
stream->read(reinterpret_cast<char *>(&res), sizeof(T));
return res;
}
template <typename TS, typename T>
static void CopyStream(TS* input, T* output, long size)
{
int left = size;
char data[1 << 16];
while (left > 0)
{
int block = (int)(std::min(left, 1 << 16));
input->read(data, block);
if(!input)
{
throw std::logic_error("EndOfStreamException");
}
std::streamsize cnt = input->gcount();
output->write(data, cnt);
left -= block;
}
}
};