1 /* tags.c - 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 https://www.gnu.org/licenses/ .
17 *
18 * Author: Carl Worth <cworth@cworth.org>
19 */
20
21 #include "notmuch-private.h"
22
23 struct _notmuch_tags {
24 notmuch_string_node_t *iterator;
25 };
26
27 /* Create a new notmuch_tags_t object, with 'ctx' as its talloc owner.
28 * The returned iterator will talloc_steal the 'list', since the list
29 * is almost always transient.
30 *
31 * This function can return NULL in case of out-of-memory.
32 */
33 notmuch_tags_t *
_notmuch_tags_create(const void * ctx,notmuch_string_list_t * list)34 _notmuch_tags_create (const void *ctx, notmuch_string_list_t *list)
35 {
36 notmuch_tags_t *tags;
37
38 tags = talloc (ctx, notmuch_tags_t);
39 if (unlikely (tags == NULL))
40 return NULL;
41
42 tags->iterator = list->head;
43 (void) talloc_steal (tags, list);
44
45 return tags;
46 }
47
48 notmuch_bool_t
notmuch_tags_valid(notmuch_tags_t * tags)49 notmuch_tags_valid (notmuch_tags_t *tags)
50 {
51 return tags->iterator != NULL;
52 }
53
54 const char *
notmuch_tags_get(notmuch_tags_t * tags)55 notmuch_tags_get (notmuch_tags_t *tags)
56 {
57 if (tags->iterator == NULL)
58 return NULL;
59
60 return (char *) tags->iterator->string;
61 }
62
63 void
notmuch_tags_move_to_next(notmuch_tags_t * tags)64 notmuch_tags_move_to_next (notmuch_tags_t *tags)
65 {
66 if (tags->iterator == NULL)
67 return;
68
69 tags->iterator = tags->iterator->next;
70 }
71
72 void
notmuch_tags_destroy(notmuch_tags_t * tags)73 notmuch_tags_destroy (notmuch_tags_t *tags)
74 {
75 talloc_free (tags);
76 }
77