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