1 /** @file xapian-metadata.cc
2 * @brief Read and write user metadata
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
28 #include <cstdlib> // For exit().
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
;
43 main(int argc
, char **argv
)
45 const char * command
= argv
[1];
52 if (command
[0] == '-') {
53 if (strcmp(command
, "--help") == 0) {
54 cout
<< PROG_NAME
" - " PROG_DESC
"\n\n";
58 if (strcmp(command
, "--version") == 0) {
59 cout
<< PROG_NAME
" - " PACKAGE_STRING
<< endl
;
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]);
72 if (argc
== 4) prefix
= argv
[3];
73 for (Xapian::TermIterator t
= db
.metadata_keys_begin(prefix
);
74 t
!= db
.metadata_keys_end(prefix
);
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]);
86 } catch (const Xapian::Error
&e
) {
87 cout
<< e
.get_description() << endl
;