Changeset 5821


Ignore:
Timestamp:
May 12, 2008, 5:54:57 PM (14 years ago)
Author:
charles
Message:

add benc-to-json conversion + unit tests.

Location:
trunk/libtransmission
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/bencode-test.c

    r5772 r5821  
    272272
    273273static int
    274 testPHPSnippet( const char * benc_str, const char * expected )
     274testJSONSnippet( const char * benc_str, const char * expected )
    275275{
    276276    tr_benc top;
    277277    char * serialized;
    278278    tr_bencLoad( benc_str, strlen( benc_str ), &top, NULL );
    279     serialized = tr_bencSaveAsSerializedPHP( &top, NULL );
     279    serialized = tr_bencSaveAsJSON( &top, NULL );
    280280    check( !strcmp( serialized, expected ) );
    281281    tr_free( serialized );
     
    285285
    286286static int
    287 testPHP( void )
     287testJSON( void )
    288288{
    289289    int val;
     
    292292
    293293    benc_str = "i6e";
    294     expected = "i:6;";
    295     if(( val = testPHPSnippet( benc_str, expected )))
     294    expected = "6";
     295    if(( val = testJSONSnippet( benc_str, expected )))
    296296        return val;
    297297
    298     benc_str = "d3:cow3:moo4:spam4:eggse";
    299     expected = "a:2:{s:3:\"cow\";s:3:\"moo\";s:4:\"spam\";s:4:\"eggs\";}";
    300     if(( val = testPHPSnippet( benc_str, expected )))
     298    benc_str = "d5:helloi1e5:worldi2ee";
     299    expected = "{ \"hello\": 1, \"world\": 2 }";
     300    if(( val = testJSONSnippet( benc_str, expected )))
    301301        return val;
    302302
    303     benc_str = "l3:cow3:moo4:spam4:eggse";
    304     expected = "a:4:{i:0;s:3:\"cow\";i:1;s:3:\"moo\";i:2;s:4:\"spam\";i:3;s:4:\"eggs\";}";
    305     if(( val = testPHPSnippet( benc_str, expected )))
     303    benc_str = "d5:helloi1e5:worldi2e3:fooli1ei2ei3ee";
     304    expected = "{ \"foo\": [ 1, 2, 3 ], \"hello\": 1, \"world\": 2 }";
     305    if(( val = testJSONSnippet( benc_str, expected )))
     306        return val;
     307
     308    benc_str = "d5:helloi1e5:worldi2e3:fooli1ei2ei3ed1:ai0eee";
     309    expected = "{ \"foo\": [ 1, 2, 3, { \"a\": 0 } ], \"hello\": 1, \"world\": 2 }";
     310    if(( val = testJSONSnippet( benc_str, expected )))
    306311        return val;
    307312
     
    355360        return i;
    356361
    357     if(( i = testPHP( )))
     362    if(( i = testJSON( )))
    358363        return i;
    359364
  • trunk/libtransmission/bencode.c

    r5810 r5821  
    10131013struct ParentState
    10141014{
    1015     int type;
    1016     int index;
     1015    int bencType;
     1016    int childIndex;
     1017    int childCount;
    10171018};
    10181019 
    1019 struct phpWalk
     1020struct jsonWalk
    10201021{
    10211022    tr_list * parents;
     
    10241025
    10251026static void
    1026 phpChildFunc( struct phpWalk * data )
     1027jsonChildFunc( struct jsonWalk * data )
    10271028{
    10281029    if( data->parents )
     
    10301031        struct ParentState * parentState = data->parents->data;
    10311032
    1032         if( parentState->type == TYPE_LIST )
    1033             evbuffer_add_printf( data->out, "i:%d;", parentState->index++ );
    1034     }
    1035 }
    1036 
    1037 static void
    1038 phpPushParent( struct phpWalk * data, int type )
     1033        switch( parentState->bencType )
     1034        {
     1035            case TYPE_DICT: {
     1036                const int i = parentState->childIndex++;
     1037                if( ! ( i % 2 ) )
     1038                    evbuffer_add_printf( data->out, ": " );
     1039                else if( parentState->childIndex < parentState->childCount )
     1040                    evbuffer_add_printf( data->out, ", " );
     1041                else
     1042                    evbuffer_add_printf( data->out, " }" );
     1043                break;
     1044            }
     1045
     1046            case TYPE_LIST: {
     1047                if( ++parentState->childIndex < parentState->childCount )
     1048                    evbuffer_add_printf( data->out, ", " );
     1049                else
     1050                    evbuffer_add_printf( data->out, " ]" );
     1051                break;
     1052            }
     1053
     1054            default:
     1055                break;
     1056        }
     1057    }
     1058}
     1059
     1060static void
     1061jsonPushParent( struct jsonWalk * data, const tr_benc * benc )
    10391062{
    10401063    struct ParentState * parentState = tr_new( struct ParentState, 1 );
    1041     parentState->type = type;
    1042     parentState->index = 0;
     1064    parentState->bencType = benc->type;
     1065    parentState->childIndex = 0;
     1066    parentState->childCount = benc->val.l.count;
    10431067    tr_list_prepend( &data->parents, parentState );
    10441068}
    10451069
    10461070static void
    1047 phpPopParent( struct phpWalk * data )
     1071jsonPopParent( struct jsonWalk * data )
    10481072{
    10491073    tr_free( tr_list_pop_front( &data->parents ) );
     
    10511075
    10521076static void
    1053 phpIntFunc( const tr_benc * val, void * vdata )
    1054 {
    1055     struct phpWalk * data = vdata;
    1056     phpChildFunc( data );
    1057     evbuffer_add_printf( data->out, "i:%"PRId64";", val->val.i );
    1058 }
    1059 static void
    1060 phpStringFunc( const tr_benc * val, void * vdata )
    1061 {
    1062     struct phpWalk * data = vdata;
    1063     phpChildFunc( data );
    1064     evbuffer_add_printf( data->out, "s:%d:\"%s\";", val->val.s.i, val->val.s.s );
    1065 }
    1066 static void
    1067 phpDictBeginFunc( const tr_benc * val, void * vdata )
    1068 {
    1069     struct phpWalk * data = vdata;
    1070     phpChildFunc( data );
    1071     phpPushParent( data, TYPE_DICT );
    1072     evbuffer_add_printf( data->out, "a:%d:{", val->val.l.count/2 );
    1073 }
    1074 static void
    1075 phpListBeginFunc( const tr_benc * val, void * vdata )
    1076 {
    1077     struct phpWalk * data = vdata;
    1078     phpChildFunc( data );
    1079     phpPushParent( data, TYPE_LIST );
    1080     evbuffer_add_printf( data->out, "a:%d:{", val->val.l.count );
    1081 }
    1082 static void
    1083 phpContainerEndFunc( const tr_benc * val UNUSED, void * vdata )
    1084 {
    1085     struct phpWalk * data = vdata;
    1086     phpPopParent( data );
    1087     evbuffer_add_printf( data->out, "}" );
     1077jsonIntFunc( const tr_benc * val, void * vdata )
     1078{
     1079    struct jsonWalk * data = vdata;
     1080    evbuffer_add_printf( data->out, "%"PRId64, val->val.i );
     1081    jsonChildFunc( data );
     1082}
     1083static void
     1084jsonStringFunc( const tr_benc * val, void * vdata )
     1085{
     1086    struct jsonWalk * data = vdata;
     1087    const char *it, *end;
     1088    evbuffer_add_printf( data->out, "\"" );
     1089    for( it=val->val.s.s, end=it+val->val.s.i; it!=end; ++it )
     1090    {
     1091        switch( *it ) {
     1092            case '"' : evbuffer_add_printf( data->out, "\\\"" ); break;
     1093            case '/' : evbuffer_add_printf( data->out, "\\/" ); break;
     1094            case '\b': evbuffer_add_printf( data->out, "\\b" ); break;
     1095            case '\f': evbuffer_add_printf( data->out, "\\f" ); break;
     1096            case '\n': evbuffer_add_printf( data->out, "\\n" ); break;
     1097            case '\r': evbuffer_add_printf( data->out, "\\n" ); break;
     1098            case '\t': evbuffer_add_printf( data->out, "\\n" ); break;
     1099            case '\\': evbuffer_add_printf( data->out, "\\\\" ); break;
     1100            default:   evbuffer_add_printf( data->out, "%c", *it ); break;
     1101        }
     1102    }
     1103    evbuffer_add_printf( data->out, "\"" );
     1104    jsonChildFunc( data );
     1105}
     1106static void
     1107jsonDictBeginFunc( const tr_benc * val, void * vdata )
     1108{
     1109    struct jsonWalk * data = vdata;
     1110    jsonPushParent( data, val );
     1111    evbuffer_add_printf( data->out, "{ " );
     1112}
     1113static void
     1114jsonListBeginFunc( const tr_benc * val, void * vdata )
     1115{
     1116    struct jsonWalk * data = vdata;
     1117    jsonPushParent( data, val );
     1118    evbuffer_add_printf( data->out, "[ " );
     1119}
     1120static void
     1121jsonContainerEndFunc( const tr_benc * val UNUSED, void * vdata )
     1122{
     1123    struct jsonWalk * data = vdata;
     1124    jsonPopParent( data );
     1125    jsonChildFunc( data );
    10881126}
    10891127char*
    1090 tr_bencSaveAsSerializedPHP( const tr_benc * top, int * len )
     1128tr_bencSaveAsJSON( const tr_benc * top, int * len )
    10911129{
    10921130    char * ret;
    10931131    struct WalkFuncs walkFuncs;
    1094     struct phpWalk data;
     1132    struct jsonWalk data;
    10951133
    10961134    data.out = evbuffer_new( );
    10971135    data.parents = NULL;
    10981136
    1099     walkFuncs.intFunc = phpIntFunc;
    1100     walkFuncs.stringFunc = phpStringFunc;
    1101     walkFuncs.dictBeginFunc = phpDictBeginFunc;
    1102     walkFuncs.listBeginFunc = phpListBeginFunc;
    1103     walkFuncs.containerEndFunc = phpContainerEndFunc;
     1137    walkFuncs.intFunc = jsonIntFunc;
     1138    walkFuncs.stringFunc = jsonStringFunc;
     1139    walkFuncs.dictBeginFunc = jsonDictBeginFunc;
     1140    walkFuncs.listBeginFunc = jsonListBeginFunc;
     1141    walkFuncs.containerEndFunc = jsonContainerEndFunc;
    11041142
    11051143    bencWalk( top, &walkFuncs, &data );
  • trunk/libtransmission/bencode.h

    r5810 r5821  
    117117
    118118char*  tr_bencSave( const tr_benc * val, int * len );
    119 char*  tr_bencSaveAsSerializedPHP( const tr_benc * top, int * len );
     119char*  tr_bencSaveAsJSON( const tr_benc * top, int * len );
    120120int    tr_bencSaveFile( const char * filename, const tr_benc * );
    121121int    tr_bencLoadFile( const char * filename, tr_benc * );
Note: See TracChangeset for help on using the changeset viewer.