1 /* filenames.c - Iterator for a list of filenames
3 * Copyright © 2010 Intel Corporation
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.
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.
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/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-private.h"
23 typedef struct _notmuch_filenames_node {
25 struct _notmuch_filenames_node *next;
26 } notmuch_filenames_node_t;
28 struct _notmuch_filenames {
29 notmuch_filenames_node_t *head;
30 notmuch_filenames_node_t **tail;
31 notmuch_filenames_node_t *iterator;
34 /* Create a new notmuch_filenames_t object, with 'ctx' as its
37 * This function can return NULL in case of out-of-memory.
40 _notmuch_filenames_create (const void *ctx)
42 notmuch_filenames_t *filenames;
44 filenames = talloc (ctx, notmuch_filenames_t);
45 if (unlikely (filenames == NULL))
48 filenames->head = NULL;
49 filenames->tail = &filenames->head;
51 filenames->iterator = NULL;
56 /* Append a single 'node' to the end of 'filenames'.
59 _notmuch_filenames_add_filename (notmuch_filenames_t *filenames,
62 /* Create and initialize new node. */
63 notmuch_filenames_node_t *node = talloc (filenames,
64 notmuch_filenames_node_t);
66 node->filename = talloc_strdup (node, filename);
69 /* Append the node to the list. */
70 *(filenames->tail) = node;
71 filenames->tail = &node->next;
75 notmuch_filenames_valid (notmuch_filenames_t *filenames)
77 if (filenames == NULL)
80 return (filenames->iterator != NULL);
84 notmuch_filenames_get (notmuch_filenames_t *filenames)
86 if (filenames->iterator == NULL)
89 return filenames->iterator->filename;
93 _notmuch_filenames_move_to_first (notmuch_filenames_t *filenames)
95 filenames->iterator = filenames->head;
99 notmuch_filenames_move_to_next (notmuch_filenames_t *filenames)
101 if (filenames->iterator == NULL)
104 filenames->iterator = filenames->iterator->next;
108 notmuch_filenames_destroy (notmuch_filenames_t *filenames)
110 talloc_free (filenames);