Range check winsock2's socket() return value
[xapian.git] / xapian-core / examples / xapian-metadata.cc
blob6a1228c65a6f34063d09dfe564dc06b10371c750
1 /** @file xapian-metadata.cc
2 * @brief Read and write user metadata
3 */
4 /* Copyright (C) 2007,2010,2015 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 #include <config.h>
23 #include <xapian.h>
25 #include <iostream>
26 #include <string>
28 #include <cstdlib> // For exit().
29 #include <cstring>
31 using namespace std;
33 #define PROG_NAME "xapian-metadata"
34 #define PROG_DESC "Read and write user metadata"
36 static void show_usage() {
37 cout << "Usage: " PROG_NAME " get PATH_TO_DATABASE KEY\n"
38 " " PROG_NAME " list PATH_TO_DATABASE [PREFIX]\n"
39 " " PROG_NAME " set PATH_TO_DATABASE KEY VALUE" << endl;
42 int
43 main(int argc, char **argv)
44 try {
45 const char * command = argv[1];
46 if (!command) {
47 syntax_error:
48 show_usage();
49 exit(1);
52 if (command[0] == '-') {
53 if (strcmp(command, "--help") == 0) {
54 cout << PROG_NAME " - " PROG_DESC "\n\n";
55 show_usage();
56 exit(0);
58 if (strcmp(command, "--version") == 0) {
59 cout << PROG_NAME " - " PACKAGE_STRING << endl;
60 exit(0);
64 if (strcmp(command, "get") == 0) {
65 if (argc != 4) goto syntax_error;
66 Xapian::Database db(argv[2]);
67 cout << db.get_metadata(argv[3]) << endl;
68 } else if (strcmp(command, "list") == 0) {
69 if (argc != 3 && argc != 4) goto syntax_error;
70 Xapian::Database db(argv[2]);
71 string prefix;
72 if (argc == 4) prefix = argv[3];
73 for (Xapian::TermIterator t = db.metadata_keys_begin(prefix);
74 t != db.metadata_keys_end(prefix);
75 ++t) {
76 cout << *t << '\n';
78 } else if (strcmp(command, "set") == 0) {
79 if (argc != 5) goto syntax_error;
80 Xapian::WritableDatabase db(argv[2], Xapian::DB_CREATE_OR_OPEN);
81 db.set_metadata(argv[3], argv[4]);
82 db.commit();
83 } else {
84 goto syntax_error;
86 } catch (const Xapian::Error &e) {
87 cout << e.get_description() << endl;
88 exit(1);