Changeset 2045

Show
Ignore:
Timestamp:
03/07/08 22:32:33 (2 months ago)
Author:
karpet
Message:

new config support

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libswish3/trunk/bindings/perl/3.xs

    r2029 r2045  
    163163 
    164164    // 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--; 
    166170             //warn("set_config ref_cnt of old config = %d", self->config->ref_cnt); 
    167171             if (self->config->ref_cnt < 1) { 
     
    189193 
    190194    // 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--; 
    192200             //warn("set_analyzer ref_cnt of old analyzer: %d", self->analyzer->ref_cnt); 
    193201             if (self->analyzer->ref_cnt < 1) { 
     
    211219 
    212220    // 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--; 
    214226             if (self->parser->ref_cnt < 1) { 
    215227                if (SWISH_DEBUG) { 
     
    342354INCLUDE: XS/Data.xs 
    343355INCLUDE: XS/Stash.xs 
    344  
     356INCLUDE: XS/Property.xs 
     357INCLUDE: XS/MetaName.xs 
     358INCLUDE: XS/PropertyHash.xs 
     359INCLUDE: XS/MetaNameHash.xs 
     360INCLUDE: XS/xml2Hash.xs 
     361 
     362 
  • libswish3/trunk/bindings/perl/XS/Analyzer.xs

    r2030 r2045  
    99    swish_Config*   config; 
    1010     
    11     PREINIT: 
    12         HV* stash; 
    13  
    14     CODE: 
    15         //RETVAL = swish_init_analyzer((swish_Config*)sp_extract_ptr( (SV*)config )); 
     11    CODE: 
    1612        RETVAL = swish_init_analyzer(config); 
    1713        RETVAL->ref_cnt++; 
    18         stash = newHV(); 
    19         RETVAL->stash = newRV_inc((SV*)stash); 
     14        RETVAL->stash = sp_Stash_new(); 
    2015         
    2116    OUTPUT: 
     
    4338 
    4439    // set_regex 
    45     case 1:  self->regex = ST(1); 
     40    case 1:  sp_SV_is_qr(ST(1)); 
     41             self->regex = ST(1); 
    4642             break; 
    4743              
    48     // TODO test refcnt 
    4944    // get_regex 
    5045    case 2:  RETVAL  = SvREFCNT_inc( self->regex ); 
    5146             break; 
    5247              
    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; 
    5459         
    5560    END_SET_OR_GET_SWITCH 
  • libswish3/trunk/bindings/perl/XS/Config.xs

    r2030 r2045  
    1616 
    1717 
    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         
     19void 
     20set_default(self) 
    4321    swish_Config *self 
    4422     
    4523    CODE: 
    46         RETVAL = sp_get_config_subconfig( self, SWISH_META ); 
     24        swish_config_set_default(self); 
    4725         
    48     OUTPUT: 
    49         RETVAL 
    5026 
    51 HV* 
    52 mimes(self) 
    53     swish_Config* self; 
     27# accessors/mutators 
     28void 
     29_set_or_get(self, ...) 
     30    swish_Config *self; 
     31ALIAS: 
     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 
     46PREINIT: 
     47    SV* RETVAL; 
     48PPCODE: 
     49
    5450     
    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; 
    57110         
    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
    71113  
    72  
    73 int 
     114void 
    74115debug(self) 
    75116    swish_Config* self 
    76117     
    77118    CODE: 
    78         RETVAL = swish_debug_config(self); 
     119        swish_debug_config(self); 
    79120         
    80     OUTPUT: 
    81         RETVAL 
    82121 
    83122 
     
    100139        warn("delete() not yet implemented\n"); 
    101140         
    102 void 
    103 subconfig_delete(self, key, subconf) 
    104     swish_Config* self 
    105     char* key 
    106     xmlHashTablePtr subconf 
    107      
    108     CODE: 
    109         warn("subconfig_delete() not yet implemented\n"); 
    110141 
    111142void 
  • libswish3/trunk/bindings/perl/XS/Constants.xs

    r2028 r2045  
    77        stash = gv_stashpv("SWISH::3",       TRUE); 
    88        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)); 
    119        newCONSTSUB(stash, "SWISH_META",           newSVpv(SWISH_META, 0)); 
    1210        newCONSTSUB(stash, "SWISH_MIME",           newSVpv(SWISH_MIME, 0)); 
  • libswish3/trunk/bindings/perl/XS/Stash.xs

    r2029 r2045  
    22 
    33PROTOTYPES: enable 
     4 
     5SV* 
     6get(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 
     21void 
     22set(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 
     32AV* 
     33keys(self) 
     34    SV* self; 
     35             
     36    CODE: 
     37        RETVAL = sp_hv_keys( sp_extract_hash(self) ); 
     38     
     39    OUTPUT: 
     40        RETVAL     
     41 
     42 
     43AV* 
     44values(self) 
     45    SV* self; 
     46             
     47    CODE: 
     48        RETVAL = sp_hv_values( sp_extract_hash(self) ); 
     49     
     50    OUTPUT: 
     51        RETVAL     
     52 
    453 
    554void 
     
    857     
    958    CODE: 
    10  
     59     
    1160        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); 
    1463             
    1564        } 
  • libswish3/trunk/bindings/perl/macros.h

    r2029 r2045  
    1414#define WORD_CLASS          "SWISH::3::Word" 
    1515#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" 
    1621#define SELF_KEY            "sp_self" 
    1722#define CONFIG_KEY          "sp_config" 
     
    3136        if (items < 2) \ 
    3237            croak("usage: $object->set_xxxxxx($val)"); \ 
     38        \ 
     39        if (!SvTRUE(ST(1))) \ 
     40            croak("usage: requires true value"); \ 
    3341    } \ 
    3442    else { \ 
  • libswish3/trunk/bindings/perl/t/20metanames.t

    r2019 r2045  
    55 
    66ok( my $s3 = SWISH::3->new( 
    7         config  => '<swishconfig><MetaNames>foo</MetaNames></swishconfig>', 
     7        config  => '<swish><MetaNames><foo /></MetaNames></swish>', 
    88        handler => \&getmeta 
    99    ), 
     
    3131    #$data->wordlist->debug; 
    3232 
    33  
    3433} 
  • 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> 
    1624 
  • libswish3/trunk/bindings/perl/typemap

    r2028 r2045  
    1313swish_NamedBuffer*        O_OBJECT 
    1414swish_3*                  O_OBJECT 
     15swish_Property*           O_OBJECT 
     16swish_MetaName*           O_OBJECT 
    1517 
    1618INPUT 
  • libswish3/trunk/bindings/perl/xs_helpers.c

    r2030 r2045  
    66/* C code to make writing XS easier */ 
    77 
     8static AV*      sp_hv_keys(HV* hash); 
     9static AV*      sp_hv_values(HV* hash); 
    810static SV*      sp_hv_store( HV* h, const char* key, SV* val ); 
    911static SV*      sp_hv_store_char( HV* h, const char* key, char *val ); 
     
    2628static void     sp_describe_object( SV* object ); 
    2729static 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); 
    3130static AV*      sp_get_xml2_hash_keys( xmlHashTablePtr xml2_hash ); 
     31static void     sp_add_key_to_array(xmlChar *val, AV *keys, xmlChar *key); 
     32static SV*      sp_xml2_hash_to_perl_hash( xmlHashTablePtr xml2_hash, const char* class ); 
     33static void     sp_perl_hash_to_xml2_hash( HV* perlhash, xmlHashTablePtr xml2hash ); 
    3234static void     sp_nb_hash_to_phash(xmlBufferPtr buf, HV *phash, xmlChar *key); 
    3335static HV*      sp_nb_to_hash( swish_NamedBuffer* nb ); 
     
    3840static void     sp_SV_is_qr( SV *qr ); 
    3941static void     sp_debug_token( swish_Token *token ); 
    40 static HV*      sp_get_config_subconfig( swish_Config* config, const char* key ); 
    4142 
    4243/* implement nearly all methods for SWISH::3::Stash, a private class */ 
    4344 
    4445static 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 ); 
     46static void     sp_Stash_set( SV *stash, const char *key, SV *value ); 
     47static void     sp_Stash_set_char( SV *stash, const char *key, char *value ); 
     48static SV*      sp_Stash_get( SV *stash, const char *key ); 
     49static char*    sp_Stash_get_char( SV *stash, const char *key ); 
     50static void     sp_Stash_replace( SV *stash, const char *key, SV *value ); 
     51static int      sp_Stash_inner_refcnt( SV *stash ); 
     52static void     sp_Stash_destroy( SV *stash ); 
     53static void     sp_Stash_dec_values( SV *stash ); 
    5254 
    5355static SV* 
     
    113115{ 
    114116    HV *hash; 
     117    sp_Stash_dec_values(object); 
    115118    hash = sp_extract_hash( object ); 
    116119    if ( SWISH_DEBUG ) { 
     
    129132    } 
    130133} 
     134 
     135static void 
     136sp_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 
    131156 
    132157 
     
    145170    } 
    146171} 
     172 
     173static AV*  
     174sp_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 
     203static AV* 
     204sp_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 
    147232 
    148233/* store SV* in a hash, incrementing its refcnt */ 
     
    402487 
    403488static 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 void  
    426489sp_add_key_to_array(xmlChar* val, AV* mykeys, xmlChar* key) 
    427490{ 
     
    429492    av_push(mykeys, newSVpvn((char*)key, strlen((char*)key))); 
    430493} 
    431  
     494  
    432495static AV*  
    433496sp_get_xml2_hash_keys( xmlHashTablePtr xml2_hash ) 
     
    438501    xmlHashScan(xml2_hash, (xmlHashScanner)sp_add_key_to_array, mykeys); 
    439502    return mykeys; 
     503} 
     504 
     505 
     506static void  
     507sp_make_perl_hash(char* value, SV* stash, xmlChar* key) 
     508{ 
     509    sp_Stash_set_char(stash, (const char*)key, value ); 
     510} 
     511 
     512 
     513static SV*  
     514sp_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 
     525static void 
     526sp_perl_hash_to_xml2_hash( HV* perlhash, xmlHashTablePtr xml2hash ) 
     527{ 
     528 // TODO 
    440529} 
    441530 
     
    733822} 
    734823 
    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