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