-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathBinaryIO.h
96 lines (79 loc) · 2.1 KB
/
BinaryIO.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
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
#ifndef INCLUDE_BINARYIO_H
#define INCLUDE_BINARYIO_H
// C++
#include <cstring>
#include <string>
#include <vector>
// Local
#include "DataSample.h"
#include "utils.h"
#include "sizes.h"
class BinaryWriter {
private:
unsigned char *m_ptr, *m_end;
public:
BinaryWriter(unsigned char *start, unsigned char *end);
bool eof() const;
// vector<DataSample<string> >
void write(const std::vector<DataSample<std::string> > &samples);
static size_t write_length(const std::vector<DataSample<std::string> > &samples);
// string
void write(const std::string &text);
static size_t write_length(const std::string &text);
static size_t write_string_length(size_t len);
// vector<T>
template <class T>
void write(const std::vector<T> &v) {
write((uint32) (v.size() * sizeof(T)));
for (unsigned i = 0; i < v.size(); i++) write(v[i]);
}
template <class T>
static size_t write_length(const std::vector<T> &v) {
size_t ret = write_length((uint32)0);
for (unsigned i = 0; i < v.size(); i++) ret += write_length(v[i]);
return ret;
}
// T
template <class T>
void write(const T &v) {
write_bytes((void*)&v, write_length(v));
}
template <class T>
static size_t write_length(const T &v) {
return sizeof(v);
}
////
void write_bytes(const void *p, size_t len);
void write_zeros(size_t len);
};
class BinaryReader {
private:
const unsigned char *m_ptr, *m_end;
public:
BinaryReader(const unsigned char *start, const unsigned char *end);
bool eof() const;
// vector<DataSample<string> >
void read(std::vector<DataSample<std::string> > &samples);
// string
void read(std::string &text);
// vector<T>
template <class T>
void read(std::vector<T> &v) {
uint32 len;
read(len);
tassert(len % sizeof(T) == 0);
v.resize(len / sizeof(T));
for (unsigned i = 0; i < v.size(); i++) read(v[i]);
}
// T
template <class T>
void read(T &v) {
tassert(m_ptr + sizeof(v) <= m_end);
memcpy((void*)&v, m_ptr, sizeof(v));
m_ptr += sizeof(v);
}
////
void read_bytes(void *dest, size_t len);
void skip_bytes(size_t len);
};
#endif