sort out some prototypes, put them where they belong.
[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 <lib-lib/lib-lib.h>
14
15 #include "list.h"
16
17
18 list2_t* list_new (void) {
19   return p_new(list2_t, 1);
20 }
21
22 void list_del (list2_t** l, list_del_t* del) {
23   ssize_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   p_delete(&(*l)->data);
30   p_delete(l);
31 }
32
33 void list_push_back (list2_t** l, void* p) {
34   if (!*l)
35     *l = list_new ();
36   p_realloc(&(*l)->data, ++(*l)->length);
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   p_realloc(&(*l)->data, ++(*l)->length);
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   p_realloc(&l->data, --(l->length));
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   p_realloc(&l->data, l->length);
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   p_realloc(&l->data, --(l->length));
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 = p_dup(l->data, l->length);
87     return ret;
88 }
89
90 list2_t* list_dup (list2_t* l, void* (*dup_f) (void*)) {
91   list2_t* ret = NULL;
92   int i = 0;
93   if (list_empty(l) || !*dup_f)
94     return (NULL);
95   ret = list_new ();
96   ret->length = l->length;
97   ret->data = p_new(void*, l->length);
98   for (i = 0; i < l->length; i++)
99     ret->data[i] = dup_f (l->data[i]);
100   return (ret);
101 }
102
103 int list_lookup (list2_t* l, int (*cmp) (const void*, const void*), const void* p) {
104   int i = 0;
105   if (list_empty(l) || !*cmp)
106     return (-1);
107   for (i = 0; i < l->length; i++)
108     if (cmp (l->data[i], p) == 0)
109       return (i);
110   return (-1);
111 }
112
113 list2_t* list_from_str (const char* str, const char* delim) {
114   list2_t* ret = NULL;
115   char* tmp = NULL, *p = NULL;
116
117   if (!str || !*str || !delim || !*delim)
118     return (NULL);
119
120   tmp = m_strdup(str);
121   for (p = strtok (tmp, delim); p; p = strtok (NULL, delim)) {
122     list_push_back (&ret, m_strdup(p));
123   }
124   p_delete(&tmp);
125   return (ret);
126 }
127