Changeset 8183


Ignore:
Timestamp:
Apr 9, 2009, 12:26:53 AM (13 years ago)
Author:
charles
Message:

more benc code simplification

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/bencode.c

    r8181 r8183  
    387387}
    388388
    389 static tr_benc*
    390 tr_bencDictFindType( tr_benc *    val,
    391                      const char * key,
    392                      int          type )
    393 {
    394     tr_benc * ret = tr_bencDictFind( val, key );
    395 
    396     return ( ret && ( ret->type == type ) ) ? ret : NULL;
     389static tr_bool
     390tr_bencDictFindType( tr_benc * dict, const char * key, int type, tr_benc ** setme )
     391{
     392    return tr_bencIsType( *setme = tr_bencDictFind( dict, key ), type );
    397393}
    398394
     
    521517
    522518tr_bool
     519tr_bencDictFindStr( tr_benc *  dict, const char *  key, const char ** setme )
     520{
     521    return tr_bencGetStr( tr_bencDictFind( dict, key ), setme );
     522}
     523
     524tr_bool
    523525tr_bencDictFindList( tr_benc * dict, const char * key, tr_benc ** setme )
    524526{
    525     tr_bool found = FALSE;
    526     tr_benc * child = tr_bencDictFindType( dict, key, TR_TYPE_LIST );
    527 
    528     if( child )
    529     {
    530         if( setme != NULL )
    531             *setme = child;
    532         found = TRUE;
    533     }
    534 
    535     return found;
     527    return tr_bencDictFindType( dict, key, TR_TYPE_LIST, setme );
    536528}
    537529
     
    539531tr_bencDictFindDict( tr_benc * dict, const char * key, tr_benc ** setme )
    540532{
    541     tr_bool found = FALSE;
    542     tr_benc * child = tr_bencDictFindType( dict, key, TR_TYPE_DICT );
    543 
    544     if( child )
    545     {
    546         if( setme != NULL )
    547             *setme = child;
    548         found = TRUE;
    549     }
    550 
    551     return found;
    552 }
    553 
    554 tr_bool
    555 tr_bencDictFindStr( tr_benc *  dict, const char *  key, const char ** setme )
    556 {
    557     tr_bool found = FALSE;
    558     tr_benc * child = tr_bencDictFindType( dict, key, TR_TYPE_STR );
    559 
    560     if( child )
    561     {
    562         if( setme )
    563             *setme = child->val.s.s;
    564         found = TRUE;
    565     }
    566 
    567     return found;
     533    return tr_bencDictFindType( dict, key, TR_TYPE_DICT, setme );
    568534}
    569535
     
    574540                    size_t          * setme_len )
    575541{
    576     tr_bool found = FALSE;
    577     tr_benc * child = tr_bencDictFindType( dict, key, TR_TYPE_STR );
    578 
    579     if( child )
    580     {
     542    tr_benc * child;
     543    const tr_bool found = tr_bencDictFindType( dict, key, TR_TYPE_STR, &child );
     544
     545    if( found ) {
    581546        *setme_raw = (uint8_t*) child->val.s.s;
    582547        *setme_len = child->val.s.i;
    583         found = TRUE;
    584548    }
    585549
     
    13501314        switch( *it )
    13511315        {
    1352             case '"': case '\\': case '/': case '\b':
    1353             case '\f': case '\n': case '\r': case '\t': {
    1354                 char buf[2] = { '\\', *it };
    1355                 evbuffer_add( data->out, buf, 2 );
    1356                 break;
    1357             }
     1316            case '/': evbuffer_add( data->out, "\\/", 2 ); break;
     1317            case '\b': evbuffer_add( data->out, "\\b", 2 ); break;
     1318            case '\f': evbuffer_add( data->out, "\\f", 2 ); break;
     1319            case '\n': evbuffer_add( data->out, "\\n", 2 ); break;
     1320            case '\r': evbuffer_add( data->out, "\\r", 2 ); break;
     1321            case '\t': evbuffer_add( data->out, "\\t", 2 ); break;
     1322            case '"': evbuffer_add( data->out, "\\\"", 2 ); break;
     1323            case '\\': evbuffer_add( data->out, "\\\\", 2 ); break;
    13581324
    13591325            default:
Note: See TracChangeset for help on using the changeset viewer.