#include "notmuch-private.h"
 #include "database-private.h"
 
-struct _notmuch_filenames {
-    Xapian::TermIterator iterator;
-    Xapian::TermIterator end;
-    int prefix_len;
-    char *filename;
-};
-
-/* We end up having to call the destructors explicitly because we had
- * to use "placement new" in order to initialize C++ objects within a
- * block that we allocated with talloc. So C++ is making talloc
- * slightly less simple to use, (we wouldn't need
- * talloc_set_destructor at all otherwise).
- */
-static int
-_notmuch_filenames_destructor (notmuch_filenames_t *filenames)
-{
-    filenames->iterator.~TermIterator ();
-    filenames->end.~TermIterator ();
-
-    return 0;
-}
-
 /* Create an iterator to iterate over the basenames of files (or
  * directories) that all share a common parent directory.
  *
  * prefix.
  */
 static notmuch_filenames_t *
-_notmuch_filenames_create (void *ctx,
-                          notmuch_database_t *notmuch,
-                          const char *prefix)
+_create_filenames_for_terms_with_prefix (void *ctx,
+                                        notmuch_database_t *notmuch,
+                                        const char *prefix)
 {
     notmuch_filenames_t *filenames;
+    Xapian::TermIterator i, end;
+    int prefix_len = strlen (prefix);
 
-    filenames = talloc (ctx, notmuch_filenames_t);
+    filenames = _notmuch_filenames_create (ctx);
     if (unlikely (filenames == NULL))
        return NULL;
 
-    new (&filenames->iterator) Xapian::TermIterator ();
-    new (&filenames->end) Xapian::TermIterator ();
-
-    talloc_set_destructor (filenames, _notmuch_filenames_destructor);
-
-    filenames->iterator = notmuch->xapian_db->allterms_begin (prefix);
-    filenames->end = notmuch->xapian_db->allterms_end (prefix);
-
-    filenames->prefix_len = strlen (prefix);
-
-    filenames->filename = NULL;
-
-    return filenames;
-}
-
-notmuch_bool_t
-notmuch_filenames_valid (notmuch_filenames_t *filenames)
-{
-    if (filenames == NULL)
-       return NULL;
-
-    return (filenames->iterator != filenames->end);
-}
-
-const char *
-notmuch_filenames_get (notmuch_filenames_t *filenames)
-{
-    if (filenames == NULL || filenames->iterator == filenames->end)
-       return NULL;
-
-    if (filenames->filename == NULL) {
-       std::string term = *filenames->iterator;
-
-       filenames->filename = talloc_strdup (filenames,
-                                            term.c_str () +
-                                            filenames->prefix_len);
-    }
-
-    return filenames->filename;
-}
+    end = notmuch->xapian_db->allterms_end (prefix);
 
-void
-notmuch_filenames_move_to_next (notmuch_filenames_t *filenames)
-{
-    if (filenames == NULL)
-       return;
+    for (i = notmuch->xapian_db->allterms_begin (prefix); i != end; i++)
+    {
+       std::string term = *i;
 
-    if (filenames->filename) {
-       talloc_free (filenames->filename);
-       filenames->filename = NULL;
+       _notmuch_filenames_add_filename (filenames, term.c_str () +
+                                        prefix_len);
     }
 
-    if (filenames->iterator != filenames->end)
-       filenames->iterator++;
-}
+    _notmuch_filenames_move_to_first (filenames);
 
-void
-notmuch_filenames_destroy (notmuch_filenames_t *filenames)
-{
-    if (filenames == NULL)
-       return;
-
-    talloc_free (filenames);
+    return filenames;
 }
 
 struct _notmuch_directory {
                            _find_prefix ("file-direntry"),
                            directory->document_id);
 
-    child_files = _notmuch_filenames_create (directory,
-                                            directory->notmuch, term);
+    child_files = _create_filenames_for_terms_with_prefix (directory,
+                                                          directory->notmuch,
+                                                          term);
 
     talloc_free (term);
 
                            _find_prefix ("directory-direntry"),
                            directory->document_id);
 
-    child_directories = _notmuch_filenames_create (directory,
-                                                  directory->notmuch, term);
+    child_directories = _create_filenames_for_terms_with_prefix (directory,
+                                                directory->notmuch, term);
 
     talloc_free (term);
 
 
--- /dev/null
+/* filenames.c - Iterator for a list of filenames
+ *
+ * Copyright © 2010 Intel Corporation
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see http://www.gnu.org/licenses/ .
+ *
+ * Author: Carl Worth <cworth@cworth.org>
+ */
+
+#include "notmuch-private.h"
+
+typedef struct _notmuch_filenames_node {
+    char *filename;
+    struct _notmuch_filenames_node *next;
+} notmuch_filenames_node_t;
+
+struct _notmuch_filenames {
+    notmuch_filenames_node_t *head;
+    notmuch_filenames_node_t **tail;
+    notmuch_filenames_node_t *iterator;
+};
+
+/* Create a new notmuch_filenames_t object, with 'ctx' as its
+ * talloc owner.
+ *
+ * This function can return NULL in case of out-of-memory.
+ */
+notmuch_filenames_t *
+_notmuch_filenames_create (const void *ctx)
+{
+    notmuch_filenames_t *filenames;
+
+    filenames = talloc (ctx, notmuch_filenames_t);
+    if (unlikely (filenames == NULL))
+       return NULL;
+
+    filenames->head = NULL;
+    filenames->tail = &filenames->head;
+
+    filenames->iterator = NULL;
+
+    return filenames;
+}
+
+/* Append a single 'node' to the end of 'filenames'.
+ */
+void
+_notmuch_filenames_add_filename (notmuch_filenames_t *filenames,
+                                const char *filename)
+{
+    /* Create and initialize new node. */
+    notmuch_filenames_node_t *node = talloc (filenames,
+                                            notmuch_filenames_node_t);
+
+    node->filename = talloc_strdup (node, filename);
+    node->next = NULL;
+
+    /* Append the node to the list. */
+    *(filenames->tail) = node;
+    filenames->tail = &node->next;
+}
+
+notmuch_bool_t
+notmuch_filenames_valid (notmuch_filenames_t *filenames)
+{
+    if (filenames == NULL)
+       return FALSE;
+
+    return (filenames->iterator != NULL);
+}
+
+const char *
+notmuch_filenames_get (notmuch_filenames_t *filenames)
+{
+    if (filenames->iterator == NULL)
+       return NULL;
+
+    return filenames->iterator->filename;
+}
+
+void
+_notmuch_filenames_move_to_first (notmuch_filenames_t *filenames)
+{
+    filenames->iterator = filenames->head;
+}
+
+void
+notmuch_filenames_move_to_next (notmuch_filenames_t *filenames)
+{
+    if (filenames->iterator == NULL)
+       return;
+
+    filenames->iterator = filenames->iterator->next;
+}
+
+void
+notmuch_filenames_destroy (notmuch_filenames_t *filenames)
+{
+    talloc_free (filenames);
+}