root/libswish3/tags/10Feb2008/src/swish_words.c

Revision 1930, 3.8 kB (checked in by karpet, 2 years ago)

refactor to buffer all MetaNames? as well as PropertyNames? in NamedBuffer?

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
21 /* test word parser */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <err.h>
27 #include <string.h>
28 #include <wctype.h>
29 #include <ctype.h>
30 #include <libxml/hash.h>
31 #include <getopt.h>
32
33 #include "libswish3.h"
34
35 static struct option longopts[] =
36 {
37     {"file", required_argument, 0, 'f'},
38     {"debug", required_argument, 0, 'd'},
39     {"help", no_argument, 0, 'h'},
40     {0, 0, 0, 0}
41 };
42
43 void            print_list(swish_WordList * list);
44 int             main(int argc, char **argv);
45 int             usage();
46
47 extern int SWISH_DEBUG;
48
49
50 int
51 usage()
52 {
53
54     char  *descr = "swish_words is an example program for testing the libswish3 tokenizer\n";
55     printf("swish_words [opts] [string(s)]\n");
56     printf("opts:\n --file file.txt\n --debug\n");
57     printf("\n%s\n\n", descr);
58     exit(1);
59 }
60
61 int
62 main(int argc, char **argv)
63 {
64     int             i, ch;
65     int             option_index = 0;
66     extern char    *optarg;
67     extern int      optind;
68     xmlChar        *string;
69     xmlChar        *meta = (xmlChar*)SWISH_DEFAULT_METANAME;
70    
71     string = NULL;
72    
73     swish_WordList *list;
74
75     while ((ch = getopt_long(argc, argv, "d:f:h", longopts, &option_index)) != -1)
76     {
77         /* printf("switch is %c\n",   ch); */
78         /* printf("optarg is %s\n", optarg); */
79         /* printf("optind = %d\n",  optind); */
80
81         switch (ch)
82         {
83         case 0:    /* If this option set a flag, do nothing else now. */
84             if (longopts[option_index].flag != 0)
85                 break;
86             printf("option %s", longopts[option_index].name);
87             if (optarg)
88                 printf(" with arg %s", optarg);
89             printf("\n");
90             break;
91
92         case 'f':
93             printf("reading %s\n", optarg);
94                    
95             string = swish_slurp_file((xmlChar *) optarg);
96            
97             break;
98
99         case 'd':
100             printf("turning on debug mode: %s\n", optarg);
101
102             if (!isdigit(optarg[0]))
103                 err(1, "-d option requires a positive integer as argument\n");
104
105             SWISH_DEBUG = (int) strtol(optarg, (char **) NULL, 10);
106             break;
107
108         case '?':
109         case 'h':
110         default:
111             usage();
112
113         }
114
115     }
116
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;
123        
124     for (; i < argc; i++)
125     {
126         list = swish_tokenize( analyzer, (xmlChar *) argv[i], 0, 0, meta, meta );
127         printf("parsed: %s\n", argv[i]);
128         swish_debug_wordlist(list);
129         swish_free_wordlist(list);
130     }
131    
132     if (string != NULL)
133     {
134         list = swish_tokenize( analyzer, string, 0, 0, meta, meta );
135         printf("parsed: %s\n", string);
136         swish_debug_wordlist(list);
137         swish_free_wordlist(list);
138         swish_xfree(string);
139     }
140    
141     swish_free_analyzer( analyzer );
142     swish_free_config( config );
143    
144     swish_cleanup();
145
146     return (0);
147 }
Note: See TracBrowser for help on using the browser.