]> git.notmuchmail.org Git - notmuch/blob - tags.c
tags: Re-implement tags iterator to avoid having C++ in the interface
[notmuch] / tags.c
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 http://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-private.h"
22
23 #include <glib.h> /* GList */
24
25 struct _notmuch_tags {
26     GList *tags;
27     GList *iterator;
28 };
29
30 /* XXX: Should write some talloc-friendly list to avoid the need for
31  * this. */
32 static int
33 _notmuch_tags_destructor (notmuch_tags_t *tags)
34 {
35     g_list_free (tags->tags);
36
37     return 0;
38 }
39
40 /* Create a new notmuch_tags_t object, with 'ctx' as its talloc owner.
41  *
42  * This function can return NULL in case of out-of-memory.
43  */
44 notmuch_tags_t *
45 _notmuch_tags_create (void *ctx)
46 {
47     notmuch_tags_t *tags;
48
49     tags = talloc (ctx, notmuch_tags_t);
50     if (unlikely (tags == NULL))
51         return NULL;
52
53     talloc_set_destructor (tags, _notmuch_tags_destructor);
54
55     tags->tags = NULL;
56     tags->iterator = NULL;
57
58     return tags;
59 }
60
61 void
62 _notmuch_tags_add_tag (notmuch_tags_t *tags, const char *tag)
63 {
64     tags->tags = g_list_prepend (tags->tags, talloc_strdup (tags, tag));
65 }
66
67 void
68 _notmuch_tags_sort (notmuch_tags_t *tags)
69 {
70     tags->tags = g_list_sort (tags->tags, (GCompareFunc) strcmp);
71 }
72
73 void
74 _notmuch_tags_reset (notmuch_tags_t *tags)
75 {
76     tags->iterator = tags->tags;
77 }
78
79 notmuch_bool_t
80 notmuch_tags_has_more (notmuch_tags_t *tags)
81 {
82     return tags->iterator != NULL;
83 }
84
85 const char *
86 notmuch_tags_get (notmuch_tags_t *tags)
87 {
88     if (tags->iterator)
89         return (char *) tags->iterator->data;
90     else
91         return NULL;
92 }
93
94 void
95 notmuch_tags_advance (notmuch_tags_t *tags)
96 {
97     tags->iterator = tags->iterator->next;
98 }
99
100 void
101 notmuch_tags_destroy (notmuch_tags_t *tags)
102 {
103     talloc_free (tags);
104 }