/
saveload.h
173 lines (149 loc) · 4.58 KB
/
saveload.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* saveload.h -- wrapper for istreams/ostreams with alignment feature
*
* Copyright (c) 2007-2010, Dmitry Prokoptsev <dprokoptsev@gmail.com>,
* Alexander Gololobov <agololobov@gmail.com>
*
* This file is part of Pire, the Perl Incompatible
* Regular Expressions library.
*
* Pire is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pire is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser Public License for more details.
* You should have received a copy of the GNU Lesser Public License
* along with Pire. If not, see <http://www.gnu.org/licenses>.
*/
#ifndef PIRE_STUB_SAVELOAD_H_INCLUDED
#define PIRE_STUB_SAVELOAD_H_INCLUDED
#include <sys/types.h>
#include <iostream>
#include <vector>
#include "stl.h"
namespace Pire {
namespace Impl {
template<class Char, class Traits = std::char_traits<Char> >
class CountingStreambuf: public std::basic_streambuf<Char, Traits> {
public:
CountingStreambuf(std::basic_streambuf<Char, Traits>* backend)
: m_backend(backend)
, m_read(0)
, m_written(0)
{}
std::streamsize read() const { return m_read; }
std::streamsize written() const { return m_written; }
protected:
typename Traits::int_type underflow()
{
typename Traits::int_type ret = m_backend->sgetc();
if (!Traits::eq_int_type(ret, Traits::eof())) {
m_ch = (Char) ret;
m_read += sizeof(Char);
this->setg(&m_ch, &m_ch, &m_ch+1);
}
return ret;
}
std::streamsize xsgetn(Char* data, std::streamsize len)
{
std::streamsize ret = m_backend->sgetn(data, len);
m_read += ret * sizeof(Char);
return ret;
}
typename Traits::int_type overflow(typename Traits::int_type c)
{
typename Traits::int_type ret = m_backend->sputc(c);
if (!Traits::eq_int_type(ret, Traits::eof()))
m_written += sizeof(Char);
return ret;
}
std::streamsize xsputn(const Char* data, std::streamsize len)
{
std::streamsize ret = m_backend->sputn(data, len);
m_written += ret * sizeof(Char);
return ret;
}
int sync() { return m_backend->pubsync(); }
private:
std::basic_streambuf<Char, Traits>* m_backend;
std::streamsize m_read;
std::streamsize m_written;
Char m_ch;
};
template<class Char, class Traits = std::char_traits<Char> >
class BasicAlignedInput: public std::basic_istream<Char, Traits> {
public:
BasicAlignedInput(std::basic_istream<Char, Traits>* backend)
: std::basic_istream<Char, Traits>(0)
, m_streambuf(backend->rdbuf())
{
rdbuf(&m_streambuf);
}
void Align(size_t divisor = sizeof(void*))
{
if (m_streambuf.read() & (divisor - 1)) {
size_t len = (divisor - (m_streambuf.read() & (divisor - 1))) / sizeof(Char);
if (m_fill.size() < len)
m_fill.resize(len);
this->read(&m_fill[0], len);
}
}
private:
CountingStreambuf<Char, Traits> m_streambuf;
std::vector<char> m_fill;
};
template<class Char, class Traits = std::char_traits<Char> >
class BasicAlignedOutput: public std::basic_ostream<Char, Traits> {
public:
BasicAlignedOutput(std::basic_ostream<Char, Traits>* backend)
: std::basic_ostream<Char, Traits>(0)
, m_streambuf(backend->rdbuf())
{
this->rdbuf(&m_streambuf);
}
void Align(size_t divisor = sizeof(void*))
{
if (m_streambuf.written() & (divisor - 1)) {
size_t len = (divisor - (m_streambuf.written() & (divisor - 1))) / sizeof(Char);
if (m_fill.size() < len)
m_fill.resize(len, char(0xCC));
this->write(&m_fill[0], len);
}
}
private:
CountingStreambuf<Char, Traits> m_streambuf;
std::vector<char> m_fill;
};
}
typedef Impl::BasicAlignedInput<char> AlignedInput;
typedef Impl::BasicAlignedOutput<char> AlignedOutput;
template<class T>
void SavePodType(yostream* s, const T& t)
{
s->write((char*) &t, sizeof(t));
}
template<class T>
void LoadPodType(yistream* s, T& t)
{
s->read((char*) &t, sizeof(t));
}
template<class T>
void Save(yostream* s, const T& t) { t.Save(s);}
template<class T>
void Load(yistream* s, T& t) { t.Load(s);}
template<class T>
void SavePodArray(yostream* s, const T* t, size_t len)
{
s->write((char*) t, len * sizeof(*t));
}
template<class T>
void LoadPodArray(yistream* s, T* t, size_t len)
{
s->read((char*) t, len * sizeof(*t));
}
};
#endif