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

Revision 1955, 5.5 kB (checked in by karpet, 1 year ago)

doc tweek; come config work

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 /* swish_lint.c -- test libswish3 */
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 <libxml/hash.h>
30 #include <getopt.h>
31
32 #include "libswish3.h"
33
34 int             debug = 0;
35
36 int             main(int argc, char **argv);
37 int             usage();
38 void            handler(swish_ParseData * parse_data);
39 void            libxml2_version();
40 void            swish_version();
41
42 int             twords = 0;
43
44 extern int SWISH_DEBUG;
45
46 static struct option longopts[] =
47 {
48     {"config", required_argument, 0, 'c'},
49     {"debug",  required_argument, 0, 'd'},
50     {"help",   no_argument, 0, 'h'},
51     {0, 0, 0, 0}
52 };
53
54
55 void libxml2_version()
56 {
57     printf("libxml2 version: %s\n",  LIBXML_DOTTED_VERSION);   
58 }
59
60 void swish_version()
61 {
62     printf("libswish3 version %s\n", SWISH_LIB_VERSION);
63     printf("swish version %s\n", SWISH_VERSION);
64 }
65
66 int 
67 usage()
68 {
69
70     char * descr = "swish_lint is an example program for using libswish3\n";
71     printf("swish_lint [opts] [- | file(s)]\n");
72     printf("opts:\n --config conf_file.xml\n --debug [lvl]\n --help\n");
73     printf("\n%s\n", descr);
74     libxml2_version();
75     swish_version();
76     exit(0);
77 }
78
79 void 
80 handler(swish_ParseData * parse_data)
81 {
82
83     /* return; */
84
85     printf("nwords: %d\n", parse_data->docinfo->nwords);
86    
87     twords += parse_data->docinfo->nwords;
88
89     if (SWISH_DEBUG)
90     {
91         swish_debug_docinfo(parse_data->docinfo);
92         swish_debug_wordlist(parse_data->wordlist);
93         swish_debug_nb(parse_data->properties, (xmlChar*)"Property");
94         swish_debug_nb(parse_data->metanames, (xmlChar*)"MetaName");
95     }
96 }
97
98 int 
99 main(int argc, char **argv)
100 {
101     int             i, ch;
102     extern char    *optarg;
103     extern int      optind;
104     int             option_index = 0;
105     int             files = 0;
106     int             overwrite = 0;
107     char           *etime;
108     double          startTime = swish_time_elapsed();
109    
110     xmlChar        *config_file = NULL;
111    
112     swish_init();
113
114     swish_Config * config;
115     swish_Analyzer * analyzer;
116     swish_Parser * parser;
117
118     while ((ch = getopt_long(argc, argv, "c:d:f:h", longopts, &option_index)) != -1)
119     {
120         /* printf("switch is %c\n",   ch); */
121         /* printf("optarg is %s\n", optarg); */
122         /* printf("optind = %d\n",  optind); */
123
124         switch (ch)
125         {
126         case 0:    /* If this option set a flag, do nothing else now. */
127             if (longopts[option_index].flag != 0)
128                 break;
129             printf("option %s", longopts[option_index].name);
130             if (optarg)
131                 printf(" with arg %s", optarg);
132             printf("\n");
133             break;
134
135         case 'c':    /* should we set up default config first ? then override
136                  * here ? */
137
138             //printf("optarg = %s\n", optarg);
139             config_file = swish_xstrdup( (xmlChar*)optarg );
140             break;
141            
142
143         case 'd':
144             printf("turning on debug mode: %s\n", optarg);
145
146             if (!isdigit(optarg[0]))
147                 err(1, "-d option requires a positive integer as argument\n");
148
149             SWISH_DEBUG = (int) strtol(optarg, (char **) NULL, 10);
150             break;
151
152
153         case 'o':
154             overwrite = 1;
155             break;
156
157         case '?':
158         case 'h':
159         default:
160             usage();
161
162         }
163
164     }
165    
166     config = swish_init_config();
167
168     if (config_file != NULL)
169     {
170         config = swish_add_config(config_file, config);
171     }
172
173     i = optind;
174    
175     /* die with no args */
176     if (!i || i >= argc)
177     {
178         swish_free_config(config);
179         usage();
180
181     }
182    
183     if (SWISH_DEBUG == 20)
184     {
185         swish_debug_config(config);   
186     }
187        
188     analyzer = swish_init_analyzer( config );
189     parser   = swish_init_parser( config, analyzer, &handler, NULL );
190    
191     for (; i < argc; i++)
192     {
193
194         if (argv[i][0] != '-')
195         {
196
197             printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
198             printf("parse_file for %s\n", argv[i]);
199             if (! swish_parse_file(parser, (unsigned char *) argv[i], NULL))
200                 files++;
201
202         }
203         else if (argv[i][0] == '-' && !argv[i][1])
204         {
205
206             printf("reading from stdin\n");
207             files = swish_parse_fh(parser, NULL, NULL);
208
209         }
210
211     }
212
213     printf("\n\n%d files indexed\n", files);
214     printf("total words: %d\n", twords);
215
216     etime = swish_print_time(swish_time_elapsed() - startTime);
217     printf("%s total time\n\n", etime);
218     swish_xfree(etime);
219    
220     swish_free_analyzer( analyzer );
221     swish_free_config( config );
222     swish_free_parser( parser );
223
224     if (config_file != NULL)
225         swish_xfree(config_file);
226    
227     swish_cleanup();   
228
229     return (0);
230 }
Note: See TracBrowser for help on using the browser.