the ‘kids don't do this at home’ commit.
[apps/madmutt.git] / lib-lib / list.h
index 1a915b3..b1b612e 100644 (file)
  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
  */
 
-#ifndef _MUTT_LIST_H
-#define _MUTT_LIST_H
+#ifndef MUTT_LIB_LIB_LIST_H
+#define MUTT_LIB_LIB_LIST_H
 
-typedef struct list_t {
+#define DO_SLIST(type, prefix, dtor)                                         \
+    static inline type *prefix##_list_pop(type **list) {                     \
+        if (*list) {                                                         \
+            type *res = *list;                                               \
+            *list = res->next;                                               \
+            res->next = NULL;                                                \
+            return res;                                                      \
+        }                                                                    \
+        return NULL;                                                         \
+    }                                                                        \
+    static inline void prefix##_list_push(type **list, type *item) {         \
+        item->next = *list;                                                  \
+        *list = item;                                                        \
+    }                                                                        \
+                                                                             \
+    static inline type **prefix##_list_last(type **list) {                   \
+        while (*list) {                                                      \
+            list = &(*list)->next;                                           \
+        }                                                                    \
+        return list;                                                         \
+    }                                                                        \
+                                                                             \
+    static inline type **prefix##_list_append(type **list, type *item) {     \
+        list = prefix##_list_last(list);                                     \
+        *list = item;                                                        \
+        return list;                                                         \
+    }                                                                        \
+                                                                             \
+    static inline type **prefix##_list_init(type **list) {                   \
+        *list = NULL;                                                        \
+        return list;                                                         \
+    }                                                                        \
+    static inline void prefix##_list_wipe(type **list) {                     \
+        while (*list) {                                                      \
+            type *item = prefix##_list_pop(list);                            \
+            dtor(&item);                                                     \
+        }                                                                    \
+    }                                                                        \
+
+
+typedef struct string_list_t {
+    struct string_list_t *next;
     char *data;
-    struct list_t *next;
-} LIST;
+} string_list_t;
 
-#define mutt_new_list() p_new(LIST, 1)
-void mutt_free_list(LIST **);
+DO_INIT(string_list_t, string_item);
+static inline void string_item_wipe(string_list_t *it) {
+    p_delete(&it->data);
+}
+DO_NEW(string_list_t, string_item);
+DO_DELETE(string_list_t, string_item);
+DO_SLIST(string_list_t, string, string_item_delete);
 
-LIST *mutt_copy_list(LIST *);
+string_list_t *string_list_dup(const string_list_t *);
 
+/* FIXME: b0rken API's, replace that at any cost */
 /* add an element to a list */
-LIST *mutt_add_list_n(LIST*, const void*, size_t len);
-static inline LIST *mutt_add_list(LIST *head, const char *data) {
+string_list_t *mutt_add_list_n(string_list_t*, const void*, size_t len);
+static inline string_list_t *mutt_add_list(string_list_t *head, const char *data) {
     size_t len = m_strlen(data);
     return mutt_add_list_n(head, data, len ? len + 1 : 0);
 }
 
-#endif /* !_MUTT_LIST_H */
+#endif /* MUTT_LIB_LIB_LIST_H */