Changeset 5772


Ignore:
Timestamp:
May 8, 2008, 3:25:21 AM (14 years ago)
Author:
charles
Message:

add a couple of benc utility functions

Location:
trunk/libtransmission
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/bencode-test.c

    r5608 r5772  
    238238    check( !err );
    239239    check( end == buf + strlen( (const char*)buf ) );
    240     check(( child = tr_bencListGetNthChild( &val, 0 )));
    241     check(( child2 = tr_bencListGetNthChild( child, 0 )));
     240    check(( child = tr_bencListChild( &val, 0 )));
     241    check(( child2 = tr_bencListChild( child, 0 )));
    242242    saved = tr_bencSave( &val, &len );
    243243    check( !strcmp( saved, "lld1:ai64e1:bi32eeee" ) );
  • trunk/libtransmission/bencode.c

    r5667 r5772  
    377377}
    378378
     379int
     380tr_bencListSize( const tr_benc * list )
     381{
     382    return tr_bencIsList( list ) ? list->val.l.count : 0;
     383}
     384
    379385tr_benc*
    380 tr_bencListGetNthChild( tr_benc * val, int i )
     386tr_bencListChild( tr_benc * val, int i )
    381387{
    382388    tr_benc * ret = NULL;
     
    389395tr_bencGetInt ( const tr_benc * val, int64_t * setme )
    390396{
    391     int success = FALSE;
    392     if( tr_bencIsInt( val )) {
     397    const int success = tr_bencIsInt( val );
     398    if( success )
    393399        *setme = val->val.i ;
    394         success = TRUE;
    395     }
     400    return success;
     401}
     402
     403int
     404tr_bencGetStr( const tr_benc * val, const char ** setme )
     405{
     406    const int success = tr_bencIsString( val );
     407    if( success )
     408        *setme = val->val.s.s;
    396409    return success;
    397410}
     
    405418        found = tr_bencGetInt( child, setme );
    406419    return found;
     420}
     421
     422int
     423tr_bencDictFindDouble( tr_benc * dict, const char * key, double * setme )
     424{
     425    const char * str;
     426    const int success = tr_bencDictFindStr( dict, key, &str );
     427    if( success )
     428        *setme = strtod( str, NULL );
     429    return success;
    407430}
    408431
     
    592615    tr_bencInitStrDup( child, val );
    593616    return child;
     617}
     618tr_benc*
     619tr_bencDictAddDouble( tr_benc * dict, const char * key, double d )
     620{
     621    char buf[128];
     622    snprintf( buf, sizeof( buf ), "%f", d );
     623    return tr_bencDictAddStr( dict, key, buf );
    594624}
    595625tr_benc*
  • trunk/libtransmission/bencode.h

    r5667 r5772  
    7070void      tr_bencFree( tr_benc * );
    7171int       tr_bencDictFindInt( tr_benc * dict, const char * key, int64_t * setme );
     72int       tr_bencDictFindDouble( tr_benc * dict, const char * key, double * setme );
    7273int       tr_bencDictFindStr( tr_benc * dict, const char * key, const char ** setme );
    7374int       tr_bencDictFindList( tr_benc * dict, const char * key, tr_benc ** setme );
     
    104105/* note: key must not be freed or modified while val is in use */
    105106tr_benc    * tr_bencDictAdd( tr_benc * dict, const char * key );
     107tr_benc    * tr_bencDictAddDouble( tr_benc * dict, const char * key, double d );
    106108tr_benc    * tr_bencDictAddInt( tr_benc * dict, const char * key, int64_t val );
    107109tr_benc    * tr_bencDictAddStr( tr_benc * dict, const char * key, const char * val );
     
    116118
    117119int tr_bencGetInt( const tr_benc * val, int64_t * setme );
     120int tr_bencGetStr( const tr_benc * val, const char ** setme );
    118121
    119122int tr_bencIsType( const tr_benc *, int type );
     
    143146**/
    144147
    145 tr_benc * tr_bencListGetNthChild( tr_benc * list, int n );
     148int       tr_bencListSize( const tr_benc * list );
     149tr_benc * tr_bencListChild( tr_benc * list, int n );
    146150
    147151
Note: See TracChangeset for help on using the changeset viewer.