Rocco Rutte:
[apps/madmutt.git] / lib / list.h
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 /*
11  * this aims to provide a generic list 
12  * implementation using arrays only
13  * mostly untested
14  */
15
16 #ifndef _LIB_LIST_H
17 #define _LIB_LIST_H
18
19 #include <sys/types.h>
20
21 typedef struct list2_t {
22   void** data;
23   size_t length;
24 } list2_t;
25
26 /*
27  * basics
28  */
29
30 list2_t* list_new (void);
31
32 typedef void list_del_t (void**);
33
34 /* free() all memory used by list and optionally
35  * use del function to free() items as well */
36 void list_del (list2_t**, list_del_t* del);
37
38 #define list_empty(l) (!l || l->length == 0 || !l->data)
39
40 /*
41  * insertion, removal
42  * the list_push_* functions create a list if empty so far
43  * for convenience
44  */
45 void list_push_back (list2_t**, void*);
46 void list_push_front (list2_t**, void*);
47 void* list_pop_back (list2_t*);
48 void* list_pop_front (list2_t*);
49 void* list_pop_idx (list2_t*, int);
50
51 /*
52  * copying
53  */
54
55 /* plain copy */
56 list2_t* list_cpy (list2_t*);
57 /* "hard copy" using callback to copy items */
58 list2_t* list_dup (list2_t*, void* (*dup) (void*));
59
60 /*
61  * misc
62  */
63
64 /* looks up item in list using callback function and comparison item
65  * return:
66  *      -1 if not found
67  *      index in data array otherwise
68  * the callback must return 0 on equality
69  */
70 int list_lookup (list2_t*, int (*cmp) (const void*, const void*), const void*);
71
72 /*
73  * dumb-splits string at boundary characters into list
74  */
75 list2_t* list_from_str (const char* str, const char* delim);
76
77 #endif /* !_LIB_LIST_H */