root/libswish3/trunk/src/swish_words.c

Revision 2104, 3.0 kB (checked in by karpet, 4 months ago)

add some mem debugging and clean up swish_words example

Line 
1 /*
2  * This file is part of libswish3
3  * Copyright (C) 2007 Peter Karman
4  *
5  *  libswish3 is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  libswish3 is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with libswish3; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19
20 /* test word parser */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <err.h>
26 #include <string.h>
27 #include <wctype.h>
28 #include <ctype.h>
29 #include <getopt.h>
30
31 #include "libswish3.h"
32
33 static struct option longopts[] = {
34     {"file", required_argument, 0, 'f'},
35     {"help", no_argument, 0, 'h'},
36     {0, 0, 0, 0}
37 };
38
39
40 int main(
41     int argc,
42     char **argv
43 );
44 int usage(
45 );
46
47 extern int SWISH_DEBUG;
48
49 int
50 usage(
51 )
52 {
53
54     char *descr =
55         "swish_words is an example program for testing the libswish3 tokenizer\n";
56     printf("swish_words [opts] [string(s)]\n");
57     printf("opts:\n --file file.txt\n");
58     printf("\n%s\n\n", descr);
59     exit(1);
60 }
61
62 int
63 main(
64     int argc,
65     char **argv
66 )
67 {
68     int i, ch;
69     int option_index;
70     extern char *optarg;
71     extern int optind;
72     xmlChar *string;
73     swish_WordList *list;
74     xmlChar *meta;
75     swish_3 *s3;
76
77     meta = (xmlChar *)SWISH_DEFAULT_METANAME;
78     option_index = 0;
79     string = NULL;
80    
81     s3 = swish_init_swish3(NULL, NULL);
82
83     while ((ch = getopt_long(argc, argv, "f:h", longopts, &option_index)) != -1) {
84
85         switch (ch) {
86         case 0:                /* If this option set a flag, do nothing else now. */
87             if (longopts[option_index].flag != 0)
88                 break;
89             printf("option %s", longopts[option_index].name);
90             if (optarg)
91                 printf(" with arg %s", optarg);
92             printf("\n");
93             break;
94
95         case 'f':
96             printf("reading %s\n", optarg);
97             string = swish_slurp_file((xmlChar *)optarg);
98             break;
99
100         case '?':
101         case 'h':
102         default:
103             usage();
104
105         }
106
107     }
108
109     i = optind;
110
111     for (; i < argc; i++) {
112         list = swish_tokenize(s3->analyzer, (xmlChar *)argv[i], 0, 0, meta, meta);
113         printf("parsed: %s\n", argv[i]);
114         swish_debug_wordlist(list);
115         list->ref_cnt--;
116         swish_free_wordlist(list);
117     }
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--;
126         swish_free_wordlist(list);
127         swish_xfree(string);
128     }
129
130     swish_free_swish3(s3);   
131    
132     return (0);
133 }
Note: See TracBrowser for help on using the browser.