| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 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 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
switch (ch) |
|---|
| 82 |
{ |
|---|
| 83 |
case 0: |
|---|
| 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(); |
|---|
| 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 |
} |
|---|