Changeset 8112


Ignore:
Timestamp:
Mar 30, 2009, 1:46:48 AM (13 years ago)
Author:
charles
Message:

(trunk libT) add tr_bencGetBool() and tr_bencGetDouble()

Location:
trunk/libtransmission
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/bencode.c

    r8108 r8112  
    415415               int64_t *       setme )
    416416{
    417     const int success = tr_bencIsInt( val );
     417    const tr_bool success = tr_bencIsInt( val );
    418418
    419419    if( success && setme )
     
    431431    if( success )
    432432        *setme = val->val.s.s;
     433
     434    return success;
     435}
     436
     437tr_bool
     438tr_bencGetBool( const tr_benc * val, tr_bool * setme )
     439{
     440    tr_bool success = FALSE;
     441
     442    if( !success && tr_bencIsInt( val ) )
     443        if(( success = ( val->val.i==0 || val->val.i==1 ) ))
     444            *setme = val->val.i!=0;
     445
     446    if( !success && tr_bencIsString( val ) )
     447        if(( success = ( !strcmp(val->val.s.s,"true") || !strcmp(val->val.s.s,"false"))))
     448            *setme = !strcmp(val->val.s.s,"true");
     449
     450    return success;
     451}
     452
     453tr_bool
     454tr_bencGetReal( const tr_benc * val, double * setme )
     455{
     456    tr_bool success = FALSE;
     457
     458    if( !success && tr_bencIsString(val) )
     459    {
     460        char * endptr;
     461        const double d = strtod( val->val.s.s, &endptr );
     462        if(( success = ( val->val.s.s != endptr ) && !*endptr ))
     463            *setme = d;
     464    }
     465
     466    if( !success && tr_bencIsInt(val) )
     467    {
     468        success = TRUE;
     469        *setme = val->val.i;
     470    }
     471
    433472    return success;
    434473}
     
    437476tr_bencDictFindInt( tr_benc * dict, const char * key, int64_t * setme )
    438477{
    439     tr_bool found = FALSE;
    440     tr_benc * child = tr_bencDictFindType( dict, key, TYPE_INT );
    441 
    442     if( child )
    443         found = tr_bencGetInt( child, setme );
    444 
    445     return found;
     478    return tr_bencGetInt( tr_bencDictFind( dict, key ), setme );
    446479}
    447480
     
    449482tr_bencDictFindBool( tr_benc * dict, const char * key, tr_bool * setme )
    450483{
    451     int64_t i = -1;
    452     const tr_bool found = tr_bencDictFindInt( dict, key, &i );
    453     if( found ) {
    454         assert( i==0 || i==1 );
    455         *setme = i!=0;
    456     }
    457     return found;
     484    return tr_bencGetBool( tr_bencDictFind( dict, key ), setme );
    458485}
    459486
     
    461488tr_bencDictFindReal( tr_benc * dict, const char * key, double * setme )
    462489{
    463     const char * str;
    464     const tr_bool success = tr_bencDictFindStr( dict, key, &str );
    465 
    466     if( success && setme )
    467         *setme = strtod( str, NULL );
    468 
    469     return success;
     490    return tr_bencGetReal( tr_bencDictFind( dict, key ), setme );
    470491}
    471492
  • trunk/libtransmission/bencode.h

    r8108 r8112  
    164164
    165165tr_bool   tr_bencGetInt( const tr_benc * val, int64_t * setme );
    166 
    167166tr_bool   tr_bencGetStr( const tr_benc * val, const char ** setme );
     167tr_bool   tr_bencGetBool( const tr_benc * val, tr_bool * setme );
     168tr_bool   tr_bencGetReal( const tr_benc * val, double * setme );
    168169
    169170static TR_INLINE tr_bool tr_bencIsType  ( const tr_benc * b, int type ) { return ( b != NULL ) && ( b->type == type ); }
Note: See TracChangeset for help on using the changeset viewer.