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 ...@@ -35,7 +35,8 @@ enum mu_itrctl_req
35 mu_itrctl_insert, /* Insert new element in the current position */ 35 mu_itrctl_insert, /* Insert new element in the current position */
36 mu_itrctl_insert_list, /* Insert a list of elements */ 36 mu_itrctl_insert_list, /* Insert a list of elements */
37 mu_itrctl_qry_direction, /* Query iteration direction */ 37 mu_itrctl_qry_direction, /* Query iteration direction */
38 mu_itrctl_set_direction /* Set iteration direction */ 38 mu_itrctl_set_direction, /* Set iteration direction */
39 mu_itrctl_count /* Get number of elements */
39 }; 40 };
40 41
41 extern int mu_iterator_create (mu_iterator_t *, void *); 42 extern int mu_iterator_create (mu_iterator_t *, void *);
......
...@@ -514,6 +514,11 @@ itrctl (void *owner, enum mu_itrctl_req req, void *arg) ...@@ -514,6 +514,11 @@ itrctl (void *owner, enum mu_itrctl_req req, void *arg)
514 else 514 else
515 itr->backwards = !!*(int*)arg; 515 itr->backwards = !!*(int*)arg;
516 break; 516 break;
517
518 case mu_itrctl_count:
519 if (!arg)
520 return EINVAL;
521 return mu_assoc_count (assoc, arg);
517 522
518 default: 523 default:
519 return ENOSYS; 524 return ENOSYS;
......
...@@ -513,6 +513,31 @@ opitr_data_dup (void **ptr, void *owner) ...@@ -513,6 +513,31 @@ opitr_data_dup (void **ptr, void *owner)
513 return 0; 513 return 0;
514 } 514 }
515 515
516 static int
517 opitr_itrctl (void *owner, enum mu_itrctl_req req, void *arg)
518 {
519 struct opool_iterator *itr = owner;
520 switch (req)
521 {
522 case mu_itrctl_count:
523 if (!arg)
524 return EINVAL;
525 else
526 {
527 size_t n = 0;
528 union mu_opool_bucket *p;
529 for (p = itr->opool->bkt_head; p; p = p->hdr.next)
530 n++;
531 *(size_t*)arg = n;
532 }
533 break;
534
535 default:
536 return ENOSYS;
537 }
538 return 0;
539 }
540
516 int 541 int
517 mu_opool_get_iterator (mu_opool_t opool, mu_iterator_t *piterator) 542 mu_opool_get_iterator (mu_opool_t opool, mu_iterator_t *piterator)
518 { 543 {
...@@ -543,7 +568,8 @@ 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)
543 mu_iterator_set_delitem (iterator, opitr_delitem); 568 mu_iterator_set_delitem (iterator, opitr_delitem);
544 mu_iterator_set_destroy (iterator, opitr_destroy); 569 mu_iterator_set_destroy (iterator, opitr_destroy);
545 mu_iterator_set_dup (iterator, opitr_data_dup); 570 mu_iterator_set_dup (iterator, opitr_data_dup);
546 571 mu_iterator_set_itrctl (iterator, opitr_itrctl);
572
547 opool->itr_count++; 573 opool->itr_count++;
548 574
549 *piterator = iterator; 575 *piterator = iterator;
......
...@@ -598,10 +598,12 @@ list_itrctl (void *owner, enum mu_itrctl_req req, void *arg) ...@@ -598,10 +598,12 @@ list_itrctl (void *owner, enum mu_itrctl_req req, void *arg)
598 switch (req) 598 switch (req)
599 { 599 {
600 case mu_itrctl_tell: 600 case mu_itrctl_tell:
601 /* Return current position in the object */ 601 /* Return current position in the object.
602 NOTE: Positions are 1-based.
603 */
602 if (!arg) 604 if (!arg)
603 return EINVAL; 605 return EINVAL;
604 *(size_t*)arg = itr->pos; 606 *(size_t*)arg = itr->pos + 1;
605 break; 607 break;
606 608
607 case mu_itrctl_delete: 609 case mu_itrctl_delete:
...@@ -632,6 +634,12 @@ list_itrctl (void *owner, enum mu_itrctl_req req, void *arg) ...@@ -632,6 +634,12 @@ list_itrctl (void *owner, enum mu_itrctl_req req, void *arg)
632 itr->flags |= ITR_BACKWARDS; 634 itr->flags |= ITR_BACKWARDS;
633 break; 635 break;
634 636
637 case mu_itrctl_count:
638 if (!arg)
639 return EINVAL;
640 *(size_t*)arg = catcnt;
641 break;
642
635 default: 643 default:
636 return ENOSYS; 644 return ENOSYS;
637 } 645 }
......
...@@ -205,6 +205,11 @@ list_itrctl (void *owner, enum mu_itrctl_req req, void *arg) ...@@ -205,6 +205,11 @@ list_itrctl (void *owner, enum mu_itrctl_req req, void *arg)
205 itr->backwards = !!*(int*)arg; 205 itr->backwards = !!*(int*)arg;
206 break; 206 break;
207 207
208 case mu_itrctl_count:
209 if (!arg)
210 return EINVAL;
211 return mu_list_count (itr->list, arg);
212
208 default: 213 default:
209 return ENOSYS; 214 return ENOSYS;
210 } 215 }
......
...@@ -172,6 +172,11 @@ hdr_itrctl (void *owner, enum mu_itrctl_req req, void *arg) ...@@ -172,6 +172,11 @@ hdr_itrctl (void *owner, enum mu_itrctl_req req, void *arg)
172 else 172 else
173 itr->backwards = !!*(int*)arg; 173 itr->backwards = !!*(int*)arg;
174 break; 174 break;
175
176 case mu_itrctl_count:
177 if (!arg)
178 return EINVAL;
179 return mu_header_get_field_count (itr->header, arg);
175 180
176 default: 181 default:
177 return ENOSYS; 182 return ENOSYS;
......
...@@ -175,6 +175,11 @@ mbx_itrctl (void *owner, enum mu_itrctl_req req, void *arg) ...@@ -175,6 +175,11 @@ mbx_itrctl (void *owner, enum mu_itrctl_req req, void *arg)
175 else 175 else
176 itr->backwards = !!*(int*)arg; 176 itr->backwards = !!*(int*)arg;
177 break; 177 break;
178
179 case mu_itrctl_count:
180 if (!arg)
181 return EINVAL;
182 return mu_mailbox_messages_count (itr->mbx, arg);
178 183
179 default: 184 default:
180 return ENOSYS; 185 return ENOSYS;
......