X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=lib-lib%2Flist.h;h=2dac0f855d1791efbe156b438f96f2934d98b698;hp=1a915b30c1c26662dddfec4fb72412b41ecd6dac;hb=d4321859bb9ce5b43fbd8303394cf98ae7720a38;hpb=9342efc81d163ea56e079b4adf2ce17c41c853b0 diff --git a/lib-lib/list.h b/lib-lib/list.h index 1a915b3..2dac0f8 100644 --- a/lib-lib/list.h +++ b/lib-lib/list.h @@ -22,24 +22,73 @@ * Copyright (C) 1996-2000 Michael R. Elkins */ -#ifndef _MUTT_LIST_H -#define _MUTT_LIST_H +#ifndef MUTT_LIB_LIB_LIST_H +#define MUTT_LIB_LIB_LIST_H -typedef struct list_t { +#include "mem.h" +#include "str.h" + +#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 */