Changeset 5821
- Timestamp:
- May 12, 2008, 5:54:57 PM (14 years ago)
- Location:
- trunk/libtransmission
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/bencode-test.c
r5772 r5821 272 272 273 273 static int 274 test PHPSnippet( const char * benc_str, const char * expected )274 testJSONSnippet( const char * benc_str, const char * expected ) 275 275 { 276 276 tr_benc top; 277 277 char * serialized; 278 278 tr_bencLoad( benc_str, strlen( benc_str ), &top, NULL ); 279 serialized = tr_bencSaveAs SerializedPHP( &top, NULL );279 serialized = tr_bencSaveAsJSON( &top, NULL ); 280 280 check( !strcmp( serialized, expected ) ); 281 281 tr_free( serialized ); … … 285 285 286 286 static int 287 test PHP( void )287 testJSON( void ) 288 288 { 289 289 int val; … … 292 292 293 293 benc_str = "i6e"; 294 expected = " i:6;";295 if(( val = test PHPSnippet( benc_str, expected )))294 expected = "6"; 295 if(( val = testJSONSnippet( benc_str, expected ))) 296 296 return val; 297 297 298 benc_str = "d 3:cow3:moo4:spam4:eggse";299 expected = " a:2:{s:3:\"cow\";s:3:\"moo\";s:4:\"spam\";s:4:\"eggs\";}";300 if(( val = test PHPSnippet( benc_str, expected )))298 benc_str = "d5:helloi1e5:worldi2ee"; 299 expected = "{ \"hello\": 1, \"world\": 2 }"; 300 if(( val = testJSONSnippet( benc_str, expected ))) 301 301 return val; 302 302 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 ))) 306 311 return val; 307 312 … … 355 360 return i; 356 361 357 if(( i = test PHP( )))362 if(( i = testJSON( ))) 358 363 return i; 359 364 -
trunk/libtransmission/bencode.c
r5810 r5821 1013 1013 struct ParentState 1014 1014 { 1015 int type; 1016 int index; 1015 int bencType; 1016 int childIndex; 1017 int childCount; 1017 1018 }; 1018 1019 1019 struct phpWalk1020 struct jsonWalk 1020 1021 { 1021 1022 tr_list * parents; … … 1024 1025 1025 1026 static void 1026 phpChildFunc( struct phpWalk * data )1027 jsonChildFunc( struct jsonWalk * data ) 1027 1028 { 1028 1029 if( data->parents ) … … 1030 1031 struct ParentState * parentState = data->parents->data; 1031 1032 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 1060 static void 1061 jsonPushParent( struct jsonWalk * data, const tr_benc * benc ) 1039 1062 { 1040 1063 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; 1043 1067 tr_list_prepend( &data->parents, parentState ); 1044 1068 } 1045 1069 1046 1070 static void 1047 phpPopParent( struct phpWalk * data )1071 jsonPopParent( struct jsonWalk * data ) 1048 1072 { 1049 1073 tr_free( tr_list_pop_front( &data->parents ) ); … … 1051 1075 1052 1076 static 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, "}" ); 1077 jsonIntFunc( 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 } 1083 static void 1084 jsonStringFunc( 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 } 1106 static void 1107 jsonDictBeginFunc( const tr_benc * val, void * vdata ) 1108 { 1109 struct jsonWalk * data = vdata; 1110 jsonPushParent( data, val ); 1111 evbuffer_add_printf( data->out, "{ " ); 1112 } 1113 static void 1114 jsonListBeginFunc( const tr_benc * val, void * vdata ) 1115 { 1116 struct jsonWalk * data = vdata; 1117 jsonPushParent( data, val ); 1118 evbuffer_add_printf( data->out, "[ " ); 1119 } 1120 static void 1121 jsonContainerEndFunc( const tr_benc * val UNUSED, void * vdata ) 1122 { 1123 struct jsonWalk * data = vdata; 1124 jsonPopParent( data ); 1125 jsonChildFunc( data ); 1088 1126 } 1089 1127 char* 1090 tr_bencSaveAs SerializedPHP( const tr_benc * top, int * len )1128 tr_bencSaveAsJSON( const tr_benc * top, int * len ) 1091 1129 { 1092 1130 char * ret; 1093 1131 struct WalkFuncs walkFuncs; 1094 struct phpWalk data;1132 struct jsonWalk data; 1095 1133 1096 1134 data.out = evbuffer_new( ); 1097 1135 data.parents = NULL; 1098 1136 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; 1104 1142 1105 1143 bencWalk( top, &walkFuncs, &data ); -
trunk/libtransmission/bencode.h
r5810 r5821 117 117 118 118 char* tr_bencSave( const tr_benc * val, int * len ); 119 char* tr_bencSaveAs SerializedPHP( const tr_benc * top, int * len );119 char* tr_bencSaveAsJSON( const tr_benc * top, int * len ); 120 120 int tr_bencSaveFile( const char * filename, const tr_benc * ); 121 121 int tr_bencLoadFile( const char * filename, tr_benc * );
Note: See TracChangeset
for help on using the changeset viewer.