(trunk) add a "source" argument to tr_jsonParse() so that if there's an error we can log the filename, or that it came from rpc, etc.

This commit is contained in:
Charles Kerr 2009-06-01 18:57:37 +00:00
parent 6508dc3a3d
commit 7f85090587
9 changed files with 20 additions and 15 deletions

View File

@ -1282,7 +1282,7 @@ processResponse( const char * host,
fprintf( stderr, "got response (len %d):\n--------\n%*.*s\n--------\n",
(int)len, (int)len, (int)len, (const char*) response );
if( tr_jsonParse( response, len, &top, NULL ) )
if( tr_jsonParse( NULL, response, len, &top, NULL ) )
{
tr_nerr( MY_NAME, "Unable to parse response \"%*.*s\"", (int)len,
(int)len, (char*)response );

View File

@ -1315,7 +1315,7 @@ readResponseIdle( void * vresponse )
struct pending_request_data * data;
response = vresponse;
tr_jsonParse( response->data, response->len, &top, NULL );
tr_jsonParse( NULL, response->data, response->len, &top, NULL );
tr_bencDictFindInt( &top, "tag", &intVal );
tag = (int)intVal;

View File

@ -1616,7 +1616,7 @@ tr_bencLoadJSONFile( const char * filename, tr_benc * b )
else if( !content )
err = ENODATA;
else
err = tr_jsonParse( content, contentLen, b, NULL );
err = tr_jsonParse( filename, content, contentLen, b, NULL );
tr_free( content );
return err;

View File

@ -43,7 +43,7 @@ test_utf8( void )
int err;
struct evbuffer * buf = tr_getBuffer( );
err = tr_jsonParse( in, strlen( in ), &top, NULL );
err = tr_jsonParse( NULL, in, strlen( in ), &top, NULL );
check( !err );
check( tr_bencIsDict( &top ) );
check( tr_bencDictFindStr( &top, "key", &str ) );
@ -52,7 +52,7 @@ test_utf8( void )
tr_bencFree( &top );
in = "{ \"key\": \"\\u005C\" }";
err = tr_jsonParse( in, strlen( in ), &top, NULL );
err = tr_jsonParse( NULL, in, strlen( in ), &top, NULL );
check( !err );
check( tr_bencIsDict( &top ) );
check( tr_bencDictFindStr( &top, "key", &str ) );
@ -69,7 +69,7 @@ test_utf8( void )
* 6. Confirm that the result is UTF-8.
*/
in = "{ \"key\": \"Let\\u00f6lt\\u00e9sek\" }";
err = tr_jsonParse( in, strlen( in ), &top, NULL );
err = tr_jsonParse( NULL, in, strlen( in ), &top, NULL );
check( !err );
check( tr_bencIsDict( &top ) );
check( tr_bencDictFindStr( &top, "key", &str ) );
@ -80,7 +80,7 @@ test_utf8( void )
check( json );
check( strstr( json, "\\u00f6" ) != NULL );
check( strstr( json, "\\u00e9" ) != NULL );
err = tr_jsonParse( json, strlen( json ), &top, NULL );
err = tr_jsonParse( NULL, json, strlen( json ), &top, NULL );
check( !err );
check( tr_bencIsDict( &top ) );
check( tr_bencDictFindStr( &top, "key", &str ) );
@ -112,7 +112,7 @@ test1( void )
tr_benc top, *headers, *body, *args, *ids;
const char * str;
int64_t i;
const int err = tr_jsonParse( in, strlen( in ), &top, NULL );
const int err = tr_jsonParse( NULL, in, strlen( in ), &top, NULL );
check( !err );
check( tr_bencIsDict( &top ) );
@ -147,7 +147,7 @@ test2( void )
int err;
top.type = 0;
err = tr_jsonParse( in, strlen( in ), &top, NULL );
err = tr_jsonParse( NULL, in, strlen( in ), &top, NULL );
check( err );
check( !tr_bencIsDict( &top ) );

View File

@ -134,7 +134,8 @@ callback( void * vdata,
}
int
tr_jsonParse( const void * vbuf,
tr_jsonParse( const char * source,
const void * vbuf,
size_t len,
tr_benc * setme_benc,
const uint8_t ** setme_end )
@ -170,7 +171,10 @@ tr_jsonParse( const void * vbuf,
}
if( buf != bufend ) {
tr_err( "JSON parser failed at line %d, column %d: \"%.16s\"", line, column, buf );
if( source )
tr_err( "JSON parser failed in %s at line %d, column %d: \"%.16s\"", source, line, column, buf );
else
tr_err( "JSON parser failed at line %d, column %d: \"%.16s\"", line, column, buf );
err = EILSEQ;
}

View File

@ -17,7 +17,8 @@ extern "C" {
#endif
/** @ingroup tr_benc */
int tr_jsonParse( const void * vbuf,
int tr_jsonParse( const char * source, /* such as a filename. only when logging an error */
const void * vbuf,
size_t len,
struct tr_benc * setme_benc,
const uint8_t ** setme_end );

View File

@ -1397,7 +1397,7 @@ tr_rpc_request_exec_json( tr_session * session,
if( request_len < 0 )
request_len = strlen( request_json );
have_content = !tr_jsonParse( request_json, request_len, &top, NULL );
have_content = !tr_jsonParse( "rpc", request_json, request_len, &top, NULL );
request_exec( session, have_content ? &top : NULL, callback, callback_user_data );
if( have_content )

View File

@ -182,7 +182,7 @@ Prefs :: ~Prefs( )
file.open( QIODevice::ReadOnly | QIODevice::Text );
const QByteArray oldPrefs = file.readAll( );
file.close( );
if( tr_jsonParse( oldPrefs.data(), oldPrefs.length(), &top, NULL ) )
if( tr_jsonParse( "settings.json", oldPrefs.data(), oldPrefs.length(), &top, NULL ) )
tr_bencInitDict( &top, PREFS_COUNT );
/* merge our own settings with the ones already in the file */

View File

@ -624,7 +624,7 @@ Session :: parseResponse( const char * json, size_t jsonLength )
{
tr_benc top;
const uint8_t * end( 0 );
const int err( tr_jsonParse( json, jsonLength, &top, &end ) );
const int err( tr_jsonParse( "rpc", json, jsonLength, &top, &end ) );
if( !err )
{
int64_t tag = -1;