Ignore:
Timestamp:
Apr 17, 2008, 8:57:58 PM (14 years ago)
Author:
charles
Message:

benc API cleanup

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/daemon/torrents.c

    r5579 r5639  
    659659loadstate( void )
    660660{
     661    const char * str;
     662    int64_t      tmp;
    661663    uint8_t   *  buf;
    662664    size_t       len;
    663     benc_val_t   top, * num, * str, * list;
     665    benc_val_t   top, * list;
    664666    int          ii;
    665667    struct tor * tor;
    666     const char * dir;
    667668
    668669    buf = readfile( gl_state, &len );
     
    680681    free( buf );
    681682
    682     num = tr_bencDictFind( &top, "autostart" );
    683     if( tr_bencIsInt( num ) )
    684         gl_autostart = ( num->val.i ? 1 : 0 );
    685 
    686     num = tr_bencDictFind( &top, "port" );
    687     if( tr_bencIsInt( num ) && 0 < num->val.i && 0xffff > num->val.i )
    688     {
    689         gl_port = num->val.i;
    690     }
     683    if( tr_bencDictFindInt( &top, "autostart", &tmp ) )
     684        gl_autostart = tmp != 0;
     685
     686    if( tr_bencDictFindInt( &top, "port", &tmp ) && ( 0 < tmp ) && ( tmp <= 0xffff ) )
     687        gl_port = tmp;
    691688    tr_setBindPort( gl_handle, gl_port );
    692689
    693     num = tr_bencDictFind( &top, "default-pex" );
    694     if( tr_bencIsInt( num ) )
    695         gl_pex = ( num->val.i ? 1 : 0 );
    696 
    697     num = tr_bencDictFind( &top, "port-mapping" );
    698     if( tr_bencIsInt( num ) )
    699         gl_mapping = ( num->val.i ? 1 : 0 );
     690    if( tr_bencDictFindInt( &top, "default-pex", &tmp ) )
     691        gl_pex = tmp != 0;
     692
     693    if( tr_bencDictFindInt( &top, "port-mapping", &tmp ) )
     694        gl_mapping = tmp != 0;
    700695    tr_natTraversalEnable( gl_handle, gl_mapping );
    701696
    702     num = tr_bencDictFind( &top, "upload-limit" );
    703     if( tr_bencIsInt( num ) )
    704         gl_uplimit = num->val.i;
     697    if( tr_bencDictFindInt( &top, "upload-limit", &tmp ) )
     698        gl_uplimit = tmp;
    705699    tr_setGlobalSpeedLimit( gl_handle, TR_UP, gl_uplimit );
    706700    tr_setUseGlobalSpeedLimit( gl_handle, TR_UP, gl_uplimit > 0 );
    707701
    708     num = tr_bencDictFind( &top, "download-limit" );
    709     if( tr_bencIsInt( num ) )
    710         gl_downlimit = num->val.i;
     702    if( tr_bencDictFindInt( &top, "download-limit", &tmp ) )
     703        gl_downlimit = tmp;
    711704    tr_setGlobalSpeedLimit( gl_handle, TR_DOWN, gl_downlimit );
    712705    tr_setUseGlobalSpeedLimit( gl_handle, TR_DOWN, gl_downlimit > 0 );
    713706
    714     str = tr_bencDictFind( &top, "default-directory" );
    715     if( tr_bencIsString( str ) )
    716         strlcpy( gl_dir, str->val.s.s, sizeof gl_dir );
    717 
    718     str = tr_bencDictFind( &top, "encryption-mode" );
    719     if( tr_bencIsString( str ) )
    720     {
    721         if(!strcasecmp(str->val.s.s, "preferred"))
     707    if( tr_bencDictFindStr( &top, "default-directory", &str ) )
     708        strlcpy( gl_dir, str, sizeof gl_dir );
     709
     710    if( tr_bencDictFindStr( &top, "encryption-mode", &str ) )
     711    {
     712        if(!strcasecmp( str, "preferred"))
    722713            gl_crypto = TR_ENCRYPTION_PREFERRED;
    723         else if(!strcasecmp(str->val.s.s, "required"))
     714        else if(!strcasecmp( str, "required"))
    724715            gl_crypto = TR_ENCRYPTION_REQUIRED;
    725716    }
     
    733724    for( ii = 0; ii < list->val.l.count; ii++ )
    734725    {
     726        const char * directory = NULL;
     727        const char * hash = NULL;
     728
    735729        tr_benc * dict = &list->val.l.vals[ii];
    736730        if( !tr_bencIsDict( dict ) )
    737731            continue;
    738732
    739         str = tr_bencDictFind( dict, "directory" );
    740         dir = tr_bencIsString( str ) ? str->val.s.s : NULL;
    741 
    742         str = tr_bencDictFind( dict, "hash" );
    743         if( !tr_bencIsString( str ) || 2 * SHA_DIGEST_LENGTH != str->val.s.i )
     733        if( !tr_bencDictFindStr( dict, "directory", &directory ) ||
     734            !tr_bencDictFindStr( dict, "hash", &hash ) )
    744735            continue;
    745736
    746         tor = opentor( NULL, str->val.s.s, NULL, 0, dir );
     737        tor = opentor( NULL, hash, NULL, 0, directory );
    747738        if( !tor )
    748739            continue;
    749740
    750         num = tr_bencDictFind( dict, "pex" );
    751         if( tr_bencIsInt( num ) )
     741        if( tr_bencDictFindInt( dict, "pex", &tmp ) )
    752742            fprintf( stderr, "warning: obsolete command 'pex'\n" );
    753743
    754         num = tr_bencDictFind( dict, "paused" );
    755         if( tr_bencIsInt( num ) && !num->val.i )
     744        if( tr_bencDictFindInt( dict, "paused", &tmp ) && !tmp )
    756745            tr_torrentStart( tor->tor );
    757746    }
Note: See TracChangeset for help on using the changeset viewer.