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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html *
// * Copyright (C) ZenJu (zhnmju123 AT gmx DOT de) - All Rights Reserved *
// **************************************************************************
#ifndef SERIALIZE_H_INCLUDED
#define SERIALIZE_H_INCLUDED
#include <cstdint>
#include <wx/stream.h>
#include <zen/file_io.h>
namespace zen
{
//unchecked, unformatted serialization
template <class T> T readPOD (wxInputStream& stream);
template <class T> void writePOD(wxOutputStream& stream, const T& pod);
template <class S> S readString (wxInputStream& stream);
template <class S> void writeString(wxOutputStream& stream, const S& str);
//############# wxWidgets stream adapter #############
class FileInputStream : public wxInputStream
{
public:
FileInputStream(const Zstring& filename) : fileObj(filename) {} //throw FileError
private:
virtual size_t OnSysRead(void* buffer, size_t bufsize) { return fileObj.read(buffer, bufsize); } //throw FileError
zen::FileInput fileObj;
};
class FileOutputStream : public wxOutputStream
{
public:
FileOutputStream(const Zstring& filename) : fileObj(filename, zen::FileOutput::ACC_OVERWRITE) {} //throw FileError
private:
virtual size_t OnSysWrite(const void* buffer, size_t bufsize)
{
fileObj.write(buffer, bufsize); //throw FileError
return bufsize;
}
zen::FileOutput fileObj;
};
class CheckedIo
{
protected:
CheckedIo(wxStreamBase& stream) : stream_(stream) {}
void check() const
{
if (stream_.GetLastError() != wxSTREAM_NO_ERROR)
throwException();
}
virtual void throwException() const = 0;
private:
wxStreamBase& stream_;
};
//wxInputStream proxy throwing exception on error
class CheckedReader : private CheckedIo
{
public:
CheckedReader(wxInputStream& stream) : CheckedIo(stream), stream_(stream) {}
template <class T>
T readPOD() const; //throw!
template <class S>
S readString() const; //throw!
private:
wxInputStream& stream_;
};
//wxOutputStream proxy throwing FileError on error
class CheckedWriter : private CheckedIo
{
public:
CheckedWriter(wxOutputStream& stream) : CheckedIo(stream), stream_(stream) {}
template <class T>
void writePOD(T number) const; //throw!
template <class S>
void writeString(const S& str) const; //throw!
private:
wxOutputStream& stream_;
};
//-----------------------implementation-------------------------------
template <class T> inline
T readPOD(wxInputStream& stream)
{
T pod = 0;
stream.Read(reinterpret_cast<char*>(&pod), sizeof(T));
return pod;
}
template <class T> inline
void writePOD(wxOutputStream& stream, const T& pod)
{
stream.Write(reinterpret_cast<const char*>(&pod), sizeof(T));
}
template <class S> inline
S readString(wxInputStream& stream)
{
//don't even consider UTF8 conversions here! "string" is expected to handle arbitrary binary data!
typedef typename S::value_type CharType;
const auto strLength = readPOD<std::uint32_t>(stream);
S output;
if (strLength > 0)
{
output.resize(strLength); //throw std::bad_alloc
stream.Read(&*output.begin(), sizeof(CharType) * strLength);
}
return output;
}
template <class S> inline
void writeString(wxOutputStream& stream, const S& str)
{
writePOD(stream, static_cast<std::uint32_t>(str.length()));
stream.Write(str.c_str(), sizeof(typename S::value_type) * str.length());
}
template <class T>
inline
T CheckedReader::readPOD() const //checked read operation
{
T output = zen::readPOD<T>(stream_);
check();
return output;
}
template <class S> inline
S CheckedReader::readString() const //checked read operation
{
S output;
try
{
output = zen::readString<S>(stream_); //throw std::bad_alloc
}
catch (std::exception&) { throwException(); }
check();
if (stream_.LastRead() != output.length() * sizeof(typename S::value_type)) //some additional check
throwException();
return output;
}
template <class T> inline
void CheckedWriter::writePOD(T number) const //checked write operation
{
zen::writePOD<T>(stream_, number);
check();
}
template <class S> inline
void CheckedWriter::writeString(const S& str) const //checked write operation
{
zen::writeString(stream_, str);
check();
if (stream_.LastWrite() != str.length() * sizeof(typename S::value_type)) //some additional check
throwException();
}
}
#endif //SERIALIZE_H_INCLUDED
|