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
|
#ifndef SERIALIZE_H_INCLUDED
#define SERIALIZE_H_INCLUDED
#include "zstring.h"
#include <wx/stream.h>
#include "fileError.h"
#include <boost/scoped_array.hpp>
#include <boost/shared_ptr.hpp>
namespace Utility
{
template <class T>
T readNumber(wxInputStream& stream);
template <class T>
void writeNumber(wxOutputStream& stream, T number);
Zstring readString(wxInputStream& stream);
void writeString(wxOutputStream& stream, const Zstring& str);
class ReadInputStream //throw FileError()
{
protected:
ReadInputStream(wxInputStream& stream, const wxString& errorObjName) : stream_(stream), errorObjName_(errorObjName) {}
template <class T>
T readNumberC(); //checked read operation
Zstring readStringC(); //checked read operation
typedef boost::shared_ptr<std::vector<char> > CharArray;
CharArray readArrayC();
void check();
protected:
wxInputStream& stream_;
private:
void throwReadError(); //throw FileError()
const wxString& errorObjName_; //used for error text only
};
class WriteOutputStream //throw FileError()
{
protected:
WriteOutputStream(const wxString& errorObjName, wxOutputStream& stream) : stream_(stream), errorObjName_(errorObjName) {}
template <class T>
void writeNumberC(T number); //checked write operation
void writeStringC(const Zstring& str); //checked write operation
void writeArrayC(const std::vector<char>& buffer);
void check();
protected:
wxOutputStream& stream_;
private:
void throwWriteError(); //throw FileError()
const wxString& errorObjName_; //used for error text only!
};
//---------------Inline Implementation---------------------------------------------------
template <class T>
inline
T readNumber(wxInputStream& stream)
{
T result = 0;
stream.Read(&result, sizeof(T));
return result;
}
template <class T>
inline
void writeNumber(wxOutputStream& stream, T number)
{
stream.Write(&number, sizeof(T));
}
inline
Zstring readString(wxInputStream& stream) //read string from file stream
{
const size_t strLength = readNumber<size_t>(stream);
if (strLength <= 1000)
{
DefaultChar buffer[1000];
stream.Read(buffer, sizeof(DefaultChar) * strLength);
return Zstring(buffer, strLength);
}
else
{
boost::scoped_array<DefaultChar> buffer(new DefaultChar[strLength]);
stream.Read(buffer.get(), sizeof(DefaultChar) * strLength);
return Zstring(buffer.get(), strLength);
}
}
inline
void writeString(wxOutputStream& stream, const Zstring& str) //write string to filestream
{
writeNumber<size_t>(stream, str.length());
stream.Write(str.c_str(), sizeof(DefaultChar) * str.length());
}
inline
void ReadInputStream::check()
{
if (stream_.GetLastError() != wxSTREAM_NO_ERROR)
throwReadError();
}
template <class T>
inline
T ReadInputStream::readNumberC() //checked read operation
{
T output = readNumber<T>(stream_);
check();
return output;
}
inline
Zstring ReadInputStream::readStringC() //checked read operation
{
Zstring output = readString(stream_);
check();
return output;
}
template <class T>
inline
void WriteOutputStream::writeNumberC(T number) //checked write operation
{
writeNumber<T>(stream_, number);
check();
}
inline
void WriteOutputStream::writeStringC(const Zstring& str) //checked write operation
{
writeString(stream_, str);
check();
}
inline
void WriteOutputStream::check()
{
if (stream_.GetLastError() != wxSTREAM_NO_ERROR)
throwWriteError();
}
}
#endif // SERIALIZE_H_INCLUDED
|