Show
Ignore:
Timestamp:
03/28/08 23:00:02 (7 months ago)
Author:
karpet
Message:

add some mem debugging and clean up swish_words example

Files:

Legend:

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

    r2010 r2104  
    1818 */ 
    1919 
    20  
    2120/* test word parser */ 
    2221 
     
    2827#include <wctype.h> 
    2928#include <ctype.h> 
    30 #include <libxml/hash.h> 
    3129#include <getopt.h> 
    3230 
    3331#include "libswish3.h" 
    3432 
    35 static struct option longopts[] = 
    36 
     33static struct option longopts[] = { 
    3734    {"file", required_argument, 0, 'f'}, 
    38     {"debug", required_argument, 0, 'd'}, 
    3935    {"help", no_argument, 0, 'h'}, 
    4036    {0, 0, 0, 0} 
    4137}; 
    4238 
    43 void            print_list(swish_WordList * list); 
    44 int             main(int argc, char **argv); 
    45 int             usage(); 
     39 
     40int main( 
     41    int argc, 
     42    char **argv 
     43); 
     44int usage( 
     45); 
    4646 
    4747extern int SWISH_DEBUG; 
    4848 
    49  
    5049int 
    51 usage() 
     50usage( 
     51
    5252{ 
    5353 
    54     char  *descr = "swish_words is an example program for testing the libswish3 tokenizer\n"; 
     54    char *descr = 
     55        "swish_words is an example program for testing the libswish3 tokenizer\n"; 
    5556    printf("swish_words [opts] [string(s)]\n"); 
    56     printf("opts:\n --file file.txt\n --debug\n"); 
     57    printf("opts:\n --file file.txt\n"); 
    5758    printf("\n%s\n\n", descr); 
    5859    exit(1); 
     
    6061 
    6162int 
    62 main(int argc, char **argv) 
     63main( 
     64    int argc, 
     65    char **argv 
     66
    6367{ 
    64     int             i, ch; 
    65     int             option_index; 
    66     extern char    *optarg; 
    67     extern int      optind; 
    68     xmlChar        *string; 
     68    int i, ch; 
     69    int option_index; 
     70    extern char *optarg; 
     71    extern int optind; 
     72    xmlChar *string; 
    6973    swish_WordList *list; 
    70     xmlChar        *meta; 
    71     swish_3        *s3; 
     74    xmlChar *meta; 
     75    swish_3 *s3; 
     76 
     77    meta = (xmlChar *)SWISH_DEFAULT_METANAME; 
     78    option_index = 0; 
     79    string = NULL; 
    7280     
    73     meta            = (xmlChar*)SWISH_DEFAULT_METANAME; 
    74     option_index    = 0; 
    75     string          = NULL; 
     81    s3 = swish_init_swish3(NULL, NULL); 
    7682 
    77     while ((ch = getopt_long(argc, argv, "d:f:h", longopts, &option_index)) != -1) 
    78     { 
    79         /* printf("switch is %c\n",   ch); */ 
    80         /* printf("optarg is %s\n", optarg); */ 
    81         /* printf("optind = %d\n",  optind); */ 
     83    while ((ch = getopt_long(argc, argv, "f:h", longopts, &option_index)) != -1) { 
    8284 
    83         switch (ch) 
    84         { 
    85         case 0:    /* If this option set a flag, do nothing else now. */ 
     85        switch (ch) { 
     86        case 0:                /* If this option set a flag, do nothing else now. */ 
    8687            if (longopts[option_index].flag != 0) 
    8788                break; 
     
    9495        case 'f': 
    9596            printf("reading %s\n", optarg); 
    96                      
    97             string = swish_slurp_file((xmlChar *) optarg); 
    98              
    99             break; 
    100  
    101         case 'd': 
    102             printf("turning on debug mode: %s\n", optarg); 
    103  
    104             if (!isdigit(optarg[0])) 
    105                 err(1, "-d option requires a positive integer as argument\n"); 
    106  
    107             SWISH_DEBUG = (int) strtol(optarg, (char **) NULL, 10); 
     97            string = swish_slurp_file((xmlChar *)optarg); 
    10898            break; 
    10999 
     
    117107    } 
    118108 
    119     s3 = swish_init_swish3( NULL, NULL );   /* call after we have set optional debug flag */ 
    120     i  = optind; 
    121          
    122     for (; i < argc; i++) 
    123     { 
    124         list = swish_tokenize( s3->analyzer, (xmlChar *) argv[i], 0, 0, meta, meta ); 
     109    i = optind; 
     110 
     111    for (; i < argc; i++) { 
     112        list = swish_tokenize(s3->analyzer, (xmlChar *)argv[i], 0, 0, meta, meta); 
    125113        printf("parsed: %s\n", argv[i]); 
    126114        swish_debug_wordlist(list); 
     115        list->ref_cnt--; 
    127116        swish_free_wordlist(list); 
    128117    } 
    129      
    130     if (string != NULL) 
    131     { 
    132         list = swish_tokenize( s3->analyzer, string, 0, 0, meta, meta ); 
    133         printf("parsed: %s\n", string); 
    134         swish_debug_wordlist(list); 
     118 
     119    if (string != NULL) { 
     120        list = swish_tokenize(s3->analyzer, string, 0, 0, meta, meta); 
     121         
     122        if (SWISH_DEBUG & SWISH_DEBUG_WORDLIST) 
     123            swish_debug_wordlist(list); 
     124             
     125        list->ref_cnt--; 
    135126        swish_free_wordlist(list); 
    136127        swish_xfree(string); 
    137128    } 
    138      
    139     swish_free_swish3( s3 ); 
     129 
     130    swish_free_swish3(s3);     
    140131     
    141132    return (0);