/* * 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/archivebinary.h" #include "bu/stream.h" #include "bu/sio.h" Bu::ArchiveBinary::ArchiveBinary( Stream &rStream, bool bLoading ) : bLoading( bLoading ), rStream( rStream ), nNextID( 1 ) { } Bu::ArchiveBinary::~ArchiveBinary() { } void Bu::ArchiveBinary::write( const void *pData, size_t nSize ) { if( nSize == 0 || pData == NULL ) return; rStream.write( (const char *)pData, nSize ); } void Bu::ArchiveBinary::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::ArchiveBinary::close() { rStream.close(); } bool Bu::ArchiveBinary::isLoading() { return bLoading; } uint32_t Bu::ArchiveBinary::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::ArchiveBinary::assocPtrID( void **ptr, uint32_t id ) { if( hPtrID.has( id ) ) { *ptr = (void *)hPtrID.get( id ); return; } if( !hPtrDest.has( id ) ) hPtrDest.insert( id, List() ); hPtrDest[id].getValue().append( ptr ); } void Bu::ArchiveBinary::readID( const void *ptr, uint32_t id ) { hPtrID.insert( id, (ptrdiff_t)ptr ); if( hPtrDest.has( id ) ) { Bu::List &l = hPtrDest.get( id ); for( Bu::List::iterator i = l.begin(); i != l.end(); i++ ) { *(*i) = (void *)ptr; } hPtrDest.erase( id ); } } void Bu::ArchiveBinary::setProperty( const Bu::Blob &rKey, const Bu::Variant &rValue ) { hProps.insert( rKey, rValue ); } Bu::Variant Bu::ArchiveBinary::getProperty( const Bu::Blob &rKey ) const { return hProps.get( rKey ); }