drop mem_alloc and mem_free, use my own hand crafted optmized macros that
[apps/madmutt.git] / list.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  *
5  * This file is part of mutt-ng, see http://www.muttng.org/.
6  * It's licensed under the GNU General Public License,
7  * please see the file GPL in the top level source directory.
8  */
9 #if HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include <lib-lib/mem.h>
17
18 #include "list.h"
19 #include "lib/mem.h"
20 #include "lib/str.h"
21
22 LIST *mutt_copy_list (LIST * p) {
23   LIST *t, *r = NULL, *l = NULL;
24
25   for (; p; p = p->next) {
26     t = p_new(LIST, 1);
27     t->data = str_dup (p->data);
28     t->next = NULL;
29     if (l) {
30       r->next = t;
31       r = r->next;
32     }
33     else
34       l = r = t;
35   }
36   return (l);
37 }
38
39
40 LIST *mutt_add_list (LIST * head, const char *data) {
41   size_t len = str_len (data);
42   return (mutt_add_list_n (head, data, len ? len + 1 : 0));
43 }
44
45 LIST *mutt_add_list_n (LIST *head, const void *data, size_t len) {
46   LIST *tmp;
47
48   for (tmp = head; tmp && tmp->next; tmp = tmp->next);
49
50   if (tmp) {
51     tmp->next = p_new(LIST, 1);
52     tmp = tmp->next;
53   } else
54     head = tmp = p_new(LIST, 1);
55
56   tmp->data = p_new(char, len);
57   if (len)
58     memcpy (tmp->data, data, len);
59   tmp->next = NULL;
60   return head;
61 }
62
63 void mutt_free_list (LIST ** list) {
64   LIST *p;
65
66   if (!list)
67     return;
68   while (*list) {
69     p = *list;
70     *list = (*list)->next;
71     p_delete(&p->data);
72     p_delete(&p);
73   }
74 }