]> git.notmuchmail.org Git - notmuch/blob - tags.cc
a921c606c300760d290cc95195937e0b17bda1a3
[notmuch] / tags.cc
1 /* tags.cc - Iterator for tags returned from message or thread
2  *
3  * Copyright © 2009 Carl Worth
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see http://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-private-cxx.h"
22 #include "database-private.h"
23
24 #include <xapian.h>
25
26 typedef struct _notmuch_terms {
27     char prefix_char;
28     Xapian::TermIterator iterator;
29     Xapian::TermIterator iterator_end;
30 } notmuch_terms_t;
31
32 struct _notmuch_tags {
33     notmuch_terms_t terms;
34 };
35
36 notmuch_terms_t *
37 _notmuch_terms_create (void *ctx,
38                        Xapian::Document doc,
39                        const char *prefix_name);
40
41 /* The assertion is to ensure that 'type' is a derivative of
42  * notmuch_terms_t in that it contains a notmuch_terms_t as its first
43  * member. We do this by name of 'terms' as opposed to type, because
44  * that's as clever as I've been so far. */
45 #define _notmuch_terms_create_type(ctx, doc, prefix_name, type) \
46     (COMPILE_TIME_ASSERT(offsetof(type, terms) == 0),           \
47      (type *) _notmuch_terms_create (ctx, doc, prefix_name))
48
49 /* We end up having to call the destructors explicitly because we had
50  * to use "placement new" in order to initialize C++ objects within a
51  * block that we allocated with talloc. So C++ is making talloc
52  * slightly less simple to use, (we wouldn't need
53  * talloc_set_destructor at all otherwise).
54  */
55 static int
56 _notmuch_terms_destructor (notmuch_terms_t *terms)
57 {
58     terms->iterator.~TermIterator ();
59     terms->iterator_end.~TermIterator ();
60
61     return 0;
62 }
63
64 notmuch_terms_t *
65 _notmuch_terms_create (void *ctx,
66                        Xapian::Document doc,
67                        const char *prefix_name)
68 {
69     notmuch_terms_t *terms;
70     const char *prefix = _find_prefix (prefix_name);
71
72     /* Currently, notmuch_terms_t is written with the assumption that
73      * any prefix its derivatives use will be only a single
74      * character. */
75     assert (strlen (prefix) == 1);
76
77     terms = talloc (ctx, notmuch_terms_t);
78     if (unlikely (terms == NULL))
79         return NULL;
80
81     terms->prefix_char = *prefix;
82     new (&terms->iterator) Xapian::TermIterator;
83     new (&terms->iterator_end) Xapian::TermIterator;
84
85     talloc_set_destructor (terms, _notmuch_terms_destructor);
86
87     terms->iterator = doc.termlist_begin ();
88     terms->iterator.skip_to (prefix);
89     terms->iterator_end = doc.termlist_end ();
90
91     return terms;
92 }
93
94 static notmuch_bool_t
95 _notmuch_terms_has_more (notmuch_terms_t *terms)
96 {
97     std::string s;
98
99     if (terms->iterator == terms->iterator_end)
100         return FALSE;
101
102     s = *terms->iterator;
103     if (! s.empty () && s[0] == terms->prefix_char)
104         return TRUE;
105     else
106         return FALSE;
107 }
108
109 static const char *
110 _notmuch_terms_get (notmuch_terms_t *terms)
111 {
112     return talloc_strdup (terms, (*terms->iterator).c_str () + 1);
113 }
114
115 static void
116 _notmuch_terms_advance (notmuch_terms_t *terms)
117 {
118     terms->iterator++;
119 }
120
121 static void
122 _notmuch_terms_destroy (notmuch_terms_t *terms)
123 {
124     talloc_free (terms);
125 }
126
127 notmuch_tags_t *
128 _notmuch_tags_create_terms (void *ctx,
129                             Xapian::Document doc,
130                             const char *prefix_name)
131 {
132     return _notmuch_terms_create_type (ctx, doc, prefix_name, notmuch_tags_t);
133 }
134
135 notmuch_bool_t
136 notmuch_tags_has_more (notmuch_tags_t *tags)
137 {
138     return _notmuch_terms_has_more (&tags->terms);
139 }
140
141 const char *
142 notmuch_tags_get (notmuch_tags_t *tags)
143 {
144     return _notmuch_terms_get (&tags->terms);
145 }
146
147 void
148 notmuch_tags_advance (notmuch_tags_t *tags)
149 {
150     return _notmuch_terms_advance (&tags->terms);
151 }
152
153 void
154 notmuch_tags_destroy (notmuch_tags_t *tags)
155 {
156     return _notmuch_terms_destroy (&tags->terms);
157 }