Ignore:
Timestamp:
Apr 18, 2008, 4:23:59 PM (14 years ago)
Author:
charles
Message:

benc cleanup

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/makemeta.c

    r5636 r5643  
    275275             tr_benc                         * uninitialized_path )
    276276{
    277     tr_benc *sub;
    278277    const char *pch, *prev;
    279278    const size_t topLen = strlen(topFile) + 1; /* +1 for '/' */
     
    299298        buf[pch-prev] = '\0';
    300299
    301         sub = tr_bencListAdd( uninitialized_path );
    302         tr_bencInitStrDup( sub, buf );
     300        tr_bencListAddStr( uninitialized_path, buf );
    303301
    304302        prev = pch + 1;
     
    309307
    310308static void
    311 makeFilesList( tr_benc                    * list,
    312                const tr_metainfo_builder  * builder )
    313 {
    314     uint32_t i = 0;
    315 
    316     tr_bencListReserve( list, builder->fileCount );
    317 
    318     for( i=0; i<builder->fileCount; ++i )
    319     {
    320         tr_benc * dict = tr_bencListAdd( list );
    321         tr_benc *length, *pathVal;
    322 
    323         tr_bencInitDict( dict, 2 );
    324         length = tr_bencDictAdd( dict, "length" );
    325         pathVal = tr_bencDictAdd( dict, "path" );
    326         getFileInfo( builder->top, &builder->files[i], length, pathVal );
    327     }
    328 }
    329 
    330 static void
    331309makeInfoDict ( tr_benc              * dict,
    332310               tr_metainfo_builder  * builder )
    333311{
    334312    uint8_t * pch;
    335     tr_benc * val;
    336313    char base[MAX_PATH_LENGTH];
    337314
     
    339316   
    340317    if ( builder->isSingleFile )
    341     {
    342         val = tr_bencDictAdd( dict, "length" );
    343         tr_bencInitInt( val, builder->files[0].size );
    344     }
    345     else
    346     {
    347         val = tr_bencDictAdd( dict, "files" );
    348         tr_bencInit( val, TYPE_LIST );
    349         makeFilesList( val, builder );
    350     }
    351 
    352     val = tr_bencDictAdd( dict, "name" );
     318        tr_bencDictAddInt( dict, "length", builder->files[0].size );
     319    else {
     320        uint32_t i;
     321        tr_benc * list = tr_bencDictAddList( dict, "files", builder->fileCount );
     322        for( i=0; i<builder->fileCount; ++i ) {
     323            tr_benc * dict = tr_bencListAddDict( list, 2 );
     324            tr_benc * length = tr_bencDictAdd( dict, "length" );
     325            tr_benc * pathVal = tr_bencDictAdd( dict, "path" );
     326            getFileInfo( builder->top, &builder->files[i], length, pathVal );
     327        }
     328    }
     329
    353330    strlcpy( base, builder->top, sizeof( base ) );
    354     tr_bencInitStrDup ( val, basename( base ) );
    355 
    356     val = tr_bencDictAdd( dict, "piece length" );
    357     tr_bencInitInt( val, builder->pieceSize );
    358 
    359     if( ( pch = getHashInfo( builder ) ) ) {
    360         val = tr_bencDictAdd( dict, "pieces" );
    361         tr_bencInitStr( val, pch, SHA_DIGEST_LENGTH * builder->pieceCount, 0 );
    362     }
    363 
    364     val = tr_bencDictAdd( dict, "private" );
    365     tr_bencInitInt( val, builder->isPrivate ? 1 : 0 );
     331    tr_bencDictAddStr( dict, "name", basename( base ) );
     332
     333    tr_bencDictAddInt( dict, "piece length", builder->pieceSize );
     334
     335    if(( pch = getHashInfo( builder ))) {
     336        tr_bencDictAddRaw( dict, "pieces", pch, SHA_DIGEST_LENGTH * builder->pieceCount );
     337        tr_free( pch );
     338    }
     339
     340    tr_bencDictAddInt( dict, "private", builder->isPrivate ? 1 : 0 );
    366341}
    367342
     
    370345{
    371346    int n = 5;
    372     tr_benc top, *val;
     347    tr_benc top;
    373348
    374349    if ( builder->comment && *builder->comment ) ++n;
    375350    tr_bencInitDict( &top, n );
    376351
    377     val = tr_bencDictAdd( &top, "announce" );
    378     tr_bencInitStrDup( val, builder->announce );
     352    tr_bencDictAddStr( &top, "announce", builder->announce );
     353
    379354    if( tr_httpParseURL( builder->announce, -1, NULL, NULL, NULL ) )
    380     {
    381355        builder->result = TR_MAKEMETA_URL;
    382     }
    383356   
    384357    if( !builder->result && !builder->abortFlag )
    385358    {
    386         if( builder->comment && *builder->comment ) {
    387             val = tr_bencDictAdd( &top, "comment" );
    388             tr_bencInitStrDup( val, builder->comment );
    389         }
    390 
    391         val = tr_bencDictAdd( &top, "created by" );
    392         tr_bencInitStrDup( val, TR_NAME "/" LONG_VERSION_STRING );
    393 
    394         val = tr_bencDictAdd( &top, "creation date" );
    395         tr_bencInitInt( val, time(0) );
    396 
    397         val = tr_bencDictAdd( &top, "encoding" );
    398         tr_bencInitStrDup( val, "UTF-8" );
    399 
    400         val = tr_bencDictAdd( &top, "info" );
    401         tr_bencInitDict( val, 666 );
    402         makeInfoDict( val, builder );
     359        if( builder->comment && *builder->comment )
     360            tr_bencDictAddStr( &top, "comment", builder->comment );
     361        tr_bencDictAddStr( &top, "created by", TR_NAME "/" LONG_VERSION_STRING );
     362        tr_bencDictAddInt( &top, "creation date", time(0) );
     363        tr_bencDictAddStr( &top, "encoding", "UTF-8" );
     364        makeInfoDict( tr_bencDictAddDict( &top, "info", 666 ), builder );
    403365    }
    404366
    405367    /* save the file */
    406368    if ( !builder->result && !builder->abortFlag ) {
    407         char * pch = tr_bencSave( &top, &n );
    408         FILE * fp = fopen( builder->outputFile, "wb+" );
    409         size_t nmemb = n;
    410         if( !fp || ( fwrite( pch, 1, nmemb, fp ) != nmemb ) ) {
     369        if( tr_bencSaveFile( builder->outputFile, &top ) ) {
    411370            builder->my_errno = errno;
    412371            strlcpy( builder->errfile, builder->outputFile, sizeof( builder->errfile ) );
    413372            builder->result = TR_MAKEMETA_IO_WRITE;
    414373        }
    415         if( fp )
    416             fclose( fp );
    417         tr_free( pch );
    418374    }
    419375
Note: See TracChangeset for help on using the changeset viewer.