Implement mailbox iterators.
* include/mailutils/mailbox.h (mu_mailbox_get_iterator): New function. * libproto/include/mailbox0.h (struct _mu_mailbox)<iterator>: New member. * mailbox/mbxitr.c: New file. * mailbox/Makefile.am (libmailutils_la_SOURCES): Add mbxitr.c
Showing
4 changed files
with
167 additions
and
1 deletions
... | @@ -107,6 +107,9 @@ extern int mu_mailbox_get_observable (mu_mailbox_t, mu_observable_t *); | ... | @@ -107,6 +107,9 @@ extern int mu_mailbox_get_observable (mu_mailbox_t, mu_observable_t *); |
107 | /* Locking */ | 107 | /* Locking */ |
108 | extern int mu_mailbox_lock (mu_mailbox_t mbox); | 108 | extern int mu_mailbox_lock (mu_mailbox_t mbox); |
109 | extern int mu_mailbox_unlock (mu_mailbox_t mbox); | 109 | extern int mu_mailbox_unlock (mu_mailbox_t mbox); |
110 | |||
111 | extern int mu_mailbox_get_iterator (mu_mailbox_t mbx, | ||
112 | mu_iterator_t *piterator); | ||
110 | 113 | ||
111 | #ifdef __cplusplus | 114 | #ifdef __cplusplus |
112 | } | 115 | } | ... | ... |
... | @@ -29,6 +29,7 @@ | ... | @@ -29,6 +29,7 @@ |
29 | 29 | ||
30 | #include <mailutils/monitor.h> | 30 | #include <mailutils/monitor.h> |
31 | #include <mailutils/mailbox.h> | 31 | #include <mailutils/mailbox.h> |
32 | #include <mailutils/iterator.h> | ||
32 | 33 | ||
33 | #ifdef __cplusplus | 34 | #ifdef __cplusplus |
34 | extern "C" { | 35 | extern "C" { |
... | @@ -46,7 +47,8 @@ struct _mu_mailbox | ... | @@ -46,7 +47,8 @@ struct _mu_mailbox |
46 | int flags; | 47 | int flags; |
47 | mu_folder_t folder; | 48 | mu_folder_t folder; |
48 | mu_monitor_t monitor; | 49 | mu_monitor_t monitor; |
49 | 50 | mu_iterator_t iterator; | |
51 | |||
50 | /* Back pointer to the specific mailbox */ | 52 | /* Back pointer to the specific mailbox */ |
51 | void *data; | 53 | void *data; |
52 | 54 | ... | ... |
... | @@ -95,6 +95,7 @@ libmailutils_la_SOURCES = \ | ... | @@ -95,6 +95,7 @@ libmailutils_la_SOURCES = \ |
95 | mailer.c\ | 95 | mailer.c\ |
96 | mapfile_stream.c\ | 96 | mapfile_stream.c\ |
97 | mbx_default.c\ | 97 | mbx_default.c\ |
98 | mbxitr.c\ | ||
98 | md5.c\ | 99 | md5.c\ |
99 | message.c\ | 100 | message.c\ |
100 | memory_stream.c\ | 101 | memory_stream.c\ | ... | ... |
mailbox/mbxitr.c
0 → 100644
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | ||
2 | Copyright (C) 2010 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, write to the | ||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
17 | Boston, MA 02110-1301 USA */ | ||
18 | |||
19 | #ifdef HAVE_CONFIG_H | ||
20 | #include <config.h> | ||
21 | #endif | ||
22 | |||
23 | #include <stdlib.h> | ||
24 | #include <errno.h> | ||
25 | #include <string.h> | ||
26 | |||
27 | #include <mailutils/debug.h> | ||
28 | #include <mailutils/errno.h> | ||
29 | #include <mailutils/error.h> | ||
30 | #include <mailutils/iterator.h> | ||
31 | |||
32 | #include <mailbox0.h> | ||
33 | |||
34 | struct mailbox_iterator | ||
35 | { | ||
36 | mu_mailbox_t mbx; | ||
37 | size_t idx; | ||
38 | }; | ||
39 | |||
40 | static int | ||
41 | mbx_first (void *owner) | ||
42 | { | ||
43 | struct mailbox_iterator *itr = owner; | ||
44 | itr->idx = 1; | ||
45 | return 0; | ||
46 | } | ||
47 | |||
48 | static int | ||
49 | mbx_next (void *owner) | ||
50 | { | ||
51 | struct mailbox_iterator *itr = owner; | ||
52 | itr->idx++; | ||
53 | return 0; | ||
54 | } | ||
55 | |||
56 | static int | ||
57 | mbx_getitem (void *owner, void **pret, const void **pkey) | ||
58 | { | ||
59 | struct mailbox_iterator *itr = owner; | ||
60 | size_t count; | ||
61 | int rc; | ||
62 | |||
63 | rc = mu_mailbox_messages_count (itr->mbx, &count); | ||
64 | if (rc) | ||
65 | return rc; | ||
66 | if (itr->idx > count) | ||
67 | return MU_ERR_NOENT; | ||
68 | rc = mu_mailbox_get_message (itr->mbx, itr->idx, (mu_message_t*)pret); | ||
69 | if (rc == 0 && pkey) | ||
70 | *pkey = NULL; /* FIXME: Perhaps return UIDL or other unique id? */ | ||
71 | return rc; | ||
72 | } | ||
73 | |||
74 | static int | ||
75 | mbx_finished_p (void *owner) | ||
76 | { | ||
77 | struct mailbox_iterator *itr = owner; | ||
78 | size_t count; | ||
79 | |||
80 | if (mu_mailbox_messages_count (itr->mbx, &count)) | ||
81 | return 1; | ||
82 | return itr->idx > count; | ||
83 | } | ||
84 | |||
85 | static int | ||
86 | mbx_destroy (mu_iterator_t iterator, void *data) | ||
87 | { | ||
88 | struct mailbox_iterator *itr = data; | ||
89 | |||
90 | mu_iterator_detach (&itr->mbx->iterator, iterator); | ||
91 | free (data); | ||
92 | return 0; | ||
93 | } | ||
94 | |||
95 | static int | ||
96 | mbx_curitem_p (void *owner, void *item) | ||
97 | { | ||
98 | void *ptr; | ||
99 | |||
100 | if (mbx_getitem (owner, &ptr, NULL)) | ||
101 | return 0; | ||
102 | return ptr == item;/* FIXME: Is it ok? */ | ||
103 | } | ||
104 | |||
105 | static int | ||
106 | mbx_data_dup (void **ptr, void *owner) | ||
107 | { | ||
108 | struct mailbox_iterator *itr = owner; | ||
109 | |||
110 | *ptr = malloc (sizeof (struct mailbox_iterator)); | ||
111 | if (*ptr == NULL) | ||
112 | return ENOMEM; | ||
113 | memcpy (*ptr, owner, sizeof (struct mailbox_iterator)); | ||
114 | mu_iterator_attach (&itr->mbx->iterator, *ptr); | ||
115 | return 0; | ||
116 | } | ||
117 | |||
118 | int | ||
119 | mu_mailbox_get_iterator (mu_mailbox_t mbx, mu_iterator_t *piterator) | ||
120 | { | ||
121 | mu_iterator_t iterator; | ||
122 | int status; | ||
123 | struct mailbox_iterator *itr; | ||
124 | |||
125 | if (!mbx) | ||
126 | return EINVAL; | ||
127 | |||
128 | itr = calloc (1, sizeof *itr); | ||
129 | if (!itr) | ||
130 | return ENOMEM; | ||
131 | itr->mbx = mbx; | ||
132 | itr->idx = 1; | ||
133 | |||
134 | status = mu_iterator_create (&iterator, itr); | ||
135 | if (status) | ||
136 | { | ||
137 | free (itr); | ||
138 | return status; | ||
139 | } | ||
140 | |||
141 | mu_iterator_set_first (iterator, mbx_first); | ||
142 | mu_iterator_set_next (iterator, mbx_next); | ||
143 | mu_iterator_set_getitem (iterator, mbx_getitem); | ||
144 | mu_iterator_set_finished_p (iterator, mbx_finished_p); | ||
145 | mu_iterator_set_curitem_p (iterator, mbx_curitem_p); | ||
146 | mu_iterator_set_destroy (iterator, mbx_destroy); | ||
147 | mu_iterator_set_dup (iterator, mbx_data_dup); | ||
148 | |||
149 | mu_iterator_attach (&mbx->iterator, iterator); | ||
150 | |||
151 | *piterator = iterator; | ||
152 | return 0; | ||
153 | } | ||
154 | |||
155 | |||
156 | |||
157 | |||
158 | |||
159 | |||
160 |
-
Please register or sign in to post a comment