[ci] Update macos jobs
[xapian.git] / xapian-bindings / csharp / docs / examples / SimpleIndex.cs
blob65f9b5023a1017c4d4e9d8d1c7dbdc6eb293dbd4
1 // Index each paragraph of a text file as a Xapian document.
2 //
3 // Copyright (c) 2003 James Aylett
4 // Copyright (c) 2004,2006,2007 Olly Betts
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License as
8 // published by the Free Software Foundation; either version 2 of the
9 // License, or (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
19 // USA
21 using System;
23 class SimpleIndex {
24 public static void Main(string[] argv) {
25 if (argv.Length != 1) {
26 Console.Error.WriteLine("Usage: SimpleIndex PATH_TO_DATABASE");
27 Environment.Exit(1);
30 try {
31 // Open the database for update, creating a new database if
32 // necessary.
33 Xapian.WritableDatabase database;
34 database = new Xapian.WritableDatabase(argv[0], Xapian.Xapian.DB_CREATE_OR_OPEN);
36 Xapian.TermGenerator indexer = new Xapian.TermGenerator();
37 Xapian.Stem stemmer = new Xapian.Stem("english");
38 indexer.SetStemmer(stemmer);
40 string para = "";
41 while (true) {
42 string line = Console.In.ReadLine();
43 if (line == null) {
44 if (para == "") break;
45 line = "";
47 line = line.Trim();
48 if (line == "") {
49 if (para != "") {
50 // We've reached the end of a paragraph, so index it.
51 Xapian.Document doc = new Xapian.Document();
52 doc.SetData(para);
54 indexer.SetDocument(doc);
55 indexer.IndexText(para);
57 // Add the document to the database.
58 database.AddDocument(doc);
59 para = "";
61 } else {
62 if (para != "") para += " ";
63 para += line;
66 } catch (Exception e) {
67 Console.Error.WriteLine("Exception: " + e.ToString());
68 Environment.Exit(1);