(trunk) #2131: clean up benc read/write API

This commit is contained in:
Charles Kerr 2009-06-02 01:48:48 +00:00
parent a13e66ef54
commit 9238fb0299
21 changed files with 145 additions and 207 deletions

View File

@ -298,12 +298,9 @@ main( int argc, char ** argv )
if( dumpSettings )
{
struct evbuffer * buf = tr_getBuffer( );
tr_bencSaveAsJSON( &settings, buf, TRUE );
fprintf( stderr, "%s", (char*)EVBUFFER_DATA(buf) );
tr_releaseBuffer( buf );
char * str = tr_bencToStr( &settings, TR_FMT_JSON, NULL );
fprintf( stderr, "%s", str );
tr_free( str );
return 0;
}

View File

@ -632,9 +632,7 @@ readargs( int argc,
if( addArg )
{
struct evbuffer * buf = tr_getBuffer( );
reqs[reqCount++] = tr_strdup( tr_bencSaveAsJSON( &top, buf, FALSE ) );
tr_releaseBuffer( buf );
reqs[reqCount++] = tr_bencToStr( &top, TR_FMT_JSON_LEAN, NULL );
}
tr_bencFree( &top );

View File

@ -428,7 +428,7 @@ translate_keyfile_to_json( const char * old_file,
}
g_key_file_free( keyfile );
tr_bencSaveJSONFile( new_file, &dict );
tr_bencToFile( &dict, TR_FMT_JSON, new_file );
tr_bencFree( &dict );
}

View File

@ -1444,7 +1444,7 @@ tr_core_exec_json( TrCore * core, const char * json )
void
tr_core_exec( TrCore * core, const tr_benc * top )
{
char * json = tr_bencToJSON( top, FALSE );
char * json = tr_bencToStr( top, TR_FMT_JSON_LEAN, NULL );
tr_core_exec_json( core, json );
tr_free( json );
}

View File

@ -173,7 +173,7 @@ testString( const char * str,
fprintf( stderr, "out:\n%s", tr_bencSaveAsJSON( &val, NULL ) );
#endif
check( end == (const uint8_t*)str + len );
saved = tr_bencSave( &val, &savedLen );
saved = tr_bencToStr( &val, TR_FMT_BENC, &savedLen );
check( !strcmp( saved, str ) );
check( len == (size_t)savedLen );
tr_free( saved );
@ -214,7 +214,7 @@ testParse( void )
check( i == 32 );
check( tr_bencGetInt( &val.val.l.vals[2], &i ) );
check( i == 16 );
saved = tr_bencSave( &val, &len );
saved = tr_bencToStr( &val, TR_FMT_BENC, &len );
check( !strcmp( saved, (char*)buf ) );
tr_free( saved );
tr_bencFree( &val );
@ -230,7 +230,7 @@ testParse( void )
err = tr_bencParse( buf, buf + sizeof( buf ), &val, &end );
check( !err );
check( end == buf + 2 );
saved = tr_bencSave( &val, &len );
saved = tr_bencToStr( &val, TR_FMT_BENC, &len );
check( !strcmp( saved, "le" ) );
tr_free( saved );
tr_bencFree( &val );
@ -273,7 +273,7 @@ testParse( void )
check( end == buf + strlen( (const char*)buf ) );
check( ( child = tr_bencListChild( &val, 0 ) ) );
check( ( child2 = tr_bencListChild( child, 0 ) ) );
saved = tr_bencSave( &val, &len );
saved = tr_bencToStr( &val, TR_FMT_BENC, &len );
check( !strcmp( saved, "lld1:ai64e1:bi32eeee" ) );
tr_free( saved );
tr_bencFree( &val );
@ -284,7 +284,7 @@ testParse( void )
err = tr_bencParse( buf, buf + sizeof( buf ), &val, &end );
check( !err );
check( end == buf + 2 );
saved = tr_bencSave( &val, &len );
saved = tr_bencToStr( &val, TR_FMT_BENC, &len );
check( !strcmp( saved, "le" ) );
tr_free( saved );
tr_bencFree( &val );
@ -324,7 +324,8 @@ testJSONSnippet( const char * benc_str,
char * serialized;
tr_bencLoad( benc_str, strlen( benc_str ), &top, NULL );
serialized = tr_bencSaveAsJSON( &top, buf, TRUE );
tr_bencToBuf( &top, TR_FMT_JSON, buf );
serialized = (char*) EVBUFFER_DATA( buf );
stripWhitespace( serialized );
#if 0
fprintf( stderr, "benc: %s\n", benc_str );
@ -443,7 +444,7 @@ testStackSmash( int depth )
err = tr_bencParse( in, in + ( depth * 2 ), &val, &end );
check( !err );
check( end == in + ( depth * 2 ) );
saved = tr_bencSave( &val, &len );
saved = tr_bencToStr( &val, TR_FMT_BENC, &len );
check( !strcmp( saved, (char*)in ) );
tr_free( in );
tr_free( saved );
@ -504,7 +505,7 @@ testParse2( void )
tr_bencDictAddReal( &top, "this-is-a-real", 0.5 );
tr_bencDictAddStr( &top, "this-is-a-string", "this-is-a-string" );
benc = tr_bencSave( &top, &len );
benc = tr_bencToStr( &top, TR_FMT_BENC, &len );
check( !strcmp( benc, "d14:this-is-a-booli1e14:this-is-a-real8:0.50000016:this-is-a-string16:this-is-a-string14:this-is-an-inti1234ee" ) )
check( !tr_bencParse( benc, benc+len, &top2, &end ) )
check( (char*)end == benc + len )

View File

@ -17,6 +17,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> /* close() */
#include <locale.h>
#include <unistd.h> /* close() */
@ -1134,23 +1135,6 @@ static const struct WalkFuncs saveFuncs = { saveIntFunc,
saveListBeginFunc,
saveContainerEndFunc };
char*
tr_bencSave( const tr_benc * top,
int * len )
{
char * ret;
struct evbuffer * out = tr_getBuffer( );
bencWalk( top, &saveFuncs, out );
if( len )
*len = EVBUFFER_LENGTH( out );
ret = tr_strndup( EVBUFFER_DATA( out ), EVBUFFER_LENGTH( out ) );
tr_releaseBuffer( out );
return ret;
}
/***
****
***/
@ -1240,19 +1224,23 @@ jsonChildFunc( struct jsonWalk * data )
const int i = parentState->childIndex++;
if( !( i % 2 ) )
evbuffer_add( data->out, ": ", data->doIndent ? 2 : 1 );
else
{
evbuffer_add( data->out, ", ", data->doIndent ? 2 : 1 );
jsonIndent( data );
else {
const tr_bool isLast = parentState->childIndex == parentState->childCount;
if( !isLast ) {
evbuffer_add( data->out, ", ", data->doIndent ? 2 : 1 );
jsonIndent( data );
}
}
break;
}
case TR_TYPE_LIST:
{
++parentState->childIndex;
evbuffer_add( data->out, ", ", data->doIndent ? 2 : 1 );
jsonIndent( data );
const tr_bool isLast = ++parentState->childIndex == parentState->childCount;
if( !isLast ) {
evbuffer_add( data->out, ", ", data->doIndent ? 2 : 1 );
jsonIndent( data );
}
break;
}
@ -1395,23 +1383,9 @@ static void
jsonContainerEndFunc( const tr_benc * val,
void * vdata )
{
size_t i;
struct jsonWalk * data = vdata;
char * str;
int emptyContainer = FALSE;
/* trim out the trailing comma, if any */
str = (char*) EVBUFFER_DATA( data->out );
for( i = EVBUFFER_LENGTH( data->out ) - 1; i > 0; --i )
{
if( isspace( str[i] ) ) continue;
if( str[i] == ',' )
EVBUFFER_LENGTH( data->out ) = i;
if( str[i] == '{' || str[i] == '[' )
emptyContainer = TRUE;
break;
}
jsonPopParent( data );
if( !emptyContainer )
jsonIndent( data );
@ -1430,41 +1404,6 @@ static const struct WalkFuncs jsonWalkFuncs = { jsonIntFunc,
jsonListBeginFunc,
jsonContainerEndFunc };
static void
tr_bencSaveAsJSONImpl( const tr_benc * top, struct evbuffer * out, tr_bool doIndent )
{
struct jsonWalk data;
evbuffer_drain( out, EVBUFFER_LENGTH( out ) );
data.doIndent = doIndent;
data.out = out;
data.parents = NULL;
bencWalk( top, &jsonWalkFuncs, &data );
if( EVBUFFER_LENGTH( out ) )
evbuffer_add_printf( out, "\n" );
}
char*
tr_bencSaveAsJSON( const tr_benc * top, struct evbuffer * out, tr_bool doIndent )
{
tr_bencSaveAsJSONImpl( top, out, doIndent );
return (char*) EVBUFFER_DATA( out );
}
char*
tr_bencToJSON( const tr_benc * top, tr_bool doIndent )
{
char * ret;
struct evbuffer * buf = evbuffer_new( );
tr_bencSaveAsJSON( top, buf, doIndent );
ret = tr_strndup( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
evbuffer_free( buf );
return ret;
}
/***
****
***/
@ -1551,12 +1490,50 @@ tr_bencMergeDicts( tr_benc * target, const tr_benc * source )
}
}
/***
****
***/
/***
****
***/
static int
saveFile( const char * filename, struct evbuffer * buf )
void
tr_bencToBuf( const tr_benc * top, tr_fmt_mode mode, struct evbuffer * buf )
{
evbuffer_drain( buf, EVBUFFER_LENGTH( buf ) );
switch( mode )
{
case TR_FMT_BENC:
bencWalk( top, &saveFuncs, buf );
break;
case TR_FMT_JSON:
case TR_FMT_JSON_LEAN: {
struct jsonWalk data;
data.doIndent = mode==TR_FMT_JSON;
data.out = buf;
data.parents = NULL;
bencWalk( top, &jsonWalkFuncs, &data );
if( EVBUFFER_LENGTH( buf ) )
evbuffer_add_printf( buf, "\n" );
break;
}
}
}
char*
tr_bencToStr( const tr_benc * top, tr_fmt_mode mode, int * len )
{
char * ret;
struct evbuffer * buf = evbuffer_new( );
tr_bencToBuf( top, mode, buf );
ret = tr_strndup( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
if( len != NULL )
*len = (int) EVBUFFER_LENGTH( buf );
evbuffer_free( buf );
return ret;
}
int
tr_bencToFile( const tr_benc * top, tr_fmt_mode mode, const char * filename )
{
int err = 0;
int fd = tr_open_file_for_writing( filename );
@ -1567,40 +1544,22 @@ saveFile( const char * filename, struct evbuffer * buf )
tr_err( _( "Couldn't open \"%1$s\": %2$s" ),
filename, tr_strerror( errno ) );
}
else if( evbuffer_write( buf, fd ) == -1 )
else
{
err = errno;
tr_err( _( "Couldn't save file \"%1$s\": %2$s" ),
filename, tr_strerror( errno ) );
struct evbuffer * buf = evbuffer_new( );
tr_bencToBuf( top, mode, buf );
if( evbuffer_write( buf, fd ) == -1 )
{
err = errno;
tr_err( _( "Couldn't save file \"%1$s\": %2$s" ),
filename, tr_strerror( errno ) );
}
if( !err )
tr_dbg( "tr_bencToFile saved \"%s\"", filename );
close( fd );
}
if( !err )
tr_dbg( "tr_bencSaveFile saved \"%s\"", filename );
if( fd >= 0 )
close( fd );
return err;
}
int
tr_bencSaveFile( const char * filename, const tr_benc * top )
{
int err;
struct evbuffer * buf = evbuffer_new( );
bencWalk( top, &saveFuncs, buf );
err = saveFile( filename, buf );
evbuffer_free( buf );
return err;
}
int
tr_bencSaveJSONFile( const char * filename, const tr_benc * top )
{
int err;
struct evbuffer * buf = evbuffer_new( );
tr_bencSaveAsJSONImpl( top, buf, TRUE );
err = saveFile( filename, buf );
evbuffer_free( buf );
return err;
}
@ -1609,10 +1568,10 @@ tr_bencSaveJSONFile( const char * filename, const tr_benc * top )
***/
int
tr_bencLoadFile( const char * filename, tr_benc * b )
tr_bencLoadFile( tr_benc * setme, tr_fmt_mode mode, const char * filename )
{
int err;
size_t contentLen;
int err;
size_t contentLen;
uint8_t * content;
content = tr_loadFile( filename, &contentLen );
@ -1620,27 +1579,12 @@ tr_bencLoadFile( const char * filename, tr_benc * b )
err = errno;
else if( !content )
err = ENODATA;
else
err = tr_bencLoad( content, contentLen, b, NULL );
tr_free( content );
return err;
}
int
tr_bencLoadJSONFile( const char * filename, tr_benc * b )
{
int err;
size_t contentLen;
uint8_t * content;
content = tr_loadFile( filename, &contentLen );
if( !content && errno )
err = errno;
else if( !content )
err = ENODATA;
else
err = tr_jsonParse( filename, content, contentLen, b, NULL );
else {
if( mode == TR_FMT_BENC )
err = tr_bencLoad( content, contentLen, setme, NULL );
else
err = tr_jsonParse( filename, content, contentLen, setme, NULL );
}
tr_free( content );
return err;

View File

@ -91,22 +91,8 @@ int tr_bencLoad( const void * buf,
tr_benc * setme_benc,
char ** setme_end );
int tr_bencLoadFile( const char * filename, tr_benc * setme );
int tr_bencLoadJSONFile( const char * filename, tr_benc * setme );
void tr_bencFree( tr_benc * );
char* tr_bencSave( const tr_benc * val, int * len );
char* tr_bencSaveAsJSON( const tr_benc * top, struct evbuffer * out, tr_bool doIndent );
char* tr_bencToJSON( const tr_benc * top, tr_bool doIndent );
int tr_bencSaveFile( const char * filename, const tr_benc * );
int tr_bencSaveJSONFile( const char * filename, const tr_benc * );
void tr_bencInitStr( tr_benc *, const void * str, int str_len );
void tr_bencInitRaw( tr_benc *, const void * raw, size_t raw_len );
@ -125,7 +111,28 @@ void tr_bencInitReal( tr_benc *, double value );
****
***/
int tr_bencListReserve( tr_benc *, size_t reserveCount );
typedef enum
{
TR_FMT_BENC,
TR_FMT_JSON,
TR_FMT_JSON_LEAN /* saves bandwidth by omitting all whitespace. */
}
tr_fmt_mode;
int tr_bencToFile( const tr_benc *, tr_fmt_mode, const char * filename );
char* tr_bencToStr( const tr_benc *, tr_fmt_mode, int * len );
void tr_bencToBuf( const tr_benc *, tr_fmt_mode, struct evbuffer * );
/* TR_FMT_JSON_LEAN and TR_FMT_JSON are equivalent in this function. */
int tr_bencLoadFile( tr_benc * setme, tr_fmt_mode, const char * filename );
/***
****
***/
int tr_bencListReserve( tr_benc *, size_t reserveCount );
tr_benc * tr_bencListAdd( tr_benc * );

View File

@ -41,7 +41,6 @@ test_utf8( void )
const char * str;
char * json;
int err;
struct evbuffer * buf = tr_getBuffer( );
err = tr_jsonParse( NULL, in, strlen( in ), &top, NULL );
check( !err );
@ -74,7 +73,7 @@ test_utf8( void )
check( tr_bencIsDict( &top ) );
check( tr_bencDictFindStr( &top, "key", &str ) );
check( !strcmp( str, "Letöltések" ) );
json = tr_bencSaveAsJSON( &top, buf, TRUE );
json = tr_bencToStr( &top, TR_FMT_JSON, NULL );
if( !err )
tr_bencFree( &top );
check( json );
@ -89,7 +88,6 @@ test_utf8( void )
tr_bencFree( &top );
tr_free( json );
tr_releaseBuffer( buf );
return 0;
}

View File

@ -417,7 +417,7 @@ tr_realMakeMetaInfo( tr_metainfo_builder * builder )
/* save the file */
if( !builder->result && !builder->abortFlag )
{
if( tr_bencSaveFile( builder->outputFile, &top ) )
if( tr_bencToFile( &top, TR_FMT_BENC, builder->outputFile ) )
{
builder->my_errno = errno;
tr_strlcpy( builder->errfile, builder->outputFile,

View File

@ -366,8 +366,8 @@ tr_metainfoParseImpl( const tr_session * session,
return "info";
else
{
int len;
char * bstr = tr_bencSave( beInfo, &len );
int len;
char * bstr = tr_bencToStr( beInfo, TR_FMT_BENC, &len );
tr_sha1( inf->hash, bstr, len, NULL );
tr_sha1_to_hex( inf->hashString, inf->hash );
tr_free( bstr );

View File

@ -991,7 +991,7 @@ sendLtepHandshake( tr_peermsgs * msgs )
m = tr_bencDictAddDict( &val, "m", 1 );
if( pex )
tr_bencDictAddInt( m, "ut_pex", TR_LTEP_PEX );
buf = tr_bencSave( &val, &len );
buf = tr_bencToStr( &val, TR_FMT_BENC, &len );
tr_peerIoWriteUint32( msgs->peer->io, out, 2 * sizeof( uint8_t ) + len );
tr_peerIoWriteUint8 ( msgs->peer->io, out, BT_LTEP );
@ -2066,7 +2066,7 @@ sendPex( tr_peermsgs * msgs )
tr_free( tmp );
/* write the pex message */
benc = tr_bencSave( &val, &bencLen );
benc = tr_bencToStr( &val, TR_FMT_BENC, &bencLen );
tr_peerIoWriteUint32( io, out, 2 * sizeof( uint8_t ) + bencLen );
tr_peerIoWriteUint8 ( io, out, BT_LTEP );
tr_peerIoWriteUint8 ( io, out, msgs->ut_pex_id );

View File

@ -512,7 +512,7 @@ tr_torrentSaveResume( const tr_torrent * tor )
saveRatioLimits( &top, tor );
filename = getResumeFilename( tor );
tr_bencSaveFile( filename, &top );
tr_bencToFile( &top, TR_FMT_BENC, filename );
tr_free( filename );
tr_bencFree( &top );
@ -531,7 +531,7 @@ loadFromFile( tr_torrent * tor,
filename = getResumeFilename( tor );
if( tr_bencLoadFile( filename, &top ) )
if( tr_bencLoadFile( &top, TR_FMT_BENC, filename ) )
{
tr_tordbg( tor, "Couldn't read \"%s\"; trying old format.",
filename );

View File

@ -253,7 +253,7 @@ handle_upload( struct evhttp_request * req,
b64 = tr_base64_encode( body, body_len, NULL );
tr_bencDictAddStr( args, "metainfo", b64 );
tr_bencDictAddBool( args, "paused", paused );
tr_bencSaveAsJSON( &top, json, FALSE );
tr_bencToBuf( &top, TR_FMT_JSON_LEAN, json );
tr_rpc_request_exec_json( server->session,
EVBUFFER_DATA( json ),
EVBUFFER_LENGTH( json ),

View File

@ -86,17 +86,17 @@ struct tr_rpc_idle_data
static void
tr_idle_function_done( struct tr_rpc_idle_data * data, const char * result )
{
struct evbuffer * buf = tr_getBuffer( );
struct evbuffer * buf = evbuffer_new( );
if( result == NULL )
result = "success";
tr_bencDictAddStr( data->response, "result", result );
tr_bencSaveAsJSON( data->response, buf, FALSE );
tr_bencToBuf( data->response, TR_FMT_JSON_LEAN, buf );
(*data->callback)( data->session, (const char*)EVBUFFER_DATA(buf),
EVBUFFER_LENGTH(buf), data->callback_user_data );
tr_releaseBuffer( buf );
evbuffer_free( buf );
tr_bencFree( data->response );
tr_free( data->response );
tr_free( data );
@ -1332,18 +1332,18 @@ request_exec( tr_session * session,
{
int64_t tag;
tr_benc response;
struct evbuffer * buf = tr_getBuffer( );
struct evbuffer * buf = evbuffer_new( );
tr_bencInitDict( &response, 3 );
tr_bencDictAddDict( &response, "arguments", 0 );
tr_bencDictAddStr( &response, "result", result );
if( tr_bencDictFindInt( request, "tag", &tag ) )
tr_bencDictAddInt( &response, "tag", tag );
tr_bencSaveAsJSON( &response, buf, FALSE );
tr_bencToBuf( &response, TR_FMT_JSON_LEAN, buf );
(*callback)( session, (const char*)EVBUFFER_DATA(buf),
EVBUFFER_LENGTH( buf ), callback_user_data );
tr_releaseBuffer( buf );
evbuffer_free( buf );
tr_bencFree( &response );
}
else if( methods[i].immediate )
@ -1361,7 +1361,7 @@ request_exec( tr_session * session,
tr_bencDictAddStr( &response, "result", result );
if( tr_bencDictFindInt( request, "tag", &tag ) )
tr_bencDictAddInt( &response, "tag", tag );
tr_bencSaveAsJSON( &response, buf, FALSE );
tr_bencToBuf( &response, TR_FMT_JSON_LEAN, buf );
(*callback)( session, (const char*)EVBUFFER_DATA(buf),
EVBUFFER_LENGTH(buf), callback_user_data );

View File

@ -476,7 +476,7 @@ tr_sessionLoadSettings( tr_benc * d, const char * configDir, const char * appNam
/* file settings override the defaults */
filename = tr_buildPath( configDir, "settings.json", NULL );
if( !tr_bencLoadJSONFile( filename, &fileSettings ) ) {
if( !tr_bencLoadFile( &fileSettings, TR_FMT_JSON, filename ) ) {
tr_bencMergeDicts( d, &fileSettings );
tr_bencFree( &fileSettings );
}
@ -501,7 +501,7 @@ tr_sessionSaveSettings( tr_session * session,
/* the existing file settings are the fallback values */
{
tr_benc fileSettings;
if( !tr_bencLoadJSONFile( filename, &fileSettings ) )
if( !tr_bencLoadFile( &fileSettings, TR_FMT_JSON, filename ) )
{
tr_bencMergeDicts( &settings, &fileSettings );
tr_bencFree( &fileSettings );
@ -521,7 +521,7 @@ tr_sessionSaveSettings( tr_session * session,
}
/* save the result */
tr_bencSaveJSONFile( filename, &settings );
tr_bencToFile( &settings, TR_FMT_JSON, filename );
tr_inf( "Saved \"%s\"", filename );
/* cleanup */

View File

@ -51,13 +51,13 @@ loadCumulativeStats( const tr_session * session,
tr_benc top;
filename = getFilename( session );
loaded = !tr_bencLoadJSONFile( filename, &top );
loaded = !tr_bencLoadFile( &top, TR_FMT_JSON, filename );
tr_free( filename );
if( !loaded )
{
filename = getOldFilename( session );
loaded = !tr_bencLoadFile( filename, &top );
loaded = !tr_bencLoadFile( &top, TR_FMT_BENC, filename );
tr_free( filename );
}
@ -96,7 +96,7 @@ saveCumulativeStats( const tr_session * session,
filename = getFilename( session );
tr_deepLog( __FILE__, __LINE__, NULL, "Saving stats to \"%s\"", filename );
tr_bencSaveJSONFile( filename, &top );
tr_bencToFile( &top, TR_FMT_JSON, filename );
tr_free( filename );
tr_bencFree( &top );

View File

@ -660,9 +660,8 @@ torrentRealInit( tr_torrent * tor, const tr_ctor * ctor )
if( !tr_ctorGetMetainfo( ctor, &val ) )
{
const char * filename = tor->info.torrent;
tr_bencSaveFile( filename, val );
tr_sessionSetTorrentFile( tor->session, tor->info.hashString,
filename );
tr_bencToFile( val, TR_FMT_BENC, filename );
tr_sessionSetTorrentFile( tor->session, tor->info.hashString, filename );
}
}
@ -1947,7 +1946,7 @@ tr_torrentSetAnnounceList( tr_torrent * tor,
assert( tr_isTorrent( tor ) );
/* save to the .torrent file */
if( !tr_bencLoadFile( tor->info.torrent, &metainfo ) )
if( !tr_bencLoadFile( &metainfo, TR_FMT_BENC, tor->info.torrent ) )
{
int i;
int prevTier = -1;
@ -1985,7 +1984,7 @@ tr_torrentSetAnnounceList( tr_torrent * tor,
tmpInfo.trackerCount = swap.trackerCount;
tr_metainfoFree( &tmpInfo );
tr_bencSaveFile( tor->info.torrent, &metainfo );
tr_bencToFile( &metainfo, TR_FMT_BENC, tor->info.torrent );
}
/* cleanup */

View File

@ -135,7 +135,7 @@ tr_dhtInit(tr_session *ss)
dht_debug = stderr;
dat_file = tr_buildPath( ss->configDir, "dht.dat", NULL );
rc = tr_bencLoadFile(dat_file, &benc);
rc = tr_bencLoadFile( &benc, TR_FMT_BENC, dat_file );
tr_free( dat_file );
if(rc == 0) {
if(( have_id = tr_bencDictFindRaw( &benc, "id", &raw, &len ) && len==20 ))
@ -215,7 +215,7 @@ tr_dhtUninit(tr_session *ss)
tr_bencDictAddRaw( &benc, "id", myid, 20 );
tr_bencDictAddRaw( &benc, "nodes", compact, j );
dat_file = tr_buildPath( ss->configDir, "dht.dat", NULL );
tr_bencSaveFile( dat_file, &benc );
tr_bencToFile( &benc, TR_FMT_BENC, dat_file );
tr_free( dat_file );
}

View File

@ -218,13 +218,7 @@ Prefs :: ~Prefs( )
}
/* write back out the serialized preferences */
char * json = tr_bencToJSON( &top, TRUE );
if( json && *json ) {
file.open( QIODevice::WriteOnly | QIODevice::Text );
file.write( json );
file.close( );
}
tr_free( json );
tr_bencToFile( &top, TR_FMT_JSON, file.fileName().toUtf8().constData() );
tr_bencFree( &top );
}

View File

@ -532,7 +532,7 @@ Session :: updateBlocklist( )
void
Session :: exec( const tr_benc * request )
{
char * str( tr_bencToJSON( request, FALSE ) );
char * str = tr_bencToStr( request, TR_FMT_JSON_LEAN, NULL );
exec( str );
tr_free( str );
}

View File

@ -320,7 +320,7 @@ common_bits(const unsigned char *id1, const unsigned char *id2)
}
/* Determine whether id1 or id2 is closer to ref */
int
static int
xorcmp(const unsigned char *id1, const unsigned char *id2,
const unsigned char *ref)
{