1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-02-03 21:12:05 +00:00

add a couple of benc utility functions

This commit is contained in:
Charles Kerr 2008-05-08 03:25:21 +00:00
parent 2447428044
commit 73d77f8418
3 changed files with 42 additions and 8 deletions

View file

@ -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 );

View file

@ -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 );

View file

@ -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