Refactor to avoid warning with GCC 12.2
[xapian.git] / xapian-core / examples / simpleindex.cc
blob6b8cd855b14586619b20d3fefbc598560ccd8fc4
1 /** @file
2 * @brief Index each paragraph of a text file as a Xapian document.
3 */
4 /* Copyright (C) 2007-2022 Olly Betts
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #include <xapian.h>
27 #include <iostream>
28 #include <string>
30 #include <cstdlib> // For exit().
31 #include <cstring>
33 using namespace std;
35 int
36 main(int argc, char **argv)
37 try {
38 if (argc != 2 || argv[1][0] == '-') {
39 int rc = 1;
40 if (argv[1]) {
41 if (strcmp(argv[1], "--version") == 0) {
42 cout << "simpleindex\n";
43 exit(0);
45 if (strcmp(argv[1], "--help") == 0) {
46 rc = 0;
49 cout << "Usage: " << argv[0] << " PATH_TO_DATABASE\n"
50 "Index each paragraph of a text file as a Xapian document.\n";
51 exit(rc);
54 // Open the database for update, creating a new database if necessary.
55 Xapian::WritableDatabase db(argv[1], Xapian::DB_CREATE_OR_OPEN);
57 Xapian::TermGenerator indexer;
58 Xapian::Stem stemmer("english");
59 indexer.set_stemmer(stemmer);
60 indexer.set_stemming_strategy(indexer.STEM_SOME_FULL_POS);
62 string para;
63 while (true) {
64 string line;
65 if (cin.eof()) {
66 if (para.empty()) break;
67 } else {
68 getline(cin, line);
71 if (line.empty()) {
72 if (!para.empty()) {
73 // We've reached the end of a paragraph, so index it.
74 Xapian::Document doc;
75 doc.set_data(para);
77 indexer.set_document(doc);
78 indexer.index_text(para);
80 // Add the document to the database.
81 db.add_document(doc);
83 para.resize(0);
85 } else {
86 if (!para.empty()) para += ' ';
87 para += line;
91 // Explicitly commit so that we get to see any errors. WritableDatabase's
92 // destructor will commit implicitly (unless we're in a transaction) but
93 // will swallow any exceptions produced.
94 db.commit();
95 } catch (const Xapian::Error &e) {
96 cout << e.get_description() << '\n';
97 exit(1);