diff options
Diffstat (limited to '')
-rw-r--r-- | src/archive.cpp | 386 |
1 files changed, 386 insertions, 0 deletions
diff --git a/src/archive.cpp b/src/archive.cpp new file mode 100644 index 0000000..7208bae --- /dev/null +++ b/src/archive.cpp | |||
@@ -0,0 +1,386 @@ | |||
1 | #include "archive.h" | ||
2 | |||
3 | Bu::Archive::Archive( Stream &rStream, bool bLoading ) : | ||
4 | bLoading( bLoading ), | ||
5 | rStream( rStream ), | ||
6 | nNextID( 1 ) | ||
7 | { | ||
8 | } | ||
9 | |||
10 | Bu::Archive::~Archive() | ||
11 | { | ||
12 | } | ||
13 | |||
14 | void Bu::Archive::write( const void *pData, int32_t nSize ) | ||
15 | { | ||
16 | if( nSize == 0 || pData == NULL ) | ||
17 | return; | ||
18 | |||
19 | rStream.write( (const char *)pData, nSize ); | ||
20 | } | ||
21 | |||
22 | void Bu::Archive::read( void *pData, int32_t nSize ) | ||
23 | { | ||
24 | if( nSize == 0 || pData == NULL ) | ||
25 | return; | ||
26 | |||
27 | rStream.read( (char *)pData, nSize ); | ||
28 | } | ||
29 | |||
30 | void Bu::Archive::close() | ||
31 | { | ||
32 | rStream.close(); | ||
33 | } | ||
34 | |||
35 | bool Bu::Archive::isLoading() | ||
36 | { | ||
37 | return bLoading; | ||
38 | } | ||
39 | Bu::Archive &Bu::Archive::operator<<(bool p) | ||
40 | { | ||
41 | write( &p, sizeof(p) ); | ||
42 | return *this; | ||
43 | } | ||
44 | Bu::Archive &Bu::Archive::operator<<(int8_t p) | ||
45 | { | ||
46 | write( &p, sizeof(p) ); | ||
47 | return *this; | ||
48 | } | ||
49 | Bu::Archive &Bu::Archive::operator<<(int16_t p) | ||
50 | { | ||
51 | write( &p, sizeof(p) ); | ||
52 | return *this; | ||
53 | } | ||
54 | Bu::Archive &Bu::Archive::operator<<(int32_t p) | ||
55 | { | ||
56 | write( &p, sizeof(p) ); | ||
57 | return *this; | ||
58 | } | ||
59 | Bu::Archive &Bu::Archive::operator<<(int64_t p) | ||
60 | { | ||
61 | write( &p, sizeof(p) ); | ||
62 | return *this; | ||
63 | } | ||
64 | Bu::Archive &Bu::Archive::operator<<(uint8_t p) | ||
65 | { | ||
66 | write( &p, sizeof(p) ); | ||
67 | return *this; | ||
68 | } | ||
69 | Bu::Archive &Bu::Archive::operator<<(uint16_t p) | ||
70 | { | ||
71 | write( &p, sizeof(p) ); | ||
72 | return *this; | ||
73 | } | ||
74 | Bu::Archive &Bu::Archive::operator<<(uint32_t p) | ||
75 | { | ||
76 | write( &p, sizeof(p) ); | ||
77 | return *this; | ||
78 | } | ||
79 | Bu::Archive &Bu::Archive::operator<<(uint64_t p) | ||
80 | { | ||
81 | write( &p, sizeof(p) ); | ||
82 | return *this; | ||
83 | } | ||
84 | Bu::Archive &Bu::Archive::operator<<(long p) | ||
85 | { | ||
86 | write( &p, sizeof(p) ); | ||
87 | return *this; | ||
88 | } | ||
89 | Bu::Archive &Bu::Archive::operator<<(float p) | ||
90 | { | ||
91 | write( &p, sizeof(p) ); | ||
92 | return *this; | ||
93 | } | ||
94 | Bu::Archive &Bu::Archive::operator<<(double p) | ||
95 | { | ||
96 | write( &p, sizeof(p) ); | ||
97 | return *this; | ||
98 | } | ||
99 | Bu::Archive &Bu::Archive::operator<<(long double p) | ||
100 | { | ||
101 | write( &p, sizeof(p) ); | ||
102 | return *this; | ||
103 | } | ||
104 | |||
105 | Bu::Archive &Bu::Archive::operator>>(bool &p) | ||
106 | { | ||
107 | read( &p, sizeof(p) ); | ||
108 | return *this; | ||
109 | } | ||
110 | Bu::Archive &Bu::Archive::operator>>(int8_t &p) | ||
111 | { | ||
112 | read( &p, sizeof(p) ); | ||
113 | return *this; | ||
114 | } | ||
115 | Bu::Archive &Bu::Archive::operator>>(int16_t &p) | ||
116 | { | ||
117 | read( &p, sizeof(p) ); | ||
118 | return *this; | ||
119 | } | ||
120 | Bu::Archive &Bu::Archive::operator>>(int32_t &p) | ||
121 | { | ||
122 | read( &p, sizeof(p) ); | ||
123 | return *this; | ||
124 | } | ||
125 | Bu::Archive &Bu::Archive::operator>>(int64_t &p) | ||
126 | { | ||
127 | read( &p, sizeof(p) ); | ||
128 | return *this; | ||
129 | } | ||
130 | Bu::Archive &Bu::Archive::operator>>(uint8_t &p) | ||
131 | { | ||
132 | read( &p, sizeof(p) ); | ||
133 | return *this; | ||
134 | } | ||
135 | Bu::Archive &Bu::Archive::operator>>(uint16_t &p) | ||
136 | { | ||
137 | read( &p, sizeof(p) ); | ||
138 | return *this; | ||
139 | } | ||
140 | Bu::Archive &Bu::Archive::operator>>(uint32_t &p) | ||
141 | { | ||
142 | read( &p, sizeof(p) ); | ||
143 | return *this; | ||
144 | } | ||
145 | Bu::Archive &Bu::Archive::operator>>(uint64_t &p) | ||
146 | { | ||
147 | read( &p, sizeof(p) ); | ||
148 | return *this; | ||
149 | } | ||
150 | Bu::Archive &Bu::Archive::operator>>(long &p) | ||
151 | { | ||
152 | read( &p, sizeof(p) ); | ||
153 | return *this; | ||
154 | } | ||
155 | Bu::Archive &Bu::Archive::operator>>(float &p) | ||
156 | { | ||
157 | read( &p, sizeof(p) ); | ||
158 | return *this; | ||
159 | } | ||
160 | Bu::Archive &Bu::Archive::operator>>(double &p) | ||
161 | { | ||
162 | read( &p, sizeof(p) ); | ||
163 | return *this; | ||
164 | } | ||
165 | Bu::Archive &Bu::Archive::operator>>(long double &p) | ||
166 | { | ||
167 | read( &p, sizeof(p) ); | ||
168 | return *this; | ||
169 | } | ||
170 | |||
171 | Bu::Archive &Bu::Archive::operator&&(bool &p) | ||
172 | { | ||
173 | if (bLoading) | ||
174 | { | ||
175 | return *this >> p; | ||
176 | } | ||
177 | else | ||
178 | { | ||
179 | return *this << p; | ||
180 | } | ||
181 | } | ||
182 | |||
183 | Bu::Archive &Bu::Archive::operator&&(int8_t &p) | ||
184 | { | ||
185 | if (bLoading) | ||
186 | { | ||
187 | return *this >> p; | ||
188 | } | ||
189 | else | ||
190 | { | ||
191 | return *this << p; | ||
192 | } | ||
193 | } | ||
194 | |||
195 | Bu::Archive &Bu::Archive::operator&&(int16_t &p) | ||
196 | { | ||
197 | if (bLoading) | ||
198 | { | ||
199 | return *this >> p; | ||
200 | } | ||
201 | else | ||
202 | { | ||
203 | return *this << p; | ||
204 | } | ||
205 | } | ||
206 | |||
207 | Bu::Archive &Bu::Archive::operator&&(int32_t &p) | ||
208 | { | ||
209 | if (bLoading) | ||
210 | { | ||
211 | return *this >> p; | ||
212 | } | ||
213 | else | ||
214 | { | ||
215 | return *this << p; | ||
216 | } | ||
217 | } | ||
218 | |||
219 | Bu::Archive &Bu::Archive::operator&&(int64_t &p) | ||
220 | { | ||
221 | if (bLoading) | ||
222 | { | ||
223 | return *this >> p; | ||
224 | } | ||
225 | else | ||
226 | { | ||
227 | return *this << p; | ||
228 | } | ||
229 | } | ||
230 | |||
231 | Bu::Archive &Bu::Archive::operator&&(uint8_t &p) | ||
232 | { | ||
233 | if (bLoading) | ||
234 | { | ||
235 | return *this >> p; | ||
236 | } | ||
237 | else | ||
238 | { | ||
239 | return *this << p; | ||
240 | } | ||
241 | } | ||
242 | |||
243 | Bu::Archive &Bu::Archive::operator&&(uint16_t &p) | ||
244 | { | ||
245 | if (bLoading) | ||
246 | { | ||
247 | return *this >> p; | ||
248 | } | ||
249 | else | ||
250 | { | ||
251 | return *this << p; | ||
252 | } | ||
253 | } | ||
254 | |||
255 | Bu::Archive &Bu::Archive::operator&&(uint32_t &p) | ||
256 | { | ||
257 | if (bLoading) | ||
258 | { | ||
259 | return *this >> p; | ||
260 | } | ||
261 | else | ||
262 | { | ||
263 | return *this << p; | ||
264 | } | ||
265 | } | ||
266 | |||
267 | Bu::Archive &Bu::Archive::operator&&(uint64_t &p) | ||
268 | { | ||
269 | if (bLoading) | ||
270 | { | ||
271 | return *this >> p; | ||
272 | } | ||
273 | else | ||
274 | { | ||
275 | return *this << p; | ||
276 | } | ||
277 | } | ||
278 | |||
279 | Bu::Archive &Bu::Archive::operator&&(float &p) | ||
280 | { | ||
281 | if (bLoading) | ||
282 | { | ||
283 | return *this >> p; | ||
284 | } | ||
285 | else | ||
286 | { | ||
287 | return *this << p; | ||
288 | } | ||
289 | } | ||
290 | |||
291 | Bu::Archive &Bu::Archive::operator&&(double &p) | ||
292 | { | ||
293 | if (bLoading) | ||
294 | { | ||
295 | return *this >> p; | ||
296 | } | ||
297 | else | ||
298 | { | ||
299 | return *this << p; | ||
300 | } | ||
301 | } | ||
302 | |||
303 | Bu::Archive &Bu::Archive::operator&&(long double &p) | ||
304 | { | ||
305 | if (bLoading) | ||
306 | { | ||
307 | return *this >> p; | ||
308 | } | ||
309 | else | ||
310 | { | ||
311 | return *this << p; | ||
312 | } | ||
313 | } | ||
314 | |||
315 | |||
316 | Bu::Archive &Bu::operator<<(Bu::Archive &s, Bu::Archival &p) | ||
317 | { | ||
318 | p.archive( s ); | ||
319 | return s; | ||
320 | } | ||
321 | |||
322 | Bu::Archive &Bu::operator>>(Bu::Archive &s, Bu::Archival &p) | ||
323 | { | ||
324 | p.archive( s ); | ||
325 | return s; | ||
326 | } | ||
327 | |||
328 | Bu::Archive &Bu::operator<<( Bu::Archive &ar, std::string &s ) | ||
329 | { | ||
330 | ar << (uint32_t)s.length(); | ||
331 | ar.write( s.c_str(), s.length() ); | ||
332 | |||
333 | return ar; | ||
334 | } | ||
335 | |||
336 | Bu::Archive &Bu::operator>>( Bu::Archive &ar, std::string &s ) | ||
337 | { | ||
338 | uint32_t l; | ||
339 | ar >> l; | ||
340 | char *tmp = new char[l+1]; | ||
341 | tmp[l] = '\0'; | ||
342 | ar.read( tmp, l ); | ||
343 | s = tmp; | ||
344 | delete[] tmp; | ||
345 | |||
346 | return ar; | ||
347 | } | ||
348 | |||
349 | uint32_t Bu::Archive::getID( const void *ptr ) | ||
350 | { | ||
351 | if( hPtrID.has( (int)ptr ) ) | ||
352 | return hPtrID.get( (int)ptr ); | ||
353 | hPtrID.insert( (int)ptr, nNextID ); | ||
354 | return nNextID++; | ||
355 | } | ||
356 | |||
357 | void Bu::Archive::assocPtrID( void **ptr, uint32_t id ) | ||
358 | { | ||
359 | if( hPtrID.has( id ) ) | ||
360 | { | ||
361 | *ptr = (void *)hPtrID.get( id ); | ||
362 | return; | ||
363 | } | ||
364 | |||
365 | if( !hPtrDest.has( id ) ) | ||
366 | hPtrDest.insert( id, List<void **>() ); | ||
367 | |||
368 | hPtrDest[id].value().append( ptr ); | ||
369 | } | ||
370 | |||
371 | void Bu::Archive::readID( const void *ptr, uint32_t id ) | ||
372 | { | ||
373 | hPtrID.insert( id, (int)ptr ); | ||
374 | |||
375 | if( hPtrDest.has( id ) ) | ||
376 | { | ||
377 | Bu::List<void **> &l = hPtrDest.get( id ); | ||
378 | for( Bu::List<void **>::iterator i = l.begin(); i != l.end(); i++ ) | ||
379 | { | ||
380 | *(*i) = (void *)ptr; | ||
381 | } | ||
382 | |||
383 | hPtrDest.erase( id ); | ||
384 | } | ||
385 | } | ||
386 | |||