More string and buffer functions.
[apps/madmutt.git] / lib-lib / list.h
index 2d6e228..4f54d32 100644 (file)
 #ifndef MUTT_LIB_LIB_LIST_H
 #define MUTT_LIB_LIB_LIST_H
 
-#include "str.h"
-
-typedef struct list_t {
-    char *data;
-    struct list_t *next;
-} LIST;
-
-#define mutt_new_list() p_new(LIST, 1)
-void mutt_free_list(LIST **);
-
-LIST *mutt_copy_list(LIST *);
-
-/* 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) {
-    size_t len = m_strlen(data);
-    return mutt_add_list_n(head, data, len ? len + 1 : 0);
-}
-
-#define DO_SLIST(type, prefix)                                               \
+#define DO_SLIST(type, prefix, dtor)                                         \
     static inline type *prefix##_list_pop(type **list) {                     \
         if (*list) {                                                         \
             type *res = *list;                                               \
@@ -59,23 +40,62 @@ static inline LIST *mutt_add_list(LIST *head, const char *data) {
         *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_rev(type *list) {                      \
+        type *l = NULL;                                                      \
+        while (list) {                                                       \
+            prefix##_list_push(&l, prefix##_list_pop(&list));                \
+        }                                                                    \
+        return l;                                                            \
+    }                                                                        \
+                                                                             \
     static inline type **prefix##_list_init(type **list) {                   \
         *list = NULL;                                                        \
         return list;                                                         \
     }                                                                        \
-    static inline void prefix##_list_wipe(type **list, int del) {            \
-        if (del) {                                                           \
-            while (*list) {                                                  \
-                type *item = prefix##_list_pop(list);                        \
-                prefix##_delete(&item);                                      \
-            }                                                                \
-        } else {                                                             \
-            *list = NULL;                                                    \
+    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;
+} string_list_t;
 
+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);
+
+string_list_t *string_list_dup(const string_list_t *);
+int string_list_contains(const string_list_t *, const char *, const char *);
+void string_list_add(string_list_t **, const char *);
+void string_list_remove(string_list_t **l, const char *str);
 
+/* FIXME: b0rken API's, replace that at any cost */
+/* add an element to a list */
+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_LIB_LIB_LIST_H */