Changeset 11780
- Timestamp:
- Jan 29, 2011, 6:14:35 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/bencode.c
r11748 r11780 943 943 944 944 static void 945 nodeInitDict( struct SaveNode * node, const tr_benc * val ) 946 { 947 int i, j; 948 int nKeys; 949 struct KeyIndex * indices; 945 nodeInitDict( struct SaveNode * node, const tr_benc * val, tr_bool sort_dicts ) 946 { 947 int nKeys; 948 const int n = val->val.l.count; 950 949 951 950 assert( tr_bencIsDict( val ) ); 952 951 953 nKeys = val->val.l.count/ 2;952 nKeys = n / 2; 954 953 node->val = val; 955 954 node->children = tr_new0( int, nKeys * 2 ); 956 955 957 /* ugh, a dictionary's children have to be sorted by key... */ 958 indices = tr_new( struct KeyIndex, nKeys ); 959 for( i = j = 0; i < ( nKeys * 2 ); i += 2, ++j ) 956 if( sort_dicts ) 960 957 { 961 indices[j].key = getStr(&val->val.l.vals[i]); 962 indices[j].index = i; 963 } 964 qsort( indices, j, sizeof( struct KeyIndex ), compareKeyIndex ); 965 for( i = 0; i < j; ++i ) 958 int i, j; 959 struct KeyIndex * indices = tr_new( struct KeyIndex, nKeys ); 960 for( i=j=0; i<n; i+=2, ++j ) 961 { 962 indices[j].key = getStr(&val->val.l.vals[i]); 963 indices[j].index = i; 964 } 965 qsort( indices, j, sizeof( struct KeyIndex ), compareKeyIndex ); 966 for( i = 0; i < j; ++i ) 967 { 968 const int index = indices[i].index; 969 node->children[node->childCount++] = index; 970 node->children[node->childCount++] = index + 1; 971 } 972 973 tr_free( indices ); 974 } 975 else 966 976 { 967 const int index = indices[i].index;968 node->children[node->childCount++] = index; 969 node->children[node->childCount++] = index + 1;970 }971 972 assert( node->childCount == nKeys * 2 ); 973 tr_free( indices);977 int i ; 978 979 for( i=0; i<n; ++i ) 980 node->children[node->childCount++] = i; 981 } 982 983 assert( node->childCount == n ); 974 984 } 975 985 … … 998 1008 999 1009 static void 1000 nodeInit( struct SaveNode * node, const tr_benc * val )1010 nodeInit( struct SaveNode * node, const tr_benc * val, tr_bool sort_dicts ) 1001 1011 { 1002 1012 static const struct SaveNode INIT_NODE = { NULL, 0, 0, 0, NULL }; … … 1004 1014 1005 1015 if( tr_bencIsList( val ) ) nodeInitList( node, val ); 1006 else if( tr_bencIsDict( val ) ) nodeInitDict( node, val );1016 else if( tr_bencIsDict( val ) ) nodeInitDict( node, val, sort_dicts ); 1007 1017 else nodeInitLeaf( node, val ); 1008 1018 } … … 1029 1039 bencWalk( const tr_benc * top, 1030 1040 const struct WalkFuncs * walkFuncs, 1031 void * user_data ) 1041 void * user_data, 1042 tr_bool sort_dicts ) 1032 1043 { 1033 1044 int stackSize = 0; … … 1035 1046 struct SaveNode * stack = tr_new( struct SaveNode, stackAlloc ); 1036 1047 1037 nodeInit( &stack[stackSize++], top );1048 nodeInit( &stack[stackSize++], top, sort_dicts ); 1038 1049 1039 1050 while( stackSize > 0 ) … … 1087 1098 stack = tr_renew( struct SaveNode, stack, stackAlloc ); 1088 1099 } 1089 nodeInit( &stack[stackSize++], val );1100 nodeInit( &stack[stackSize++], val, sort_dicts ); 1090 1101 } 1091 1102 break; … … 1099 1110 stack = tr_renew( struct SaveNode, stack, stackAlloc ); 1100 1111 } 1101 nodeInit( &stack[stackSize++], val );1112 nodeInit( &stack[stackSize++], val, sort_dicts ); 1102 1113 } 1103 1114 break; … … 1216 1227 { 1217 1228 if( isSomething( val ) ) 1218 bencWalk( val, &freeWalkFuncs, NULL );1229 bencWalk( val, &freeWalkFuncs, NULL, FALSE ); 1219 1230 } 1220 1231 … … 1606 1617 { 1607 1618 case TR_FMT_BENC: 1608 bencWalk( top, &saveFuncs, buf );1619 bencWalk( top, &saveFuncs, buf, TRUE ); 1609 1620 break; 1610 1621 … … 1615 1626 data.out = buf; 1616 1627 data.parents = NULL; 1617 bencWalk( top, &jsonWalkFuncs, &data );1628 bencWalk( top, &jsonWalkFuncs, &data, TRUE ); 1618 1629 if( evbuffer_get_length( buf ) ) 1619 1630 evbuffer_add_printf( buf, "\n" );
Note: See TracChangeset
for help on using the changeset viewer.