Changeset 6612


Ignore:
Timestamp:
Aug 21, 2008, 2:57:59 PM (13 years ago)
Author:
charles
Message:

bencode cleanup: remove unused functions and unnecessary #includes

Location:
trunk
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/gtk/file-list.c

    r6369 r6612  
    2626#include <stdio.h>
    2727#include <stdlib.h>
     28#include <string.h>
    2829#include <glib/gi18n.h>
    2930#include <gtk/gtk.h>
  • trunk/libtransmission/bencode-test.c

    r6591 r6612  
    11#include <ctype.h>
    22#include <stdio.h>
     3#include <string.h>
    34#include "transmission.h"
    45#include "bencode.h"
  • trunk/libtransmission/bencode.c

    r6600 r6612  
    1414#include <ctype.h> /* isdigit, isprint, isspace */
    1515#include <errno.h>
    16 #include <stdarg.h>
    1716#include <stdio.h>
    1817#include <stdlib.h>
     18#include <string.h>
    1919
    2020#include <event.h> /* evbuffer */
     
    294294            }
    295295
    296             tr_bencInitStr( node, str, str_len, 0 );
     296            tr_bencInitStr( node, str, str_len );
    297297            buf = end;
    298298
     
    388388}
    389389
    390 tr_benc *
    391 tr_bencDictFindFirst( tr_benc * val, ... )
    392 {
    393     const char * key;
    394     tr_benc * ret;
    395     va_list      ap;
    396 
    397     ret = NULL;
    398     va_start( ap, val );
    399     while(( key = va_arg( ap, const char * )))
    400         if(( ret = tr_bencDictFind( val, key )))
    401             break;
    402     va_end( ap );
    403 
    404     return ret;
    405 }
    406 
    407390int
    408391tr_bencListSize( const tr_benc * list )
     
    516499
    517500void
    518 _tr_bencInitStr( tr_benc * val, char * str, int len, int nofree )
    519 {
    520     tr_bencInit( val, TYPE_STR );
    521     val->val.s.s      = str;
    522     val->val.s.nofree = nofree;
    523     if( 0 >= len )
    524     {
    525         len = ( NULL == str ? 0 : strlen( str ) );
    526     }
    527     val->val.s.i = len;
    528 }
    529 
    530 void
    531501tr_bencInitRaw( tr_benc * val, const void * src, size_t byteCount )
    532502{
     
    534504    val->val.s.i = byteCount;
    535505    val->val.s.s = tr_memdup( src, byteCount );
    536     val->val.s.nofree = 0;
    537 }
    538 
    539 int
    540 tr_bencInitStrDup( tr_benc * val, const char * str )
    541 {
    542     char * newStr = tr_strdup( str );
    543     if( newStr == NULL )
    544         return 1;
    545 
    546     _tr_bencInitStr( val, newStr, 0, 0 );
    547     return 0;
     506}
     507
     508void
     509tr_bencInitStr( tr_benc * val, const void * str, int len )
     510{
     511    tr_bencInit( val, TYPE_STR );
     512    val->val.s.s = tr_strndup( str, len );
     513    val->val.s.i = val->val.s.s ? strlen( val->val.s.s ) : 0;
    548514}
    549515
     
    612578{
    613579    tr_benc * node = tr_bencListAdd( list );
    614     tr_bencInitStrDup( node, val );
     580    tr_bencInitStr( node, val, -1 );
    615581    return node;
    616582}
     
    641607
    642608    keyval = dict->val.l.vals + dict->val.l.count++;
    643     tr_bencInitStrDup( keyval, key );
     609    tr_bencInitStr( keyval, key, -1 );
    644610
    645611    itemval = dict->val.l.vals + dict->val.l.count++;
     
    659625{
    660626    tr_benc * child = tr_bencDictAdd( dict, key );
    661     tr_bencInitStrDup( child, val );
     627    tr_bencInitStr( child, val, -1 );
    662628    return child;
    663629}
     
    963929freeStringFunc( const tr_benc * val, void * freeme )
    964930{
    965     if( !val->val.s.nofree )
    966         tr_ptrArrayAppend( freeme, val->val.s.s );
     931    tr_ptrArrayAppend( freeme, val->val.s.s );
    967932}
    968933static void
     
    994959***/
    995960
     961#if 0
    996962struct WalkPrint
    997963{
     
    10701036    bencWalk( val, &walkFuncs, &walkPrint );
    10711037}
     1038#endif
    10721039
    10731040/***
     
    13021269
    13031270int
     1271tr_bencSaveJSONFile( const char * filename, const tr_benc * b )
     1272{
     1273    int len;
     1274    char * content = tr_bencSaveAsJSON( b, &len );
     1275    const int err = saveFile( filename, content, len );
     1276    tr_free( content );
     1277    return err;
     1278}
     1279
     1280/***
     1281****
     1282***/
     1283
     1284int
    13041285tr_bencLoadFile( const char * filename, tr_benc * b )
    13051286{
     
    13141295
    13151296int
    1316 tr_bencSaveJSONFile( const char * filename, const tr_benc * b )
    1317 {
    1318     int len;
    1319     char * content = tr_bencSaveAsJSON( b, &len );
    1320     const int err = saveFile( filename, content, len );
    1321     tr_free( content );
    1322     return err;
    1323 }
    1324 
    1325 int
    13261297tr_bencLoadJSONFile( const char * filename, tr_benc * b )
    13271298{
  • trunk/libtransmission/bencode.h

    r6600 r6612  
    1 /******************************************************************************
     1/*
     2 * This file Copyright (C) 2008 Charles Kerr <charles@rebelbase.com>
     3 *
     4 * This file is licensed by the GPL version 2.  Works owned by the
     5 * Transmission project are granted a special exemption to clause 2(b)
     6 * so that the bulk of its code can remain under the MIT license.
     7 * This exemption does not extend to derived works not owned by
     8 * the Transmission project.
     9 *
    210 * $Id$
    3  *
    4  * Copyright (c) 2005-2008 Transmission authors and contributors
    5  *
    6  * Permission is hereby granted, free of charge, to any person obtaining a
    7  * copy of this software and associated documentation files (the "Software"),
    8  * to deal in the Software without restriction, including without limitation
    9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
    10  * and/or sell copies of the Software, and to permit persons to whom the
    11  * Software is furnished to do so, subject to the following conditions:
    12  *
    13  * The above copyright notice and this permission notice shall be included in
    14  * all copies or substantial portions of the Software.
    15  *
    16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    22  * DEALINGS IN THE SOFTWARE.
    23  *****************************************************************************/
     11 */
    2412
    2513#ifndef TR_BENCODE_H
     
    2715
    2816#include <inttypes.h> /* for int64_t */
    29 #include <string.h> /* for memset */
    3017
     18enum
     19{
     20    TYPE_INT  = 1,
     21    TYPE_STR  = 2,
     22    TYPE_LIST = 4,
     23    TYPE_DICT = 8
     24};
     25   
    3126typedef struct tr_benc
    3227{
    33 #define TYPE_INT  1
    34 #define TYPE_STR  2
    35 #define TYPE_LIST 4
    36 #define TYPE_DICT 8
    3728    char   type;
    3829    union
     
    4233        {
    4334            int    i;
    44             int    nofree;
    4535            char * s;
    4636        } s;
     
    5444} tr_benc;
    5545
    56 /* backwards compatability */
    57 typedef tr_benc benc_val_t;
     46/***
     47****
     48***/
    5849
    5950int tr_bencParse( const void      * buf,
     
    6758                 char       ** setme_end );
    6859
    69 void      tr_bencPrint( const tr_benc * );
    70 void      tr_bencFree( tr_benc * );
    71 int       tr_bencDictFindInt( tr_benc * dict, const char * key, int64_t * setme );
    72 int       tr_bencDictFindDouble( tr_benc * dict, const char * key, double * setme );
    73 int       tr_bencDictFindStr( tr_benc * dict, const char * key, const char ** setme );
    74 int       tr_bencDictFindRaw( tr_benc * dict, const char * key, const uint8_t ** setme_raw,
    75                                                                 size_t * setme_len );
    76 int       tr_bencDictFindList( tr_benc * dict, const char * key, tr_benc ** setme );
    77 int       tr_bencDictFindDict( tr_benc * dict, const char * key, tr_benc ** setme );
    78 tr_benc * tr_bencDictFind( tr_benc * dict, const char * key );
    79 tr_benc * tr_bencDictFindFirst( tr_benc * dict, ... );
     60int    tr_bencLoadFile     ( const char * filename, tr_benc * );
     61int    tr_bencLoadJSONFile ( const char * filename, tr_benc * );
    8062
    81 /* convenience functions for building tr_benc    structures */
     63#if 0
     64void  tr_bencPrint( const tr_benc * );
     65#endif
    8266
    83 #define tr_bencInitStr( a, b, c, d ) \
    84     _tr_bencInitStr( (a), ( char * )(b), (c), (d) )
    85 void   _tr_bencInitStr( tr_benc * val, char * str, int len, int nofree );
    86 int    tr_bencInitStrDup( tr_benc * val, const char * str );
    87 void   tr_bencInitRaw( tr_benc * val, const void * src, size_t byteCount );
    88 void   tr_bencInitInt( tr_benc * val, int64_t num );
    89 int   tr_bencInitDict( tr_benc * val, int reserveCount );
    90 int   tr_bencInitList( tr_benc * val, int reserveCount );
    91 int   tr_bencListReserve( tr_benc * list, int count );
    92 /* note that for one key-value pair, count should be 1, not 2 */
    93 int   tr_bencDictReserve( tr_benc * dict, int count );
    94 tr_benc    * tr_bencListAdd( tr_benc  * list );
    95 tr_benc    * tr_bencListAddInt( tr_benc  * list, int64_t val );
    96 tr_benc    * tr_bencListAddStr( tr_benc  * list, const char * val );
    97 tr_benc    * tr_bencListAddList( tr_benc  * list, int reserveCount );
    98 tr_benc    * tr_bencListAddDict( tr_benc  * list, int reserveCount );
    99 tr_benc    * tr_bencDictAdd( tr_benc * dict, const char * key );
    100 tr_benc    * tr_bencDictAddDouble( tr_benc * dict, const char * key, double d );
    101 tr_benc    * tr_bencDictAddInt( tr_benc * dict, const char * key, int64_t val );
    102 tr_benc    * tr_bencDictAddStr( tr_benc * dict, const char * key, const char * val );
    103 tr_benc    * tr_bencDictAddList( tr_benc * dict, const char * key, int reserveCount );
    104 tr_benc    * tr_bencDictAddDict( tr_benc * dict, const char * key, int reserveCount );
    105 tr_benc    * tr_bencDictAddRaw( tr_benc * dict, const char * key, const void *, size_t len );
    106 int          tr_bencDictRemove( tr_benc * dict, const char * key );
     67void  tr_bencFree( tr_benc * );
    10768
    10869char*  tr_bencSave         ( const tr_benc * val, int * len );
     
    11071int    tr_bencSaveFile     ( const char * filename, const tr_benc * );
    11172int    tr_bencSaveJSONFile ( const char * filename, const tr_benc * );
    112 int    tr_bencLoadFile     ( const char * filename, tr_benc * );
    113 int    tr_bencLoadJSONFile ( const char * filename, tr_benc * );
     73
     74void  tr_bencInitStr( tr_benc *, const void * str, int str_len );
     75void  tr_bencInitRaw( tr_benc *, const void * raw, size_t raw_len );
     76void  tr_bencInitInt( tr_benc *, int64_t num );
     77int   tr_bencInitDict( tr_benc *, int reserveCount );
     78int   tr_bencInitList( tr_benc *, int reserveCount );
     79
     80/***
     81****
     82***/
     83
     84int tr_bencListReserve( tr_benc *, int count );
     85tr_benc * tr_bencListAdd( tr_benc * );
     86tr_benc * tr_bencListAddInt( tr_benc *, int64_t val );
     87tr_benc * tr_bencListAddStr( tr_benc *, const char * val );
     88tr_benc * tr_bencListAddList( tr_benc *, int reserveCount );
     89tr_benc * tr_bencListAddDict( tr_benc *, int reserveCount );
     90
     91int tr_bencListSize( const tr_benc * list );
     92tr_benc * tr_bencListChild( tr_benc * list, int n );
     93
     94/***
     95****
     96***/
     97
     98int tr_bencDictReserve( tr_benc *, int count );
     99int tr_bencDictRemove( tr_benc *, const char * key );
     100tr_benc * tr_bencDictAdd( tr_benc *, const char * key );
     101tr_benc * tr_bencDictAddDouble( tr_benc *, const char * key, double d );
     102tr_benc * tr_bencDictAddInt( tr_benc *, const char * key, int64_t val );
     103tr_benc * tr_bencDictAddStr( tr_benc *, const char * key, const char * val );
     104tr_benc * tr_bencDictAddList( tr_benc *, const char * key, int reserveCount );
     105tr_benc * tr_bencDictAddDict( tr_benc *, const char * key, int reserveCount );
     106tr_benc * tr_bencDictAddRaw( tr_benc *, const char * key,
     107                             const void *, size_t len );
     108
     109tr_benc* tr_bencDictFind( tr_benc *, const char * key );
     110int tr_bencDictFindList( tr_benc *, const char * key, tr_benc ** setme );
     111int tr_bencDictFindDict( tr_benc *, const char * key, tr_benc ** setme );
     112int tr_bencDictFindInt( tr_benc *, const char * key, int64_t * setme );
     113int tr_bencDictFindDouble( tr_benc *, const char * key, double * setme );
     114int tr_bencDictFindStr( tr_benc *, const char * key, const char ** setme );
     115int tr_bencDictFindRaw( tr_benc *, const char * key,
     116                          const uint8_t ** setme_raw, size_t * setme_len );
     117
     118/***
     119****
     120***/
    114121
    115122int tr_bencGetInt( const tr_benc * val, int64_t * setme );
    116123int tr_bencGetStr( const tr_benc * val, const char ** setme );
    117 
    118124int tr_bencIsType( const tr_benc *, int type );
    119 #define tr_bencIsInt(b) (tr_bencIsType(b,TYPE_INT))
    120 #define tr_bencIsDict(b) (tr_bencIsType(b,TYPE_DICT))
    121 #define tr_bencIsList(b) (tr_bencIsType(b,TYPE_LIST))
    122 #define tr_bencIsString(b) (tr_bencIsType(b,TYPE_STR))
     125#define tr_bencIsInt( b )    tr_bencIsType( ( b ), TYPE_INT )
     126#define tr_bencIsDict( b )   tr_bencIsType( ( b ), TYPE_DICT )
     127#define tr_bencIsList( b )   tr_bencIsType( ( b ), TYPE_LIST )
     128#define tr_bencIsString( b ) tr_bencIsType( ( b ), TYPE_STR )
    123129
    124130/**
     
    138144                      size_t         * setme_strlen );
    139145
    140 /**
    141 ***
    142 **/
    143 
    144 int       tr_bencListSize( const tr_benc * list );
    145 tr_benc * tr_bencListChild( tr_benc * list, int n );
    146 
    147 
    148146#endif
  • trunk/libtransmission/json-test.c

    r6591 r6612  
    11#include <stdio.h>
     2#include <string.h>
    23#include "transmission.h"
    34#include "bencode.h"
  • trunk/libtransmission/json.c

    r6584 r6612  
    8888            char buf[128];
    8989            tr_snprintf( buf, sizeof( buf ), "%f", (double)value->vu.float_value );
    90             tr_bencInitStrDup( getNode( data ), buf );
     90            tr_bencInitStr( getNode( data ), buf, -1 );
    9191            break;
    9292        }
     
    108108
    109109        case JSON_T_STRING:
    110             tr_bencInitStrDup( getNode( data ), value->vu.str.value );
     110            tr_bencInitStr( getNode( data ), value->vu.str.value, value->vu.str.length );
    111111            break;
    112112
  • trunk/libtransmission/makemeta.c

    r6498 r6612  
    1515#include <stdio.h> /* FILE, stderr */
    1616#include <stdlib.h> /* qsort */
     17#include <string.h> /* strcmp, strlen */
    1718
    1819#include <sys/types.h>
  • trunk/libtransmission/metainfo.c

    r6603 r6612  
    2626#include <errno.h>
    2727#include <stdio.h>
     28#include <string.h>
    2829
    2930#include <sys/types.h>
     
    295296geturllist( tr_info * inf, tr_benc * meta )
    296297{
    297     benc_val_t * urls;
     298    tr_benc * urls;
    298299
    299300    if( tr_bencDictFindList( meta, "url-list", &urls ) )
  • trunk/libtransmission/peer-msgs.c

    r6600 r6612  
    18591859        const int newCount = tr_peerMgrGetPeers( msgs->session->peerMgr, msgs->torrent->info.hash, &newPex );
    18601860        PexDiffs diffs;
    1861         tr_benc val, *added, *dropped;
     1861        tr_benc val;
    18621862        uint8_t *tmp, *walk;
    18631863        char * benc;
     
    18871887
    18881888        /* "added" */
    1889         added = tr_bencDictAdd( &val, "added" );
    18901889        tmp = walk = tr_new( uint8_t, diffs.addedCount * 6 );
    18911890        for( i=0; i<diffs.addedCount; ++i ) {
     
    18941893        }
    18951894        assert( ( walk - tmp ) == diffs.addedCount * 6 );
    1896         tr_bencInitStr( added, tmp, walk-tmp, FALSE );
     1895        tr_bencDictAddRaw( &val, "added", tmp, walk-tmp );
     1896        tr_free( tmp );
    18971897
    18981898        /* "added.f" */
     
    19051905
    19061906        /* "dropped" */
    1907         dropped = tr_bencDictAdd( &val, "dropped" );
    19081907        tmp = walk = tr_new( uint8_t, diffs.droppedCount * 6 );
    19091908        for( i=0; i<diffs.droppedCount; ++i ) {
     
    19121911        }
    19131912        assert( ( walk - tmp ) == diffs.droppedCount * 6 );
    1914         tr_bencInitStr( dropped, tmp, walk-tmp, FALSE );
     1913        tr_bencDictAddRaw( &val, "dropped", tmp, walk-tmp );
     1914        tr_free( tmp );
    19151915
    19161916        /* write the pex message */
  • trunk/libtransmission/rpc.c

    r6584 r6612  
    778778        tr_bencInitInt( setme, strtol( str, NULL, 10 ) );
    779779    else if( !isNumList )
    780         tr_bencInitStrDup( setme, str );
     780        tr_bencInitStr( setme, str, len );
    781781    else {
    782782        tr_bencInitList( setme, commaCount + 1 );
  • trunk/libtransmission/torrent-ctor.c

    r6600 r6612  
    109109        tr_benc * info;
    110110        if( tr_bencDictFindDict( &ctor->metainfo, "info", &info ) ) {
    111             tr_benc * name = tr_bencDictFindFirst( info, "name.utf-8", "name", NULL );
    112             if( name == NULL )
    113                 name = tr_bencDictAdd( info, "name" );
    114             if( name->type!=TYPE_STR || !name->val.s.s || !*name->val.s.s ) {
     111            const char * name;
     112            if( !tr_bencDictFindStr( info, "name.utf-8", &name ) )
     113                if( !tr_bencDictFindStr( info, "name", &name ) )
     114                    name = NULL;
     115            if( !name || !*name ) {
    115116                char * tmp = tr_strdup( filename );
    116                 tr_bencInitStrDup( name, basename( tmp ) );
     117                tr_bencDictAddStr( info, "name", basename( tmp ) );
    117118                tr_free( tmp );
    118119            }
Note: See TracChangeset for help on using the changeset viewer.