Commit db7e58b4 db7e58b41501fda7629080195e33946f6d26e2a3 by Sergey Poznyakoff

Move list support into a separate convenience library.

* configure.ac: Build libmailutils/list/Makefile
* include/mailutils/sys/list.h (DESTROY_ITEM): New define.
(_mu_list_insert_item): New proto.
* libmailutils/Makefile.am (SUBDIRS): Add list.
(libmailutils_la_LIBADD): Add list/liblist.la
* libmailutils/base/Makefile.am (libbase_la_SOURCES): Remove list sources.
* libmailutils/base/list.c: Split into smaller files (see below).
* libmailutils/base/listlist.c: Move to libmailutils/list/listlist.c
* libmailutils/list/Makefile.am: New file.
* libmailutils/list/append.c: New file.
* libmailutils/list/array.c: New file.
* libmailutils/list/clear.c: New file.
* libmailutils/list/count.c: New file.
* libmailutils/list/create.c: New file.
* libmailutils/list/destroy.c: New file.
* libmailutils/list/do.c: New file.
* libmailutils/list/empty.c: New file.
* libmailutils/list/get.c: New file.
* libmailutils/list/getcomp.c: New file.
* libmailutils/list/insert.c: New file.
* libmailutils/list/intersect.c: New file.
* libmailutils/list/iterator.c: New file.
* libmailutils/list/locate.c: New file.
* libmailutils/list/prepend.c: New file.
* libmailutils/list/remove.c: New file.
* libmailutils/list/replace.c: New file.
* libmailutils/list/setcomp.c: New file.
* libmailutils/list/setdestr.c: New file.
1 parent f7942ced
...@@ -1473,6 +1473,7 @@ AC_CONFIG_FILES([ ...@@ -1473,6 +1473,7 @@ AC_CONFIG_FILES([
1473 libmailutils/diag/Makefile 1473 libmailutils/diag/Makefile
1474 libmailutils/filter/Makefile 1474 libmailutils/filter/Makefile
1475 libmailutils/imapio/Makefile 1475 libmailutils/imapio/Makefile
1476 libmailutils/list/Makefile
1476 libmailutils/mailbox/Makefile 1477 libmailutils/mailbox/Makefile
1477 libmailutils/mailer/Makefile 1478 libmailutils/mailer/Makefile
1478 libmailutils/mime/Makefile 1479 libmailutils/mime/Makefile
......
...@@ -46,6 +46,14 @@ struct _mu_list ...@@ -46,6 +46,14 @@ struct _mu_list
46 struct _mu_iterator *itr; 46 struct _mu_iterator *itr;
47 }; 47 };
48 48
49 #define DESTROY_ITEM(list, elt) \
50 do \
51 { \
52 if ((list)->destroy_item) \
53 (list)->destroy_item ((elt)->item); \
54 } \
55 while (0)
56
49 extern void _mu_list_clear (mu_list_t list); 57 extern void _mu_list_clear (mu_list_t list);
50 extern void _mu_list_insert_sublist (mu_list_t list, 58 extern void _mu_list_insert_sublist (mu_list_t list,
51 struct list_data *current, 59 struct list_data *current,
...@@ -53,6 +61,9 @@ extern void _mu_list_insert_sublist (mu_list_t list, ...@@ -53,6 +61,9 @@ extern void _mu_list_insert_sublist (mu_list_t list,
53 struct list_data *tail, 61 struct list_data *tail,
54 size_t count, 62 size_t count,
55 int insert_before); 63 int insert_before);
64 extern int _mu_list_insert_item (mu_list_t list, struct list_data *current,
65 void *new_item,
66 int insert_before);
56 67
57 # ifdef __cplusplus 68 # ifdef __cplusplus
58 } 69 }
......
...@@ -16,8 +16,10 @@ ...@@ -16,8 +16,10 @@
16 # Public License along with this library. If not, see 16 # Public License along with this library. If not, see
17 # <http://www.gnu.org/licenses/>. 17 # <http://www.gnu.org/licenses/>.
18 18
19 SUBDIRS = auth base address sockaddr cidr cfg diag filter mailbox mailer mime\ 19 SUBDIRS = \
20 server string stream stdstream property url imapio . tests 20 auth base address list sockaddr cidr cfg diag\
21 filter mailbox mailer mime server string stream stdstream\
22 property url imapio . tests
21 23
22 lib_LTLIBRARIES = libmailutils.la 24 lib_LTLIBRARIES = libmailutils.la
23 25
...@@ -34,6 +36,7 @@ libmailutils_la_LIBADD = \ ...@@ -34,6 +36,7 @@ libmailutils_la_LIBADD = \
34 diag/libdiag.la\ 36 diag/libdiag.la\
35 filter/libfilter.la\ 37 filter/libfilter.la\
36 imapio/libimapio.la\ 38 imapio/libimapio.la\
39 list/liblist.la\
37 mailbox/libmailbox.la\ 40 mailbox/libmailbox.la\
38 mailer/libmailer.la\ 41 mailer/libmailer.la\
39 mime/libmime.la\ 42 mime/libmime.la\
......
...@@ -39,8 +39,6 @@ libbase_la_SOURCES = \ ...@@ -39,8 +39,6 @@ libbase_la_SOURCES = \
39 iterator.c\ 39 iterator.c\
40 kwd.c\ 40 kwd.c\
41 lcall.c\ 41 lcall.c\
42 list.c\
43 listlist.c\
44 locale.c\ 42 locale.c\
45 locker.c\ 43 locker.c\
46 mailcap.c\ 44 mailcap.c\
......
1 # GNU Mailutils -- a suite of utilities for electronic mail
2 # Copyright (C) 2011 Free Software Foundation, Inc.
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 3 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General
15 # Public License along with this library. If not, see
16 # <http://www.gnu.org/licenses/>.
17
18 noinst_LTLIBRARIES = liblist.la
19
20 liblist_la_SOURCES = \
21 append.c\
22 array.c\
23 clear.c\
24 count.c\
25 create.c\
26 destroy.c\
27 do.c\
28 empty.c\
29 get.c\
30 getcomp.c\
31 insert.c\
32 intersect.c\
33 iterator.c\
34 listlist.c\
35 locate.c\
36 prepend.c\
37 remove.c\
38 replace.c\
39 setcomp.c\
40 setdestr.c
41
42 INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
43
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 2010, 2011
3 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #include <stdlib.h>
23 #include <mailutils/sys/list.h>
24 #include <mailutils/errno.h>
25
26 int
27 mu_list_append (mu_list_t list, void *item)
28 {
29 struct list_data *ldata;
30 struct list_data *last;
31
32 if (list == NULL)
33 return EINVAL;
34 last = list->head.prev;
35 ldata = calloc (sizeof (*ldata), 1);
36 if (ldata == NULL)
37 return ENOMEM;
38 ldata->item = item;
39 mu_monitor_wrlock (list->monitor);
40 ldata->next = &list->head;
41 ldata->prev = list->head.prev;
42 last->next = ldata;
43 list->head.prev = ldata;
44 list->count++;
45 mu_monitor_unlock (list->monitor);
46 return 0;
47 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 2010, 2011
3 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #include <mailutils/sys/list.h>
23 #include <mailutils/errno.h>
24
25 int
26 mu_list_to_array (mu_list_t list, void **array, size_t count, size_t *pcount)
27 {
28 size_t total = 0;
29
30 if (!list)
31 return EINVAL;
32
33 total = (count < list->count) ? count : list->count;
34
35 if (array)
36 {
37 size_t i;
38 struct list_data *current;
39
40 for (i = 0, current = list->head.next;
41 i < total && current != &list->head; current = current->next)
42 array[i++] = current->item;
43 }
44 if (pcount)
45 *pcount = total;
46 return 0;
47 }
48
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 2010, 2011
3 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #include <stdlib.h>
23 #include <mailutils/sys/list.h>
24
25 void
26 mu_list_clear (mu_list_t list)
27 {
28 struct list_data *current;
29 struct list_data *previous;
30
31 if (!list)
32 return;
33 mu_monitor_wrlock (list->monitor);
34 for (current = list->head.next; current != &list->head;)
35 {
36 previous = current;
37 current = current->next;
38 DESTROY_ITEM (list, previous);
39 free (previous);
40 }
41 list->head.next = list->head.prev = &list->head;
42 mu_monitor_unlock (list->monitor);
43 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 2010, 2011
3 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #include <stdlib.h>
23 #include <mailutils/sys/list.h>
24 #include <mailutils/errno.h>
25
26 int
27 mu_list_count (mu_list_t list, size_t *pcount)
28 {
29 if (list == NULL)
30 return EINVAL;
31 if (pcount == NULL)
32 return MU_ERR_OUT_PTR_NULL;
33 *pcount = list->count;
34 return 0;
35 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 2010, 2011
3 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <stdlib.h>
24
25 #include <mailutils/sys/list.h>
26 #include <mailutils/errno.h>
27
28 int
29 mu_list_create (mu_list_t *plist)
30 {
31 mu_list_t list;
32 int status;
33
34 if (plist == NULL)
35 return MU_ERR_OUT_PTR_NULL;
36 list = calloc (sizeof (*list), 1);
37 if (list == NULL)
38 return ENOMEM;
39 status = mu_monitor_create (&list->monitor, 0, list);
40 if (status != 0)
41 {
42 free (list);
43 return status;
44 }
45 list->head.next = &list->head;
46 list->head.prev = &list->head;
47 *plist = list;
48 return 0;
49 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 2010, 2011
3 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #include <stdlib.h>
23 #include <mailutils/sys/list.h>
24
25 void
26 mu_list_destroy (mu_list_t *plist)
27 {
28 if (plist && *plist)
29 {
30 mu_list_t list = *plist;
31 mu_list_clear (list);
32 mu_monitor_destroy (&list->monitor, list);
33 free (list);
34 *plist = NULL;
35 }
36 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 2010, 2011
3 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #include <stdlib.h>
23 #include <mailutils/sys/list.h>
24 #include <mailutils/errno.h>
25
26 int
27 mu_list_do (mu_list_t list, mu_list_action_t *action, void *cbdata)
28 {
29 mu_iterator_t itr;
30 int status = 0;
31
32 if (list == NULL || action == NULL)
33 return EINVAL;
34 status = mu_list_get_iterator (list, &itr);
35 if (status)
36 return status;
37 for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
38 mu_iterator_next (itr))
39 {
40 void *item;
41 mu_iterator_current (itr, &item);
42 if ((status = action (item, cbdata)))
43 break;
44 }
45 mu_iterator_destroy (&itr);
46 return status;
47 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 2010, 2011
3 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #include <mailutils/sys/list.h>
23
24 int
25 mu_list_is_empty (mu_list_t list)
26 {
27 size_t n = 0;
28
29 mu_list_count (list, &n);
30 return (n == 0);
31 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 2010, 2011
3 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #include <stdlib.h>
23 #include <mailutils/sys/list.h>
24 #include <mailutils/errno.h>
25
26 int
27 mu_list_get (mu_list_t list, size_t indx, void **pitem)
28 {
29 struct list_data *current;
30 size_t count;
31 int status = MU_ERR_NOENT;
32
33 if (list == NULL)
34 return EINVAL;
35 if (pitem == NULL)
36 return MU_ERR_OUT_PTR_NULL;
37 mu_monitor_rdlock (list->monitor);
38 for (current = list->head.next, count = 0; current != &list->head;
39 current = current->next, count++)
40 {
41 if (count == indx)
42 {
43 *pitem = current->item;
44 status = 0;
45 break;
46 }
47 }
48 mu_monitor_unlock (list->monitor);
49 return status;
50 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 2010, 2011
3 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #include <mailutils/sys/list.h>
23 #include <mailutils/errno.h>
24
25 int
26 mu_list_get_comparator (mu_list_t list, mu_list_comparator_t *comp)
27 {
28 if (!list)
29 return EINVAL;
30 *comp = list->comp;
31 return 0;
32 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 2010, 2011
3 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #include <stdlib.h>
23 #include <mailutils/sys/list.h>
24 #include <mailutils/errno.h>
25
26 int
27 _mu_list_insert_item (mu_list_t list, struct list_data *current,
28 void *new_item,
29 int insert_before)
30 {
31 int status;
32 struct list_data *ldata = calloc (sizeof (*ldata), 1);
33 if (ldata == NULL)
34 status = ENOMEM;
35 else
36 {
37 ldata->item = new_item;
38 _mu_list_insert_sublist (list, current,
39 ldata, ldata,
40 1,
41 insert_before);
42 status = 0;
43 }
44 return status;
45 }
46
47 int
48 mu_list_insert (mu_list_t list, void *item, void *new_item, int insert_before)
49 {
50 struct list_data *current;
51 mu_list_comparator_t comp;
52 int status = MU_ERR_NOENT;
53
54 if (list == NULL)
55 return EINVAL;
56 comp = list->comp ? list->comp : _mu_list_ptr_comparator;
57
58 mu_monitor_wrlock (list->monitor);
59 for (current = list->head.next;
60 current != &list->head;
61 current = current->next)
62 {
63 if (comp (current->item, item) == 0)
64 {
65 status = _mu_list_insert_item (list, current, new_item,
66 insert_before);
67 break;
68 }
69 }
70 mu_monitor_unlock (list->monitor);
71 return status;
72 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 2010, 2011
3 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #include <stdlib.h>
23 #include <mailutils/sys/list.h>
24 #include <mailutils/errno.h>
25
26 /* Computes an intersection of two lists and returns it in PDEST.
27 The resulting list contains elements from A that are
28 also encountered in B (as per comparison function of
29 the latter).
30
31 If DUP_ITEM is not NULL, it is used to create copies of
32 items to be stored in PDEST. In this case, the destroy_item
33 function of B is also attached to PDEST. Otherwise, if
34 DUP_ITEM is NULL, pointers to elements are stored and
35 no destroy_item function is assigned. */
36 int
37 mu_list_intersect_dup (mu_list_t *pdest, mu_list_t a, mu_list_t b,
38 int (*dup_item) (void **, void *, void *),
39 void *dup_closure)
40 {
41 mu_list_t dest;
42 int rc;
43 mu_iterator_t itr;
44
45 rc = mu_list_create (&dest);
46 if (rc)
47 return rc;
48
49 mu_list_set_comparator (dest, b->comp);
50 if (dup_item)
51 mu_list_set_destroy_item (dest, b->destroy_item);
52
53 rc = mu_list_get_iterator (a, &itr);
54 if (rc)
55 {
56 mu_list_destroy (&dest);
57 return rc;
58 }
59
60 rc = 0;
61 for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
62 mu_iterator_next (itr))
63 {
64 void *data;
65 mu_iterator_current (itr, &data);
66 if (mu_list_locate (b, data, NULL) == 0)
67 {
68 void *new_data;
69 if (dup_item && data)
70 {
71 rc = dup_item (&new_data, data, dup_closure);
72 if (rc)
73 break;
74 }
75 else
76 new_data = data;
77
78 mu_list_append (dest, new_data); /* FIXME: Check return, and? */
79 }
80 }
81 mu_iterator_destroy (&itr);
82 *pdest = dest;
83 return rc;
84 }
85
86 int
87 mu_list_intersect (mu_list_t *pdest, mu_list_t a, mu_list_t b)
88 {
89 return mu_list_intersect_dup (pdest, a, b, NULL, NULL);
90 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 2010, 2011
3 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <mailutils/sys/list.h>
26 #include <mailutils/sys/iterator.h>
27 #include <mailutils/errno.h>
28
29 /* Iterator interface */
30
31 struct list_iterator
32 {
33 mu_list_t list;
34 struct list_data *cur;
35 int backwards; /* true if iterating backwards */
36 };
37
38 static int
39 first (void *owner)
40 {
41 struct list_iterator *itr = owner;
42 if (itr->backwards)
43 itr->cur = itr->list->head.prev;
44 else
45 itr->cur = itr->list->head.next;
46 return 0;
47 }
48
49 static int
50 next (void *owner)
51 {
52 struct list_iterator *itr = owner;
53 if (itr->backwards)
54 itr->cur = itr->cur->prev;
55 else
56 itr->cur = itr->cur->next;
57 return 0;
58 }
59
60 static int
61 getitem (void *owner, void **pret, const void **pkey)
62 {
63 struct list_iterator *itr = owner;
64 *pret = itr->cur->item;
65 if (pkey)
66 *pkey = NULL;
67 return 0;
68 }
69
70 static int
71 finished_p (void *owner)
72 {
73 struct list_iterator *itr = owner;
74 return itr->cur == &itr->list->head;
75 }
76
77 static int
78 destroy (mu_iterator_t iterator, void *data)
79 {
80 struct list_iterator *itr = data;
81 mu_iterator_detach (&itr->list->itr, iterator);
82 free (data);
83 return 0;
84 }
85
86 static int
87 curitem_p (void *owner, void *item)
88 {
89 struct list_iterator *itr = owner;
90 return itr->cur == item;
91 }
92
93 static int
94 list_data_dup (void **ptr, void *owner)
95 {
96 *ptr = malloc (sizeof (struct list_iterator));
97 if (*ptr == NULL)
98 return ENOMEM;
99 memcpy (*ptr, owner, sizeof (struct list_iterator));
100 return 0;
101 }
102
103 static int
104 list_itrctl (void *owner, enum mu_itrctl_req req, void *arg)
105 {
106 struct list_iterator *itr = owner;
107 mu_list_t list = itr->list;
108 struct list_data *ptr;
109
110 switch (req)
111 {
112 case mu_itrctl_tell:
113 /* Return current position in the object */
114 if (itr->cur == NULL)
115 return MU_ERR_NOENT;
116 else
117 {
118 size_t count;
119
120 for (count = 0, ptr = list->head.next; ptr != &list->head;
121 ptr = ptr->next, count++)
122 {
123 if (ptr == itr->cur)
124 {
125 *(size_t*)arg = count;
126 return 0;
127 }
128 }
129 return MU_ERR_NOENT;
130 }
131 break;
132
133 case mu_itrctl_delete:
134 case mu_itrctl_delete_nd:
135 /* Delete current element */
136 if (itr->cur == NULL)
137 return MU_ERR_NOENT;
138 else
139 {
140 struct list_data *prev;
141
142 ptr = itr->cur;
143 prev = ptr->prev;
144
145 mu_iterator_advance (list->itr, ptr);
146 prev->next = ptr->next;
147 ptr->next->prev = prev;
148 if (req == mu_itrctl_delete)
149 DESTROY_ITEM (list, ptr);
150 free (ptr);
151 list->count--;
152 }
153 break;
154
155 case mu_itrctl_replace:
156 case mu_itrctl_replace_nd:
157 /* Replace current element */
158 if (itr->cur == NULL)
159 return MU_ERR_NOENT;
160 if (!arg)
161 return EINVAL;
162 ptr = itr->cur;
163 if (req == mu_itrctl_replace)
164 DESTROY_ITEM (list, ptr);
165 ptr = itr->cur;
166 ptr->item = arg;
167 break;
168
169 case mu_itrctl_insert:
170 /* Insert new element in the current position */
171 if (itr->cur == NULL)
172 return MU_ERR_NOENT;
173 if (!arg)
174 return EINVAL;
175 return _mu_list_insert_item (list, itr->cur, arg, 0);
176
177 case mu_itrctl_insert_list:
178 /* Insert a list of elements */
179 if (itr->cur == NULL)
180 return MU_ERR_NOENT;
181 if (!arg)
182 return EINVAL;
183 else
184 {
185 mu_list_t new_list = arg;
186 _mu_list_insert_sublist (list, itr->cur,
187 new_list->head.next, new_list->head.prev,
188 new_list->count,
189 0);
190 _mu_list_clear (new_list);
191 }
192 break;
193
194 case mu_itrctl_qry_direction:
195 if (!arg)
196 return EINVAL;
197 else
198 *(int*)arg = itr->backwards;
199 break;
200
201 case mu_itrctl_set_direction:
202 if (!arg)
203 return EINVAL;
204 else
205 itr->backwards = !!*(int*)arg;
206 break;
207
208 default:
209 return ENOSYS;
210 }
211 return 0;
212 }
213
214 int
215 mu_list_get_iterator (mu_list_t list, mu_iterator_t *piterator)
216 {
217 mu_iterator_t iterator;
218 int status;
219 struct list_iterator *itr;
220
221 if (!list)
222 return EINVAL;
223
224 itr = calloc (1, sizeof *itr);
225 if (!itr)
226 return ENOMEM;
227 itr->list = list;
228 itr->cur = NULL;
229
230 status = mu_iterator_create (&iterator, itr);
231 if (status)
232 {
233 free (itr);
234 return status;
235 }
236
237 mu_iterator_set_first (iterator, first);
238 mu_iterator_set_next (iterator, next);
239 mu_iterator_set_getitem (iterator, getitem);
240 mu_iterator_set_finished_p (iterator, finished_p);
241 mu_iterator_set_curitem_p (iterator, curitem_p);
242 mu_iterator_set_destroy (iterator, destroy);
243 mu_iterator_set_dup (iterator, list_data_dup);
244 mu_iterator_set_itrctl (iterator, list_itrctl);
245
246 mu_iterator_attach (&list->itr, iterator);
247
248 *piterator = iterator;
249 return 0;
250 }
...@@ -19,11 +19,7 @@ ...@@ -19,11 +19,7 @@
19 #ifdef HAVE_CONFIG_H 19 #ifdef HAVE_CONFIG_H
20 # include <config.h> 20 # include <config.h>
21 #endif 21 #endif
22
23 #include <errno.h>
24 #include <stdlib.h> 22 #include <stdlib.h>
25 #include <string.h>
26
27 #include <mailutils/sys/list.h> 23 #include <mailutils/sys/list.h>
28 #include <mailutils/sys/iterator.h> 24 #include <mailutils/sys/iterator.h>
29 #include <mailutils/errno.h> 25 #include <mailutils/errno.h>
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 2010, 2011
3 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #include <stdlib.h>
23 #include <mailutils/sys/list.h>
24 #include <mailutils/errno.h>
25
26 int
27 _mu_list_ptr_comparator (const void *item, const void *value)
28 {
29 return item != value;
30 }
31
32 int
33 mu_list_locate (mu_list_t list, void *item, void **ret_item)
34 {
35 struct list_data *current, *previous;
36 mu_list_comparator_t comp;
37 int status = MU_ERR_NOENT;
38
39 if (list == NULL)
40 return EINVAL;
41 comp = list->comp ? list->comp : _mu_list_ptr_comparator;
42 mu_monitor_wrlock (list->monitor);
43 for (previous = &list->head, current = list->head.next;
44 current != &list->head; previous = current, current = current->next)
45 {
46 if (comp (current->item, item) == 0)
47 {
48 if (ret_item)
49 *ret_item = current->item;
50 status = 0;
51 break;
52 }
53 }
54 mu_monitor_unlock (list->monitor);
55 return status;
56 }
57
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 2010, 2011
3 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <stdlib.h>
24 #include <mailutils/sys/list.h>
25 #include <mailutils/errno.h>
26
27 int
28 mu_list_prepend (mu_list_t list, void *item)
29 {
30 struct list_data *ldata;
31 struct list_data *first;
32
33 if (list == NULL)
34 return EINVAL;
35 first = list->head.next;
36 ldata = calloc (sizeof (*ldata), 1);
37 if (ldata == NULL)
38 return ENOMEM;
39 ldata->item = item;
40 mu_monitor_wrlock (list->monitor);
41 ldata->prev = &list->head;
42 ldata->next = list->head.next;
43 first->prev = ldata;
44 list->head.next = ldata;
45 list->count++;
46 mu_monitor_unlock (list->monitor);
47 return 0;
48 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 2010, 2011
3 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #include <stdlib.h>
23 #include <mailutils/sys/list.h>
24 #include <mailutils/errno.h>
25
26 int
27 mu_list_remove (mu_list_t list, void *item)
28 {
29 struct list_data *current;
30 mu_list_comparator_t comp;
31 int status = MU_ERR_NOENT;
32
33 if (list == NULL)
34 return EINVAL;
35 comp = list->comp ? list->comp : _mu_list_ptr_comparator;
36 mu_monitor_wrlock (list->monitor);
37 for (current = list->head.next;
38 current != &list->head; current = current->next)
39 {
40 if (comp (current->item, item) == 0)
41 {
42 struct list_data *previous = current->prev;
43
44 mu_iterator_advance (list->itr, current);
45 previous->next = current->next;
46 current->next->prev = previous;
47 DESTROY_ITEM (list, current);
48 free (current);
49 list->count--;
50 status = 0;
51 break;
52 }
53 }
54 mu_monitor_unlock (list->monitor);
55 return status;
56 }
57
58 int
59 mu_list_remove_nd (mu_list_t list, void *item)
60 {
61 mu_list_destroy_item_t dptr = mu_list_set_destroy_item (list, NULL);
62 int rc = mu_list_remove (list, item);
63 mu_list_set_destroy_item (list, dptr);
64 return rc;
65 }
66
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 2010, 2011
3 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #include <stdlib.h>
23 #include <mailutils/sys/list.h>
24 #include <mailutils/errno.h>
25
26 int
27 mu_list_replace (mu_list_t list, void *old_item, void *new_item)
28 {
29 struct list_data *current, *previous;
30 mu_list_comparator_t comp;
31 int status = MU_ERR_NOENT;
32
33 if (list == NULL)
34 return EINVAL;
35 comp = list->comp ? list->comp : _mu_list_ptr_comparator;
36 mu_monitor_wrlock (list->monitor);
37 for (previous = &list->head, current = list->head.next;
38 current != &list->head; previous = current, current = current->next)
39 {
40 if (comp (current->item, old_item) == 0)
41 {
42 DESTROY_ITEM (list, current);
43 current->item = new_item;
44 status = 0;
45 break;
46 }
47 }
48 mu_monitor_unlock (list->monitor);
49 return status;
50 }
51
52 int
53 mu_list_replace_nd (mu_list_t list, void *item, void *new_item)
54 {
55 mu_list_destroy_item_t dptr = mu_list_set_destroy_item (list, NULL);
56 int rc = mu_list_replace (list, item, new_item);
57 mu_list_set_destroy_item (list, dptr);
58 return rc;
59 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 2010, 2011
3 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #include <stdlib.h>
23 #include <mailutils/sys/list.h>
24
25 mu_list_comparator_t
26 mu_list_set_comparator (mu_list_t list, mu_list_comparator_t comp)
27 {
28 mu_list_comparator_t old_comp;
29
30 if (list == NULL)
31 return NULL;
32 old_comp = list->comp;
33 list->comp = comp;
34 return old_comp;
35 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 2010, 2011
3 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #include <stdlib.h>
23 #include <mailutils/sys/list.h>
24
25 mu_list_destroy_item_t
26 mu_list_set_destroy_item (mu_list_t list, void (*destroy_item)(void *item))
27 {
28 mu_list_destroy_item_t ret = list->destroy_item;
29 list->destroy_item = destroy_item;
30 return ret;
31 }