www.pudn.com > BTSERVER.rar > atom.cpp
/***
*
* RainbowBT Beta 7.7 - A C++ BitTorrent Tracker
* Copyright (C) 2003 Trevor Hogan
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
***/
#include "bnbt.h"
#include "atom.h"
#include "util.h"
//
// CAtomInt
//
CAtomInt :: CAtomInt( )
{
setValue( 0 );
}
CAtomInt :: CAtomInt( int iInt )
{
setValue( iInt );
}
CAtomInt :: CAtomInt( const CAtomInt &c )
{
// copy constructor
setValue( c.getValue( ) );
}
CAtomInt :: ~CAtomInt( )
{
}
int CAtomInt :: EncodedLength( )
{
return toString( ).size( ) + 2;
}
int CAtomInt :: Length( )
{
return toString( ).size( );
}
string CAtomInt :: toString( )
{
char pBuf[32];
memset( pBuf, 0, sizeof( char ) * 32 );
sprintf( pBuf, "%d", getValue( ) );
return pBuf;
}
int CAtomInt :: getValue( ) const
{
return m_iInt;
}
void CAtomInt :: setValue( int iInt )
{
m_iInt = iInt;
}
//
// CAtomLong
//
CAtomLong :: CAtomLong( )
{
setValue( 0 );
}
CAtomLong :: CAtomLong( int64 iLong )
{
setValue( iLong );
}
CAtomLong :: CAtomLong( const CAtomLong &c )
{
// copy constructor
setValue( c.getValue( ) );
}
CAtomLong :: ~CAtomLong( )
{
}
int CAtomLong :: EncodedLength( )
{
return toString( ).size( ) + 2;
}
int CAtomLong :: Length( )
{
return toString( ).size( );
}
string CAtomLong :: toString( )
{
char pBuf[32];
memset( pBuf, 0, sizeof( char ) * 32 );
#if defined( WIN32 )
sprintf( pBuf, "%I64d", getValue( ) );
#elif defined( FreeBSD )
sprintf( pBuf, "%qd", getValue( ) );
#else
sprintf( pBuf, "%lld", getValue( ) );
#endif
return pBuf;
}
int64 CAtomLong :: getValue( ) const
{
return m_iLong;
}
void CAtomLong :: setValue( int64 iLong )
{
m_iLong = iLong;
}
//
// CAtomString
//
CAtomString :: CAtomString( )
{
}
CAtomString :: CAtomString( string strString )
{
setValue( strString );
}
CAtomString :: CAtomString( const CAtomString &c )
{
// copy constructor
setValue( c.getValue( ) );
}
CAtomString :: ~CAtomString( )
{
}
int CAtomString :: EncodedLength( )
{
int iSize = getValue( ).size( );
char pBuf[32];
memset( pBuf, 0, sizeof( char ) * 32 );
sprintf( pBuf, "%d", iSize );
return iSize + strlen( pBuf ) + 1;
}
int CAtomString :: Length( )
{
return getValue( ).size( );
}
string CAtomString :: toString( )
{
return getValue( );
}
string CAtomString :: getValue( ) const
{
return m_strString;
}
void CAtomString :: setValue( string strString )
{
m_strString = strString;
}
//
// CAtomList
//
CAtomList :: CAtomList( )
{
}
CAtomList :: CAtomList( vector vecList )
{
setValue( vecList );
}
CAtomList :: CAtomList( const CAtomList &c )
{
// copy constructor
vector vecList = c.getValue( );
for( vector :: iterator i = vecList.begin( ); i != vecList.end( ); i++ )
{
if( dynamic_cast( *i ) )
addItem( new CAtomInt( *dynamic_cast( *i ) ) );
else if( dynamic_cast( *i ) )
addItem( new CAtomLong( *dynamic_cast( *i ) ) );
else if( dynamic_cast( *i ) )
addItem( new CAtomString( *dynamic_cast( *i ) ) );
else if( dynamic_cast( *i ) )
addItem( new CAtomList( *dynamic_cast( *i ) ) );
else if( dynamic_cast( *i ) )
addItem( new CAtomDicti( *dynamic_cast( *i ) ) );
else
UTIL_LogPrint( "error copying list - found invalid atom, ignoring\n" );
}
}
CAtomList :: ~CAtomList( )
{
clear( );
}
int CAtomList :: EncodedLength( )
{
int iLen = 0;
for( vector :: iterator i = m_vecList.begin( ); i != m_vecList.end( ); i++ )
iLen += (*i)->EncodedLength( );
return iLen + 2;
}
int CAtomList :: Length( )
{
// nobody cares about you
return 0;
}
string CAtomList :: toString( )
{
return string( );
}
bool CAtomList :: isEmpty( )
{
return getValue( ).empty( );
}
void CAtomList :: clear( )
{
for( vector :: iterator i = m_vecList.begin( ); i != m_vecList.end( ); i++ )
delete *i;
m_vecList.clear( );
}
void CAtomList :: Randomize( )
{
random_shuffle( m_vecList.begin( ), m_vecList.end( ) );
}
vector CAtomList :: getValue( ) const
{
return m_vecList;
}
vector *CAtomList :: getValuePtr( )
{
return &m_vecList;
}
void CAtomList :: setValue( vector vecList )
{
m_vecList = vecList;
}
void CAtomList :: delItem( CAtom *atmItem )
{
for( vector :: iterator i = m_vecList.begin( ); i != m_vecList.end( ); i++ )
{
if( *i == atmItem )
{
delete *i;
m_vecList.erase( i );
return;
}
}
}
void CAtomList :: addItem( CAtom *atmItem )
{
m_vecList.push_back( atmItem );
}
//
// CAtomDicti
//
CAtomDicti :: CAtomDicti( )
{
}
CAtomDicti :: CAtomDicti( const CAtomDicti &c )
{
// copy constructor
map mapDicti = c.getValue( );
for( map :: iterator i = mapDicti.begin( ); i != mapDicti.end( ); i++ )
{
if( dynamic_cast( (*i).second ) )
setItem( (*i).first, new CAtomInt( *dynamic_cast( (*i).second ) ) );
else if( dynamic_cast( (*i).second ) )
setItem( (*i).first, new CAtomLong( *dynamic_cast( (*i).second ) ) );
else if( dynamic_cast( (*i).second ) )
setItem( (*i).first, new CAtomString( *dynamic_cast( (*i).second ) ) );
else if( dynamic_cast( (*i).second ) )
setItem( (*i).first, new CAtomList( *dynamic_cast( (*i).second ) ) );
else if( dynamic_cast( (*i).second ) )
setItem( (*i).first, new CAtomDicti( *dynamic_cast( (*i).second ) ) );
else
UTIL_LogPrint( "error copying dictionary - found invalid atom, ignoring\n" );
}
}
CAtomDicti :: ~CAtomDicti( )
{
clear( );
}
int CAtomDicti :: EncodedLength( )
{
int iLen = 0;
for( map :: iterator i = m_mapDicti.begin( ); i != m_mapDicti.end( ); i++ )
iLen += CAtomString( (*i).first ).EncodedLength( ) + (*i).second->EncodedLength( );
return iLen + 2;
}
int CAtomDicti :: Length( )
{
// nobody cares about you
return 0;
}
string CAtomDicti :: toString( )
{
return string( );
}
bool CAtomDicti :: isEmpty( )
{
return getValue( ).empty( );
}
void CAtomDicti :: clear( )
{
for( map :: iterator i = m_mapDicti.begin( ); i != m_mapDicti.end( ); i++ )
{
// UTIL_LogPrint( "deleting element %s\n", (*i).first.c_str( ) );
delete (*i).second;
}
m_mapDicti.clear( );
}
map CAtomDicti :: getValue( ) const
{
return m_mapDicti;
}
map *CAtomDicti :: getValuePtr( )
{
return &m_mapDicti;
}
void CAtomDicti :: delItem( string strKey )
{
map :: iterator i = m_mapDicti.find( strKey );
if( i != m_mapDicti.end( ) )
{
delete (*i).second;
m_mapDicti.erase( i );
}
}
CAtom *CAtomDicti :: getItem( string strKey )
{
map :: iterator i = m_mapDicti.find( strKey );
if( i == m_mapDicti.end( ) )
return NULL;
else
return (*i).second;
}
CAtom *CAtomDicti :: getItem( string strKey, CAtom *pReturn )
{
map :: iterator i = m_mapDicti.find( strKey );
if( i == m_mapDicti.end( ) )
return pReturn;
else
return (*i).second;
}
void CAtomDicti :: setItem( string strKey, CAtom *pValue )
{
map :: iterator i = m_mapDicti.find( strKey );
if( i == m_mapDicti.end( ) )
m_mapDicti.insert( pair( strKey, pValue ) );
else
{
delete (*i).second;
(*i).second = pValue;
}
}