Commit a6ed2f92 a6ed2f92b4e7cd21cd1634f1ee4443f40b12a68b by Sergey Poznyakoff

New itrctl request: mu_itrctl_count

* include/mailutils/iterator.h (mu_itrctl_req) <mu_itrctl_count>: New request
type.
* libmailutils/base/assoc.c (itrctl): Support mu_itrctl_count.
* libmailutils/list/iterator.c (list_itrctl): Likewise.
* libmailutils/mailbox/hdritr.c (hdr_itrctl): Likewise.
* libmailutils/mailbox/mbxitr.c (mbx_itrctl): Likewise.
* libmailutils/base/opool.c (opitr_itrctl): New function - support
mu_itrctl_count.
(mu_opool_get_iterator): Register opitr_itrctl.
* libmailutils/diag/debug.c (list_itrctl): Return 1-based position
indices on mu_itrctl_tell.
Support mu_itrctl_count.
1 parent afbb33cf
......@@ -35,7 +35,8 @@ enum mu_itrctl_req
mu_itrctl_insert, /* Insert new element in the current position */
mu_itrctl_insert_list, /* Insert a list of elements */
mu_itrctl_qry_direction, /* Query iteration direction */
mu_itrctl_set_direction /* Set iteration direction */
mu_itrctl_set_direction, /* Set iteration direction */
mu_itrctl_count /* Get number of elements */
};
extern int mu_iterator_create (mu_iterator_t *, void *);
......
......@@ -514,6 +514,11 @@ itrctl (void *owner, enum mu_itrctl_req req, void *arg)
else
itr->backwards = !!*(int*)arg;
break;
case mu_itrctl_count:
if (!arg)
return EINVAL;
return mu_assoc_count (assoc, arg);
default:
return ENOSYS;
......
......@@ -513,6 +513,31 @@ opitr_data_dup (void **ptr, void *owner)
return 0;
}
static int
opitr_itrctl (void *owner, enum mu_itrctl_req req, void *arg)
{
struct opool_iterator *itr = owner;
switch (req)
{
case mu_itrctl_count:
if (!arg)
return EINVAL;
else
{
size_t n = 0;
union mu_opool_bucket *p;
for (p = itr->opool->bkt_head; p; p = p->hdr.next)
n++;
*(size_t*)arg = n;
}
break;
default:
return ENOSYS;
}
return 0;
}
int
mu_opool_get_iterator (mu_opool_t opool, mu_iterator_t *piterator)
{
......@@ -543,7 +568,8 @@ mu_opool_get_iterator (mu_opool_t opool, mu_iterator_t *piterator)
mu_iterator_set_delitem (iterator, opitr_delitem);
mu_iterator_set_destroy (iterator, opitr_destroy);
mu_iterator_set_dup (iterator, opitr_data_dup);
mu_iterator_set_itrctl (iterator, opitr_itrctl);
opool->itr_count++;
*piterator = iterator;
......
......@@ -598,10 +598,12 @@ list_itrctl (void *owner, enum mu_itrctl_req req, void *arg)
switch (req)
{
case mu_itrctl_tell:
/* Return current position in the object */
/* Return current position in the object.
NOTE: Positions are 1-based.
*/
if (!arg)
return EINVAL;
*(size_t*)arg = itr->pos;
*(size_t*)arg = itr->pos + 1;
break;
case mu_itrctl_delete:
......@@ -632,6 +634,12 @@ list_itrctl (void *owner, enum mu_itrctl_req req, void *arg)
itr->flags |= ITR_BACKWARDS;
break;
case mu_itrctl_count:
if (!arg)
return EINVAL;
*(size_t*)arg = catcnt;
break;
default:
return ENOSYS;
}
......
......@@ -205,6 +205,11 @@ list_itrctl (void *owner, enum mu_itrctl_req req, void *arg)
itr->backwards = !!*(int*)arg;
break;
case mu_itrctl_count:
if (!arg)
return EINVAL;
return mu_list_count (itr->list, arg);
default:
return ENOSYS;
}
......
......@@ -172,6 +172,11 @@ hdr_itrctl (void *owner, enum mu_itrctl_req req, void *arg)
else
itr->backwards = !!*(int*)arg;
break;
case mu_itrctl_count:
if (!arg)
return EINVAL;
return mu_header_get_field_count (itr->header, arg);
default:
return ENOSYS;
......
......@@ -175,6 +175,11 @@ mbx_itrctl (void *owner, enum mu_itrctl_req req, void *arg)
else
itr->backwards = !!*(int*)arg;
break;
case mu_itrctl_count:
if (!arg)
return EINVAL;
return mu_mailbox_messages_count (itr->mbx, arg);
default:
return ENOSYS;
......