Fixed stemtest getopt usage so it works again.
[xapian.git] / xapian-core / tests / stemtest.cc
blob46837c814b9953fb28c99d710d57ca759e4ff8a4
1 /* stemtest.cc
3 * ----START-LICENCE----
4 * Copyright 1999,2000,2001 BrightStation PLC
5 * Copyright 2002 Ananova Ltd
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * USA
21 * -----END-LICENCE-----
24 #include <stdio.h>
25 #include <ctype.h>
26 #include <getopt.h>
28 #include <string>
29 #include <iostream>
31 using std::string;
32 using std::cout;
33 using std::cerr;
34 using std::endl;
36 #include "om/omstem.h"
38 static void
39 stemfile(const OmStem &stemmer, FILE * f)
41 while (true) {
42 int ch = getc(f);
43 if (ch == EOF) return;
44 if (isspace(ch)) {
45 putchar(ch);
46 continue;
49 std::string word;
50 while (true) {
51 word += tolower(ch);
53 ch = getc(f);
54 if (ch == EOF) break;
55 if (isspace(ch)) {
56 ungetc(ch, f);
57 break;
61 cout << stemmer.stem_word(word);
65 int main(int argc, char **argv)
67 std::string lang = "english";
69 struct option opts[] = {
70 {"language", required_argument, 0, 'l'},
71 {NULL, 0, 0, 0}
74 bool syntax_error = false;
76 int c;
77 while ((c = getopt_long(argc, argv, "l", opts, NULL)) != EOF) {
78 if (c == 'l') {
79 lang = argv[optind];
80 } else {
81 syntax_error = true;
85 try {
86 OmStem stemmer(lang);
87 while (argv[optind]) {
88 FILE * f = fopen(argv[optind], "r");
89 if (f == NULL) {
90 cerr << "File " << argv[optind] << " not found\n";
91 exit(1);
93 stemfile(stemmer, f);
94 ++optind;
96 } catch (const OmError &e) {
97 cout << e.get_msg() << endl;
98 return 1;
101 return 0;