Show
Ignore:
Timestamp:
02/10/08 22:26:06 (1 year ago)
Author:
karpet
Message:

simplify API with top-level swish_3 struct

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libswish3/trunk/src/libswish3/analyzer.c

    r1952 r2010  
    3030swish_init_analyzer( swish_Config * config ) 
    3131{ 
    32     swish_Analyzer *a = (swish_Analyzer *) swish_xmalloc(sizeof(swish_Analyzer)); 
     32    swish_Analyzer *a; 
     33    a = swish_xmalloc(sizeof(swish_Analyzer)); 
    3334     
    3435    /* TODO get this from config */ 
  • libswish3/trunk/src/libswish3/config.c

    r1955 r2010  
    522522{ 
    523523    xmlNode        *root = NULL; 
    524     xmlChar        *toptag = "swishconfig"; 
     524    xmlChar        *toptag = (xmlChar*)"swishconfig"; 
    525525 
    526526    root = xmlDocGetRootElement(doc); 
  • libswish3/trunk/src/libswish3/docinfo.c

    r1952 r2010  
    205205        swish_xfree(i->mime); 
    206206         
    207     i->mime = swish_get_mime_type( parse_data->config, i->ext ); 
     207    i->mime = swish_get_mime_type( parse_data->s3->config, i->ext ); 
    208208         
    209209    if (SWISH_DEBUG > 9) 
     
    213213        swish_xfree(i->parser); 
    214214         
    215     i->parser = swish_get_parser( parse_data->config, i->mime ); 
     215    i->parser = swish_get_parser( parse_data->s3->config, i->mime ); 
    216216     
    217217    return 1; 
  • libswish3/trunk/src/libswish3/libswish3.h

    r2009 r2010  
    146146#endif 
    147147 
     148typedef struct swish_3                  swish_3; 
     149typedef struct swish_Token              swish_Token; 
    148150typedef struct swish_StringList         swish_StringList; 
    149151typedef struct swish_Config             swish_Config; 
     
    169171*/ 
    170172 
     173struct swish_3 
     174{ 
     175    int             ref_cnt; 
     176    void           *stash; 
     177    swish_Config   *config; 
     178    swish_Analyzer *analyzer; 
     179    swish_Parser   *parser; 
     180}; 
     181 
     182struct swish_Token 
     183{ 
     184    xmlChar         *start_ptr; 
     185    int              tok_bytes; 
     186    int              start; 
     187    int              end; 
     188    xmlChar         *meta; 
     189    xmlChar         *ctxt; 
     190    unsigned int     wpos; 
     191    unsigned int     offset; 
     192    swish_Analyzer  *analyzer; 
     193    swish_WordList  *list; 
     194}; 
     195 
    171196struct swish_StringList 
    172197{ 
     
    286311{ 
    287312    int                    ref_cnt;             // for script bindings 
    288     swish_Config          *config;              // config object 
    289     swish_Analyzer        *analyzer;            // analyzer object 
    290313    void                 (*handler)(swish_ParseData*); // handler reference 
    291314    void                  *stash;               // for script bindings 
     
    295318struct swish_ParseData 
    296319{ 
     320    swish_3               *s3;                 // main object 
    297321    xmlBufferPtr           meta_buf;           // tmp MetaName buffer 
    298322    xmlBufferPtr           prop_buf;           // tmp Property buffer 
    299323    xmlChar               *tag;                // current tag name 
    300324    swish_DocInfo         *docinfo;            // document-specific properties 
    301     swish_Config          *config;             // global config 
    302325    unsigned int           context_as_meta;    // index tokens under all applicable MetaNames 
    303326    unsigned int           no_index;           // toggle flag for special comments 
     
    312335    swish_NamedBuffer     *properties;         // buffer all properties 
    313336    swish_NamedBuffer     *metanames;          // buffer all metanames 
    314     swish_Analyzer        *analyzer;           // Analyzer struct 
    315     void                  *stash;              // for script bindings 
    316 }; 
    317  
    318 /* 
    319 =cut 
    320 */ 
    321  
    322 /* 
    323 =head2 Global Functions 
    324 */ 
    325 void        swish_init(); 
    326 void        swish_cleanup(); 
     337}; 
     338 
     339/* 
     340=cut 
     341*/ 
     342 
     343/* 
     344=head2 Object Functions 
     345*/ 
     346swish_3 *   swish_init_swish3( void (*handler) (swish_ParseData *), void *stash ); 
     347void        swish_free_swish3( swish_3 *s3 ); 
    327348/* 
    328349=cut 
     
    436457=head2 Parser Functions 
    437458*/ 
    438 swish_Parser *  swish_init_parser(  swish_Config * config,  
    439                                     swish_Analyzer * analyzer,  
    440                                     void (*handler) (swish_ParseData *), 
    441                                     void *stash                                  
    442                                     ); 
    443 void  swish_free_parser( swish_Parser * parser ); 
    444 int swish_parse_file(   swish_Parser * parser, 
    445                         xmlChar *filename, 
    446                         void * stash ); 
    447 int swish_parse_fh(     swish_Parser * parser, 
    448                         FILE * fh, 
    449                         void * stash  ); 
    450 int swish_parse_buffer( swish_Parser * parser, 
    451                         xmlChar * buf,  
    452                         void * stash  ); 
     459swish_Parser *  swish_init_parser(  void (*handler) (swish_ParseData *) ); 
     460void            swish_free_parser(  swish_Parser * parser ); 
     461int             swish_parse_file(   swish_3 * s3, 
     462                                    xmlChar *filename); 
     463int             swish_parse_fh(     swish_3 * s3, 
     464                                    FILE * fh); 
     465int             swish_parse_buffer( swish_3 * s3, 
     466                                    xmlChar * buf); 
    453467/* 
    454468=cut 
  • libswish3/trunk/src/libswish3/parser.c

    r2009 r2010  
    123123 
    124124static swish_ParseData * 
    125                 init_parse_data(swish_Config * config, swish_Analyzer * analyzer, void * stash); 
     125                init_parse_data(swish_3 * s3); 
    126126static void     free_parse_data(swish_ParseData * parse_data); 
    127127 
     
    150150swish_Parser * 
    151151swish_init_parser(  
    152     swish_Config * config,  
    153     swish_Analyzer * analyzer,  
    154     void (*handler) (swish_ParseData *), 
    155     void * stash 
     152    void (*handler) (swish_ParseData *) 
    156153) 
    157154{    
    158155    swish_Parser * p = (swish_Parser*) swish_xmalloc(sizeof(swish_Parser)); 
    159     p->config   = config; 
    160     p->analyzer = analyzer; 
    161     p->stash    = stash; 
    162156    p->handler  = handler; 
    163157    p->ref_cnt  = 0; 
     
    296290 
    297291    /* change our internal name for this tag if it is aliased in config */ 
    298     alias = swish_get_config_value(parse_data->config, (xmlChar*)SWISH_ALIAS, swishtag); 
     292    alias = swish_get_config_value(parse_data->s3->config, (xmlChar*)SWISH_ALIAS, swishtag); 
    299293    if (alias) 
    300294    { 
     
    351345    }                     
    352346 
    353     if (parse_data->analyzer->tokenize) 
     347    if (parse_data->s3->analyzer->tokenize) 
    354348    { 
    355349 
     
    450444 
    451445    /* set property if this tag is configured for it */ 
    452     if (swish_config_value_exists(parse_data->config, (xmlChar*)SWISH_PROP, parse_data->tag)) 
     446    if (swish_config_value_exists(parse_data->s3->config, (xmlChar*)SWISH_PROP, parse_data->tag)) 
    453447    { 
    454448        if (SWISH_DEBUG == SWISH_DEBUG_PARSER) 
     
    464458 
    465459    /* likewise for metastack */ 
    466     if (swish_config_value_exists(parse_data->config, (xmlChar*)SWISH_META, parse_data->tag)) 
     460    if (swish_config_value_exists(parse_data->s3->config, (xmlChar*)SWISH_META, parse_data->tag)) 
    467461    { 
    468462        if (SWISH_DEBUG == SWISH_DEBUG_PARSER) 
     
    736730 
    737731static swish_ParseData * 
    738 init_parse_data(swish_Config * config, swish_Analyzer * analyzer, void * stash
     732init_parse_data( swish_3 * s3
    739733{ 
    740734 
     
    744738    swish_ParseData *ptr = (swish_ParseData *) swish_xmalloc(sizeof(swish_ParseData)); 
    745739     
    746     ptr->stash = stash
     740    ptr->s3 = s3
    747741     
    748742    ptr->meta_buf = xmlBufferCreateSize(SWISH_BUFFER_CHUNK_SIZE); 
    749743    ptr->prop_buf = xmlBufferCreateSize(SWISH_BUFFER_CHUNK_SIZE); 
    750      
    751     ptr->config = config; 
    752     ptr->analyzer = analyzer; 
    753      
     744         
    754745    ptr->tag = NULL; 
    755746    ptr->wordlist   = swish_init_wordlist(); 
    756     ptr->properties = swish_init_nb(config, (xmlChar*)SWISH_PROP); 
    757     ptr->metanames  = swish_init_nb(config, (xmlChar*)SWISH_META); 
     747    ptr->properties = swish_init_nb(s3->config, (xmlChar*)SWISH_PROP); 
     748    ptr->metanames  = swish_init_nb(s3->config, (xmlChar*)SWISH_META); 
    758749 
    759750    /* prime the stacks */ 
     
    11931184int 
    11941185swish_parse_fh( 
    1195     swish_Parser * parser, 
    1196     FILE * fh, 
    1197     void * stash  
     1186    swish_3 * s3, 
     1187    FILE * fh 
    11981188) 
    11991189{ 
     
    12201210    swish_mem_debug(); 
    12211211 
    1222     ln = swish_xmalloc(SWISH_MAXSTRLEN + 1); 
    1223     head_buf = xmlBufferCreateSize((SWISH_MAX_HEADERS * SWISH_MAXSTRLEN) + SWISH_MAX_HEADERS); 
     1212    ln          = swish_xmalloc(SWISH_MAXSTRLEN + 1); 
     1213    head_buf    = xmlBufferCreateSize((SWISH_MAX_HEADERS * SWISH_MAXSTRLEN) + SWISH_MAX_HEADERS); 
    12241214 
    12251215    swish_mem_debug(); 
     
    12541244        /* blank line indicates body */ 
    12551245            curTime      = swish_time_elapsed(); 
    1256             parse_data   = init_parse_data(parser->config, parser->analyzer, stash); 
     1246            parse_data   = init_parse_data(s3); 
    12571247            head         = buf_to_head( (xmlChar*)xmlBufferContent(head_buf) ); 
    12581248            parse_data->docinfo = head_to_docinfo(head); 
    1259             swish_check_docinfo(parse_data->docinfo, parser->config); 
     1249            swish_check_docinfo(parse_data->docinfo, s3->config); 
    12601250 
    12611251            if (SWISH_DEBUG > 9) 
     
    12841274 
    12851275            /* pass to callback function */ 
    1286             (*parser->handler)(parse_data); 
     1276            (*s3->parser->handler)(parse_data); 
    12871277 
    12881278            if (SWISH_DEBUG > 9) 
     
    13731363 */ 
    13741364int 
    1375 swish_parse_buffer( 
    1376         swish_Parser * parser, 
    1377         xmlChar * buf,  
    1378         void * stash  
    1379 
     1365swish_parse_buffer( swish_3 * s3, xmlChar * buf ) 
    13801366{ 
    13811367 
     
    13911377        SWISH_DEBUG_MSG("number of headlines: %d", head->nlines); 
    13921378 
    1393     swish_ParseData *parse_data    = init_parse_data(parser->config, parser->analyzer, stash); 
     1379    swish_ParseData *parse_data    = init_parse_data(s3); 
    13941380    parse_data->docinfo            = head_to_docinfo(head); 
    1395     swish_check_docinfo(parse_data->docinfo, parser->config); 
     1381    swish_check_docinfo(parse_data->docinfo, s3->config); 
    13961382 
    13971383    /* reposition buf pointer at start of body (just past head) */ 
     
    14021388 
    14031389    /* pass to callback function */ 
    1404     (*parser->handler)(parse_data); 
     1390    (*s3->parser->handler)(parse_data); 
    14051391 
    14061392    if (SWISH_DEBUG > 1) 
     
    14321418int 
    14331419swish_parse_file( 
    1434         swish_Parser * parser, 
    1435         xmlChar * filename, 
    1436         void * stash  
     1420        swish_3 * s3, 
     1421        xmlChar * filename 
    14371422) 
    14381423{ 
     
    14411426    char           *etime; 
    14421427 
    1443     swish_ParseData *parse_data = init_parse_data(parser->config, parser->analyzer, stash); 
     1428    swish_ParseData *parse_data = init_parse_data(s3); 
    14441429    parse_data->docinfo         = swish_init_docinfo(); 
    14451430 
     
    14541439 
    14551440    /* pass to callback function */ 
    1456     (*parser->handler) (parse_data); 
     1441    (*s3->parser->handler) (parse_data); 
    14571442 
    14581443    if (SWISH_DEBUG > 1) 
     
    16091594     
    16101595    out = NULL; 
    1611     enc = getenv("SWISH_ENCODING"); 
     1596    enc = (xmlChar*)getenv("SWISH_ENCODING"); 
    16121597         
    16131598    /* TODO better encoding detection. for now we assume unknown text files are latin1 */ 
     
    17541739    swish_WordList *tmplist; 
    17551740     
    1756     if (parse_data->analyzer->tokenizer == NULL) 
     1741    if (parse_data->s3->analyzer->tokenizer == NULL) 
    17571742    { 
    17581743     
     
    17601745     
    17611746        tmplist = swish_tokenize( 
    1762                               parse_data->analyzer, 
     1747                              parse_data->s3->analyzer, 
    17631748                              string, 
    17641749                              parse_data->offset, 
     
    17751760    /* user-defined tokenizer */ 
    17761761     
    1777         tmplist = (*parse_data->analyzer->tokenizer) ( 
    1778                               parse_data->analyzer, 
     1762        tmplist = (*parse_data->s3->analyzer->tokenizer) ( 
     1763                              parse_data->s3->analyzer, 
    17791764                              string, 
    17801765                              parse_data->offset, 
     
    18981883    swish_TagStack *s       = parse_data->propstack; 
    18991884    int cleanwsp            = 1; 
    1900     xmlHashTablePtr props   = swish_subconfig_hash( parse_data->config, (xmlChar*)SWISH_PROP ); 
     1885    xmlHashTablePtr props   = swish_subconfig_hash( parse_data->s3->config, (xmlChar*)SWISH_PROP ); 
    19011886     
    19021887    /* should we strip whitespace from this particular property ? */ 
     
    19151900    for (s->temp = s->head; s->temp != NULL; s->temp = s->temp->next) 
    19161901    { 
    1917         if (xmlStrEqual(s->temp->name, "_"))    /* top of the stack is just a placeholder */ 
     1902        if (xmlStrEqual(s->temp->name, (xmlChar*)"_"))    /* top of the stack is just a placeholder */ 
    19181903            continue; 
    19191904             
  • libswish3/trunk/src/libswish3/swish.c

    r1927 r2010  
    2626int SWISH_DEBUG = 0; 
    2727 
     28void static swish_init(); 
     29 
     30swish_3* 
     31swish_init_swish3( void (*handler) (swish_ParseData *), void *stash ) 
     32{ 
     33    swish_3 *s3; 
     34    swish_init(); 
     35    s3              = swish_xmalloc(sizeof(swish_3)); 
     36    s3->ref_cnt++; 
     37    s3->config      = swish_init_config(); 
     38    s3->analyzer    = swish_init_analyzer(s3->config); 
     39    s3->parser      = swish_init_parser(handler); 
     40    s3->stash       = stash; 
     41    return s3; 
     42} 
     43 
    2844void 
     45swish_free_swish3(swish_3* s3) 
     46{ 
     47    swish_free_parser(s3->parser); 
     48    swish_free_analyzer(s3->analyzer); 
     49    swish_free_config(s3->config); 
     50    swish_xfree(s3); 
     51    swish_mem_debug(); 
     52} 
     53 
     54 
     55void static 
    2956swish_init() 
    3057{ 
     
    4471 
    4572} 
    46  
    47 void 
    48 swish_cleanup() 
    49 { 
    50     swish_mem_debug(); 
    51 } 
    52  
  • libswish3/trunk/src/swish_lint.c

    r1955 r2010  
    102102    extern char    *optarg; 
    103103    extern int      optind; 
    104     int             option_index = 0
    105     int             files = 0
    106     int             overwrite = 0
     104    int             option_index
     105    int             files
     106    int             overwrite
    107107    char           *etime; 
    108     double          startTime = swish_time_elapsed(); 
    109      
     108    double          start_time; 
    110109    xmlChar        *config_file = NULL; 
    111      
    112     swish_init(); 
    113  
    114     swish_Config * config; 
    115     swish_Analyzer * analyzer; 
    116     swish_Parser * parser; 
     110    swish_3        *s3; 
     111     
     112    option_index    = 0; 
     113    files           = 0; 
     114    overwrite       = 0; 
     115    start_time      = swish_time_elapsed(); 
     116    s3              = swish_init_swish3( &handler, NULL ); 
    117117 
    118118    while ((ch = getopt_long(argc, argv, "c:d:f:h", longopts, &option_index)) != -1) 
     
    164164    } 
    165165     
    166     config = swish_init_config(); 
    167  
    168166    if (config_file != NULL) 
    169167    { 
    170         config = swish_add_config(config_file, config); 
     168        s3->config = swish_add_config(config_file, s3->config); 
    171169    } 
    172170 
     
    176174    if (!i || i >= argc) 
    177175    { 
    178         swish_free_config(config); 
     176        swish_free_swish3( s3 ); 
    179177        usage(); 
    180178 
     
    183181    if (SWISH_DEBUG == 20) 
    184182    { 
    185         swish_debug_config(config);    
    186     } 
    187          
    188     analyzer = swish_init_analyzer( config ); 
    189     parser   = swish_init_parser( config, analyzer, &handler, NULL ); 
    190      
     183        swish_debug_config(s3->config);    
     184    } 
     185             
    191186    for (; i < argc; i++) 
    192187    { 
     
    197192            printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); 
    198193            printf("parse_file for %s\n", argv[i]); 
    199             if (! swish_parse_file(parser, (unsigned char *) argv[i], NULL)) 
     194            if (! swish_parse_file(s3, (unsigned char *) argv[i])) 
    200195                files++; 
    201196 
     
    205200 
    206201            printf("reading from stdin\n"); 
    207             files = swish_parse_fh(parser, NULL, NULL); 
     202            files = swish_parse_fh(s3, NULL); 
    208203 
    209204        } 
     
    214209    printf("total words: %d\n", twords); 
    215210 
    216     etime = swish_print_time(swish_time_elapsed() - startTime); 
     211    etime = swish_print_time(swish_time_elapsed() - start_time); 
    217212    printf("%s total time\n\n", etime); 
    218213    swish_xfree(etime); 
    219214     
    220     swish_free_analyzer( analyzer ); 
    221     swish_free_config( config ); 
    222     swish_free_parser( parser ); 
     215    swish_free_swish3( s3 ); 
    223216 
    224217    if (config_file != NULL) 
    225218        swish_xfree(config_file); 
    226219     
    227     swish_cleanup();     
    228  
    229220    return (0); 
    230221} 
  • libswish3/trunk/src/swish_words.c

    r1930 r2010  
    6363{ 
    6464    int             i, ch; 
    65     int             option_index = 0
     65    int             option_index
    6666    extern char    *optarg; 
    6767    extern int      optind; 
    6868    xmlChar        *string; 
    69     xmlChar        *meta = (xmlChar*)SWISH_DEFAULT_METANAME; 
     69    swish_WordList *list; 
     70    xmlChar        *meta; 
     71    swish_3        *s3; 
    7072     
    71     string = NULL
    72      
    73     swish_WordList *list
     73    meta            = (xmlChar*)SWISH_DEFAULT_METANAME
     74    option_index    = 0; 
     75    string          = NULL
    7476 
    7577    while ((ch = getopt_long(argc, argv, "d:f:h", longopts, &option_index)) != -1) 
     
    115117    } 
    116118 
    117     swish_init();   /* call after we have set optional debug flag */ 
    118      
    119     swish_Config * config = swish_init_config(); 
    120     swish_Analyzer * analyzer = swish_init_analyzer( config ); 
    121  
    122     i = optind; 
     119    s3 = swish_init_swish3( NULL, NULL );   /* call after we have set optional debug flag */ 
     120    i  = optind; 
    123121         
    124122    for (; i < argc; i++) 
    125123    { 
    126         list = swish_tokenize( analyzer, (xmlChar *) argv[i], 0, 0, meta, meta ); 
     124        list = swish_tokenize( s3->analyzer, (xmlChar *) argv[i], 0, 0, meta, meta ); 
    127125        printf("parsed: %s\n", argv[i]); 
    128126        swish_debug_wordlist(list); 
     
    132130    if (string != NULL) 
    133131    { 
    134         list = swish_tokenize( analyzer, string, 0, 0, meta, meta ); 
     132        list = swish_tokenize( s3->analyzer, string, 0, 0, meta, meta ); 
    135133        printf("parsed: %s\n", string); 
    136134        swish_debug_wordlist(list); 
     
    139137    } 
    140138     
    141     swish_free_analyzer( analyzer ); 
    142     swish_free_config( config ); 
     139    swish_free_swish3( s3 ); 
    143140     
    144     swish_cleanup(); 
    145  
    146141    return (0); 
    147142}