diff --git a/libtransmission/bencode-test.c b/libtransmission/bencode-test.c index 2ffd6b84d..1d7a779d1 100644 --- a/libtransmission/bencode-test.c +++ b/libtransmission/bencode-test.c @@ -237,8 +237,8 @@ testParse( void ) err = tr_bencParse( buf, buf + sizeof( buf ), &val, &end ); check( !err ); check( end == buf + strlen( (const char*)buf ) ); - check(( child = tr_bencListGetNthChild( &val, 0 ))); - check(( child2 = tr_bencListGetNthChild( child, 0 ))); + check(( child = tr_bencListChild( &val, 0 ))); + check(( child2 = tr_bencListChild( child, 0 ))); saved = tr_bencSave( &val, &len ); check( !strcmp( saved, "lld1:ai64e1:bi32eeee" ) ); tr_free( saved ); diff --git a/libtransmission/bencode.c b/libtransmission/bencode.c index 71e613849..9b8ff73d7 100644 --- a/libtransmission/bencode.c +++ b/libtransmission/bencode.c @@ -376,8 +376,14 @@ tr_bencDictFindFirst( tr_benc * val, ... ) return ret; } +int +tr_bencListSize( const tr_benc * list ) +{ + return tr_bencIsList( list ) ? list->val.l.count : 0; +} + tr_benc* -tr_bencListGetNthChild( tr_benc * val, int i ) +tr_bencListChild( tr_benc * val, int i ) { tr_benc * ret = NULL; if( tr_bencIsList( val ) && ( i >= 0 ) && ( i < val->val.l.count ) ) @@ -388,11 +394,18 @@ tr_bencListGetNthChild( tr_benc * val, int i ) int tr_bencGetInt ( const tr_benc * val, int64_t * setme ) { - int success = FALSE; - if( tr_bencIsInt( val )) { + const int success = tr_bencIsInt( val ); + if( success ) *setme = val->val.i ; - success = TRUE; - } + return success; +} + +int +tr_bencGetStr( const tr_benc * val, const char ** setme ) +{ + const int success = tr_bencIsString( val ); + if( success ) + *setme = val->val.s.s; return success; } @@ -406,6 +419,16 @@ tr_bencDictFindInt( tr_benc * dict, const char * key, int64_t * setme ) return found; } +int +tr_bencDictFindDouble( tr_benc * dict, const char * key, double * setme ) +{ + const char * str; + const int success = tr_bencDictFindStr( dict, key, &str ); + if( success ) + *setme = strtod( str, NULL ); + return success; +} + int tr_bencDictFindList( tr_benc * dict, const char * key, tr_benc ** setme ) { @@ -593,6 +616,13 @@ tr_bencDictAddStr( tr_benc * dict, const char * key, const char * val ) return child; } tr_benc* +tr_bencDictAddDouble( tr_benc * dict, const char * key, double d ) +{ + char buf[128]; + snprintf( buf, sizeof( buf ), "%f", d ); + return tr_bencDictAddStr( dict, key, buf ); +} +tr_benc* tr_bencDictAddList( tr_benc * dict, const char * key, int reserveCount ) { tr_benc * child = tr_bencDictAdd( dict, key ); diff --git a/libtransmission/bencode.h b/libtransmission/bencode.h index 1d9276cbb..7876d68d1 100644 --- a/libtransmission/bencode.h +++ b/libtransmission/bencode.h @@ -69,6 +69,7 @@ int tr_bencLoad( const void * buf, void tr_bencPrint( const tr_benc * ); void tr_bencFree( tr_benc * ); int tr_bencDictFindInt( tr_benc * dict, const char * key, int64_t * setme ); +int tr_bencDictFindDouble( tr_benc * dict, const char * key, double * setme ); int tr_bencDictFindStr( tr_benc * dict, const char * key, const char ** setme ); int tr_bencDictFindList( tr_benc * dict, const char * key, tr_benc ** setme ); int tr_bencDictFindDict( tr_benc * dict, const char * key, tr_benc ** setme ); @@ -103,6 +104,7 @@ tr_benc * tr_bencListAddList( tr_benc * list, int reserveCount ); tr_benc * tr_bencListAddDict( tr_benc * list, int reserveCount ); /* note: key must not be freed or modified while val is in use */ tr_benc * tr_bencDictAdd( tr_benc * dict, const char * key ); +tr_benc * tr_bencDictAddDouble( tr_benc * dict, const char * key, double d ); tr_benc * tr_bencDictAddInt( tr_benc * dict, const char * key, int64_t val ); tr_benc * tr_bencDictAddStr( tr_benc * dict, const char * key, const char * val ); tr_benc * tr_bencDictAddList( tr_benc * dict, const char * key, int reserveCount ); @@ -115,6 +117,7 @@ int tr_bencSaveFile( const char * filename, const tr_benc * ); int tr_bencLoadFile( const char * filename, tr_benc * ); int tr_bencGetInt( const tr_benc * val, int64_t * setme ); +int tr_bencGetStr( const tr_benc * val, const char ** setme ); int tr_bencIsType( const tr_benc *, int type ); #define tr_bencIsInt(b) (tr_bencIsType(b,TYPE_INT)) @@ -142,7 +145,8 @@ int tr_bencParseStr( const uint8_t * buf, *** **/ -tr_benc * tr_bencListGetNthChild( tr_benc * list, int n ); +int tr_bencListSize( const tr_benc * list ); +tr_benc * tr_bencListChild( tr_benc * list, int n ); #endif