]> git.notmuchmail.org Git - notmuch/blob - lib/filenames.c
f078c95579c9fabbb1c54f101de809386130f85a
[notmuch] / lib / filenames.c
1 /* filenames.c - Iterator for a list of filenames
2  *
3  * Copyright © 2010 Intel Corporation
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 struct _notmuch_filenames {
24     notmuch_filename_node_t *iterator;
25 };
26
27 /* Create a new notmuch_filename_list_t object, with 'ctx' as its
28  * talloc owner.
29  *
30  * This function can return NULL in case of out-of-memory.
31  */
32 notmuch_filename_list_t *
33 _notmuch_filename_list_create (const void *ctx)
34 {
35     notmuch_filename_list_t *list;
36
37     list = talloc (ctx, notmuch_filename_list_t);
38     if (unlikely (list == NULL))
39         return NULL;
40
41     list->head = NULL;
42     list->tail = &list->head;
43
44     return list;
45 }
46
47 void
48 _notmuch_filename_list_add_filename (notmuch_filename_list_t *list,
49                                      const char *filename)
50 {
51     /* Create and initialize new node. */
52     notmuch_filename_node_t *node = talloc (list,
53                                             notmuch_filename_node_t);
54
55     node->filename = talloc_strdup (node, filename);
56     node->next = NULL;
57
58     /* Append the node to the list. */
59     *(list->tail) = node;
60     list->tail = &node->next;
61 }
62
63 void
64 _notmuch_filename_list_destroy (notmuch_filename_list_t *list)
65 {
66     talloc_free (list);
67 }
68
69 /* The notmuch_filenames_t is an iterator object for a
70  * notmuch_filename_list_t */
71 notmuch_filenames_t *
72 _notmuch_filenames_create (const void *ctx,
73                            notmuch_filename_list_t *list)
74 {
75     notmuch_filenames_t *filenames;
76
77     filenames = talloc (ctx, notmuch_filenames_t);
78     if (unlikely (filenames == NULL))
79         return NULL;
80
81     filenames->iterator = list->head;
82     (void) talloc_reference (filenames, list);
83
84     return filenames;
85 }
86
87 notmuch_bool_t
88 notmuch_filenames_valid (notmuch_filenames_t *filenames)
89 {
90     if (filenames == NULL)
91         return FALSE;
92
93     return (filenames->iterator != NULL);
94 }
95
96 const char *
97 notmuch_filenames_get (notmuch_filenames_t *filenames)
98 {
99     if (filenames->iterator == NULL)
100         return NULL;
101
102     return filenames->iterator->filename;
103 }
104
105 void
106 notmuch_filenames_move_to_next (notmuch_filenames_t *filenames)
107 {
108     if (filenames->iterator == NULL)
109         return;
110
111     filenames->iterator = filenames->iterator->next;
112 }
113
114 void
115 notmuch_filenames_destroy (notmuch_filenames_t *filenames)
116 {
117     talloc_free (filenames);
118 }