summaryrefslogtreecommitdiff
path: root/src/stable/archive.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/stable/archive.cpp')
-rw-r--r--src/stable/archive.cpp89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/stable/archive.cpp b/src/stable/archive.cpp
new file mode 100644
index 0000000..d300a87
--- /dev/null
+++ b/src/stable/archive.cpp
@@ -0,0 +1,89 @@
1/*
2 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#include "bu/archive.h"
9#include "bu/stream.h"
10#include "bu/archival.h"
11
12#include "bu/sio.h"
13
14Bu::Archive::Archive( Stream &rStream, bool bLoading ) :
15 bLoading( bLoading ),
16 rStream( rStream ),
17 nNextID( 1 )
18{
19}
20
21Bu::Archive::~Archive()
22{
23}
24
25void Bu::Archive::write( const void *pData, size_t nSize )
26{
27 if( nSize == 0 || pData == NULL )
28 return;
29
30 rStream.write( (const char *)pData, nSize );
31}
32
33void Bu::Archive::read( void *pData, size_t nSize )
34{
35 if( nSize == 0 || pData == NULL )
36 return;
37
38 if( rStream.read( (char *)pData, nSize ) < nSize )
39 throw Bu::ExceptionBase("Insufficient data to unarchive object.");
40}
41
42void Bu::Archive::close()
43{
44 rStream.close();
45}
46
47bool Bu::Archive::isLoading()
48{
49 return bLoading;
50}
51
52uint32_t Bu::Archive::getID( const void *ptr )
53{
54 if( hPtrID.has( (ptrdiff_t)ptr ) )
55 return hPtrID.get( (ptrdiff_t)ptr );
56 hPtrID.insert( (ptrdiff_t)ptr, nNextID );
57 return nNextID++;
58}
59
60void Bu::Archive::assocPtrID( void **ptr, uint32_t id )
61{
62 if( hPtrID.has( id ) )
63 {
64 *ptr = (void *)hPtrID.get( id );
65 return;
66 }
67
68 if( !hPtrDest.has( id ) )
69 hPtrDest.insert( id, List<void **>() );
70
71 hPtrDest[id].getValue().append( ptr );
72}
73
74void Bu::Archive::readID( const void *ptr, uint32_t id )
75{
76 hPtrID.insert( id, (ptrdiff_t)ptr );
77
78 if( hPtrDest.has( id ) )
79 {
80 Bu::List<void **> &l = hPtrDest.get( id );
81 for( Bu::List<void **>::iterator i = l.begin(); i != l.end(); i++ )
82 {
83 *(*i) = (void *)ptr;
84 }
85
86 hPtrDest.erase( id );
87 }
88}
89