lib: Add new, public notmuch_message_get_filenames
[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
83     return filenames;
84 }
85
86 notmuch_bool_t
87 notmuch_filenames_valid (notmuch_filenames_t *filenames)
88 {
89     if (filenames == NULL)
90         return FALSE;
91
92     return (filenames->iterator != NULL);
93 }
94
95 const char *
96 notmuch_filenames_get (notmuch_filenames_t *filenames)
97 {
98     if (filenames->iterator == NULL)
99         return NULL;
100
101     return filenames->iterator->filename;
102 }
103
104 void
105 notmuch_filenames_move_to_next (notmuch_filenames_t *filenames)
106 {
107     if (filenames->iterator == NULL)
108         return;
109
110     filenames->iterator = filenames->iterator->next;
111 }
112
113 void
114 notmuch_filenames_destroy (notmuch_filenames_t *filenames)
115 {
116     talloc_free (filenames);
117 }