aboutsummaryrefslogtreecommitdiff
path: root/src/stable/archive.cpp
blob: 31a4ba715e522d6e31e038f19c84659c2d6f95a5 (plain)
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
/*
 * Copyright (C) 2007-2019 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "bu/archive.h"
#include "bu/stream.h"
#include "bu/archival.h"

#include "bu/sio.h"

Bu::Archive::Archive( Stream &rStream, bool bLoading ) :
    bLoading( bLoading ),
    rStream( rStream ),
    nNextID( 1 )
{
}

Bu::Archive::~Archive()
{
}

void Bu::Archive::write( const void *pData, size_t nSize )
{
    if( nSize == 0 || pData == NULL )
        return;

    rStream.write( (const char *)pData, nSize );
}

void Bu::Archive::read( void *pData, size_t nSize )
{
    if( nSize == 0 || pData == NULL )
        return;

    if( (size_t)rStream.read( (char *)pData, nSize ) < nSize )
        throw Bu::ExceptionBase("Insufficient data to unarchive object.");
}

void Bu::Archive::close()
{
    rStream.close();
}

bool Bu::Archive::isLoading()
{
    return bLoading;
}

uint32_t Bu::Archive::getID( const void *ptr )
{
    if( hPtrID.has( (ptrdiff_t)ptr ) )
        return hPtrID.get( (ptrdiff_t)ptr );
    hPtrID.insert( (ptrdiff_t)ptr, nNextID );
    return nNextID++;
}

void Bu::Archive::assocPtrID( void **ptr, uint32_t id )
{
    if( hPtrID.has( id ) )
    {
        *ptr = (void *)hPtrID.get( id );
        return;
    }

    if( !hPtrDest.has( id ) )
        hPtrDest.insert( id, List<void **>() );
            
    hPtrDest[id].getValue().append( ptr );
}

void Bu::Archive::readID( const void *ptr, uint32_t id )
{
    hPtrID.insert( id, (ptrdiff_t)ptr );

    if( hPtrDest.has( id ) )
    {
        Bu::List<void **> &l = hPtrDest.get( id );
        for( Bu::List<void **>::iterator i = l.begin(); i != l.end(); i++ )
        {
            *(*i) = (void *)ptr;
        }

        hPtrDest.erase( id );
    }
}