Implement mu_assoc_foreach function
* include/mailutils/assoc.h (mu_assoc_action_t): New typedef. (mu_assoc_foreach): New function. * libmailutils/base/assoc.c (mu_assoc_foreach): New function.
Showing
2 changed files
with
31 additions
and
2 deletions
... | @@ -43,6 +43,9 @@ int mu_assoc_remove (mu_assoc_t assoc, const char *name); | ... | @@ -43,6 +43,9 @@ int mu_assoc_remove (mu_assoc_t assoc, const char *name); |
43 | int mu_assoc_set_destroy_item (mu_assoc_t assoc, mu_deallocator_t fn); | 43 | int mu_assoc_set_destroy_item (mu_assoc_t assoc, mu_deallocator_t fn); |
44 | int mu_assoc_count (mu_assoc_t assoc, size_t *pcount); | 44 | int mu_assoc_count (mu_assoc_t assoc, size_t *pcount); |
45 | 45 | ||
46 | typedef int (*mu_assoc_action_t) (char const *, void *, void *); | ||
47 | int mu_assoc_foreach (mu_assoc_t assoc, mu_assoc_action_t action, void *data); | ||
48 | |||
46 | #ifdef __cplusplus | 49 | #ifdef __cplusplus |
47 | } | 50 | } |
48 | #endif | 51 | #endif | ... | ... |
... | @@ -621,9 +621,7 @@ mu_assoc_get_iterator (mu_assoc_t assoc, mu_iterator_t *piterator) | ... | @@ -621,9 +621,7 @@ mu_assoc_get_iterator (mu_assoc_t assoc, mu_iterator_t *piterator) |
621 | *piterator = iterator; | 621 | *piterator = iterator; |
622 | return 0; | 622 | return 0; |
623 | } | 623 | } |
624 | |||
625 | 624 | ||
626 | |||
627 | int | 625 | int |
628 | mu_assoc_count (mu_assoc_t assoc, size_t *pcount) | 626 | mu_assoc_count (mu_assoc_t assoc, size_t *pcount) |
629 | { | 627 | { |
... | @@ -643,4 +641,32 @@ mu_assoc_count (mu_assoc_t assoc, size_t *pcount) | ... | @@ -643,4 +641,32 @@ mu_assoc_count (mu_assoc_t assoc, size_t *pcount) |
643 | *pcount = count; | 641 | *pcount = count; |
644 | return 0; | 642 | return 0; |
645 | } | 643 | } |
644 | |||
645 | int | ||
646 | mu_assoc_foreach (mu_assoc_t assoc, mu_assoc_action_t action, void *data) | ||
647 | { | ||
648 | mu_iterator_t itr; | ||
649 | int rc; | ||
650 | |||
651 | if (!assoc || !action) | ||
652 | return EINVAL; | ||
653 | rc = mu_assoc_get_iterator (assoc, &itr); | ||
654 | if (rc) | ||
655 | return rc; | ||
656 | for (mu_iterator_first (itr); !mu_iterator_is_done (itr); | ||
657 | mu_iterator_next (itr)) | ||
658 | { | ||
659 | char *name; | ||
660 | void *value; | ||
646 | 661 | ||
662 | rc = mu_iterator_current_kv (itr, (const void **)&name, (void**)&value); | ||
663 | if (rc) | ||
664 | break; | ||
665 | |||
666 | rc = action (name, value, data); | ||
667 | if (rc) | ||
668 | break; | ||
669 | } | ||
670 | mu_iterator_destroy (&itr); | ||
671 | return rc; | ||
672 | } | ... | ... |
-
Please register or sign in to post a comment