Changeset 2045 for libswish3/trunk/bindings
- Timestamp:
- 03/07/08 22:32:33 (10 months ago)
- Files:
-
- libswish3/trunk/bindings/perl/3.xs (modified) (4 diffs)
- libswish3/trunk/bindings/perl/XS/Analyzer.xs (modified) (2 diffs)
- libswish3/trunk/bindings/perl/XS/Config.xs (modified) (2 diffs)
- libswish3/trunk/bindings/perl/XS/Constants.xs (modified) (1 diff)
- libswish3/trunk/bindings/perl/XS/MetaName.xs (added)
- libswish3/trunk/bindings/perl/XS/MetaNameHash.xs (added)
- libswish3/trunk/bindings/perl/XS/Property.xs (added)
- libswish3/trunk/bindings/perl/XS/PropertyHash.xs (added)
- libswish3/trunk/bindings/perl/XS/Stash.xs (modified) (2 diffs)
- libswish3/trunk/bindings/perl/XS/xml2Hash.xs (added)
- libswish3/trunk/bindings/perl/macros.h (modified) (2 diffs)
- libswish3/trunk/bindings/perl/t/14-config.t (added)
- libswish3/trunk/bindings/perl/t/15-analyzer.t (added)
- libswish3/trunk/bindings/perl/t/20metanames.t (modified) (2 diffs)
- libswish3/trunk/bindings/perl/t/t.conf (modified) (1 diff)
- libswish3/trunk/bindings/perl/typemap (modified) (1 diff)
- libswish3/trunk/bindings/perl/xs_helpers.c (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
libswish3/trunk/bindings/perl/3.xs
r2029 r2045 163 163 164 164 // set_config 165 case 1: self->config->ref_cnt--; 165 case 1: if (!sv_derived_from(ST(1), CONFIG_CLASS)) { 166 croak("usage: must be a %s object", CONFIG_CLASS); 167 } 168 169 self->config->ref_cnt--; 166 170 //warn("set_config ref_cnt of old config = %d", self->config->ref_cnt); 167 171 if (self->config->ref_cnt < 1) { … … 189 193 190 194 // set_analyzer 191 case 3: self->analyzer->ref_cnt--; 195 case 3: if (!sv_derived_from(ST(1), ANALYZER_CLASS)) { 196 croak("usage: must be a %s object", ANALYZER_CLASS); 197 } 198 199 self->analyzer->ref_cnt--; 192 200 //warn("set_analyzer ref_cnt of old analyzer: %d", self->analyzer->ref_cnt); 193 201 if (self->analyzer->ref_cnt < 1) { … … 211 219 212 220 // set_parser 213 case 5: self->parser->ref_cnt--; 221 case 5: if (!sv_derived_from(ST(1), PARSER_CLASS)) { 222 croak("usage: must be a %s object", PARSER_CLASS); 223 } 224 225 self->parser->ref_cnt--; 214 226 if (self->parser->ref_cnt < 1) { 215 227 if (SWISH_DEBUG) { … … 342 354 INCLUDE: XS/Data.xs 343 355 INCLUDE: XS/Stash.xs 344 356 INCLUDE: XS/Property.xs 357 INCLUDE: XS/MetaName.xs 358 INCLUDE: XS/PropertyHash.xs 359 INCLUDE: XS/MetaNameHash.xs 360 INCLUDE: XS/xml2Hash.xs 361 362 libswish3/trunk/bindings/perl/XS/Analyzer.xs
r2030 r2045 9 9 swish_Config* config; 10 10 11 PREINIT: 12 HV* stash; 13 14 CODE: 15 //RETVAL = swish_init_analyzer((swish_Config*)sp_extract_ptr( (SV*)config )); 11 CODE: 16 12 RETVAL = swish_init_analyzer(config); 17 13 RETVAL->ref_cnt++; 18 stash = newHV(); 19 RETVAL->stash = newRV_inc((SV*)stash); 14 RETVAL->stash = sp_Stash_new(); 20 15 21 16 OUTPUT: … … 43 38 44 39 // set_regex 45 case 1: self->regex = ST(1); 40 case 1: sp_SV_is_qr(ST(1)); 41 self->regex = ST(1); 46 42 break; 47 43 48 // TODO test refcnt49 44 // get_regex 50 45 case 2: RETVAL = SvREFCNT_inc( self->regex ); 51 46 break; 52 47 53 // TODO set token_handler 48 // set token handler 49 case 3: sp_Stash_replace(self->stash, TOKEN_HANDLER_KEY, ST(1)); 50 break; 51 52 // get token handler 53 case 4: if (!sp_hvref_exists(self->stash, TOKEN_HANDLER_KEY)) { 54 croak("no token handler set"); 55 } 56 57 RETVAL = sp_Stash_get(self->stash, TOKEN_HANDLER_KEY); 58 break; 54 59 55 60 END_SET_OR_GET_SWITCH libswish3/trunk/bindings/perl/XS/Config.xs
r2030 r2045 16 16 17 17 18 AV* 19 keys(self) 20 swish_Config* self 21 22 CODE: 23 RETVAL = sp_get_xml2_hash_keys(self->conf); 24 25 OUTPUT: 26 RETVAL 27 28 29 # translate xml2 hashes into Perl hashes -- NOTE these are effectively read-only hashes 30 # you must use add() and delete() to actually write to the active config in memory 31 HV* 32 properties(self) 33 swish_Config* self 34 35 CODE: 36 RETVAL = sp_get_config_subconfig( self, SWISH_PROP ); 37 38 OUTPUT: 39 RETVAL 40 41 HV* 42 metanames(self) 18 19 void 20 set_default(self) 43 21 swish_Config *self 44 22 45 23 CODE: 46 RETVAL = sp_get_config_subconfig( self, SWISH_META);24 swish_config_set_default(self); 47 25 48 OUTPUT:49 RETVAL50 26 51 HV* 52 mimes(self) 53 swish_Config* self; 27 # accessors/mutators 28 void 29 _set_or_get(self, ...) 30 swish_Config *self; 31 ALIAS: 32 set_properties = 1 33 get_properties = 2 34 set_metanames = 3 35 get_metanames = 4 36 set_mimes = 5 37 get_mimes = 6 38 set_parsers = 7 39 get_parsers = 8 40 set_aliases = 9 41 get_aliases = 10 42 set_index = 11 43 get_index = 12 44 set_misc = 13 45 get_misc = 14 46 PREINIT: 47 SV* RETVAL; 48 PPCODE: 49 { 54 50 55 CODE: 56 RETVAL = sp_get_config_subconfig( self, SWISH_MIME ); 51 //warn("number of items %d for ix %d", items, ix); 52 53 START_SET_OR_GET_SWITCH 54 55 // set properties 56 case 1: croak("TODO"); 57 break; 58 59 // get properties 60 case 2: RETVAL = sp_bless_ptr( PROPERTY_HASH_CLASS, (IV)self->properties ); 61 break; 62 63 // set metanames 64 case 3: croak("TODO"); 65 break; 66 67 // get metanames 68 case 4: RETVAL = sp_bless_ptr( METANAME_HASH_CLASS, (IV)self->metanames ); 69 break; 70 71 // set mimes 72 case 5: croak("TODO"); 73 break; 74 75 // get mimes 76 case 6: RETVAL = sp_bless_ptr( XML2_HASH_CLASS, (IV)self->mimes ); 77 break; 78 79 // set parsers 80 case 7: croak("TODO"); 81 break; 82 83 // get parsers 84 case 8: RETVAL = sp_bless_ptr( XML2_HASH_CLASS, (IV)self->parsers ); 85 break; 86 87 // set aliases 88 case 9: croak("TODO"); 89 break; 90 91 // get aliases 92 case 10: RETVAL = sp_bless_ptr( XML2_HASH_CLASS, (IV)self->tag_aliases ); 93 break; 94 95 // set index 96 case 11: croak("TODO"); 97 break; 98 99 // get index 100 case 12: RETVAL = sp_bless_ptr( XML2_HASH_CLASS, (IV)self->index ); 101 break; 102 103 // set misc 104 case 13: croak("TODO"); 105 break; 106 107 // get misc 108 case 14: RETVAL = sp_bless_ptr( XML2_HASH_CLASS, (IV)self->misc ); 109 break; 57 110 58 OUTPUT: 59 RETVAL 60 61 62 HV* 63 parsers(self) 64 swish_Config* self; 65 66 CODE: 67 RETVAL = sp_get_config_subconfig( self, SWISH_PARSERS ); 68 69 OUTPUT: 70 RETVAL 111 END_SET_OR_GET_SWITCH 112 } 71 113 72 73 int 114 void 74 115 debug(self) 75 116 swish_Config* self 76 117 77 118 CODE: 78 RETVAL =swish_debug_config(self);119 swish_debug_config(self); 79 120 80 OUTPUT:81 RETVAL82 121 83 122 … … 100 139 warn("delete() not yet implemented\n"); 101 140 102 void103 subconfig_delete(self, key, subconf)104 swish_Config* self105 char* key106 xmlHashTablePtr subconf107 108 CODE:109 warn("subconfig_delete() not yet implemented\n");110 141 111 142 void libswish3/trunk/bindings/perl/XS/Constants.xs
r2028 r2045 7 7 stash = gv_stashpv("SWISH::3", TRUE); 8 8 newCONSTSUB(stash, "SWISH_PROP", newSVpv(SWISH_PROP, 0)); 9 newCONSTSUB(stash, "SWISH_PROP_MAX", newSVpv(SWISH_PROP_MAX, 0));10 newCONSTSUB(stash, "SWISH_PROP_SORT", newSVpv(SWISH_PROP_SORT, 0));11 9 newCONSTSUB(stash, "SWISH_META", newSVpv(SWISH_META, 0)); 12 10 newCONSTSUB(stash, "SWISH_MIME", newSVpv(SWISH_MIME, 0)); libswish3/trunk/bindings/perl/XS/Stash.xs
r2029 r2045 2 2 3 3 PROTOTYPES: enable 4 5 SV* 6 get(self,key) 7 SV* self; 8 SV* key; 9 10 PREINIT: 11 SV* value; 12 13 CODE: 14 RETVAL = sp_Stash_get( self, SvPV(key, PL_na) ); 15 SvREFCNT_inc(RETVAL); 16 17 OUTPUT: 18 RETVAL 19 20 21 void 22 set(self,key,value) 23 SV* self; 24 SV* key; 25 SV* value; 26 27 CODE: 28 sp_Stash_set(self, SvPV(key, PL_na), value); 29 30 31 32 AV* 33 keys(self) 34 SV* self; 35 36 CODE: 37 RETVAL = sp_hv_keys( sp_extract_hash(self) ); 38 39 OUTPUT: 40 RETVAL 41 42 43 AV* 44 values(self) 45 SV* self; 46 47 CODE: 48 RETVAL = sp_hv_values( sp_extract_hash(self) ); 49 50 OUTPUT: 51 RETVAL 52 4 53 5 54 void … … 8 57 9 58 CODE: 10 59 11 60 if (SWISH_DEBUG) { 12 warn("DESTROYing Stash object %s [%d]",13 SvPV(ST(0), PL_na), self);61 warn("DESTROYing Stash object %s [0x%x]", 62 SvPV(ST(0), PL_na), (int)self); 14 63 15 64 } libswish3/trunk/bindings/perl/macros.h
r2029 r2045 14 14 #define WORD_CLASS "SWISH::3::Word" 15 15 #define DOC_CLASS "SWISH::3::Doc" 16 #define PROPERTY_CLASS "SWISH::3::Property" 17 #define METANAME_CLASS "SWISH::3::MetaName" 18 #define PROPERTY_HASH_CLASS "SWISH::3::PropertyHash" 19 #define METANAME_HASH_CLASS "SWISH::3::MetaNameHash" 20 #define XML2_HASH_CLASS "SWISH::3::xml2Hash" 16 21 #define SELF_KEY "sp_self" 17 22 #define CONFIG_KEY "sp_config" … … 31 36 if (items < 2) \ 32 37 croak("usage: $object->set_xxxxxx($val)"); \ 38 \ 39 if (!SvTRUE(ST(1))) \ 40 croak("usage: requires true value"); \ 33 41 } \ 34 42 else { \ libswish3/trunk/bindings/perl/t/20metanames.t
r2019 r2045 5 5 6 6 ok( my $s3 = SWISH::3->new( 7 config => '<swish config><MetaNames>foo</MetaNames></swishconfig>',7 config => '<swish><MetaNames><foo /></MetaNames></swish>', 8 8 handler => \&getmeta 9 9 ), … … 31 31 #$data->wordlist->debug; 32 32 33 34 33 } libswish3/trunk/bindings/perl/t/t.conf
r2014 r2045 1 <swishconfig> 2 <!-- v attr indicates what handler should do with property buffer --> 3 <PropertyNames>title</PropertyNames> 4 <PropertyNames type="nothing">fooname</PropertyNames> 5 <PropertyNames type="numeric">someNum</PropertyNames> 6 <PropertyNames type="comparecase">SoMeCaSe</PropertyNames> 7 <PropertyNames type="ignorecase">SoMeOTHERCaSe</PropertyNames> 8 <PropertyNames type="date">dAte</PropertyNames> 9 <PropertyNames type="nostripchars">propWithNewlines</PropertyNames> 10 <!-- these are used when buffering and storing properties --> 11 <PropertyNamesMaxLength type="1000">fooname</PropertyNamesMaxLength> 12 <PropertyNamesSortKeyLength type="100">fooname</PropertyNamesSortKeyLength> 13 <!-- every PropertyName must also be listed as a MetaName --> 14 <MetaNames>title fooname someNum SoMeCase soMeOTHERCaSe dAte propWithNewlines</MetaNames> 15 </swishconfig> 1 <swish> 2 <PropertyNames> 3 <title /> 4 <foobar /> 5 <someNum type="int" /> 6 <SoMeCaSe ignore_case="0" /> 7 <SoMeOTHERCaSe ignore_case="1" /> 8 <dAte type="date" /> 9 <propWithNewlines verbatim="1" /> 10 <!-- these are used when buffering and storing properties --> 11 <fooname max="1000" sort="1" /> 12 </PropertyNames> 13 <MetaNames> 14 <!-- every PropertyName must also be listed as a MetaName --> 15 <title /> 16 <fooname /> 17 <someNum /> 18 <SoMeCase /> 19 <soMeOTHERCaSe /> 20 <dAte /> 21 <propWithNewlines /> 22 </MetaNames> 23 </swish> 16 24 libswish3/trunk/bindings/perl/typemap
r2028 r2045 13 13 swish_NamedBuffer* O_OBJECT 14 14 swish_3* O_OBJECT 15 swish_Property* O_OBJECT 16 swish_MetaName* O_OBJECT 15 17 16 18 INPUT libswish3/trunk/bindings/perl/xs_helpers.c
r2030 r2045 6 6 /* C code to make writing XS easier */ 7 7 8 static AV* sp_hv_keys(HV* hash); 9 static AV* sp_hv_values(HV* hash); 8 10 static SV* sp_hv_store( HV* h, const char* key, SV* val ); 9 11 static SV* sp_hv_store_char( HV* h, const char* key, char *val ); … … 26 28 static void sp_describe_object( SV* object ); 27 29 static IV sp_extract_ptr( SV* object ); 28 static void sp_store_xml2_pair_in_perl_hash( xmlChar* val, HV* perl_hash, xmlChar* key );29 static HV* sp_xml2_hash_to_perl_hash( xmlHashTablePtr xml2_hash );30 static void sp_add_key_to_array(xmlChar* val, AV* mykeys, xmlChar* key);31 30 static AV* sp_get_xml2_hash_keys( xmlHashTablePtr xml2_hash ); 31 static void sp_add_key_to_array(xmlChar *val, AV *keys, xmlChar *key); 32 static SV* sp_xml2_hash_to_perl_hash( xmlHashTablePtr xml2_hash, const char* class ); 33 static void sp_perl_hash_to_xml2_hash( HV* perlhash, xmlHashTablePtr xml2hash ); 32 34 static void sp_nb_hash_to_phash(xmlBufferPtr buf, HV *phash, xmlChar *key); 33 35 static HV* sp_nb_to_hash( swish_NamedBuffer* nb ); … … 38 40 static void sp_SV_is_qr( SV *qr ); 39 41 static void sp_debug_token( swish_Token *token ); 40 static HV* sp_get_config_subconfig( swish_Config* config, const char* key );41 42 42 43 /* implement nearly all methods for SWISH::3::Stash, a private class */ 43 44 44 45 static SV* sp_Stash_new(); 45 static void sp_Stash_set( SV *object, const char *key, SV *value ); 46 static void sp_Stash_set_char( SV *object, const char *key, char *value ); 47 static SV* sp_Stash_get( SV *object, const char *key ); 48 static char* sp_Stash_get_char( SV *object, const char *key ); 49 static void sp_Stash_replace( SV *object, const char *key, SV *value ); 50 static int sp_Stash_inner_refcnt( SV *object ); 51 static void sp_Stash_destroy( SV *object ); 46 static void sp_Stash_set( SV *stash, const char *key, SV *value ); 47 static void sp_Stash_set_char( SV *stash, const char *key, char *value ); 48 static SV* sp_Stash_get( SV *stash, const char *key ); 49 static char* sp_Stash_get_char( SV *stash, const char *key ); 50 static void sp_Stash_replace( SV *stash, const char *key, SV *value ); 51 static int sp_Stash_inner_refcnt( SV *stash ); 52 static void sp_Stash_destroy( SV *stash ); 53 static void sp_Stash_dec_values( SV *stash ); 52 54 53 55 static SV* … … 113 115 { 114 116 HV *hash; 117 sp_Stash_dec_values(object); 115 118 hash = sp_extract_hash( object ); 116 119 if ( SWISH_DEBUG ) { … … 129 132 } 130 133 } 134 135 static void 136 sp_Stash_dec_values(SV* stash) 137 { 138 HV* hash; 139 HE* hash_entry; 140 int num_keys, i; 141 SV* sv_val; 142 143 hash = sp_extract_hash( stash ); 144 num_keys = hv_iterinit(hash); 145 //warn("Stash has %d keys", num_keys); 146 for (i = 0; i < num_keys; i++) { 147 hash_entry = hv_iternext(hash); 148 sv_val = hv_iterval(hash, hash_entry); 149 if( SvREFCNT(sv_val) > 1 ) { //&& SvTYPE(SvRV(sv_val)) == SVt_IV ) { 150 warn("Stash value is a ptr object with refcount = %d", SvREFCNT(sv_val)); 151 SvREFCNT_dec( sv_val ); 152 } 153 } 154 } 155 131 156 132 157 … … 145 170 } 146 171 } 172 173 static AV* 174 sp_hv_keys(HV* hash) 175 { 176 HE* hash_entry; 177 int num_keys, i; 178 SV* sv_key; 179 char* key; 180 SV* sv_keep; 181 AV* keys; 182 183 keys = newAV(); 184 num_keys = hv_iterinit(hash); 185 av_extend(keys, (I32)num_keys); 186 187 for (i = 0; i < num_keys; i++) { 188 hash_entry = hv_iternext(hash); 189 sv_key = hv_iterkeysv(hash_entry); 190 key = SvPV(sv_key, PL_na); 191 if ( xmlStrEqual( (xmlChar*)SELF_CLASS_KEY, (xmlChar*)key ) ) 192 continue; 193 194 sv_keep = newSVpv( key, 0 ); 195 av_push(keys, sv_keep); 196 } 197 198 //SvREFCNT_inc(keys); 199 200 return keys; 201 } 202 203 static AV* 204 sp_hv_values(HV* hash) 205 { 206 HE* hash_entry; 207 int num_keys, i; 208 SV* sv_val; 209 SV* sv_key; 210 char* key; 211 AV* values; 212 213 values = newAV(); 214 num_keys = hv_iterinit(hash); 215 av_extend(values, (I32)num_keys); 216 217 for (i = 0; i < num_keys; i++) { 218 hash_entry = hv_iternext(hash); 219 sv_key = hv_iterkeysv(hash_entry); 220 key = SvPV(sv_key, PL_na); 221 if ( xmlStrEqual( (xmlChar*)SELF_CLASS_KEY, (xmlChar*)key ) ) 222 continue; 223 224 sv_val = hv_iterval(hash, hash_entry); 225 av_push(values, sv_val); 226 } 227 228 return values; 229 } 230 231 147 232 148 233 /* store SV* in a hash, incrementing its refcnt */ … … 402 487 403 488 static void 404 sp_store_xml2_pair_in_perl_hash(xmlChar* val, HV* perl_hash, xmlChar* key)405 {406 dTHX;407 hv_store( perl_hash,408 (char*)key,409 strlen((char*)key),410 newSVpvn((char*)val, strlen((char*)val)),411 0);412 }413 414 static HV*415 sp_xml2_hash_to_perl_hash( xmlHashTablePtr xml2_hash )416 {417 dTHX;418 HV* perl_hash = newHV();419 /* perl bug means we must increm ref count manually */420 SvREFCNT_inc((SV*)perl_hash);421 xmlHashScan(xml2_hash, (xmlHashScanner)sp_store_xml2_pair_in_perl_hash, perl_hash);422 return perl_hash;423 }424 425 static void426 489 sp_add_key_to_array(xmlChar* val, AV* mykeys, xmlChar* key) 427 490 { … … 429 492 av_push(mykeys, newSVpvn((char*)key, strlen((char*)key))); 430 493 } 431 494 432 495 static AV* 433 496 sp_get_xml2_hash_keys( xmlHashTablePtr xml2_hash ) … … 438 501 xmlHashScan(xml2_hash, (xmlHashScanner)sp_add_key_to_array, mykeys); 439 502 return mykeys; 503 } 504 505 506 static void 507 sp_make_perl_hash(char* value, SV* stash, xmlChar* key) 508 { 509 sp_Stash_set_char(stash, (const char*)key, value ); 510 } 511 512 513 static SV* 514 sp_xml2_hash_to_perl_hash( xmlHashTablePtr xml2_hash, const char* class ) 515 { 516 dTHX; 517 SV* stash; 518 stash = sp_Stash_new(); 519 sp_Stash_set_char(stash, SELF_CLASS_KEY, "xml2hash"); 520 xmlHashScan(xml2_hash, (xmlHashScanner)sp_make_perl_hash, stash); 521 sp_describe_object( stash ); 522 return stash; 523 } 524 525 static void 526 sp_perl_hash_to_xml2_hash( HV* perlhash, xmlHashTablePtr xml2hash ) 527 { 528 // TODO 440 529 } 441 530 … … 733 822 } 734 823 735 static HV*736 sp_get_config_subconfig( swish_Config* config, const char* key )737 {738 return sp_xml2_hash_to_perl_hash( swish_subconfig_hash( config, (xmlChar*)key ) );739 }740
