Added a non-variadic version of linked_list_for_each.

This commit is contained in:
Ulysse Cura 2026-08-10 18:21:52 +02:00
parent 2e180b6832
commit 3605aeee27
2 changed files with 40 additions and 0 deletions

View File

@ -274,4 +274,16 @@ int linked_list_remove_if(linked_list_t *linked_list, const condition_t conditio
*/
int linked_list_for_each(const linked_list_t *linked_list, const action_t action, ...);
/**
* @brief Apply an action to every elements in the list.
* The action can be anything that doesn't destroy the element.
*
* @param linked_list Pointer to an linked list
* @param action Action to apply
* @param args Argument to pass to the action in the form of a va_list
*
* @return EXIT_SUCCESS, EXIT_FAILURE on error.
*/
int linked_list_for_eachv(const linked_list_t *linked_list, const action_t action, va_list args);
#endif // LINKED_LIST_H

View File

@ -478,3 +478,31 @@ int linked_list_for_each(const linked_list_t *linked_list, const action_t action
return EXIT_SUCCESS;
}
int linked_list_for_eachv(const linked_list_t *linked_list, const action_t action, va_list args)
{
assert("Linked list cannot be NULL" && linked_list);
assert("Action cannot be NULL" && action);
elem_t *current_elem = linked_list->first;
while(current_elem)
{
va_list args_copy;
va_copy(args_copy, args);
if(action(current_elem, args_copy))
{
va_end(args_copy);
va_end(args);
return EXIT_FAILURE;
}
va_end(args_copy);
current_elem = current_elem->next;
}
return EXIT_SUCCESS;
}