(list_get_iterator): New function
Showing
2 changed files
with
101 additions
and
1 deletions
... | @@ -36,6 +36,7 @@ extern int list_replace __P ((list_t list, void *old_item, void *new_item)); | ... | @@ -36,6 +36,7 @@ extern int list_replace __P ((list_t list, void *old_item, void *new_item)); |
36 | extern int list_get __P ((list_t, size_t _index, void **pitem)); | 36 | extern int list_get __P ((list_t, size_t _index, void **pitem)); |
37 | extern int list_to_array __P ((list_t list, void **array, size_t count, size_t *pcount)); | 37 | extern int list_to_array __P ((list_t list, void **array, size_t count, size_t *pcount)); |
38 | extern int list_locate __P ((list_t list, void *item, void **ret_item)); | 38 | extern int list_locate __P ((list_t list, void *item, void **ret_item)); |
39 | extern int list_get_iterator __P ((list_t, iterator_t *)); | ||
39 | 40 | ||
40 | typedef int list_action_t __PMT ((void* item, void* cbdata)); | 41 | typedef int list_action_t __PMT ((void* item, void* cbdata)); |
41 | 42 | ... | ... |
... | @@ -316,7 +316,7 @@ list_do (list_t list, list_action_t * action, void *cbdata) | ... | @@ -316,7 +316,7 @@ list_do (list_t list, list_action_t * action, void *cbdata) |
316 | 316 | ||
317 | if (list == NULL || action == NULL) | 317 | if (list == NULL || action == NULL) |
318 | return EINVAL; | 318 | return EINVAL; |
319 | status = iterator_create(&itr, list); | 319 | status = list_get_iterator(list, &itr); |
320 | if (status) | 320 | if (status) |
321 | return status; | 321 | return status; |
322 | for (iterator_first (itr); !iterator_is_done (itr); iterator_next (itr)) | 322 | for (iterator_first (itr); !iterator_is_done (itr); iterator_next (itr)) |
... | @@ -362,3 +362,102 @@ list_to_array (list_t list, void **array, size_t count, size_t *pcount) | ... | @@ -362,3 +362,102 @@ list_to_array (list_t list, void **array, size_t count, size_t *pcount) |
362 | *pcount = total; | 362 | *pcount = total; |
363 | return 0; | 363 | return 0; |
364 | } | 364 | } |
365 | |||
366 | |||
367 | /* Iterator interface */ | ||
368 | |||
369 | struct list_iterator | ||
370 | { | ||
371 | list_t list; | ||
372 | struct list_data *cur; | ||
373 | }; | ||
374 | |||
375 | static int | ||
376 | first (void *owner) | ||
377 | { | ||
378 | struct list_iterator *itr = owner; | ||
379 | itr->cur = itr->list->head.next; | ||
380 | return 0; | ||
381 | } | ||
382 | |||
383 | static int | ||
384 | next (void *owner) | ||
385 | { | ||
386 | struct list_iterator *itr = owner; | ||
387 | itr->cur = itr->cur->next; | ||
388 | return 0; | ||
389 | } | ||
390 | |||
391 | static int | ||
392 | getitem (void *owner, void **pret) | ||
393 | { | ||
394 | struct list_iterator *itr = owner; | ||
395 | *pret = itr->cur->item; | ||
396 | return 0; | ||
397 | } | ||
398 | |||
399 | static int | ||
400 | finished_p (void *owner) | ||
401 | { | ||
402 | struct list_iterator *itr = owner; | ||
403 | return itr->cur == &itr->list->head; | ||
404 | } | ||
405 | |||
406 | static int | ||
407 | destroy (iterator_t iterator, void *data) | ||
408 | { | ||
409 | struct list_iterator *itr = data; | ||
410 | iterator_detach (&itr->list->itr, iterator); | ||
411 | free (data); | ||
412 | return 0; | ||
413 | } | ||
414 | |||
415 | static int | ||
416 | curitem_p (void *owner, void *item) | ||
417 | { | ||
418 | struct list_iterator *itr = owner; | ||
419 | return itr->cur == item; | ||
420 | } | ||
421 | |||
422 | static int | ||
423 | list_data_dup (void **ptr, void *data) | ||
424 | { | ||
425 | *ptr = malloc (sizeof (struct list_iterator *)); | ||
426 | memcpy (*ptr, data, sizeof (struct list_iterator *)); | ||
427 | return 0; | ||
428 | } | ||
429 | |||
430 | int | ||
431 | list_get_iterator (list_t list, iterator_t *piterator) | ||
432 | { | ||
433 | iterator_t iterator; | ||
434 | int status; | ||
435 | struct list_iterator *itr; | ||
436 | |||
437 | if (!list) | ||
438 | return EINVAL; | ||
439 | |||
440 | itr = calloc (1, sizeof (struct list_iterator *)); | ||
441 | if (!itr) | ||
442 | return ENOMEM; | ||
443 | itr->list = list; | ||
444 | itr->cur = NULL; | ||
445 | |||
446 | status = iterator_create (&iterator, itr); | ||
447 | if (status) | ||
448 | return status; | ||
449 | |||
450 | iterator_set_first (iterator, first); | ||
451 | iterator_set_next (iterator, next); | ||
452 | iterator_set_getitem (iterator, getitem); | ||
453 | iterator_set_finished_p (iterator, finished_p); | ||
454 | iterator_set_curitem_p (iterator, curitem_p); | ||
455 | iterator_set_destroy (iterator, destroy); | ||
456 | iterator_set_dup (iterator, list_data_dup); | ||
457 | |||
458 | iterator_attach (&list->itr, iterator); | ||
459 | |||
460 | *piterator = iterator; | ||
461 | return 0; | ||
462 | } | ||
463 | ... | ... |
-
Please register or sign in to post a comment