Changeset 4883
- Timestamp:
- Jan 31, 2008, 3:20:44 PM (14 years ago)
- Location:
- branches/1.0x/libtransmission
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/1.0x/libtransmission/bencode-test.c
r4877 r4883 152 152 err = tr_bencParse( buf, buf + sizeof( buf ), &val, &end ); 153 153 check( !err ); 154 check( tr_bencIsInt( &val ) );155 154 check( tr_bencGetInt( &val ) == 64 ); 156 155 check( end == buf + 4 ); … … 160 159 err = tr_bencParse( buf, buf + sizeof( buf ), &val, &end ); 161 160 check( !err ); 162 check( tr_bencIsList( &val ) );163 161 check( end == buf + strlen( (char*)buf ) ); 164 162 check( val.val.l.count == 3 ); … … 205 203 check( !err ); 206 204 check( end == buf + strlen( (const char*)buf ) ); 207 check( tr_bencIsList( &val ) );208 205 check(( child = tr_bencListGetNthChild( &val, 0 ))); 209 check( tr_bencIsList( child ) );210 206 check(( child2 = tr_bencListGetNthChild( child, 0 ))); 211 check( tr_bencIsDict( child2 ) );212 207 saved = tr_bencSave( &val, &len ); 213 208 check( !strcmp( saved, "lld1:ai64e1:bi32eeee" ) ); … … 217 212 end = NULL; 218 213 snprintf( (char*)buf, sizeof( buf ), "d8:completei1e8:intervali1800e12:min intervali1800e5:peers0:e" ); 219 err = tr_benc Load( buf, sizeof( buf ), &val, (char**)&end );214 err = tr_bencParse( buf, buf+sizeof( buf ), &val, &end ); 220 215 check( !err ); 221 216 check( end == buf + strlen( (const char*)buf ) ); … … 246 241 err = tr_bencParse( in, in+(depth*2), &val, &end ); 247 242 check( !err ); 248 check( tr_bencIsList( &val ) );249 243 check( end == in+(depth*2) ); 250 244 saved = tr_bencSave( &val, &len ); -
branches/1.0x/libtransmission/bencode.c
r4880 r4883 41 41 **/ 42 42 43 int43 static int 44 44 tr_bencIsInt( const benc_val_t * val ) 45 45 { … … 47 47 } 48 48 49 int 50 tr_bencIsString( const benc_val_t * val ) 51 { 52 return val!=NULL && val->type==TYPE_STR; 53 } 54 55 int 49 static int 56 50 tr_bencIsList( const benc_val_t * val ) 57 51 { … … 59 53 } 60 54 61 int55 static int 62 56 tr_bencIsDict( const benc_val_t * val ) 63 57 { … … 581 575 nodeNew( const benc_val_t * val ) 582 576 { 583 switch( val->type ) 584 { 585 case TYPE_INT: 586 case TYPE_STR: 587 return nodeNewLeaf( val ); 588 break; 589 case TYPE_LIST: 590 return nodeNewList( val ); 591 break; 592 case TYPE_DICT: 593 return nodeNewDict( val ); 594 break; 595 } 596 597 assert( 0 && "invalid type!" ); 598 return NULL; 599 } 600 601 typedef void (*BencNodeWalkFunc)( const benc_val_t * val, void * user_data ); 577 struct SaveNode * node; 578 579 if( val->type == TYPE_LIST ) 580 node = nodeNewList( val ); 581 else if( val->type == TYPE_DICT ) 582 node = nodeNewDict( val ); 583 else 584 node = nodeNewLeaf( val ); 585 586 return node; 587 } 588 589 typedef void (*BencWalkFunc)( const benc_val_t * val, void * user_data ); 602 590 603 591 struct WalkFuncs 604 592 { 605 Benc NodeWalkFunc intFunc;606 Benc NodeWalkFunc stringFunc;607 Benc NodeWalkFunc dictBeginFunc;608 Benc NodeWalkFunc listBeginFunc;609 Benc NodeWalkFunc containerEndFunc;593 BencWalkFunc intFunc; 594 BencWalkFunc stringFunc; 595 BencWalkFunc dictBeginFunc; 596 BencWalkFunc listBeginFunc; 597 BencWalkFunc containerEndFunc; 610 598 }; 611 599 … … 673 661 674 662 default: 675 assert( 0 && "invalid type!" ); 663 /* did caller give us an uninitialized val? */ 664 tr_err( "Invalid benc type %d", val->type ); 665 break; 676 666 } 677 667 } … … 685 675 686 676 static void 687 saveIntFunc( const benc_val_t * val, void * buf )688 { 689 evbuffer_add_printf( buf, "i%"PRId64"e", tr_bencGetInt(val) );690 } 691 static void 692 saveStringFunc( const benc_val_t * val, void * user_data)693 { 694 struct evbuffer * out = user_data;695 evbuffer_add_printf( out, "%i:", val->val.s.i );696 evbuffer_add( out, val->val.s.s, val->val.s.i );697 } 698 static void 699 saveDictBeginFunc( const benc_val_t * val UNUSED, void * buf )700 { 701 evbuffer_add_printf( buf, "d" );702 } 703 static void 704 saveListBeginFunc( const benc_val_t * val UNUSED, void * buf )705 { 706 evbuffer_add_printf( buf, "l" );707 } 708 static void 709 saveContainerEndFunc( const benc_val_t * val UNUSED, void * buf )710 { 711 evbuffer_add_printf( buf, "e" );677 saveIntFunc( const benc_val_t * val, void * evbuf ) 678 { 679 evbuffer_add_printf( evbuf, "i%"PRId64"e", tr_bencGetInt(val) ); 680 } 681 static void 682 saveStringFunc( const benc_val_t * val, void * vevbuf ) 683 { 684 struct evbuffer * evbuf = vevbuf; 685 evbuffer_add_printf( evbuf, "%i:", val->val.s.i ); 686 evbuffer_add( evbuf, val->val.s.s, val->val.s.i ); 687 } 688 static void 689 saveDictBeginFunc( const benc_val_t * val UNUSED, void * evbuf ) 690 { 691 evbuffer_add_printf( evbuf, "d" ); 692 } 693 static void 694 saveListBeginFunc( const benc_val_t * val UNUSED, void * evbuf ) 695 { 696 evbuffer_add_printf( evbuf, "l" ); 697 } 698 static void 699 saveContainerEndFunc( const benc_val_t * val UNUSED, void * evbuf ) 700 { 701 evbuffer_add_printf( evbuf, "e" ); 712 702 } 713 703 char* -
branches/1.0x/libtransmission/bencode.h
r4875 r4883 119 119 **/ 120 120 121 int tr_bencIsInt( const benc_val_t * val );122 int tr_bencIsList( const benc_val_t * val );123 int tr_bencIsDict( const benc_val_t * val );124 int tr_bencIsString( const benc_val_t * val );125 126 121 benc_val_t* tr_bencListGetNthChild( benc_val_t * val, int i ); 127 122
Note: See TracChangeset
for help on using the changeset viewer.