0b852e07b63265198a44a53694b4e2edf6d47eca
[apps/madmutt.git] / lib / list.c
1 /*
2  * written for mutt-ng by:
3  * Rocco Rutte <pdmef@cs.tu-berlin.de>
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
10 #include <lib-lib/lib-lib.h>
11 #include "list.h"
12
13 list2_t* list_new (void) {
14   return p_new(list2_t, 1);
15 }
16
17 void list_del (list2_t** l, list_del_t* del) {
18   ssize_t i = 0;
19   if (!l || !*l)
20     return;
21   if (del)
22     for (i = 0; i < (*l)->length; i++)
23       del (&(*l)->data[i]);
24   p_delete(&(*l)->data);
25   p_delete(l);
26 }
27
28 void list_push_back (list2_t** l, void* p) {
29   if (!*l)
30     *l = list_new ();
31   p_realloc(&(*l)->data, ++(*l)->length);
32   (*l)->data[(*l)->length-1] = p;
33 }
34
35 void list_push_front (list2_t** l, void* p) {
36   if (!*l)
37     *l = list_new ();
38   p_realloc(&(*l)->data, ++(*l)->length);
39   if ((*l)->length > 1)
40     memmove (&(*l)->data[1], &(*l)->data[0], ((*l)->length-1)*sizeof(void*));
41   (*l)->data[0] = p;
42 }
43
44 void* list_pop_back (list2_t* l) {
45   void* p = NULL;
46   if (list_empty(l))
47     return (NULL);
48   p = l->data[l->length-1];
49   p_realloc(&l->data, --(l->length));
50   return (p);
51 }
52
53 void* list_pop_front (list2_t* l) {
54   void* p = NULL;
55   if (list_empty(l))
56     return (NULL);
57   p = l->data[0];
58   memmove (&l->data[0], &l->data[1], (--(l->length))*sizeof(void*));
59   p_realloc(&l->data, l->length);
60   return (p);
61 }
62
63 void* list_pop_idx (list2_t* l, int c) {
64   void* p = NULL;
65   if (list_empty(l) || c < 0 || c >= l->length)
66     return (NULL);
67   if (c == l->length-1)
68     return (list_pop_back (l));
69   p = l->data[c];
70   memmove (&l->data[c], &l->data[c+1], (l->length-c)*sizeof(void*));
71   p_realloc(&l->data, --(l->length));
72   return (p);
73 }
74
75 list2_t *list_cpy(list2_t *l) {
76     list2_t* ret = NULL;
77     if (list_empty(l))
78         return NULL;
79     ret = list_new();
80     ret->length = l->length;
81     ret->data = p_dup(l->data, l->length);
82     return ret;
83 }
84
85 list2_t* list_dup (list2_t* l, void* (*dup_f) (void*)) {
86   list2_t* ret = NULL;
87   int i = 0;
88   if (list_empty(l) || !*dup_f)
89     return (NULL);
90   ret = list_new ();
91   ret->length = l->length;
92   ret->data = p_new(void*, l->length);
93   for (i = 0; i < l->length; i++)
94     ret->data[i] = dup_f (l->data[i]);
95   return (ret);
96 }
97
98 int list_lookup (list2_t* l, int (*cmp) (const void*, const void*), const void* p) {
99   int i = 0;
100   if (list_empty(l) || !*cmp)
101     return (-1);
102   for (i = 0; i < l->length; i++)
103     if (cmp (l->data[i], p) == 0)
104       return (i);
105   return (-1);
106 }
107
108 list2_t* list_from_str (const char* str, const char* delim) {
109   list2_t* ret = NULL;
110   char* tmp = NULL, *p = NULL;
111
112   if (!str || !*str || !delim || !*delim)
113     return (NULL);
114
115   tmp = m_strdup(str);
116   for (p = strtok (tmp, delim); p; p = strtok (NULL, delim)) {
117     list_push_back (&ret, m_strdup(p));
118   }
119   p_delete(&tmp);
120   return (ret);
121 }
122