#972: merge upstream version of Jean Gressmann's JSON_parser

This commit is contained in:
Charles Kerr 2008-05-29 03:24:26 +00:00
parent 66ca0e588e
commit eb02b3e494
4 changed files with 42 additions and 28 deletions

View File

@ -30,6 +30,17 @@ SOFTWARE.
Callbacks, comments, Unicode handling by Jean Gressmann (jean@0x42.de), 2007-2008.
For the added features the license above applies also.
Changelog:
2008/05/28
- Made JSON_value structure ansi C compliant. This bug was report by
trisk@acm.jhu.edu
2008/05/20
- Fixed bug reported by Charles.Kerr@noaa.gov where the switching
from static to dynamic parse buffer did not copy the static parse
buffer's content.
*/
@ -54,7 +65,7 @@ SOFTWARE.
#define false 0
#define __ -1 /* the universal error code */
/* values chosen so that the object approx. fits into a page (4K) */
/* values chosen so that the object size is approx equal to one page (4K) */
#ifndef JSON_PARSER_STACK_SIZE
# define JSON_PARSER_STACK_SIZE 128
#endif
@ -452,20 +463,20 @@ static int parse_parse_buffer(JSON_parser jc)
case JSON_T_FLOAT:
arg = &value;
if (jc->handle_floats_manually) {
value.string_value = jc->parse_buffer;
value.string_length = jc->parse_buffer_count;
value.vu.str.value = jc->parse_buffer;
value.vu.str.length = jc->parse_buffer_count;
} else {
result = sscanf(jc->parse_buffer, "%Lf", &value.float_value);
result = sscanf(jc->parse_buffer, "%Lf", &value.vu.float_value);
}
break;
case JSON_T_INTEGER:
arg = &value;
result = sscanf(jc->parse_buffer, JSON_PARSER_INTEGER_SSCANF_TOKEN, &value.integer_value);
result = sscanf(jc->parse_buffer, JSON_PARSER_INTEGER_SSCANF_TOKEN, &value.vu.integer_value);
break;
case JSON_T_STRING:
arg = &value;
value.string_value = jc->parse_buffer;
value.string_length = jc->parse_buffer_count;
value.vu.str.value = jc->parse_buffer;
value.vu.str.length = jc->parse_buffer_count;
break;
}
@ -829,8 +840,8 @@ JSON_parser_char(JSON_parser jc, int next_char)
if (jc->callback) {
JSON_value value;
value.string_value = jc->parse_buffer;
value.string_length = jc->parse_buffer_count;
value.vu.str.value = jc->parse_buffer;
value.vu.str.length = jc->parse_buffer_count;
if (!(*jc->callback)(jc->ctx, JSON_T_KEY, &value)) {
return false;
}

View File

@ -57,10 +57,10 @@ typedef struct JSON_value_struct {
long double float_value;
struct {
const char* string_value;
size_t string_length;
};
};
const char* value;
size_t length;
} str;
} vu;
} JSON_value;
/*! \brief JSON parser callback
@ -83,7 +83,7 @@ typedef int (*JSON_parser_callback)(void* ctx, int type, const struct JSON_value
the depth is the limit
\param Pointer to a callback. This parameter may be NULL. In this case the input is merely checked for validity.
\param Callback context. This parameter may be NULL.
\param allowComments. To allow C style comments in JSON, set to non-zero.
\param depth. Specifies the levels of nested JSON to allow. Negative numbers yield unlimited nesting.
\param allowComments. To allow C style comments in JSON, set to non-zero.
\param handleFloatsManually. To decode floating point numbers manually set this parameter to non-zero.

View File

@ -56,7 +56,7 @@ getNode( struct json_benc_data * data )
}
static int
callback( void * vdata, int type, const struct JSON_value_struct * value )
callback( void * vdata, int type, const JSON_value * value )
{
struct json_benc_data * data = vdata;
tr_benc * node;
@ -85,7 +85,7 @@ callback( void * vdata, int type, const struct JSON_value_struct * value )
case JSON_T_FLOAT: {
char buf[128];
snprintf( buf, sizeof( buf ), "%f", (double)value->float_value );
snprintf( buf, sizeof( buf ), "%f", (double)value->vu.float_value );
tr_bencInitStrDup( getNode( data ), buf );
break;
}
@ -94,7 +94,7 @@ callback( void * vdata, int type, const struct JSON_value_struct * value )
break;
case JSON_T_INTEGER:
tr_bencInitInt( getNode( data ), value->integer_value );
tr_bencInitInt( getNode( data ), value->vu.integer_value );
break;
case JSON_T_TRUE:
@ -106,12 +106,12 @@ callback( void * vdata, int type, const struct JSON_value_struct * value )
break;
case JSON_T_STRING:
tr_bencInitStrDup( getNode( data ), value->string_value );
tr_bencInitStrDup( getNode( data ), value->vu.str.value );
break;
case JSON_T_KEY:
assert( !data->key );
data->key = tr_strdup( value->string_value );
data->key = tr_strdup( value->vu.str.value );
break;
}

View File

@ -856,9 +856,18 @@ typedef struct tr_peer_stat tr_peer_stat;
tr_peer_stat * tr_torrentPeers( const tr_torrent *, int * peerCount );
void tr_torrentPeersFree( tr_peer_stat *, int peerCount );
typedef struct tr_file_stat tr_file_stat;
tr_file_stat * tr_torrentFiles( const tr_torrent *, tr_file_index_t * fileCount );
void tr_torrentFilesFree( tr_file_stat *, tr_file_index_t fileCount );
typedef struct tr_file_stat
{
uint64_t bytesCompleted;
float progress;
}
tr_file_stat;
tr_file_stat * tr_torrentFiles( const tr_torrent * tor,
tr_file_index_t * fileCount );
void tr_torrentFilesFree( tr_file_stat * stat,
tr_file_index_t fileCount );
/***********************************************************************
@ -1174,12 +1183,6 @@ struct tr_stat
time_t activityDate;
};
struct tr_file_stat
{
uint64_t bytesCompleted;
float progress;
};
struct tr_peer_stat
{
char addr[16];